blob: 92b0e8746536d37bb9b727fb52cb989f88285560 [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
caryclark08bc8482015-04-24 09:08:57 -0700292const int gTestFirst = 20;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000293static 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 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700301 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000302 }
caryclark1049f122015-04-20 08:31:59 -0700303 SkDebugf("static void %s%d%s(skiatest::Reporter* reporter, const char* filename) {\n",
304 testName, gTestNo, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000305 *gTestOp.append() = shapeOp;
306 ++gTestNo;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000307 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700308 SkPathOpsDebug::ShowOnePath(a, "path", false);
309 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000310 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000311 SkDebugf("}\n");
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +0000312 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000313}
314
caryclark1049f122015-04-20 08:31:59 -0700315void ShowTestArray(const char* testName) {
316 if (!testName) {
317 testName = "xOp";
318 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000319 for (int x = gTestFirst; x < gTestNo; ++x) {
caryclark1049f122015-04-20 08:31:59 -0700320 SkDebugf(" TEST(%s%d%s),\n", testName, x, opSuffixes[gTestOp[x - gTestFirst]]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000321 }
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
caryclark1049f122015-04-20 08:31:59 -0700551bool testPathOpFailCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
552 const SkPathOp shapeOp, const char* testName) {
553 return innerPathOp(reporter, a, b, shapeOp, testName, false, false);
554}
555
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000556bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
557 const SkPathOp shapeOp, const char* testName) {
558#if DEBUG_SHOW_TEST_NAME
559 showName(a, b, shapeOp);
560#endif
caryclark1049f122015-04-20 08:31:59 -0700561 SkPath orig;
562 orig.lineTo(54, 43);
563 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000564 if (Op(a, b, shapeOp, &out) ) {
565 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
566 REPORTER_ASSERT(reporter, 0);
567 return false;
568 }
caryclark1049f122015-04-20 08:31:59 -0700569 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000570 return true;
571}
572
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000573bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
574 const SkPathOp shapeOp, const char* testName) {
caryclark65f55312014-11-13 06:58:52 -0800575 return innerPathOp(reporter, a, b, shapeOp, testName, true, true);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000576}
577
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000578SK_DECLARE_STATIC_MUTEX(gMutex);
579
mtklein406654b2014-09-03 15:34:37 -0700580void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000581#if 0 // doesn't work yet
582 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
583 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
584#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000585 if (reporter->verbose()) {
586 SkAutoMutexAcquire lock(gMutex);
587 testName = test;
588 size_t testNameSize = strlen(test);
589 SkFILEStream inFile("../../experimental/Intersection/op.htm");
590 if (inFile.isValid()) {
591 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000592 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000593 size_t inLen = inData.count();
594 inFile.read(inData.begin(), inLen);
595 inFile.setPath(NULL);
596 char* insert = strstr(inData.begin(), marker);
597 if (insert) {
598 insert += sizeof(marker) - 1;
599 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
600 testNumber = atoi(numLoc) + 1;
601 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000602 }
603 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000604}
605
caryclark@google.com66089e42013-04-10 15:55:37 +0000606void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
607 const char testFunction[] = "testSimplify(path);";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000608 const char* pathPrefix = NULL;
609 const char* nameSuffix = NULL;
610 if (pathFillType == SkPath::kEvenOdd_FillType) {
611 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
612 nameSuffix = "x";
613 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000614 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
615 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000616}
617
caryclark@google.com66089e42013-04-10 15:55:37 +0000618void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
619 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000620 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000621 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000622 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
623 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000624}
625
626void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000627 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700628 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000629 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000630 size_t index;
631 if (firstTest) {
632 index = count - 1;
633 while (index > 0 && tests[index].fun != firstTest) {
634 --index;
635 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000636#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700637 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000638#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000639 (*tests[index].fun)(reporter, tests[index].str);
640 if (tests[index].fun == stopTest) {
641 return;
642 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000643 }
644 index = reverse ? count - 1 : 0;
645 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700646 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000647 do {
caryclark54359292015-03-26 07:52:43 -0700648 if (tests[index].fun == skipTest) {
649 foundSkip = true;
650 }
651 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000652 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700653 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000654 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000655 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000656 }
caryclark03b03ca2015-04-23 09:13:37 -0700657 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000658 break;
659 }
660 index += reverse ? -1 : 1;
661 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700662#if DEBUG_SHOW_TEST_NAME
663 SkDebugf(
664 "\n"
665 "</div>\n"
666 "\n"
667 "<script type=\"text/javascript\">\n"
668 "\n"
669 "var testDivs = [\n"
670 );
671 index = reverse ? count - 1 : 0;
672 last = reverse ? 0 : count - 1;
673 foundSkip = !skipTest;
674 do {
675 if (tests[index].fun == skipTest) {
676 foundSkip = true;
677 }
678 if (foundSkip && tests[index].fun != firstTest) {
679 SkDebugf(" %s,\n", tests[index].str);
680 }
681 if (tests[index].fun == stopTest || index == last) {
682 break;
683 }
684 index += reverse ? -1 : 1;
685 } while (true);
686#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000687}