blob: c96cbcdae5ff11ddbf2285f9f2a862337d85a44d [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
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000025__SK_FORCE_IMAGE_DECODER_LINKING;
26
caryclark65b427c2014-09-18 10:32:57 -070027DEFINE_bool2(runFail, f, false, "run tests known to fail.");
caryclark54359292015-03-26 07:52:43 -070028DEFINE_bool2(runBinary, f, false, "run tests known to fail binary sect.");
caryclark65b427c2014-09-18 10:32:57 -070029
caryclark@google.com818b0cc2013-04-08 11:50:46 +000030static const char marker[] =
31 "</div>\n"
32 "\n"
33 "<script type=\"text/javascript\">\n"
34 "\n"
35 "var testDivs = [\n";
36
37static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070038 "kDifference_SkPathOp",
39 "kIntersect_SkPathOp",
40 "kUnion_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000041 "kXor_PathOp",
caryclark54359292015-03-26 07:52:43 -070042 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000043};
44
45static const char* opSuffixes[] = {
46 "d",
47 "i",
48 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000049 "o",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000050};
51
caryclark@google.comcffbcc32013-06-04 17:59:42 +000052#if DEBUG_SHOW_TEST_NAME
53static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000054 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000055 uint8_t verb;
56 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000057 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000058 bool firstPtSet = false;
59 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000060 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
61 switch (verb) {
62 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070063 if (firstPtSet && lastPtSet && firstPt != lastPt) {
64 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
65 firstPt.fX, firstPt.fY);
66 lastPtSet = false;
67 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000068 firstPt = pts[0];
69 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000070 continue;
71 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000072 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
73 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000074 lastPt = pts[1];
75 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000076 break;
77 case SkPath::kQuad_Verb:
78 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +000079 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 +000080 lastPt = pts[2];
81 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000082 break;
caryclark54359292015-03-26 07:52:43 -070083 case SkPath::kConic_Verb:
84 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
85 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
86 iter.conicWeight());
87 lastPt = pts[2];
88 lastPtSet = true;
89 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000090 case SkPath::kCubic_Verb:
91 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 +000092 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
93 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000094 lastPt = pts[3];
95 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000096 break;
97 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000098 if (firstPtSet && lastPtSet && firstPt != lastPt) {
99 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
100 firstPt.fX, firstPt.fY);
101 }
102 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000103 break;
104 default:
105 SkDEBUGFAIL("bad verb");
106 return;
107 }
108 }
caryclarkdac1d172014-06-17 05:15:38 -0700109 if (firstPtSet && lastPtSet && firstPt != lastPt) {
110 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
111 firstPt.fX, firstPt.fY);
112 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000113}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000114#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000115
116void showOp(const SkPathOp op) {
117 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700118 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000119 SkDebugf("op difference\n");
120 break;
caryclark54359292015-03-26 07:52:43 -0700121 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122 SkDebugf("op intersect\n");
123 break;
caryclark54359292015-03-26 07:52:43 -0700124 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000125 SkDebugf("op union\n");
126 break;
caryclark54359292015-03-26 07:52:43 -0700127 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000128 SkDebugf("op xor\n");
129 break;
caryclark54359292015-03-26 07:52:43 -0700130 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000131 SkDebugf("op reverse difference\n");
132 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000133 default:
134 SkASSERT(0);
135 }
136}
137
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000138#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000139static char hexorator(int x) {
140 if (x < 10) {
141 return x + '0';
142 }
143 x -= 10;
144 SkASSERT(x < 26);
145 return x + 'A';
146}
147#endif
148
149void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
150#if DEBUG_SHOW_TEST_NAME
151 state->fSerialNo[0] = hexorator(state->fA);
152 state->fSerialNo[1] = hexorator(state->fB);
153 state->fSerialNo[2] = hexorator(state->fC);
154 state->fSerialNo[3] = hexorator(state->fD);
155 state->fSerialNo[4] = hexorator(a);
156 state->fSerialNo[5] = hexorator(b);
157 state->fSerialNo[6] = hexorator(c);
158 state->fSerialNo[7] = hexorator(d);
159 state->fSerialNo[8] = '\0';
160 SkDebugf("%s\n", state->fSerialNo);
161 if (strcmp(state->fSerialNo, state->fKey) == 0) {
162 SkDebugf("%s\n", state->fPathStr);
163 }
164#endif
165}
166
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000167const int bitWidth = 64;
168const int bitHeight = 64;
169
170static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
171 SkRect larger = one.getBounds();
172 larger.join(two.getBounds());
173 SkScalar largerWidth = larger.width();
174 if (largerWidth < 4) {
175 largerWidth = 4;
176 }
177 SkScalar largerHeight = larger.height();
178 if (largerHeight < 4) {
179 largerHeight = 4;
180 }
181 SkScalar hScale = (bitWidth - 2) / largerWidth;
182 SkScalar vScale = (bitHeight - 2) / largerHeight;
183 scale.reset();
184 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700185 larger.fLeft *= hScale;
186 larger.fRight *= hScale;
187 larger.fTop *= vScale;
188 larger.fBottom *= vScale;
189 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
190 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
191 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
192 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
193 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000194}
195
196static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
197 int& error2x2) {
198 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000199 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000200 }
201 SkCanvas canvas(bits);
202 canvas.drawColor(SK_ColorWHITE);
203 SkPaint paint;
204 canvas.save();
205 const SkRect& bounds1 = scaledOne.getBounds();
206 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
207 canvas.drawPath(scaledOne, paint);
208 canvas.restore();
209 canvas.save();
210 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
211 canvas.drawPath(scaledTwo, paint);
212 canvas.restore();
213 int errors2 = 0;
214 int errors = 0;
215 for (int y = 0; y < bitHeight - 1; ++y) {
216 uint32_t* addr1 = bits.getAddr32(0, y);
217 uint32_t* addr2 = bits.getAddr32(0, y + 1);
218 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
219 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
220 for (int x = 0; x < bitWidth - 1; ++x) {
221 // count 2x2 blocks
222 bool err = addr1[x] != addr3[x];
223 if (err) {
224 errors2 += addr1[x + 1] != addr3[x + 1]
225 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
226 errors++;
227 }
228 }
229 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000230 error2x2 = errors2;
231 return errors;
232}
233
234static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
235 SkPath& scaledTwo, int& error2x2) {
236 SkMatrix scale;
237 scaleMatrix(one, two, scale);
238 one.transform(scale, &scaledOne);
239 two.transform(scale, &scaledTwo);
240 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
241}
242
243bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
244 if (!drawPaths) {
245 return true;
246 }
247 const SkRect& bounds1 = one.getBounds();
248 const SkRect& bounds2 = two.getBounds();
249 SkRect larger = bounds1;
250 larger.join(bounds2);
251 SkBitmap bits;
252 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000253 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000254 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
255 return false;
256 }
reed@google.come1ca7052013-12-17 19:22:07 +0000257 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000258 if (bitHeight >= (int) sizeof(out)) {
259 return false;
260 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000261 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000262 SkCanvas canvas(bits);
263 canvas.drawColor(SK_ColorWHITE);
264 SkPaint paint;
265 canvas.save();
266 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
267 canvas.drawPath(one, paint);
268 canvas.restore();
269 canvas.save();
270 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
271 canvas.drawPath(two, paint);
272 canvas.restore();
273 for (int y = 0; y < bitHeight; ++y) {
274 uint32_t* addr1 = bits.getAddr32(0, y);
275 int x;
276 char* outPtr = out;
277 for (x = 0; x < bitWidth; ++x) {
278 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
279 }
280 *outPtr++ = '|';
281 for (x = bitWidth; x < bitWidth * 2; ++x) {
282 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
283 }
284 *outPtr++ = '\0';
285 SkDebugf("%s\n", out);
286 }
287 return true;
288}
289
caryclark54359292015-03-26 07:52:43 -0700290int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000291 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000292 int errors2x2;
293 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000294 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000295 if (errors2x2 == 0) {
296 return 0;
297 }
298 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000299 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
300}
301
caryclarkaec25102015-04-29 08:28:30 -0700302const int gTestFirst = 41;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000303static int gTestNo = gTestFirst;
304static SkTDArray<SkPathOp> gTestOp;
305
306static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
307 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
308 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000309 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000310 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700311 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000312 }
caryclark1049f122015-04-20 08:31:59 -0700313 SkDebugf("static void %s%d%s(skiatest::Reporter* reporter, const char* filename) {\n",
314 testName, gTestNo, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000315 *gTestOp.append() = shapeOp;
316 ++gTestNo;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000317 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700318 SkPathOpsDebug::ShowOnePath(a, "path", false);
319 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000320 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000321 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700322 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000323}
324
caryclark1049f122015-04-20 08:31:59 -0700325void ShowTestArray(const char* testName) {
326 if (!testName) {
327 testName = "xOp";
328 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000329 for (int x = gTestFirst; x < gTestNo; ++x) {
caryclark1049f122015-04-20 08:31:59 -0700330 SkDebugf(" TEST(%s%d%s),\n", testName, x, opSuffixes[gTestOp[x - gTestFirst]]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000331 }
332}
333
bungemand6aeb6d2014-07-25 11:52:47 -0700334SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700335
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000336static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
337 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800338 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclark38a017b2015-05-13 10:13:17 -0700339 bool expectSuccess) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000340 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800341 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000342 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclark65f55312014-11-13 06:58:52 -0800343 if (!expectSuccess) {
caryclark38a017b2015-05-13 10:13:17 -0700344 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800345 REPORTER_ASSERT(reporter, 0);
346 }
347 return 0;
348 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000349 if (errors2x2 == 0) {
350 return 0;
351 }
caryclark38a017b2015-05-13 10:13:17 -0700352 if (errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000353 SkAutoMutexAcquire autoM(compareDebugOut3);
354 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700355 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000356 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700357 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000358 }
caryclark38a017b2015-05-13 10:13:17 -0700359 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000360}
361
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000362// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000363static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000364static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000365
366static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
367 outFile.writeText(testName);
368 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000369 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000370 if (nameSuffix) {
371 outFile.writeText(nameSuffix);
372 }
373}
374
375static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
376 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000377#if 0
caryclark54359292015-03-26 07:52:43 -0700378 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000379 writeTestName(nameSuffix, outFile);
380 outFile.writeText("\">\n");
381 if (pathPrefix) {
382 outFile.writeText(pathPrefix);
383 }
384 outFile.writeText(pathStr);
385 outFile.writeText("</div>\n\n");
386
387 outFile.writeText(marker);
388 outFile.writeText(" ");
389 writeTestName(nameSuffix, outFile);
390 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000391#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000392 outFile.writeText("static void ");
393 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000394 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000395 if (twoPaths) {
396 outFile.writeText(", pathB");
397 }
398 outFile.writeText(";\n");
399 if (pathPrefix) {
400 outFile.writeText(pathPrefix);
401 }
402 outFile.writeText(pathStr);
403 outFile.writeText(" ");
404 outFile.writeText(testFunction);
405 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000406#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000407 outFile.writeText("static void (*firstTest)() = ");
408 writeTestName(nameSuffix, outFile);
409 outFile.writeText(";\n\n");
410
411 outFile.writeText("static struct {\n");
412 outFile.writeText(" void (*fun)();\n");
413 outFile.writeText(" const char* str;\n");
414 outFile.writeText("} tests[] = {\n");
415 outFile.writeText(" TEST(");
416 writeTestName(nameSuffix, outFile);
417 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000418#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000419 outFile.flush();
420}
421
bungemand6aeb6d2014-07-25 11:52:47 -0700422SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700423
caryclark@google.com66089e42013-04-10 15:55:37 +0000424bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
425 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000426 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
427 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700428 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000429 if (!Simplify(path, &out)) {
430 SkDebugf("%s did not expect failure\n", __FUNCTION__);
431 REPORTER_ASSERT(state.fReporter, 0);
432 return false;
433 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000434 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000435 return true;
436 }
halcanary96fcdcc2015-08-27 07:41:13 -0700437 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700438 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000439 SkAutoMutexAcquire autoM(simplifyDebugOut);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000440 char temp[8192];
441 sk_bzero(temp, sizeof(temp));
442 SkMemoryWStream stream(temp, sizeof(temp));
halcanary96fcdcc2015-08-27 07:41:13 -0700443 const char* pathPrefix = nullptr;
444 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000445 if (fillType == SkPath::kEvenOdd_FillType) {
446 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
447 nameSuffix = "x";
448 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000449 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000450 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800451 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000452 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000453 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000454 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000455 return result == 0;
456}
457
caryclark54359292015-03-26 07:52:43 -0700458static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
459 bool checkFail) {
460#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000461 showPathData(path);
462#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000463 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000464 if (!Simplify(path, &out)) {
caryclark54359292015-03-26 07:52:43 -0700465 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
caryclark@google.com66560ca2013-04-26 19:51:16 +0000466 REPORTER_ASSERT(reporter, 0);
467 return false;
468 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000469 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700470 int errors = comparePaths(reporter, filename, path, out, bitmap);
471 if (!checkFail) {
472 if (!errors) {
473 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
474 REPORTER_ASSERT(reporter, 0);
475 return false;
476 }
477 } else if (errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000478 REPORTER_ASSERT(reporter, 0);
479 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000480 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700481 return errors == 0;
482}
483
484bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
485 return inner_simplify(reporter, path, filename, true);
486}
487
488bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
489 bool checkFail) {
490 return inner_simplify(reporter, path, filename, checkFail);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000491}
492
caryclark@google.coma5e55922013-05-07 18:51:31 +0000493#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000494static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
495 SkDebugf("\n");
496 showPathData(a);
497 showOp(shapeOp);
498 showPathData(b);
499}
500#endif
501
caryclarkd4349722015-07-23 12:40:22 -0700502bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result,
503 bool expectSuccess SkDEBUGPARAMS(const char* testName));
caryclark624637c2015-05-11 07:21:27 -0700504
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000505static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark38a017b2015-05-13 10:13:17 -0700506 const SkPathOp shapeOp, const char* testName, bool expectSuccess) {
caryclark54359292015-03-26 07:52:43 -0700507#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000508 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000509#endif
510 SkPath out;
caryclarkd4349722015-07-23 12:40:22 -0700511 if (!OpDebug(a, b, shapeOp, &out, expectSuccess SkDEBUGPARAMS(testName))) {
caryclark@google.com66560ca2013-04-26 19:51:16 +0000512 SkDebugf("%s did not expect failure\n", __FUNCTION__);
513 REPORTER_ASSERT(reporter, 0);
514 return false;
515 }
caryclark54359292015-03-26 07:52:43 -0700516 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000517 return true;
518 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000519 SkPath pathOut, scaledPathOut;
520 SkRegion rgnA, rgnB, openClip, rgnOut;
521 openClip.setRect(-16000, -16000, 16000, 16000);
522 rgnA.setPath(a, openClip);
523 rgnB.setPath(b, openClip);
524 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
525 rgnOut.getBoundaryPath(&pathOut);
526
527 SkMatrix scale;
528 scaleMatrix(a, b, scale);
529 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
530 SkPath scaledA, scaledB;
531 scaledA.addPath(a, scale);
532 scaledA.setFillType(a.getFillType());
533 scaledB.addPath(b, scale);
534 scaledB.setFillType(b.getFillType());
535 scaledRgnA.setPath(scaledA, openClip);
536 scaledRgnB.setPath(scaledB, openClip);
537 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
538 scaledRgnOut.getBoundaryPath(&scaledPathOut);
539 SkBitmap bitmap;
540 SkPath scaledOut;
541 scaledOut.addPath(out, scale);
542 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000543 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclark38a017b2015-05-13 10:13:17 -0700544 a, b, shapeOp, scale, expectSuccess);
caryclark@google.com66089e42013-04-10 15:55:37 +0000545 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000546 return result == 0;
547}
548
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000549bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800550 const SkPathOp shapeOp, const char* testName) {
caryclark38a017b2015-05-13 10:13:17 -0700551 return innerPathOp(reporter, a, b, shapeOp, testName, true);
caryclark65f55312014-11-13 06:58:52 -0800552}
553
554bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
555 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark38a017b2015-05-13 10:13:17 -0700556 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000557}
558
caryclark1049f122015-04-20 08:31:59 -0700559bool testPathOpFailCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
560 const SkPathOp shapeOp, const char* testName) {
caryclark38a017b2015-05-13 10:13:17 -0700561 return innerPathOp(reporter, a, b, shapeOp, testName, false);
caryclark1049f122015-04-20 08:31:59 -0700562}
563
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000564bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
565 const SkPathOp shapeOp, const char* testName) {
566#if DEBUG_SHOW_TEST_NAME
567 showName(a, b, shapeOp);
568#endif
caryclark1049f122015-04-20 08:31:59 -0700569 SkPath orig;
570 orig.lineTo(54, 43);
571 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000572 if (Op(a, b, shapeOp, &out) ) {
573 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
574 REPORTER_ASSERT(reporter, 0);
575 return false;
576 }
caryclark1049f122015-04-20 08:31:59 -0700577 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000578 return true;
579}
580
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000581SK_DECLARE_STATIC_MUTEX(gMutex);
582
mtklein406654b2014-09-03 15:34:37 -0700583void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000584#if 0 // doesn't work yet
585 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
586 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
587#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000588 if (reporter->verbose()) {
589 SkAutoMutexAcquire lock(gMutex);
590 testName = test;
591 size_t testNameSize = strlen(test);
592 SkFILEStream inFile("../../experimental/Intersection/op.htm");
593 if (inFile.isValid()) {
594 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000595 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000596 size_t inLen = inData.count();
597 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700598 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000599 char* insert = strstr(inData.begin(), marker);
600 if (insert) {
601 insert += sizeof(marker) - 1;
602 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
603 testNumber = atoi(numLoc) + 1;
604 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000605 }
606 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000607}
608
caryclark@google.com66089e42013-04-10 15:55:37 +0000609void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
610 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700611 const char* pathPrefix = nullptr;
612 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000613 if (pathFillType == SkPath::kEvenOdd_FillType) {
614 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
615 nameSuffix = "x";
616 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000617 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
618 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000619}
620
caryclark@google.com66089e42013-04-10 15:55:37 +0000621void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
622 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000623 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000624 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000625 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
halcanary96fcdcc2015-08-27 07:41:13 -0700626 outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000627}
628
629void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000630 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700631 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000632 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000633 size_t index;
634 if (firstTest) {
635 index = count - 1;
636 while (index > 0 && tests[index].fun != firstTest) {
637 --index;
638 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000639#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700640 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000641#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000642 (*tests[index].fun)(reporter, tests[index].str);
643 if (tests[index].fun == stopTest) {
644 return;
645 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000646 }
647 index = reverse ? count - 1 : 0;
648 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700649 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000650 do {
caryclark54359292015-03-26 07:52:43 -0700651 if (tests[index].fun == skipTest) {
652 foundSkip = true;
653 }
654 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000655 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700656 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000657 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000658 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000659 }
caryclark03b03ca2015-04-23 09:13:37 -0700660 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000661 break;
662 }
663 index += reverse ? -1 : 1;
664 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700665#if DEBUG_SHOW_TEST_NAME
666 SkDebugf(
667 "\n"
668 "</div>\n"
669 "\n"
670 "<script type=\"text/javascript\">\n"
671 "\n"
672 "var testDivs = [\n"
673 );
674 index = reverse ? count - 1 : 0;
675 last = reverse ? 0 : count - 1;
676 foundSkip = !skipTest;
677 do {
678 if (tests[index].fun == skipTest) {
679 foundSkip = true;
680 }
681 if (foundSkip && tests[index].fun != firstTest) {
682 SkDebugf(" %s,\n", tests[index].str);
683 }
684 if (tests[index].fun == stopTest || index == last) {
685 break;
686 }
687 index += reverse ? -1 : 1;
688 } while (true);
689#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000690}