blob: f699b8b2088e315770f578f1e600405d10021986 [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
caryclark@google.com818b0cc2013-04-08 11:50:46 +000035static const char marker[] =
36 "</div>\n"
37 "\n"
38 "<script type=\"text/javascript\">\n"
39 "\n"
40 "var testDivs = [\n";
41
42static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070043 "kDifference_SkPathOp",
44 "kIntersect_SkPathOp",
45 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070046 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070047 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000048};
49
50static const char* opSuffixes[] = {
51 "d",
52 "i",
53 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000054 "o",
caryclark55888e42016-07-18 10:01:36 -070055 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000056};
57
caryclarkd5b91732016-08-09 05:04:29 -070058enum class ExpectSuccess {
59 kNo,
60 kYes,
61 kFlaky
62};
63
64enum class SkipAssert {
65 kNo,
66 kYes
67};
68
69enum class ExpectMatch {
70 kNo,
71 kYes,
72 kFlaky
73};
74
caryclark@google.comcffbcc32013-06-04 17:59:42 +000075#if DEBUG_SHOW_TEST_NAME
76static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000077 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000078 uint8_t verb;
79 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000080 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000081 bool firstPtSet = false;
82 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000083 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
84 switch (verb) {
85 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070086 if (firstPtSet && lastPtSet && firstPt != lastPt) {
87 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
88 firstPt.fX, firstPt.fY);
89 lastPtSet = false;
90 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000091 firstPt = pts[0];
92 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000093 continue;
94 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000095 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
96 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000097 lastPt = pts[1];
98 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000099 break;
100 case SkPath::kQuad_Verb:
101 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000102 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 +0000103 lastPt = pts[2];
104 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000105 break;
caryclark54359292015-03-26 07:52:43 -0700106 case SkPath::kConic_Verb:
107 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
108 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
109 iter.conicWeight());
110 lastPt = pts[2];
111 lastPtSet = true;
112 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000113 case SkPath::kCubic_Verb:
114 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 +0000115 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
116 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000117 lastPt = pts[3];
118 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000119 break;
120 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000121 if (firstPtSet && lastPtSet && firstPt != lastPt) {
122 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
123 firstPt.fX, firstPt.fY);
124 }
125 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000126 break;
127 default:
128 SkDEBUGFAIL("bad verb");
129 return;
130 }
131 }
caryclarkdac1d172014-06-17 05:15:38 -0700132 if (firstPtSet && lastPtSet && firstPt != lastPt) {
133 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
134 firstPt.fX, firstPt.fY);
135 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000136}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000137#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000138
139void showOp(const SkPathOp op) {
140 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700141 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000142 SkDebugf("op difference\n");
143 break;
caryclark54359292015-03-26 07:52:43 -0700144 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000145 SkDebugf("op intersect\n");
146 break;
caryclark54359292015-03-26 07:52:43 -0700147 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000148 SkDebugf("op union\n");
149 break;
caryclark54359292015-03-26 07:52:43 -0700150 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000151 SkDebugf("op xor\n");
152 break;
caryclark54359292015-03-26 07:52:43 -0700153 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000154 SkDebugf("op reverse difference\n");
155 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000156 default:
157 SkASSERT(0);
158 }
159}
160
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000161#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000162static char hexorator(int x) {
163 if (x < 10) {
164 return x + '0';
165 }
166 x -= 10;
167 SkASSERT(x < 26);
168 return x + 'A';
169}
170#endif
171
172void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
173#if DEBUG_SHOW_TEST_NAME
174 state->fSerialNo[0] = hexorator(state->fA);
175 state->fSerialNo[1] = hexorator(state->fB);
176 state->fSerialNo[2] = hexorator(state->fC);
177 state->fSerialNo[3] = hexorator(state->fD);
178 state->fSerialNo[4] = hexorator(a);
179 state->fSerialNo[5] = hexorator(b);
180 state->fSerialNo[6] = hexorator(c);
181 state->fSerialNo[7] = hexorator(d);
182 state->fSerialNo[8] = '\0';
183 SkDebugf("%s\n", state->fSerialNo);
184 if (strcmp(state->fSerialNo, state->fKey) == 0) {
185 SkDebugf("%s\n", state->fPathStr);
186 }
187#endif
188}
189
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000190const int bitWidth = 64;
191const int bitHeight = 64;
192
193static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
194 SkRect larger = one.getBounds();
195 larger.join(two.getBounds());
196 SkScalar largerWidth = larger.width();
197 if (largerWidth < 4) {
198 largerWidth = 4;
199 }
200 SkScalar largerHeight = larger.height();
201 if (largerHeight < 4) {
202 largerHeight = 4;
203 }
204 SkScalar hScale = (bitWidth - 2) / largerWidth;
205 SkScalar vScale = (bitHeight - 2) / largerHeight;
206 scale.reset();
207 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700208 larger.fLeft *= hScale;
209 larger.fRight *= hScale;
210 larger.fTop *= vScale;
211 larger.fBottom *= vScale;
212 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
213 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
214 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
215 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
216 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000217}
218
219static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
220 int& error2x2) {
221 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000222 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000223 }
224 SkCanvas canvas(bits);
225 canvas.drawColor(SK_ColorWHITE);
226 SkPaint paint;
227 canvas.save();
228 const SkRect& bounds1 = scaledOne.getBounds();
229 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
230 canvas.drawPath(scaledOne, paint);
231 canvas.restore();
232 canvas.save();
233 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
234 canvas.drawPath(scaledTwo, paint);
235 canvas.restore();
236 int errors2 = 0;
237 int errors = 0;
238 for (int y = 0; y < bitHeight - 1; ++y) {
239 uint32_t* addr1 = bits.getAddr32(0, y);
240 uint32_t* addr2 = bits.getAddr32(0, y + 1);
241 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
242 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
243 for (int x = 0; x < bitWidth - 1; ++x) {
244 // count 2x2 blocks
245 bool err = addr1[x] != addr3[x];
246 if (err) {
247 errors2 += addr1[x + 1] != addr3[x + 1]
248 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
249 errors++;
250 }
251 }
252 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000253 error2x2 = errors2;
254 return errors;
255}
256
257static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
258 SkPath& scaledTwo, int& error2x2) {
259 SkMatrix scale;
260 scaleMatrix(one, two, scale);
261 one.transform(scale, &scaledOne);
262 two.transform(scale, &scaledTwo);
263 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
264}
265
266bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
267 if (!drawPaths) {
268 return true;
269 }
270 const SkRect& bounds1 = one.getBounds();
271 const SkRect& bounds2 = two.getBounds();
272 SkRect larger = bounds1;
273 larger.join(bounds2);
274 SkBitmap bits;
275 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000276 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000277 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
278 return false;
279 }
reed@google.come1ca7052013-12-17 19:22:07 +0000280 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000281 if (bitHeight >= (int) sizeof(out)) {
282 return false;
283 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000284 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000285 SkCanvas canvas(bits);
286 canvas.drawColor(SK_ColorWHITE);
287 SkPaint paint;
288 canvas.save();
289 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
290 canvas.drawPath(one, paint);
291 canvas.restore();
292 canvas.save();
293 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
294 canvas.drawPath(two, paint);
295 canvas.restore();
296 for (int y = 0; y < bitHeight; ++y) {
297 uint32_t* addr1 = bits.getAddr32(0, y);
298 int x;
299 char* outPtr = out;
300 for (x = 0; x < bitWidth; ++x) {
301 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
302 }
303 *outPtr++ = '|';
304 for (x = bitWidth; x < bitWidth * 2; ++x) {
305 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
306 }
307 *outPtr++ = '\0';
308 SkDebugf("%s\n", out);
309 }
310 return true;
311}
312
caryclark54359292015-03-26 07:52:43 -0700313int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000314 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000315 int errors2x2;
316 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000317 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000318 if (errors2x2 == 0) {
319 return 0;
320 }
321 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000322 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
323}
324
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000325static SkTDArray<SkPathOp> gTestOp;
326
327static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
328 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
329 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000330 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000331 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700332 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000333 }
caryclark2bec26a2016-05-26 09:01:47 -0700334 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
335 testName, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000336 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000337 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700338 SkPathOpsDebug::ShowOnePath(a, "path", false);
339 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000340 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000341 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700342 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000343}
344
reed086eea92016-05-04 17:12:46 -0700345SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700346
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000347static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
348 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800349 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700350 ExpectMatch expectMatch) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000351 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800352 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000353 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700354 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700355 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800356 REPORTER_ASSERT(reporter, 0);
357 }
358 return 0;
359 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000360 if (errors2x2 == 0) {
361 return 0;
362 }
caryclarkd5b91732016-08-09 05:04:29 -0700363 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000364 SkAutoMutexAcquire autoM(compareDebugOut3);
365 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700366 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000367 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700368 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000369 }
caryclark38a017b2015-05-13 10:13:17 -0700370 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000371}
372
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000373// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000374static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000375static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000376
377static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
378 outFile.writeText(testName);
379 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000380 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000381 if (nameSuffix) {
382 outFile.writeText(nameSuffix);
383 }
384}
385
386static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
387 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000388#if 0
caryclark54359292015-03-26 07:52:43 -0700389 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000390 writeTestName(nameSuffix, outFile);
391 outFile.writeText("\">\n");
392 if (pathPrefix) {
393 outFile.writeText(pathPrefix);
394 }
395 outFile.writeText(pathStr);
396 outFile.writeText("</div>\n\n");
397
398 outFile.writeText(marker);
399 outFile.writeText(" ");
400 writeTestName(nameSuffix, outFile);
401 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000402#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000403 outFile.writeText("static void ");
404 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000405 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000406 if (twoPaths) {
407 outFile.writeText(", pathB");
408 }
409 outFile.writeText(";\n");
410 if (pathPrefix) {
411 outFile.writeText(pathPrefix);
412 }
413 outFile.writeText(pathStr);
414 outFile.writeText(" ");
415 outFile.writeText(testFunction);
416 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000417#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000418 outFile.writeText("static void (*firstTest)() = ");
419 writeTestName(nameSuffix, outFile);
420 outFile.writeText(";\n\n");
421
422 outFile.writeText("static struct {\n");
423 outFile.writeText(" void (*fun)();\n");
424 outFile.writeText(" const char* str;\n");
425 outFile.writeText("} tests[] = {\n");
426 outFile.writeText(" TEST(");
427 writeTestName(nameSuffix, outFile);
428 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000429#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000430 outFile.flush();
431}
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);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000451 char temp[8192];
452 sk_bzero(temp, sizeof(temp));
453 SkMemoryWStream stream(temp, sizeof(temp));
halcanary96fcdcc2015-08-27 07:41:13 -0700454 const char* pathPrefix = nullptr;
455 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000456 if (fillType == SkPath::kEvenOdd_FillType) {
457 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
458 nameSuffix = "x";
459 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000460 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000461 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800462 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000463 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000464 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000465 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000466 return result == 0;
467}
468
caryclark54359292015-03-26 07:52:43 -0700469static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700470 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700471#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000472 showPathData(path);
473#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000474 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700475 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
476 SkDEBUGPARAMS(testName))) {
477 if (ExpectSuccess::kYes == expectSuccess) {
478 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
479 REPORTER_ASSERT(reporter, 0);
480 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000481 return false;
caryclark55888e42016-07-18 10:01:36 -0700482 } else {
483 if (ExpectSuccess::kNo == expectSuccess) {
484 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
485 REPORTER_ASSERT(reporter, 0);
486 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000487 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000488 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700489 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700490 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700491 if (!errors) {
492 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
493 REPORTER_ASSERT(reporter, 0);
494 return false;
495 }
caryclarkd5b91732016-08-09 05:04:29 -0700496 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000497 REPORTER_ASSERT(reporter, 0);
498 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000499 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700500 return errors == 0;
501}
502
503bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700504 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
505 ExpectMatch::kYes);
506}
507
caryclark30b9fdd2016-08-31 14:36:29 -0700508bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
509 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
510 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700511}
512
513bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
514 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700515 return inner_simplify(reporter, path, filename, checkFail ?
516 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000517}
518
caryclark@google.coma5e55922013-05-07 18:51:31 +0000519#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000520static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
521 SkDebugf("\n");
522 showPathData(a);
523 showOp(shapeOp);
524 showPathData(b);
525}
526#endif
527
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000528static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700529 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
530 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700531#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000532 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000533#endif
534 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700535 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700536 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700537 if (ExpectSuccess::kYes == expectSuccess) {
538 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700539 REPORTER_ASSERT(reporter, 0);
540 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000541 return false;
caryclark55888e42016-07-18 10:01:36 -0700542 } else {
543 if (ExpectSuccess::kNo == expectSuccess) {
544 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
545 REPORTER_ASSERT(reporter, 0);
546 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000547 }
caryclark54359292015-03-26 07:52:43 -0700548 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000549 return true;
550 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000551 SkPath pathOut, scaledPathOut;
552 SkRegion rgnA, rgnB, openClip, rgnOut;
553 openClip.setRect(-16000, -16000, 16000, 16000);
554 rgnA.setPath(a, openClip);
555 rgnB.setPath(b, openClip);
556 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
557 rgnOut.getBoundaryPath(&pathOut);
558
559 SkMatrix scale;
560 scaleMatrix(a, b, scale);
561 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
562 SkPath scaledA, scaledB;
563 scaledA.addPath(a, scale);
564 scaledA.setFillType(a.getFillType());
565 scaledB.addPath(b, scale);
566 scaledB.setFillType(b.getFillType());
567 scaledRgnA.setPath(scaledA, openClip);
568 scaledRgnB.setPath(scaledB, openClip);
569 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
570 scaledRgnOut.getBoundaryPath(&scaledPathOut);
571 SkBitmap bitmap;
572 SkPath scaledOut;
573 scaledOut.addPath(out, scale);
574 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000575 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700576 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000577 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000578 return result == 0;
579}
580
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000581bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800582 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700583 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
584 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800585}
586
587bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
588 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700589 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
590 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000591}
592
caryclark30b9fdd2016-08-31 14:36:29 -0700593bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700594 const SkPathOp shapeOp, const char* testName) {
595 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
596 ExpectMatch::kFlaky);
597}
598
caryclark55888e42016-07-18 10:01:36 -0700599bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000600 const SkPathOp shapeOp, const char* testName) {
601#if DEBUG_SHOW_TEST_NAME
602 showName(a, b, shapeOp);
603#endif
caryclark1049f122015-04-20 08:31:59 -0700604 SkPath orig;
605 orig.lineTo(54, 43);
606 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000607 if (Op(a, b, shapeOp, &out) ) {
608 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
609 REPORTER_ASSERT(reporter, 0);
610 return false;
611 }
caryclark1049f122015-04-20 08:31:59 -0700612 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000613 return true;
614}
615
reed086eea92016-05-04 17:12:46 -0700616SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000617
mtklein406654b2014-09-03 15:34:37 -0700618void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000619 if (reporter->verbose()) {
620 SkAutoMutexAcquire lock(gMutex);
621 testName = test;
622 size_t testNameSize = strlen(test);
623 SkFILEStream inFile("../../experimental/Intersection/op.htm");
624 if (inFile.isValid()) {
625 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000626 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000627 size_t inLen = inData.count();
628 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700629 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000630 char* insert = strstr(inData.begin(), marker);
631 if (insert) {
632 insert += sizeof(marker) - 1;
633 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
634 testNumber = atoi(numLoc) + 1;
635 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000636 }
637 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000638}
639
caryclark@google.com66089e42013-04-10 15:55:37 +0000640void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
641 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700642 const char* pathPrefix = nullptr;
643 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000644 if (pathFillType == SkPath::kEvenOdd_FillType) {
645 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
646 nameSuffix = "x";
647 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000648 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
649 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000650}
651
caryclark@google.com66089e42013-04-10 15:55:37 +0000652void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
653 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000654 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000655 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000656 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
halcanary96fcdcc2015-08-27 07:41:13 -0700657 outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000658}
659
660void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000661 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700662 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000663 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000664 size_t index;
665 if (firstTest) {
666 index = count - 1;
667 while (index > 0 && tests[index].fun != firstTest) {
668 --index;
669 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000670#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700671 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000672#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000673 (*tests[index].fun)(reporter, tests[index].str);
674 if (tests[index].fun == stopTest) {
675 return;
676 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000677 }
678 index = reverse ? count - 1 : 0;
679 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700680 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000681 do {
caryclark54359292015-03-26 07:52:43 -0700682 if (tests[index].fun == skipTest) {
683 foundSkip = true;
684 }
685 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000686 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700687 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000688 #endif
caryclark30b9fdd2016-08-31 14:36:29 -0700689 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000690 }
caryclark03b03ca2015-04-23 09:13:37 -0700691 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000692 break;
693 }
694 index += reverse ? -1 : 1;
695 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700696#if DEBUG_SHOW_TEST_NAME
697 SkDebugf(
698 "\n"
699 "</div>\n"
700 "\n"
701 "<script type=\"text/javascript\">\n"
702 "\n"
703 "var testDivs = [\n"
704 );
705 index = reverse ? count - 1 : 0;
706 last = reverse ? 0 : count - 1;
707 foundSkip = !skipTest;
708 do {
709 if (tests[index].fun == skipTest) {
710 foundSkip = true;
711 }
712 if (foundSkip && tests[index].fun != firstTest) {
713 SkDebugf(" %s,\n", tests[index].str);
714 }
715 if (tests[index].fun == stopTest || index == last) {
716 break;
717 }
718 index += reverse ? -1 : 1;
719 } while (true);
720#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000721}