blob: 2f6d99d6529d329d5f08446905ffbd36a921c2ad [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.coma2bbc6e2013-11-01 17:36:03 +000016#include "SkRTConf.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000017#include "SkStream.h"
18
bungeman60e0fee2015-08-26 05:15:46 -070019#include <stdlib.h>
20
caryclark@google.com818b0cc2013-04-08 11:50:46 +000021#ifdef SK_BUILD_FOR_MAC
22#include <sys/sysctl.h>
23#endif
24
caryclark55888e42016-07-18 10:01:36 -070025bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
26 SkDEBUGPARAMS(bool skipAssert)
27 SkDEBUGPARAMS(const char* testName));
28
29bool SimplifyDebug(const SkPath& one, SkPath* result
30 SkDEBUGPARAMS(bool skipAssert)
31 SkDEBUGPARAMS(const char* testName));
32
33
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000034__SK_FORCE_IMAGE_DECODER_LINKING;
35
caryclark65b427c2014-09-18 10:32:57 -070036DEFINE_bool2(runFail, f, false, "run tests known to fail.");
caryclark54359292015-03-26 07:52:43 -070037DEFINE_bool2(runBinary, f, false, "run tests known to fail binary sect.");
caryclark65b427c2014-09-18 10:32:57 -070038
caryclark@google.com818b0cc2013-04-08 11:50:46 +000039static const char marker[] =
40 "</div>\n"
41 "\n"
42 "<script type=\"text/javascript\">\n"
43 "\n"
44 "var testDivs = [\n";
45
46static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070047 "kDifference_SkPathOp",
48 "kIntersect_SkPathOp",
49 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070050 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070051 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000052};
53
54static const char* opSuffixes[] = {
55 "d",
56 "i",
57 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000058 "o",
caryclark55888e42016-07-18 10:01:36 -070059 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000060};
61
caryclark@google.comcffbcc32013-06-04 17:59:42 +000062#if DEBUG_SHOW_TEST_NAME
63static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000064 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000065 uint8_t verb;
66 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000067 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000068 bool firstPtSet = false;
69 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000070 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
71 switch (verb) {
72 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070073 if (firstPtSet && lastPtSet && firstPt != lastPt) {
74 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
75 firstPt.fX, firstPt.fY);
76 lastPtSet = false;
77 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000078 firstPt = pts[0];
79 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000080 continue;
81 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000082 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
83 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000084 lastPt = pts[1];
85 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000086 break;
87 case SkPath::kQuad_Verb:
88 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +000089 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 +000090 lastPt = pts[2];
91 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000092 break;
caryclark54359292015-03-26 07:52:43 -070093 case SkPath::kConic_Verb:
94 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
95 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
96 iter.conicWeight());
97 lastPt = pts[2];
98 lastPtSet = true;
99 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000100 case SkPath::kCubic_Verb:
101 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 +0000102 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
103 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000104 lastPt = pts[3];
105 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000106 break;
107 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000108 if (firstPtSet && lastPtSet && firstPt != lastPt) {
109 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
110 firstPt.fX, firstPt.fY);
111 }
112 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000113 break;
114 default:
115 SkDEBUGFAIL("bad verb");
116 return;
117 }
118 }
caryclarkdac1d172014-06-17 05:15:38 -0700119 if (firstPtSet && lastPtSet && firstPt != lastPt) {
120 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
121 firstPt.fX, firstPt.fY);
122 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000123}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000124#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000125
126void showOp(const SkPathOp op) {
127 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700128 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000129 SkDebugf("op difference\n");
130 break;
caryclark54359292015-03-26 07:52:43 -0700131 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000132 SkDebugf("op intersect\n");
133 break;
caryclark54359292015-03-26 07:52:43 -0700134 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000135 SkDebugf("op union\n");
136 break;
caryclark54359292015-03-26 07:52:43 -0700137 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000138 SkDebugf("op xor\n");
139 break;
caryclark54359292015-03-26 07:52:43 -0700140 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000141 SkDebugf("op reverse difference\n");
142 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000143 default:
144 SkASSERT(0);
145 }
146}
147
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000148#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000149static char hexorator(int x) {
150 if (x < 10) {
151 return x + '0';
152 }
153 x -= 10;
154 SkASSERT(x < 26);
155 return x + 'A';
156}
157#endif
158
159void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
160#if DEBUG_SHOW_TEST_NAME
161 state->fSerialNo[0] = hexorator(state->fA);
162 state->fSerialNo[1] = hexorator(state->fB);
163 state->fSerialNo[2] = hexorator(state->fC);
164 state->fSerialNo[3] = hexorator(state->fD);
165 state->fSerialNo[4] = hexorator(a);
166 state->fSerialNo[5] = hexorator(b);
167 state->fSerialNo[6] = hexorator(c);
168 state->fSerialNo[7] = hexorator(d);
169 state->fSerialNo[8] = '\0';
170 SkDebugf("%s\n", state->fSerialNo);
171 if (strcmp(state->fSerialNo, state->fKey) == 0) {
172 SkDebugf("%s\n", state->fPathStr);
173 }
174#endif
175}
176
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000177const int bitWidth = 64;
178const int bitHeight = 64;
179
180static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
181 SkRect larger = one.getBounds();
182 larger.join(two.getBounds());
183 SkScalar largerWidth = larger.width();
184 if (largerWidth < 4) {
185 largerWidth = 4;
186 }
187 SkScalar largerHeight = larger.height();
188 if (largerHeight < 4) {
189 largerHeight = 4;
190 }
191 SkScalar hScale = (bitWidth - 2) / largerWidth;
192 SkScalar vScale = (bitHeight - 2) / largerHeight;
193 scale.reset();
194 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700195 larger.fLeft *= hScale;
196 larger.fRight *= hScale;
197 larger.fTop *= vScale;
198 larger.fBottom *= vScale;
199 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
200 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
201 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
202 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
203 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000204}
205
206static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
207 int& error2x2) {
208 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000209 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000210 }
211 SkCanvas canvas(bits);
212 canvas.drawColor(SK_ColorWHITE);
213 SkPaint paint;
214 canvas.save();
215 const SkRect& bounds1 = scaledOne.getBounds();
216 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
217 canvas.drawPath(scaledOne, paint);
218 canvas.restore();
219 canvas.save();
220 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
221 canvas.drawPath(scaledTwo, paint);
222 canvas.restore();
223 int errors2 = 0;
224 int errors = 0;
225 for (int y = 0; y < bitHeight - 1; ++y) {
226 uint32_t* addr1 = bits.getAddr32(0, y);
227 uint32_t* addr2 = bits.getAddr32(0, y + 1);
228 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
229 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
230 for (int x = 0; x < bitWidth - 1; ++x) {
231 // count 2x2 blocks
232 bool err = addr1[x] != addr3[x];
233 if (err) {
234 errors2 += addr1[x + 1] != addr3[x + 1]
235 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
236 errors++;
237 }
238 }
239 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000240 error2x2 = errors2;
241 return errors;
242}
243
244static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
245 SkPath& scaledTwo, int& error2x2) {
246 SkMatrix scale;
247 scaleMatrix(one, two, scale);
248 one.transform(scale, &scaledOne);
249 two.transform(scale, &scaledTwo);
250 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
251}
252
253bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
254 if (!drawPaths) {
255 return true;
256 }
257 const SkRect& bounds1 = one.getBounds();
258 const SkRect& bounds2 = two.getBounds();
259 SkRect larger = bounds1;
260 larger.join(bounds2);
261 SkBitmap bits;
262 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000263 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000264 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
265 return false;
266 }
reed@google.come1ca7052013-12-17 19:22:07 +0000267 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000268 if (bitHeight >= (int) sizeof(out)) {
269 return false;
270 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000271 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000272 SkCanvas canvas(bits);
273 canvas.drawColor(SK_ColorWHITE);
274 SkPaint paint;
275 canvas.save();
276 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
277 canvas.drawPath(one, paint);
278 canvas.restore();
279 canvas.save();
280 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
281 canvas.drawPath(two, paint);
282 canvas.restore();
283 for (int y = 0; y < bitHeight; ++y) {
284 uint32_t* addr1 = bits.getAddr32(0, y);
285 int x;
286 char* outPtr = out;
287 for (x = 0; x < bitWidth; ++x) {
288 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
289 }
290 *outPtr++ = '|';
291 for (x = bitWidth; x < bitWidth * 2; ++x) {
292 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
293 }
294 *outPtr++ = '\0';
295 SkDebugf("%s\n", out);
296 }
297 return true;
298}
299
caryclark54359292015-03-26 07:52:43 -0700300int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000301 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000302 int errors2x2;
303 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000304 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000305 if (errors2x2 == 0) {
306 return 0;
307 }
308 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000309 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
310}
311
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000312static SkTDArray<SkPathOp> gTestOp;
313
314static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
315 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
316 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000317 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000318 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700319 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000320 }
caryclark2bec26a2016-05-26 09:01:47 -0700321 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
322 testName, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000323 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000324 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700325 SkPathOpsDebug::ShowOnePath(a, "path", false);
326 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000327 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000328 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700329 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000330}
331
reed086eea92016-05-04 17:12:46 -0700332SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700333
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000334static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
335 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800336 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclark38a017b2015-05-13 10:13:17 -0700337 bool expectSuccess) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000338 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800339 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000340 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclark65f55312014-11-13 06:58:52 -0800341 if (!expectSuccess) {
caryclark38a017b2015-05-13 10:13:17 -0700342 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800343 REPORTER_ASSERT(reporter, 0);
344 }
345 return 0;
346 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000347 if (errors2x2 == 0) {
348 return 0;
349 }
caryclark38a017b2015-05-13 10:13:17 -0700350 if (errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000351 SkAutoMutexAcquire autoM(compareDebugOut3);
352 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700353 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000354 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700355 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000356 }
caryclark38a017b2015-05-13 10:13:17 -0700357 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000358}
359
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000360// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000361static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000362static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000363
364static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
365 outFile.writeText(testName);
366 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000367 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000368 if (nameSuffix) {
369 outFile.writeText(nameSuffix);
370 }
371}
372
373static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
374 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000375#if 0
caryclark54359292015-03-26 07:52:43 -0700376 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000377 writeTestName(nameSuffix, outFile);
378 outFile.writeText("\">\n");
379 if (pathPrefix) {
380 outFile.writeText(pathPrefix);
381 }
382 outFile.writeText(pathStr);
383 outFile.writeText("</div>\n\n");
384
385 outFile.writeText(marker);
386 outFile.writeText(" ");
387 writeTestName(nameSuffix, outFile);
388 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000389#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000390 outFile.writeText("static void ");
391 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000392 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000393 if (twoPaths) {
394 outFile.writeText(", pathB");
395 }
396 outFile.writeText(";\n");
397 if (pathPrefix) {
398 outFile.writeText(pathPrefix);
399 }
400 outFile.writeText(pathStr);
401 outFile.writeText(" ");
402 outFile.writeText(testFunction);
403 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000404#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000405 outFile.writeText("static void (*firstTest)() = ");
406 writeTestName(nameSuffix, outFile);
407 outFile.writeText(";\n\n");
408
409 outFile.writeText("static struct {\n");
410 outFile.writeText(" void (*fun)();\n");
411 outFile.writeText(" const char* str;\n");
412 outFile.writeText("} tests[] = {\n");
413 outFile.writeText(" TEST(");
414 writeTestName(nameSuffix, outFile);
415 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000416#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000417 outFile.flush();
418}
419
reed086eea92016-05-04 17:12:46 -0700420SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700421
caryclark@google.com66089e42013-04-10 15:55:37 +0000422bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
423 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000424 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
425 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700426 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000427 if (!Simplify(path, &out)) {
428 SkDebugf("%s did not expect failure\n", __FUNCTION__);
429 REPORTER_ASSERT(state.fReporter, 0);
430 return false;
431 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000432 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000433 return true;
434 }
halcanary96fcdcc2015-08-27 07:41:13 -0700435 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700436 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000437 SkAutoMutexAcquire autoM(simplifyDebugOut);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000438 char temp[8192];
439 sk_bzero(temp, sizeof(temp));
440 SkMemoryWStream stream(temp, sizeof(temp));
halcanary96fcdcc2015-08-27 07:41:13 -0700441 const char* pathPrefix = nullptr;
442 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000443 if (fillType == SkPath::kEvenOdd_FillType) {
444 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
445 nameSuffix = "x";
446 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000447 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000448 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800449 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000450 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000451 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000452 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000453 return result == 0;
454}
455
caryclark55888e42016-07-18 10:01:36 -0700456enum class ExpectSuccess {
457 kNo,
458 kYes
459};
460
461enum class SkipAssert {
462 kNo,
463 kYes
464};
465
466enum class ExpectMatch {
467 kNo,
468 kYes
469};
470
caryclark54359292015-03-26 07:52:43 -0700471static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700472 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700473#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000474 showPathData(path);
475#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000476 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700477 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
478 SkDEBUGPARAMS(testName))) {
479 if (ExpectSuccess::kYes == expectSuccess) {
480 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
481 REPORTER_ASSERT(reporter, 0);
482 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000483 return false;
caryclark55888e42016-07-18 10:01:36 -0700484 } else {
485 if (ExpectSuccess::kNo == expectSuccess) {
486 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
487 REPORTER_ASSERT(reporter, 0);
488 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000489 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000490 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700491 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700492 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700493 if (!errors) {
494 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
495 REPORTER_ASSERT(reporter, 0);
496 return false;
497 }
498 } else if (errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000499 REPORTER_ASSERT(reporter, 0);
500 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000501 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700502 return errors == 0;
503}
504
505bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700506 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
507 ExpectMatch::kYes);
508}
509
510bool testSimplifyFailSkipAssert(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
511 return inner_simplify(reporter, path, filename, ExpectSuccess::kNo, SkipAssert::kYes,
512 ExpectMatch::kNo);
caryclark54359292015-03-26 07:52:43 -0700513}
514
515bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
516 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700517 return inner_simplify(reporter, path, filename, checkFail ?
518 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000519}
520
caryclark@google.coma5e55922013-05-07 18:51:31 +0000521#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000522static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
523 SkDebugf("\n");
524 showPathData(a);
525 showOp(shapeOp);
526 showPathData(b);
527}
528#endif
529
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000530static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700531 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
532 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700533#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000534 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000535#endif
536 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700537 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700538 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700539 if (ExpectSuccess::kYes == expectSuccess) {
540 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700541 REPORTER_ASSERT(reporter, 0);
542 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000543 return false;
caryclark55888e42016-07-18 10:01:36 -0700544 } else {
545 if (ExpectSuccess::kNo == expectSuccess) {
546 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
547 REPORTER_ASSERT(reporter, 0);
548 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000549 }
caryclark54359292015-03-26 07:52:43 -0700550 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000551 return true;
552 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000553 SkPath pathOut, scaledPathOut;
554 SkRegion rgnA, rgnB, openClip, rgnOut;
555 openClip.setRect(-16000, -16000, 16000, 16000);
556 rgnA.setPath(a, openClip);
557 rgnB.setPath(b, openClip);
558 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
559 rgnOut.getBoundaryPath(&pathOut);
560
561 SkMatrix scale;
562 scaleMatrix(a, b, scale);
563 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
564 SkPath scaledA, scaledB;
565 scaledA.addPath(a, scale);
566 scaledA.setFillType(a.getFillType());
567 scaledB.addPath(b, scale);
568 scaledB.setFillType(b.getFillType());
569 scaledRgnA.setPath(scaledA, openClip);
570 scaledRgnB.setPath(scaledB, openClip);
571 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
572 scaledRgnOut.getBoundaryPath(&scaledPathOut);
573 SkBitmap bitmap;
574 SkPath scaledOut;
575 scaledOut.addPath(out, scale);
576 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000577 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclark55888e42016-07-18 10:01:36 -0700578 a, b, shapeOp, scale, ExpectMatch::kYes == expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000579 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000580 return result == 0;
581}
582
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000583bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800584 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700585 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
586 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800587}
588
589bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
590 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700591 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
592 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000593}
594
caryclark1049f122015-04-20 08:31:59 -0700595bool testPathOpFailCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
596 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700597 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kNo, SkipAssert::kNo,
598 ExpectMatch::kNo);
caryclarkdae6b972016-06-08 04:28:19 -0700599}
600
caryclark55888e42016-07-18 10:01:36 -0700601bool testPathOpSkipAssert(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkdae6b972016-06-08 04:28:19 -0700602 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700603 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kYes,
604 ExpectMatch::kYes);
caryclark1049f122015-04-20 08:31:59 -0700605}
606
caryclark55888e42016-07-18 10:01:36 -0700607bool testPathOpFailSkipAssert(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark3f0753d2016-06-28 09:23:57 -0700608 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700609 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kNo, SkipAssert::kYes,
610 ExpectMatch::kNo);
caryclark3f0753d2016-06-28 09:23:57 -0700611}
612
caryclark55888e42016-07-18 10:01:36 -0700613bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000614 const SkPathOp shapeOp, const char* testName) {
615#if DEBUG_SHOW_TEST_NAME
616 showName(a, b, shapeOp);
617#endif
caryclark1049f122015-04-20 08:31:59 -0700618 SkPath orig;
619 orig.lineTo(54, 43);
620 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000621 if (Op(a, b, shapeOp, &out) ) {
622 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
623 REPORTER_ASSERT(reporter, 0);
624 return false;
625 }
caryclark1049f122015-04-20 08:31:59 -0700626 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000627 return true;
628}
629
reed086eea92016-05-04 17:12:46 -0700630SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000631
mtklein406654b2014-09-03 15:34:37 -0700632void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000633#if 0 // doesn't work yet
634 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
635 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
636#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000637 if (reporter->verbose()) {
638 SkAutoMutexAcquire lock(gMutex);
639 testName = test;
640 size_t testNameSize = strlen(test);
641 SkFILEStream inFile("../../experimental/Intersection/op.htm");
642 if (inFile.isValid()) {
643 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000644 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000645 size_t inLen = inData.count();
646 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700647 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000648 char* insert = strstr(inData.begin(), marker);
649 if (insert) {
650 insert += sizeof(marker) - 1;
651 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
652 testNumber = atoi(numLoc) + 1;
653 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000654 }
655 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000656}
657
caryclark@google.com66089e42013-04-10 15:55:37 +0000658void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
659 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700660 const char* pathPrefix = nullptr;
661 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000662 if (pathFillType == SkPath::kEvenOdd_FillType) {
663 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
664 nameSuffix = "x";
665 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000666 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
667 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000668}
669
caryclark@google.com66089e42013-04-10 15:55:37 +0000670void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
671 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000672 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000673 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000674 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
halcanary96fcdcc2015-08-27 07:41:13 -0700675 outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000676}
677
678void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000679 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700680 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000681 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000682 size_t index;
683 if (firstTest) {
684 index = count - 1;
685 while (index > 0 && tests[index].fun != firstTest) {
686 --index;
687 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000688#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700689 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000690#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000691 (*tests[index].fun)(reporter, tests[index].str);
692 if (tests[index].fun == stopTest) {
693 return;
694 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000695 }
696 index = reverse ? count - 1 : 0;
697 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700698 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000699 do {
caryclark54359292015-03-26 07:52:43 -0700700 if (tests[index].fun == skipTest) {
701 foundSkip = true;
702 }
703 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000704 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700705 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000706 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000707 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000708 }
caryclark03b03ca2015-04-23 09:13:37 -0700709 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000710 break;
711 }
712 index += reverse ? -1 : 1;
713 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700714#if DEBUG_SHOW_TEST_NAME
715 SkDebugf(
716 "\n"
717 "</div>\n"
718 "\n"
719 "<script type=\"text/javascript\">\n"
720 "\n"
721 "var testDivs = [\n"
722 );
723 index = reverse ? count - 1 : 0;
724 last = reverse ? 0 : count - 1;
725 foundSkip = !skipTest;
726 do {
727 if (tests[index].fun == skipTest) {
728 foundSkip = true;
729 }
730 if (foundSkip && tests[index].fun != firstTest) {
731 SkDebugf(" %s,\n", tests[index].str);
732 }
733 if (tests[index].fun == stopTest || index == last) {
734 break;
735 }
736 index += reverse ? -1 : 1;
737 } while (true);
738#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000739}