blob: 06fee80f5dd45d05b60a0081426d17cd5bdf8a47 [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"
12#include "SkMatrix.h"
mtklein1b249332015-07-07 12:21:21 -070013#include "SkMutex.h"
skia.committer@gmail.come02c5da2014-04-26 03:04:35 +000014#include "SkPaint.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000015#include "SkStream.h"
16
bungeman60e0fee2015-08-26 05:15:46 -070017#include <stdlib.h>
18
caryclark@google.com818b0cc2013-04-08 11:50:46 +000019#ifdef SK_BUILD_FOR_MAC
20#include <sys/sysctl.h>
21#endif
22
caryclark55888e42016-07-18 10:01:36 -070023bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
24 SkDEBUGPARAMS(bool skipAssert)
25 SkDEBUGPARAMS(const char* testName));
26
27bool SimplifyDebug(const SkPath& one, SkPath* result
28 SkDEBUGPARAMS(bool skipAssert)
29 SkDEBUGPARAMS(const char* testName));
30
caryclark@google.com818b0cc2013-04-08 11:50:46 +000031static const char marker[] =
32 "</div>\n"
33 "\n"
34 "<script type=\"text/javascript\">\n"
35 "\n"
36 "var testDivs = [\n";
37
38static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070039 "kDifference_SkPathOp",
40 "kIntersect_SkPathOp",
41 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070042 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070043 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000044};
45
46static const char* opSuffixes[] = {
47 "d",
48 "i",
49 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000050 "o",
caryclark55888e42016-07-18 10:01:36 -070051 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000052};
53
caryclarkd5b91732016-08-09 05:04:29 -070054enum class ExpectSuccess {
55 kNo,
56 kYes,
57 kFlaky
58};
59
60enum class SkipAssert {
61 kNo,
62 kYes
63};
64
65enum class ExpectMatch {
66 kNo,
67 kYes,
68 kFlaky
69};
70
caryclark@google.comcffbcc32013-06-04 17:59:42 +000071#if DEBUG_SHOW_TEST_NAME
72static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000073 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000074 uint8_t verb;
75 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000076 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000077 bool firstPtSet = false;
78 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000079 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
80 switch (verb) {
81 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070082 if (firstPtSet && lastPtSet && firstPt != lastPt) {
83 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
84 firstPt.fX, firstPt.fY);
85 lastPtSet = false;
86 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000087 firstPt = pts[0];
88 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000089 continue;
90 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000091 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
92 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000093 lastPt = pts[1];
94 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000095 break;
96 case SkPath::kQuad_Verb:
97 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +000098 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 +000099 lastPt = pts[2];
100 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000101 break;
caryclark54359292015-03-26 07:52:43 -0700102 case SkPath::kConic_Verb:
103 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
104 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
105 iter.conicWeight());
106 lastPt = pts[2];
107 lastPtSet = true;
108 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000109 case SkPath::kCubic_Verb:
110 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 +0000111 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
112 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000113 lastPt = pts[3];
114 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000115 break;
116 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000117 if (firstPtSet && lastPtSet && firstPt != lastPt) {
118 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
119 firstPt.fX, firstPt.fY);
120 }
121 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122 break;
123 default:
124 SkDEBUGFAIL("bad verb");
125 return;
126 }
127 }
caryclarkdac1d172014-06-17 05:15:38 -0700128 if (firstPtSet && lastPtSet && firstPt != lastPt) {
129 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
130 firstPt.fX, firstPt.fY);
131 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000132}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000133#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000134
135void showOp(const SkPathOp op) {
136 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700137 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000138 SkDebugf("op difference\n");
139 break;
caryclark54359292015-03-26 07:52:43 -0700140 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000141 SkDebugf("op intersect\n");
142 break;
caryclark54359292015-03-26 07:52:43 -0700143 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000144 SkDebugf("op union\n");
145 break;
caryclark54359292015-03-26 07:52:43 -0700146 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000147 SkDebugf("op xor\n");
148 break;
caryclark54359292015-03-26 07:52:43 -0700149 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000150 SkDebugf("op reverse difference\n");
151 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000152 default:
153 SkASSERT(0);
154 }
155}
156
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000157#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000158static char hexorator(int x) {
159 if (x < 10) {
160 return x + '0';
161 }
162 x -= 10;
163 SkASSERT(x < 26);
164 return x + 'A';
165}
166#endif
167
168void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
169#if DEBUG_SHOW_TEST_NAME
170 state->fSerialNo[0] = hexorator(state->fA);
171 state->fSerialNo[1] = hexorator(state->fB);
172 state->fSerialNo[2] = hexorator(state->fC);
173 state->fSerialNo[3] = hexorator(state->fD);
174 state->fSerialNo[4] = hexorator(a);
175 state->fSerialNo[5] = hexorator(b);
176 state->fSerialNo[6] = hexorator(c);
177 state->fSerialNo[7] = hexorator(d);
178 state->fSerialNo[8] = '\0';
179 SkDebugf("%s\n", state->fSerialNo);
180 if (strcmp(state->fSerialNo, state->fKey) == 0) {
181 SkDebugf("%s\n", state->fPathStr);
182 }
183#endif
184}
185
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000186const int bitWidth = 64;
187const int bitHeight = 64;
188
189static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
190 SkRect larger = one.getBounds();
191 larger.join(two.getBounds());
192 SkScalar largerWidth = larger.width();
193 if (largerWidth < 4) {
194 largerWidth = 4;
195 }
196 SkScalar largerHeight = larger.height();
197 if (largerHeight < 4) {
198 largerHeight = 4;
199 }
200 SkScalar hScale = (bitWidth - 2) / largerWidth;
201 SkScalar vScale = (bitHeight - 2) / largerHeight;
202 scale.reset();
203 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700204 larger.fLeft *= hScale;
205 larger.fRight *= hScale;
206 larger.fTop *= vScale;
207 larger.fBottom *= vScale;
208 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
209 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
210 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
211 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
212 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000213}
214
215static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
216 int& error2x2) {
217 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000218 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000219 }
220 SkCanvas canvas(bits);
221 canvas.drawColor(SK_ColorWHITE);
222 SkPaint paint;
223 canvas.save();
224 const SkRect& bounds1 = scaledOne.getBounds();
225 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
226 canvas.drawPath(scaledOne, paint);
227 canvas.restore();
228 canvas.save();
229 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
230 canvas.drawPath(scaledTwo, paint);
231 canvas.restore();
232 int errors2 = 0;
233 int errors = 0;
234 for (int y = 0; y < bitHeight - 1; ++y) {
235 uint32_t* addr1 = bits.getAddr32(0, y);
236 uint32_t* addr2 = bits.getAddr32(0, y + 1);
237 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
238 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
239 for (int x = 0; x < bitWidth - 1; ++x) {
240 // count 2x2 blocks
241 bool err = addr1[x] != addr3[x];
242 if (err) {
243 errors2 += addr1[x + 1] != addr3[x + 1]
244 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
245 errors++;
246 }
247 }
248 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000249 error2x2 = errors2;
250 return errors;
251}
252
253static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
254 SkPath& scaledTwo, int& error2x2) {
255 SkMatrix scale;
256 scaleMatrix(one, two, scale);
257 one.transform(scale, &scaledOne);
258 two.transform(scale, &scaledTwo);
259 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
260}
261
262bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
263 if (!drawPaths) {
264 return true;
265 }
266 const SkRect& bounds1 = one.getBounds();
267 const SkRect& bounds2 = two.getBounds();
268 SkRect larger = bounds1;
269 larger.join(bounds2);
270 SkBitmap bits;
271 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000272 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000273 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
274 return false;
275 }
reed@google.come1ca7052013-12-17 19:22:07 +0000276 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000277 if (bitHeight >= (int) sizeof(out)) {
278 return false;
279 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000280 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000281 SkCanvas canvas(bits);
282 canvas.drawColor(SK_ColorWHITE);
283 SkPaint paint;
284 canvas.save();
285 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
286 canvas.drawPath(one, paint);
287 canvas.restore();
288 canvas.save();
289 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
290 canvas.drawPath(two, paint);
291 canvas.restore();
292 for (int y = 0; y < bitHeight; ++y) {
293 uint32_t* addr1 = bits.getAddr32(0, y);
294 int x;
295 char* outPtr = out;
296 for (x = 0; x < bitWidth; ++x) {
297 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
298 }
299 *outPtr++ = '|';
300 for (x = bitWidth; x < bitWidth * 2; ++x) {
301 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
302 }
303 *outPtr++ = '\0';
304 SkDebugf("%s\n", out);
305 }
306 return true;
307}
308
caryclark54359292015-03-26 07:52:43 -0700309int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000310 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000311 int errors2x2;
312 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000313 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000314 if (errors2x2 == 0) {
315 return 0;
316 }
317 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000318 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
319}
320
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000321static SkTDArray<SkPathOp> gTestOp;
322
Cary Clarkd2eb5812017-01-18 11:00:57 -0500323static void dumpPathOpFunction(const char* testName, const SkPath& a, const SkPath& b,
324 const SkPathOp shapeOp) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000325 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700326 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000327 }
caryclark2bec26a2016-05-26 09:01:47 -0700328 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
329 testName, opSuffixes[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000330 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700331 SkPathOpsDebug::ShowOnePath(a, "path", false);
332 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000333 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000334 SkDebugf("}\n");
Cary Clarkd2eb5812017-01-18 11:00:57 -0500335}
336
337static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
338 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
339 const SkPathOp shapeOp, const SkMatrix& scale) {
340 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
341 *gTestOp.append() = shapeOp;
342 dumpPathOpFunction(testName, a, b, shapeOp);
caryclark26ad22a2015-10-16 09:03:38 -0700343 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000344}
345
reed086eea92016-05-04 17:12:46 -0700346SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700347
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000348static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
349 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800350 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700351 ExpectMatch expectMatch) {
Cary Clarkd2eb5812017-01-18 11:00:57 -0500352 if (ExpectMatch::kFlaky == expectMatch) {
353 return 0; // fuzz data may cause asserts in region generating code, so don't try
354 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000355 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800356 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000357 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700358 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700359 if (errors2x2 < MAX_ERRORS) {
Cary Clarkd2eb5812017-01-18 11:00:57 -0500360 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, testName);
caryclark65f55312014-11-13 06:58:52 -0800361 REPORTER_ASSERT(reporter, 0);
362 }
363 return 0;
364 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000365 if (errors2x2 == 0) {
366 return 0;
367 }
caryclarkd5b91732016-08-09 05:04:29 -0700368 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000369 SkAutoMutexAcquire autoM(compareDebugOut3);
370 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700371 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000372 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700373 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000374 }
caryclark38a017b2015-05-13 10:13:17 -0700375 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000376}
377
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000378// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000379static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000380static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000381
Ben Wagner561c1b02017-01-09 15:54:34 -0500382static void appendTestName(const char* nameSuffix, SkString& out) {
383 out.appendf("%s%d", testName, testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000384 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000385 if (nameSuffix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500386 out.append(nameSuffix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000387 }
388}
389
Ben Wagner561c1b02017-01-09 15:54:34 -0500390static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
391 const char* testFunction, bool twoPaths, SkString& out) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000392#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500393 out.append("\n<div id=\"");
394 appendTestName(nameSuffix, out);
395 out.append("\">\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000396 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500397 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000398 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500399 out.append(pathStr);
400 out.append("</div>\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000401
Ben Wagner561c1b02017-01-09 15:54:34 -0500402 out.append(marker);
403 out.append(" ");
404 appendTestName(nameSuffix, out);
405 out.append(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000406#endif
Ben Wagner561c1b02017-01-09 15:54:34 -0500407 out.append("static void ");
408 appendTestName(nameSuffix, out);
409 out.append("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000410 if (twoPaths) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500411 out.append(", pathB");
caryclark@google.com66089e42013-04-10 15:55:37 +0000412 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500413 out.append(";\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000414 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500415 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000416 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500417 out.appendf("%s %s\n}\n\n", pathStr, testFunction);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000418#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500419 out.append("static void (*firstTest)() = ");
420 appendTestName(nameSuffix, out);
421 out.append(";\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000422
Ben Wagner561c1b02017-01-09 15:54:34 -0500423 out.append("static struct {\n");
424 out.append(" void (*fun)();\n");
425 out.append(" const char* str;\n");
426 out.append("} tests[] = {\n");
427 out.append(" TEST(");
428 appendTestName(nameSuffix, out);
429 out.append("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000430#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000431}
432
reed086eea92016-05-04 17:12:46 -0700433SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700434
caryclark@google.com66089e42013-04-10 15:55:37 +0000435bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
436 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000437 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
438 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700439 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000440 if (!Simplify(path, &out)) {
441 SkDebugf("%s did not expect failure\n", __FUNCTION__);
442 REPORTER_ASSERT(state.fReporter, 0);
443 return false;
444 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000445 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000446 return true;
447 }
halcanary96fcdcc2015-08-27 07:41:13 -0700448 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700449 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000450 SkAutoMutexAcquire autoM(simplifyDebugOut);
Mike Reedff80c2a2017-01-07 11:16:28 -0500451 SkString str;
halcanary96fcdcc2015-08-27 07:41:13 -0700452 const char* pathPrefix = nullptr;
453 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000454 if (fillType == SkPath::kEvenOdd_FillType) {
455 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
456 nameSuffix = "x";
457 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000458 const char testFunction[] = "testSimplify(reporter, path);";
Ben Wagner561c1b02017-01-09 15:54:34 -0500459 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
Mike Reedff80c2a2017-01-07 11:16:28 -0500460 SkDebugf("%s", str.c_str());
caryclark@google.com66089e42013-04-10 15:55:37 +0000461 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000462 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000463 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000464 return result == 0;
465}
466
caryclark54359292015-03-26 07:52:43 -0700467static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700468 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700469#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000470 showPathData(path);
471#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000472 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700473 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
474 SkDEBUGPARAMS(testName))) {
475 if (ExpectSuccess::kYes == expectSuccess) {
476 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
477 REPORTER_ASSERT(reporter, 0);
478 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000479 return false;
caryclark55888e42016-07-18 10:01:36 -0700480 } else {
481 if (ExpectSuccess::kNo == expectSuccess) {
482 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
483 REPORTER_ASSERT(reporter, 0);
484 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000485 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000486 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700487 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700488 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700489 if (!errors) {
490 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
491 REPORTER_ASSERT(reporter, 0);
492 return false;
493 }
caryclarkd5b91732016-08-09 05:04:29 -0700494 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000495 REPORTER_ASSERT(reporter, 0);
496 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000497 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700498 return errors == 0;
499}
500
501bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700502 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
503 ExpectMatch::kYes);
504}
505
caryclark30b9fdd2016-08-31 14:36:29 -0700506bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
507 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
508 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700509}
510
511bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
512 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700513 return inner_simplify(reporter, path, filename, checkFail ?
514 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000515}
516
Cary Clarkd2eb5812017-01-18 11:00:57 -0500517bool testSimplifyTry(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
518 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
519 ExpectMatch::kNo);
520}
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);
Cary Clarkd2eb5812017-01-18 11:00:57 -0500542 dumpPathOpFunction(testName, a, b, shapeOp);
caryclark3f0753d2016-06-28 09:23:57 -0700543 REPORTER_ASSERT(reporter, 0);
544 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000545 return false;
caryclark55888e42016-07-18 10:01:36 -0700546 } else {
547 if (ExpectSuccess::kNo == expectSuccess) {
548 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
Cary Clarkd2eb5812017-01-18 11:00:57 -0500549 dumpPathOpFunction(testName, a, b, shapeOp);
caryclark55888e42016-07-18 10:01:36 -0700550 REPORTER_ASSERT(reporter, 0);
551 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000552 }
caryclark54359292015-03-26 07:52:43 -0700553 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000554 return true;
555 }
Cary Clarkd2eb5812017-01-18 11:00:57 -0500556 if (ExpectMatch::kFlaky == expectMatch) {
557 return true; // fuzzy data may assert in region construction: see bug.skia.org/6129
558 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000559 SkPath pathOut, scaledPathOut;
560 SkRegion rgnA, rgnB, openClip, rgnOut;
561 openClip.setRect(-16000, -16000, 16000, 16000);
562 rgnA.setPath(a, openClip);
563 rgnB.setPath(b, openClip);
564 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
565 rgnOut.getBoundaryPath(&pathOut);
566
567 SkMatrix scale;
568 scaleMatrix(a, b, scale);
569 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
570 SkPath scaledA, scaledB;
571 scaledA.addPath(a, scale);
572 scaledA.setFillType(a.getFillType());
573 scaledB.addPath(b, scale);
574 scaledB.setFillType(b.getFillType());
575 scaledRgnA.setPath(scaledA, openClip);
576 scaledRgnB.setPath(scaledB, openClip);
577 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
578 scaledRgnOut.getBoundaryPath(&scaledPathOut);
579 SkBitmap bitmap;
580 SkPath scaledOut;
581 scaledOut.addPath(out, scale);
582 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000583 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700584 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000585 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000586 return result == 0;
587}
588
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000589bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800590 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700591 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
592 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800593}
594
595bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
596 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700597 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
598 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000599}
600
Cary Clarkd2eb5812017-01-18 11:00:57 -0500601bool testPathOpTry(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
602 const SkPathOp shapeOp, const char* testName) {
603 return innerPathOp(reporter, a, b, shapeOp, testName,
604 ExpectSuccess::kYes, SkipAssert::kNo, ExpectMatch::kNo);
605}
606
caryclark30b9fdd2016-08-31 14:36:29 -0700607bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700608 const SkPathOp shapeOp, const char* testName) {
609 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
610 ExpectMatch::kFlaky);
611}
612
reed086eea92016-05-04 17:12:46 -0700613SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000614
mtklein406654b2014-09-03 15:34:37 -0700615void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000616 if (reporter->verbose()) {
617 SkAutoMutexAcquire lock(gMutex);
618 testName = test;
619 size_t testNameSize = strlen(test);
620 SkFILEStream inFile("../../experimental/Intersection/op.htm");
621 if (inFile.isValid()) {
622 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000623 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000624 size_t inLen = inData.count();
625 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700626 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000627 char* insert = strstr(inData.begin(), marker);
628 if (insert) {
629 insert += sizeof(marker) - 1;
630 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
631 testNumber = atoi(numLoc) + 1;
632 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000633 }
634 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000635}
636
Mike Reedff80c2a2017-01-07 11:16:28 -0500637void PathOpsThreadState::outputProgress(const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000638 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700639 const char* pathPrefix = nullptr;
640 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000641 if (pathFillType == SkPath::kEvenOdd_FillType) {
642 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
643 nameSuffix = "x";
644 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500645 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000646}
647
Mike Reedff80c2a2017-01-07 11:16:28 -0500648void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000649 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000650 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000651 const char* nameSuffix = opSuffixes[op];
Ben Wagner561c1b02017-01-09 15:54:34 -0500652 appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000653}
654
655void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000656 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700657 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000658 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000659 size_t index;
660 if (firstTest) {
661 index = count - 1;
662 while (index > 0 && tests[index].fun != firstTest) {
663 --index;
664 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000665#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700666 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000667#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000668 (*tests[index].fun)(reporter, tests[index].str);
669 if (tests[index].fun == stopTest) {
670 return;
671 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000672 }
673 index = reverse ? count - 1 : 0;
674 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700675 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000676 do {
caryclark54359292015-03-26 07:52:43 -0700677 if (tests[index].fun == skipTest) {
678 foundSkip = true;
679 }
680 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000681 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700682 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000683 #endif
caryclark30b9fdd2016-08-31 14:36:29 -0700684 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000685 }
caryclark03b03ca2015-04-23 09:13:37 -0700686 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000687 break;
688 }
689 index += reverse ? -1 : 1;
690 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700691#if DEBUG_SHOW_TEST_NAME
692 SkDebugf(
693 "\n"
694 "</div>\n"
695 "\n"
696 "<script type=\"text/javascript\">\n"
697 "\n"
698 "var testDivs = [\n"
699 );
700 index = reverse ? count - 1 : 0;
701 last = reverse ? 0 : count - 1;
702 foundSkip = !skipTest;
703 do {
704 if (tests[index].fun == skipTest) {
705 foundSkip = true;
706 }
707 if (foundSkip && tests[index].fun != firstTest) {
708 SkDebugf(" %s,\n", tests[index].str);
709 }
710 if (tests[index].fun == stopTest || index == last) {
711 break;
712 }
713 index += reverse ? -1 : 1;
714 } while (true);
715#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000716}