blob: d70deb30fcf2d5f0c646ba6b932424c105052ad3 [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
323static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
324 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
325 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000326 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000327 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700328 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000329 }
caryclark2bec26a2016-05-26 09:01:47 -0700330 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
331 testName, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000332 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000333 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700334 SkPathOpsDebug::ShowOnePath(a, "path", false);
335 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000336 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000337 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700338 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000339}
340
reed086eea92016-05-04 17:12:46 -0700341SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700342
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000343static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
344 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800345 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700346 ExpectMatch expectMatch) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000347 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800348 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000349 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700350 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700351 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800352 REPORTER_ASSERT(reporter, 0);
353 }
354 return 0;
355 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000356 if (errors2x2 == 0) {
357 return 0;
358 }
caryclarkd5b91732016-08-09 05:04:29 -0700359 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000360 SkAutoMutexAcquire autoM(compareDebugOut3);
361 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700362 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000363 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700364 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000365 }
caryclark38a017b2015-05-13 10:13:17 -0700366 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000367}
368
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000369// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000370static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000371static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000372
Ben Wagner561c1b02017-01-09 15:54:34 -0500373static void appendTestName(const char* nameSuffix, SkString& out) {
374 out.appendf("%s%d", testName, testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000375 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000376 if (nameSuffix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500377 out.append(nameSuffix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000378 }
379}
380
Ben Wagner561c1b02017-01-09 15:54:34 -0500381static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
382 const char* testFunction, bool twoPaths, SkString& out) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000383#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500384 out.append("\n<div id=\"");
385 appendTestName(nameSuffix, out);
386 out.append("\">\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000387 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500388 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000389 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500390 out.append(pathStr);
391 out.append("</div>\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000392
Ben Wagner561c1b02017-01-09 15:54:34 -0500393 out.append(marker);
394 out.append(" ");
395 appendTestName(nameSuffix, out);
396 out.append(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000397#endif
Ben Wagner561c1b02017-01-09 15:54:34 -0500398 out.append("static void ");
399 appendTestName(nameSuffix, out);
400 out.append("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000401 if (twoPaths) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500402 out.append(", pathB");
caryclark@google.com66089e42013-04-10 15:55:37 +0000403 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500404 out.append(";\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000405 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500406 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000407 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500408 out.appendf("%s %s\n}\n\n", pathStr, testFunction);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000409#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500410 out.append("static void (*firstTest)() = ");
411 appendTestName(nameSuffix, out);
412 out.append(";\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000413
Ben Wagner561c1b02017-01-09 15:54:34 -0500414 out.append("static struct {\n");
415 out.append(" void (*fun)();\n");
416 out.append(" const char* str;\n");
417 out.append("} tests[] = {\n");
418 out.append(" TEST(");
419 appendTestName(nameSuffix, out);
420 out.append("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000421#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000422}
423
reed086eea92016-05-04 17:12:46 -0700424SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700425
caryclark@google.com66089e42013-04-10 15:55:37 +0000426bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
427 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000428 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
429 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700430 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000431 if (!Simplify(path, &out)) {
432 SkDebugf("%s did not expect failure\n", __FUNCTION__);
433 REPORTER_ASSERT(state.fReporter, 0);
434 return false;
435 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000436 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000437 return true;
438 }
halcanary96fcdcc2015-08-27 07:41:13 -0700439 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700440 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000441 SkAutoMutexAcquire autoM(simplifyDebugOut);
Mike Reedff80c2a2017-01-07 11:16:28 -0500442 SkString str;
halcanary96fcdcc2015-08-27 07:41:13 -0700443 const char* pathPrefix = nullptr;
444 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000445 if (fillType == SkPath::kEvenOdd_FillType) {
446 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
447 nameSuffix = "x";
448 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000449 const char testFunction[] = "testSimplify(reporter, path);";
Ben Wagner561c1b02017-01-09 15:54:34 -0500450 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
Mike Reedff80c2a2017-01-07 11:16:28 -0500451 SkDebugf("%s", str.c_str());
caryclark@google.com66089e42013-04-10 15:55:37 +0000452 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000453 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000454 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000455 return result == 0;
456}
457
caryclark54359292015-03-26 07:52:43 -0700458static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700459 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700460#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000461 showPathData(path);
462#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000463 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700464 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
465 SkDEBUGPARAMS(testName))) {
466 if (ExpectSuccess::kYes == expectSuccess) {
467 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
468 REPORTER_ASSERT(reporter, 0);
469 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000470 return false;
caryclark55888e42016-07-18 10:01:36 -0700471 } else {
472 if (ExpectSuccess::kNo == expectSuccess) {
473 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
474 REPORTER_ASSERT(reporter, 0);
475 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000476 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000477 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700478 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700479 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700480 if (!errors) {
481 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
482 REPORTER_ASSERT(reporter, 0);
483 return false;
484 }
caryclarkd5b91732016-08-09 05:04:29 -0700485 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000486 REPORTER_ASSERT(reporter, 0);
487 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000488 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700489 return errors == 0;
490}
491
492bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700493 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
494 ExpectMatch::kYes);
495}
496
caryclark30b9fdd2016-08-31 14:36:29 -0700497bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
498 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
499 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700500}
501
502bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
503 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700504 return inner_simplify(reporter, path, filename, checkFail ?
505 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000506}
507
caryclark@google.coma5e55922013-05-07 18:51:31 +0000508#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000509static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
510 SkDebugf("\n");
511 showPathData(a);
512 showOp(shapeOp);
513 showPathData(b);
514}
515#endif
516
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000517static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700518 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
519 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700520#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000521 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000522#endif
523 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700524 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700525 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700526 if (ExpectSuccess::kYes == expectSuccess) {
527 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700528 REPORTER_ASSERT(reporter, 0);
529 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000530 return false;
caryclark55888e42016-07-18 10:01:36 -0700531 } else {
532 if (ExpectSuccess::kNo == expectSuccess) {
533 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
534 REPORTER_ASSERT(reporter, 0);
535 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000536 }
caryclark54359292015-03-26 07:52:43 -0700537 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000538 return true;
539 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000540 SkPath pathOut, scaledPathOut;
541 SkRegion rgnA, rgnB, openClip, rgnOut;
542 openClip.setRect(-16000, -16000, 16000, 16000);
543 rgnA.setPath(a, openClip);
544 rgnB.setPath(b, openClip);
545 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
546 rgnOut.getBoundaryPath(&pathOut);
547
548 SkMatrix scale;
549 scaleMatrix(a, b, scale);
550 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
551 SkPath scaledA, scaledB;
552 scaledA.addPath(a, scale);
553 scaledA.setFillType(a.getFillType());
554 scaledB.addPath(b, scale);
555 scaledB.setFillType(b.getFillType());
556 scaledRgnA.setPath(scaledA, openClip);
557 scaledRgnB.setPath(scaledB, openClip);
558 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
559 scaledRgnOut.getBoundaryPath(&scaledPathOut);
560 SkBitmap bitmap;
561 SkPath scaledOut;
562 scaledOut.addPath(out, scale);
563 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000564 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700565 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000566 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000567 return result == 0;
568}
569
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000570bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800571 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700572 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
573 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800574}
575
576bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
577 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700578 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
579 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000580}
581
caryclark30b9fdd2016-08-31 14:36:29 -0700582bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700583 const SkPathOp shapeOp, const char* testName) {
584 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
585 ExpectMatch::kFlaky);
586}
587
caryclark55888e42016-07-18 10:01:36 -0700588bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000589 const SkPathOp shapeOp, const char* testName) {
590#if DEBUG_SHOW_TEST_NAME
591 showName(a, b, shapeOp);
592#endif
caryclark1049f122015-04-20 08:31:59 -0700593 SkPath orig;
594 orig.lineTo(54, 43);
595 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000596 if (Op(a, b, shapeOp, &out) ) {
597 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
598 REPORTER_ASSERT(reporter, 0);
599 return false;
600 }
caryclark1049f122015-04-20 08:31:59 -0700601 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000602 return true;
603}
604
reed086eea92016-05-04 17:12:46 -0700605SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000606
mtklein406654b2014-09-03 15:34:37 -0700607void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000608 if (reporter->verbose()) {
609 SkAutoMutexAcquire lock(gMutex);
610 testName = test;
611 size_t testNameSize = strlen(test);
612 SkFILEStream inFile("../../experimental/Intersection/op.htm");
613 if (inFile.isValid()) {
614 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000615 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000616 size_t inLen = inData.count();
617 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700618 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000619 char* insert = strstr(inData.begin(), marker);
620 if (insert) {
621 insert += sizeof(marker) - 1;
622 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
623 testNumber = atoi(numLoc) + 1;
624 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000625 }
626 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000627}
628
Mike Reedff80c2a2017-01-07 11:16:28 -0500629void PathOpsThreadState::outputProgress(const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000630 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700631 const char* pathPrefix = nullptr;
632 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000633 if (pathFillType == SkPath::kEvenOdd_FillType) {
634 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
635 nameSuffix = "x";
636 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500637 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000638}
639
Mike Reedff80c2a2017-01-07 11:16:28 -0500640void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000641 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000642 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000643 const char* nameSuffix = opSuffixes[op];
Ben Wagner561c1b02017-01-09 15:54:34 -0500644 appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000645}
646
647void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000648 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700649 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000650 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000651 size_t index;
652 if (firstTest) {
653 index = count - 1;
654 while (index > 0 && tests[index].fun != firstTest) {
655 --index;
656 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000657#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700658 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000659#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000660 (*tests[index].fun)(reporter, tests[index].str);
661 if (tests[index].fun == stopTest) {
662 return;
663 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000664 }
665 index = reverse ? count - 1 : 0;
666 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700667 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000668 do {
caryclark54359292015-03-26 07:52:43 -0700669 if (tests[index].fun == skipTest) {
670 foundSkip = true;
671 }
672 if (foundSkip && tests[index].fun != firstTest) {
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
caryclark30b9fdd2016-08-31 14:36:29 -0700676 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000677 }
caryclark03b03ca2015-04-23 09:13:37 -0700678 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000679 break;
680 }
681 index += reverse ? -1 : 1;
682 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700683#if DEBUG_SHOW_TEST_NAME
684 SkDebugf(
685 "\n"
686 "</div>\n"
687 "\n"
688 "<script type=\"text/javascript\">\n"
689 "\n"
690 "var testDivs = [\n"
691 );
692 index = reverse ? count - 1 : 0;
693 last = reverse ? 0 : count - 1;
694 foundSkip = !skipTest;
695 do {
696 if (tests[index].fun == skipTest) {
697 foundSkip = true;
698 }
699 if (foundSkip && tests[index].fun != firstTest) {
700 SkDebugf(" %s,\n", tests[index].str);
701 }
702 if (tests[index].fun == stopTest || index == last) {
703 break;
704 }
705 index += reverse ? -1 : 1;
706 } while (true);
707#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000708}