blob: 5889ac428b5c16d943608fa5eb4d9f825c8dbed8 [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"
skia.committer@gmail.come02c5da2014-04-26 03:04:35 +000014#include "SkPaint.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000015#include "SkRTConf.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000016#include "SkStream.h"
mtklein406654b2014-09-03 15:34:37 -070017#include "SkTaskGroup.h"
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000018#include "SkThread.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000019
20#ifdef SK_BUILD_FOR_MAC
21#include <sys/sysctl.h>
22#endif
23
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000024__SK_FORCE_IMAGE_DECODER_LINKING;
25
caryclark65b427c2014-09-18 10:32:57 -070026DEFINE_bool2(runFail, f, false, "run tests known to fail.");
caryclark54359292015-03-26 07:52:43 -070027DEFINE_bool2(runBinary, f, false, "run tests known to fail binary sect.");
caryclark65b427c2014-09-18 10:32:57 -070028
caryclark@google.com818b0cc2013-04-08 11:50:46 +000029static const char marker[] =
30 "</div>\n"
31 "\n"
32 "<script type=\"text/javascript\">\n"
33 "\n"
34 "var testDivs = [\n";
35
36static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070037 "kDifference_SkPathOp",
38 "kIntersect_SkPathOp",
39 "kUnion_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000040 "kXor_PathOp",
caryclark54359292015-03-26 07:52:43 -070041 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000042};
43
44static const char* opSuffixes[] = {
45 "d",
46 "i",
47 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000048 "o",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000049};
50
caryclark@google.comcffbcc32013-06-04 17:59:42 +000051#if DEBUG_SHOW_TEST_NAME
52static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000053 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000054 uint8_t verb;
55 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000056 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000057 bool firstPtSet = false;
58 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000059 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
60 switch (verb) {
61 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070062 if (firstPtSet && lastPtSet && firstPt != lastPt) {
63 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
64 firstPt.fX, firstPt.fY);
65 lastPtSet = false;
66 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000067 firstPt = pts[0];
68 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000069 continue;
70 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000071 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
72 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000073 lastPt = pts[1];
74 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000075 break;
76 case SkPath::kQuad_Verb:
77 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +000078 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 +000079 lastPt = pts[2];
80 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000081 break;
caryclark54359292015-03-26 07:52:43 -070082 case SkPath::kConic_Verb:
83 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
84 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
85 iter.conicWeight());
86 lastPt = pts[2];
87 lastPtSet = true;
88 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000089 case SkPath::kCubic_Verb:
90 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 +000091 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
92 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000093 lastPt = pts[3];
94 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000095 break;
96 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000097 if (firstPtSet && lastPtSet && firstPt != lastPt) {
98 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
99 firstPt.fX, firstPt.fY);
100 }
101 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000102 break;
103 default:
104 SkDEBUGFAIL("bad verb");
105 return;
106 }
107 }
caryclarkdac1d172014-06-17 05:15:38 -0700108 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 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000112}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000113#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000114
115void showOp(const SkPathOp op) {
116 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700117 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000118 SkDebugf("op difference\n");
119 break;
caryclark54359292015-03-26 07:52:43 -0700120 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000121 SkDebugf("op intersect\n");
122 break;
caryclark54359292015-03-26 07:52:43 -0700123 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000124 SkDebugf("op union\n");
125 break;
caryclark54359292015-03-26 07:52:43 -0700126 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000127 SkDebugf("op xor\n");
128 break;
caryclark54359292015-03-26 07:52:43 -0700129 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000130 SkDebugf("op reverse difference\n");
131 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000132 default:
133 SkASSERT(0);
134 }
135}
136
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000137#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000138static char hexorator(int x) {
139 if (x < 10) {
140 return x + '0';
141 }
142 x -= 10;
143 SkASSERT(x < 26);
144 return x + 'A';
145}
146#endif
147
148void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
149#if DEBUG_SHOW_TEST_NAME
150 state->fSerialNo[0] = hexorator(state->fA);
151 state->fSerialNo[1] = hexorator(state->fB);
152 state->fSerialNo[2] = hexorator(state->fC);
153 state->fSerialNo[3] = hexorator(state->fD);
154 state->fSerialNo[4] = hexorator(a);
155 state->fSerialNo[5] = hexorator(b);
156 state->fSerialNo[6] = hexorator(c);
157 state->fSerialNo[7] = hexorator(d);
158 state->fSerialNo[8] = '\0';
159 SkDebugf("%s\n", state->fSerialNo);
160 if (strcmp(state->fSerialNo, state->fKey) == 0) {
161 SkDebugf("%s\n", state->fPathStr);
162 }
163#endif
164}
165
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000166const int bitWidth = 64;
167const int bitHeight = 64;
168
169static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
170 SkRect larger = one.getBounds();
171 larger.join(two.getBounds());
172 SkScalar largerWidth = larger.width();
173 if (largerWidth < 4) {
174 largerWidth = 4;
175 }
176 SkScalar largerHeight = larger.height();
177 if (largerHeight < 4) {
178 largerHeight = 4;
179 }
180 SkScalar hScale = (bitWidth - 2) / largerWidth;
181 SkScalar vScale = (bitHeight - 2) / largerHeight;
182 scale.reset();
183 scale.preScale(hScale, vScale);
184}
185
186static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
187 int& error2x2) {
188 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000189 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000190 }
191 SkCanvas canvas(bits);
192 canvas.drawColor(SK_ColorWHITE);
193 SkPaint paint;
194 canvas.save();
195 const SkRect& bounds1 = scaledOne.getBounds();
196 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
197 canvas.drawPath(scaledOne, paint);
198 canvas.restore();
199 canvas.save();
200 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
201 canvas.drawPath(scaledTwo, paint);
202 canvas.restore();
203 int errors2 = 0;
204 int errors = 0;
205 for (int y = 0; y < bitHeight - 1; ++y) {
206 uint32_t* addr1 = bits.getAddr32(0, y);
207 uint32_t* addr2 = bits.getAddr32(0, y + 1);
208 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
209 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
210 for (int x = 0; x < bitWidth - 1; ++x) {
211 // count 2x2 blocks
212 bool err = addr1[x] != addr3[x];
213 if (err) {
214 errors2 += addr1[x + 1] != addr3[x + 1]
215 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
216 errors++;
217 }
218 }
219 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000220 error2x2 = errors2;
221 return errors;
222}
223
224static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
225 SkPath& scaledTwo, int& error2x2) {
226 SkMatrix scale;
227 scaleMatrix(one, two, scale);
228 one.transform(scale, &scaledOne);
229 two.transform(scale, &scaledTwo);
230 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
231}
232
233bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
234 if (!drawPaths) {
235 return true;
236 }
237 const SkRect& bounds1 = one.getBounds();
238 const SkRect& bounds2 = two.getBounds();
239 SkRect larger = bounds1;
240 larger.join(bounds2);
241 SkBitmap bits;
242 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000243 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000244 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
245 return false;
246 }
reed@google.come1ca7052013-12-17 19:22:07 +0000247 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000248 if (bitHeight >= (int) sizeof(out)) {
249 return false;
250 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000251 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000252 SkCanvas canvas(bits);
253 canvas.drawColor(SK_ColorWHITE);
254 SkPaint paint;
255 canvas.save();
256 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
257 canvas.drawPath(one, paint);
258 canvas.restore();
259 canvas.save();
260 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
261 canvas.drawPath(two, paint);
262 canvas.restore();
263 for (int y = 0; y < bitHeight; ++y) {
264 uint32_t* addr1 = bits.getAddr32(0, y);
265 int x;
266 char* outPtr = out;
267 for (x = 0; x < bitWidth; ++x) {
268 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
269 }
270 *outPtr++ = '|';
271 for (x = bitWidth; x < bitWidth * 2; ++x) {
272 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
273 }
274 *outPtr++ = '\0';
275 SkDebugf("%s\n", out);
276 }
277 return true;
278}
279
caryclark54359292015-03-26 07:52:43 -0700280int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000281 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000282 int errors2x2;
283 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000284 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000285 if (errors2x2 == 0) {
286 return 0;
287 }
288 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000289 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
290}
291
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000292const int gTestFirst = 4;
293static int gTestNo = gTestFirst;
294static SkTDArray<SkPathOp> gTestOp;
295
296static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
297 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
298 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000299 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000300 SkString defaultTestName;
301 if (!testName) {
302 defaultTestName.printf("xOp%d%s", gTestNo, opSuffixes[shapeOp]);
303 testName = defaultTestName.c_str();
304 }
305 SkDebugf("static void %s(skiatest::Reporter* reporter, const char* filename) {\n", testName);
306 *gTestOp.append() = shapeOp;
307 ++gTestNo;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000308 SkDebugf(" SkPath path, pathB;\n");
caryclark54359292015-03-26 07:52:43 -0700309#if 0 && DEBUG_SHOW_TEST_NAME
caryclark19eb3b22014-07-18 05:08:14 -0700310 SkPathOpsDebug::ShowOnePath(a, "path", false);
311 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
312#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000313 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000314 SkDebugf("}\n");
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +0000315 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000316}
317
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000318void ShowTestArray() {
319 for (int x = gTestFirst; x < gTestNo; ++x) {
320 SkDebugf(" TEST(xOp%d%s),\n", x, opSuffixes[gTestOp[x - gTestFirst]]);
321 }
322}
323
bungemand6aeb6d2014-07-25 11:52:47 -0700324SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
325SK_DECLARE_STATIC_MUTEX(compareDebugOut4);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000326static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
327 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800328 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
329 bool expectSuccess) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000330 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800331 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000332 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclark65f55312014-11-13 06:58:52 -0800333 if (!expectSuccess) {
334 if (errors2x2 <= MAX_ERRORS) {
335 REPORTER_ASSERT(reporter, 0);
336 }
337 return 0;
338 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000339 if (errors2x2 == 0) {
340 return 0;
341 }
caryclark54359292015-03-26 07:52:43 -0700342 if (errors2x2 > MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000343 SkAutoMutexAcquire autoM(compareDebugOut3);
caryclarkdac1d172014-06-17 05:15:38 -0700344 SkDebugf("\n*** this test fails ***\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000345 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000346 REPORTER_ASSERT(reporter, 0);
caryclark54359292015-03-26 07:52:43 -0700347 } else if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000348 SkAutoMutexAcquire autoM(compareDebugOut4);
349 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000350 }
351 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
352}
353
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000354// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000355static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000356static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000357
358static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
359 outFile.writeText(testName);
360 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000361 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000362 if (nameSuffix) {
363 outFile.writeText(nameSuffix);
364 }
365}
366
367static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
368 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000369#if 0
caryclark54359292015-03-26 07:52:43 -0700370 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000371 writeTestName(nameSuffix, outFile);
372 outFile.writeText("\">\n");
373 if (pathPrefix) {
374 outFile.writeText(pathPrefix);
375 }
376 outFile.writeText(pathStr);
377 outFile.writeText("</div>\n\n");
378
379 outFile.writeText(marker);
380 outFile.writeText(" ");
381 writeTestName(nameSuffix, outFile);
382 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000383#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000384 outFile.writeText("static void ");
385 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000386 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000387 if (twoPaths) {
388 outFile.writeText(", pathB");
389 }
390 outFile.writeText(";\n");
391 if (pathPrefix) {
392 outFile.writeText(pathPrefix);
393 }
394 outFile.writeText(pathStr);
395 outFile.writeText(" ");
396 outFile.writeText(testFunction);
397 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000398#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000399 outFile.writeText("static void (*firstTest)() = ");
400 writeTestName(nameSuffix, outFile);
401 outFile.writeText(";\n\n");
402
403 outFile.writeText("static struct {\n");
404 outFile.writeText(" void (*fun)();\n");
405 outFile.writeText(" const char* str;\n");
406 outFile.writeText("} tests[] = {\n");
407 outFile.writeText(" TEST(");
408 writeTestName(nameSuffix, outFile);
409 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000410#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000411 outFile.flush();
412}
413
bungemand6aeb6d2014-07-25 11:52:47 -0700414SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700415
caryclark@google.com66089e42013-04-10 15:55:37 +0000416bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
417 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000418 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
419 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700420 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000421 if (!Simplify(path, &out)) {
422 SkDebugf("%s did not expect failure\n", __FUNCTION__);
423 REPORTER_ASSERT(state.fReporter, 0);
424 return false;
425 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000426 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000427 return true;
428 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000429 int result = comparePaths(state.fReporter, NULL, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700430 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000431 SkAutoMutexAcquire autoM(simplifyDebugOut);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000432 char temp[8192];
433 sk_bzero(temp, sizeof(temp));
434 SkMemoryWStream stream(temp, sizeof(temp));
435 const char* pathPrefix = NULL;
436 const char* nameSuffix = NULL;
437 if (fillType == SkPath::kEvenOdd_FillType) {
438 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
439 nameSuffix = "x";
440 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000441 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000442 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800443 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000444 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000445 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000446 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000447 return result == 0;
448}
449
caryclark54359292015-03-26 07:52:43 -0700450static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
451 bool checkFail) {
452#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000453 showPathData(path);
454#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000455 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000456 if (!Simplify(path, &out)) {
caryclark54359292015-03-26 07:52:43 -0700457 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
caryclark@google.com66560ca2013-04-26 19:51:16 +0000458 REPORTER_ASSERT(reporter, 0);
459 return false;
460 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000461 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700462 int errors = comparePaths(reporter, filename, path, out, bitmap);
463 if (!checkFail) {
464 if (!errors) {
465 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
466 REPORTER_ASSERT(reporter, 0);
467 return false;
468 }
469 } else if (errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000470 REPORTER_ASSERT(reporter, 0);
471 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000472 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700473 return errors == 0;
474}
475
476bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
477 return inner_simplify(reporter, path, filename, true);
478}
479
480bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
481 bool checkFail) {
482 return inner_simplify(reporter, path, filename, checkFail);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000483}
484
caryclark@google.coma5e55922013-05-07 18:51:31 +0000485#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000486static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
487 SkDebugf("\n");
488 showPathData(a);
489 showOp(shapeOp);
490 showPathData(b);
491}
492#endif
493
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000494static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800495 const SkPathOp shapeOp, const char* testName, bool threaded, bool expectSuccess) {
caryclark54359292015-03-26 07:52:43 -0700496#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000497 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000498#endif
499 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000500 if (!Op(a, b, shapeOp, &out) ) {
501 SkDebugf("%s did not expect failure\n", __FUNCTION__);
502 REPORTER_ASSERT(reporter, 0);
503 return false;
504 }
caryclark54359292015-03-26 07:52:43 -0700505 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000506 return true;
507 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000508 SkPath pathOut, scaledPathOut;
509 SkRegion rgnA, rgnB, openClip, rgnOut;
510 openClip.setRect(-16000, -16000, 16000, 16000);
511 rgnA.setPath(a, openClip);
512 rgnB.setPath(b, openClip);
513 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
514 rgnOut.getBoundaryPath(&pathOut);
515
516 SkMatrix scale;
517 scaleMatrix(a, b, scale);
518 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
519 SkPath scaledA, scaledB;
520 scaledA.addPath(a, scale);
521 scaledA.setFillType(a.getFillType());
522 scaledB.addPath(b, scale);
523 scaledB.setFillType(b.getFillType());
524 scaledRgnA.setPath(scaledA, openClip);
525 scaledRgnB.setPath(scaledB, openClip);
526 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
527 scaledRgnOut.getBoundaryPath(&scaledPathOut);
528 SkBitmap bitmap;
529 SkPath scaledOut;
530 scaledOut.addPath(out, scale);
531 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000532 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclark65f55312014-11-13 06:58:52 -0800533 a, b, shapeOp, scale, expectSuccess);
caryclark54359292015-03-26 07:52:43 -0700534 if (result) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000535 REPORTER_ASSERT(reporter, 0);
536 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000537 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000538 return result == 0;
539}
540
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000541bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800542 const SkPathOp shapeOp, const char* testName) {
543 return innerPathOp(reporter, a, b, shapeOp, testName, false, true);
544}
545
546bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
547 const SkPathOp shapeOp, const char* testName, bool checkFail) {
548 return innerPathOp(reporter, a, b, shapeOp, testName, false, checkFail);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000549}
550
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000551bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
552 const SkPathOp shapeOp, const char* testName) {
553#if DEBUG_SHOW_TEST_NAME
554 showName(a, b, shapeOp);
555#endif
556 SkPath out;
557 if (Op(a, b, shapeOp, &out) ) {
558 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
559 REPORTER_ASSERT(reporter, 0);
560 return false;
561 }
562 return true;
563}
564
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000565bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
566 const SkPathOp shapeOp, const char* testName) {
caryclark65f55312014-11-13 06:58:52 -0800567 return innerPathOp(reporter, a, b, shapeOp, testName, true, true);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000568}
569
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000570SK_DECLARE_STATIC_MUTEX(gMutex);
571
mtklein406654b2014-09-03 15:34:37 -0700572void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000573#if 0 // doesn't work yet
574 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
575 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
576#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000577 if (reporter->verbose()) {
578 SkAutoMutexAcquire lock(gMutex);
579 testName = test;
580 size_t testNameSize = strlen(test);
581 SkFILEStream inFile("../../experimental/Intersection/op.htm");
582 if (inFile.isValid()) {
583 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000584 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000585 size_t inLen = inData.count();
586 inFile.read(inData.begin(), inLen);
587 inFile.setPath(NULL);
588 char* insert = strstr(inData.begin(), marker);
589 if (insert) {
590 insert += sizeof(marker) - 1;
591 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
592 testNumber = atoi(numLoc) + 1;
593 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000594 }
595 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000596}
597
caryclark@google.com66089e42013-04-10 15:55:37 +0000598void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
599 const char testFunction[] = "testSimplify(path);";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000600 const char* pathPrefix = NULL;
601 const char* nameSuffix = NULL;
602 if (pathFillType == SkPath::kEvenOdd_FillType) {
603 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
604 nameSuffix = "x";
605 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000606 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
607 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000608}
609
caryclark@google.com66089e42013-04-10 15:55:37 +0000610void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
611 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000612 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000613 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000614 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
615 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000616}
617
618void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000619 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700620 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000621 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000622 size_t index;
623 if (firstTest) {
624 index = count - 1;
625 while (index > 0 && tests[index].fun != firstTest) {
626 --index;
627 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000628#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700629 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000630#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000631 (*tests[index].fun)(reporter, tests[index].str);
632 if (tests[index].fun == stopTest) {
633 return;
634 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000635 }
636 index = reverse ? count - 1 : 0;
637 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700638 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000639 do {
caryclark54359292015-03-26 07:52:43 -0700640 if (tests[index].fun == skipTest) {
641 foundSkip = true;
642 }
643 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000644 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700645 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000646 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000647 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000648 }
649 if (tests[index].fun == stopTest) {
650 SkDebugf("lastTest\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000651 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000652 }
653 if (index == last) {
654 break;
655 }
656 index += reverse ? -1 : 1;
657 } while (true);
658}