blob: c668963185e4a1249fd69d7f91f20cbe5637d7fd [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"
12#include "SkMatrix.h"
mtklein1b249332015-07-07 12:21:21 -070013#include "SkMutex.h"
skia.committer@gmail.come02c5da2014-04-26 03:04:35 +000014#include "SkPaint.h"
Mike Reed267be7f2017-02-13 09:32:54 -050015#include "SkRegion.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000016#include "SkStream.h"
17
bungeman60e0fee2015-08-26 05:15:46 -070018#include <stdlib.h>
19
caryclark@google.com818b0cc2013-04-08 11:50:46 +000020#ifdef SK_BUILD_FOR_MAC
21#include <sys/sysctl.h>
22#endif
23
Cary Clark389c5572017-04-14 07:46:07 -040024// std::to_string isn't implemented on android
25#include <sstream>
26
27template <typename T>
28std::string std_to_string(T value)
29{
30 std::ostringstream os ;
31 os << value ;
32 return os.str() ;
33}
34
caryclark55888e42016-07-18 10:01:36 -070035bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
36 SkDEBUGPARAMS(bool skipAssert)
37 SkDEBUGPARAMS(const char* testName));
38
39bool SimplifyDebug(const SkPath& one, SkPath* result
40 SkDEBUGPARAMS(bool skipAssert)
41 SkDEBUGPARAMS(const char* testName));
42
caryclark@google.com818b0cc2013-04-08 11:50:46 +000043static const char marker[] =
44 "</div>\n"
45 "\n"
46 "<script type=\"text/javascript\">\n"
47 "\n"
48 "var testDivs = [\n";
49
50static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070051 "kDifference_SkPathOp",
52 "kIntersect_SkPathOp",
53 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070054 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070055 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000056};
57
58static const char* opSuffixes[] = {
59 "d",
60 "i",
61 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000062 "o",
caryclark55888e42016-07-18 10:01:36 -070063 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000064};
65
caryclarkd5b91732016-08-09 05:04:29 -070066enum class ExpectSuccess {
67 kNo,
68 kYes,
69 kFlaky
70};
71
72enum class SkipAssert {
73 kNo,
74 kYes
75};
76
77enum class ExpectMatch {
78 kNo,
79 kYes,
80 kFlaky
81};
82
caryclark@google.comcffbcc32013-06-04 17:59:42 +000083#if DEBUG_SHOW_TEST_NAME
84static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000085 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000086 uint8_t verb;
87 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000088 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000089 bool firstPtSet = false;
90 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000091 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
92 switch (verb) {
93 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070094 if (firstPtSet && lastPtSet && firstPt != lastPt) {
95 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
96 firstPt.fX, firstPt.fY);
97 lastPtSet = false;
98 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000099 firstPt = pts[0];
100 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000101 continue;
102 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +0000103 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
104 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000105 lastPt = pts[1];
106 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000107 break;
108 case SkPath::kQuad_Verb:
109 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000110 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 +0000111 lastPt = pts[2];
112 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000113 break;
caryclark54359292015-03-26 07:52:43 -0700114 case SkPath::kConic_Verb:
115 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
116 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
117 iter.conicWeight());
118 lastPt = pts[2];
119 lastPtSet = true;
120 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000121 case SkPath::kCubic_Verb:
122 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 +0000123 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
124 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000125 lastPt = pts[3];
126 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000127 break;
128 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000129 if (firstPtSet && lastPtSet && firstPt != lastPt) {
130 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
131 firstPt.fX, firstPt.fY);
132 }
133 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000134 break;
135 default:
136 SkDEBUGFAIL("bad verb");
137 return;
138 }
139 }
caryclarkdac1d172014-06-17 05:15:38 -0700140 if (firstPtSet && lastPtSet && firstPt != lastPt) {
141 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
142 firstPt.fX, firstPt.fY);
143 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000144}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000145#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000146
147void showOp(const SkPathOp op) {
148 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700149 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000150 SkDebugf("op difference\n");
151 break;
caryclark54359292015-03-26 07:52:43 -0700152 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000153 SkDebugf("op intersect\n");
154 break;
caryclark54359292015-03-26 07:52:43 -0700155 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000156 SkDebugf("op union\n");
157 break;
caryclark54359292015-03-26 07:52:43 -0700158 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000159 SkDebugf("op xor\n");
160 break;
caryclark54359292015-03-26 07:52:43 -0700161 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000162 SkDebugf("op reverse difference\n");
163 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000164 default:
165 SkASSERT(0);
166 }
167}
168
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000169#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000170static char hexorator(int x) {
171 if (x < 10) {
172 return x + '0';
173 }
174 x -= 10;
175 SkASSERT(x < 26);
176 return x + 'A';
177}
178#endif
179
180void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
181#if DEBUG_SHOW_TEST_NAME
182 state->fSerialNo[0] = hexorator(state->fA);
183 state->fSerialNo[1] = hexorator(state->fB);
184 state->fSerialNo[2] = hexorator(state->fC);
185 state->fSerialNo[3] = hexorator(state->fD);
186 state->fSerialNo[4] = hexorator(a);
187 state->fSerialNo[5] = hexorator(b);
188 state->fSerialNo[6] = hexorator(c);
189 state->fSerialNo[7] = hexorator(d);
190 state->fSerialNo[8] = '\0';
191 SkDebugf("%s\n", state->fSerialNo);
192 if (strcmp(state->fSerialNo, state->fKey) == 0) {
Cary Clark389c5572017-04-14 07:46:07 -0400193 SkDebugf("%s\n", state->fPathStr.c_str());
caryclark@google.com03610322013-04-18 15:58:21 +0000194 }
195#endif
196}
197
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000198const int bitWidth = 64;
199const int bitHeight = 64;
200
201static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
202 SkRect larger = one.getBounds();
203 larger.join(two.getBounds());
204 SkScalar largerWidth = larger.width();
205 if (largerWidth < 4) {
206 largerWidth = 4;
207 }
208 SkScalar largerHeight = larger.height();
209 if (largerHeight < 4) {
210 largerHeight = 4;
211 }
212 SkScalar hScale = (bitWidth - 2) / largerWidth;
213 SkScalar vScale = (bitHeight - 2) / largerHeight;
214 scale.reset();
215 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700216 larger.fLeft *= hScale;
217 larger.fRight *= hScale;
218 larger.fTop *= vScale;
219 larger.fBottom *= vScale;
220 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
221 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
222 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
223 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
224 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000225}
226
227static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
228 int& error2x2) {
229 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000230 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000231 }
232 SkCanvas canvas(bits);
233 canvas.drawColor(SK_ColorWHITE);
234 SkPaint paint;
235 canvas.save();
236 const SkRect& bounds1 = scaledOne.getBounds();
237 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
238 canvas.drawPath(scaledOne, paint);
239 canvas.restore();
240 canvas.save();
241 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
242 canvas.drawPath(scaledTwo, paint);
243 canvas.restore();
244 int errors2 = 0;
245 int errors = 0;
246 for (int y = 0; y < bitHeight - 1; ++y) {
247 uint32_t* addr1 = bits.getAddr32(0, y);
248 uint32_t* addr2 = bits.getAddr32(0, y + 1);
249 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
250 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
251 for (int x = 0; x < bitWidth - 1; ++x) {
252 // count 2x2 blocks
253 bool err = addr1[x] != addr3[x];
254 if (err) {
255 errors2 += addr1[x + 1] != addr3[x + 1]
256 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
257 errors++;
258 }
259 }
260 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000261 error2x2 = errors2;
262 return errors;
263}
264
265static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
266 SkPath& scaledTwo, int& error2x2) {
267 SkMatrix scale;
268 scaleMatrix(one, two, scale);
269 one.transform(scale, &scaledOne);
270 two.transform(scale, &scaledTwo);
271 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
272}
273
274bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
275 if (!drawPaths) {
276 return true;
277 }
278 const SkRect& bounds1 = one.getBounds();
279 const SkRect& bounds2 = two.getBounds();
280 SkRect larger = bounds1;
281 larger.join(bounds2);
282 SkBitmap bits;
283 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000284 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000285 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
286 return false;
287 }
reed@google.come1ca7052013-12-17 19:22:07 +0000288 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000289 if (bitHeight >= (int) sizeof(out)) {
290 return false;
291 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000292 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000293 SkCanvas canvas(bits);
294 canvas.drawColor(SK_ColorWHITE);
295 SkPaint paint;
296 canvas.save();
297 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
298 canvas.drawPath(one, paint);
299 canvas.restore();
300 canvas.save();
301 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
302 canvas.drawPath(two, paint);
303 canvas.restore();
304 for (int y = 0; y < bitHeight; ++y) {
305 uint32_t* addr1 = bits.getAddr32(0, y);
306 int x;
307 char* outPtr = out;
308 for (x = 0; x < bitWidth; ++x) {
309 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
310 }
311 *outPtr++ = '|';
312 for (x = bitWidth; x < bitWidth * 2; ++x) {
313 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
314 }
315 *outPtr++ = '\0';
316 SkDebugf("%s\n", out);
317 }
318 return true;
319}
320
caryclark54359292015-03-26 07:52:43 -0700321int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000322 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000323 int errors2x2;
324 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000325 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000326 if (errors2x2 == 0) {
327 return 0;
328 }
329 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000330 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
331}
332
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000333static SkTDArray<SkPathOp> gTestOp;
334
Cary Clark59d5a0e2017-01-23 14:38:52 +0000335static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
336 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
337 const SkPathOp shapeOp, const SkMatrix& scale) {
338 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000339 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700340 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000341 }
caryclark2bec26a2016-05-26 09:01:47 -0700342 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
343 testName, opSuffixes[shapeOp]);
Cary Clark59d5a0e2017-01-23 14:38:52 +0000344 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000345 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700346 SkPathOpsDebug::ShowOnePath(a, "path", false);
347 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000348 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000349 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700350 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000351}
352
reed086eea92016-05-04 17:12:46 -0700353SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700354
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000355static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
356 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800357 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700358 ExpectMatch expectMatch) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000359 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800360 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000361 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700362 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700363 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800364 REPORTER_ASSERT(reporter, 0);
365 }
366 return 0;
367 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000368 if (errors2x2 == 0) {
369 return 0;
370 }
caryclarkd5b91732016-08-09 05:04:29 -0700371 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000372 SkAutoMutexAcquire autoM(compareDebugOut3);
373 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700374 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000375 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700376 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000377 }
caryclark38a017b2015-05-13 10:13:17 -0700378 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000379}
380
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000381// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000382static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000383static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000384
Cary Clark389c5572017-04-14 07:46:07 -0400385static void appendTestName(const char* nameSuffix, std::string& out) {
386 out += testName;
387 out += std_to_string(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000388 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000389 if (nameSuffix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500390 out.append(nameSuffix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000391 }
392}
393
Ben Wagner561c1b02017-01-09 15:54:34 -0500394static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
Cary Clark389c5572017-04-14 07:46:07 -0400395 const char* testFunction, bool twoPaths, std::string& out) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000396#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500397 out.append("\n<div id=\"");
398 appendTestName(nameSuffix, out);
399 out.append("\">\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000400 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500401 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000402 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500403 out.append(pathStr);
404 out.append("</div>\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000405
Ben Wagner561c1b02017-01-09 15:54:34 -0500406 out.append(marker);
407 out.append(" ");
408 appendTestName(nameSuffix, out);
409 out.append(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000410#endif
Ben Wagner561c1b02017-01-09 15:54:34 -0500411 out.append("static void ");
412 appendTestName(nameSuffix, out);
413 out.append("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000414 if (twoPaths) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500415 out.append(", pathB");
caryclark@google.com66089e42013-04-10 15:55:37 +0000416 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500417 out.append(";\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000418 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500419 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000420 }
Cary Clark389c5572017-04-14 07:46:07 -0400421 out += pathStr;
422 out += " ";
423 out += testFunction;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000424#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500425 out.append("static void (*firstTest)() = ");
426 appendTestName(nameSuffix, out);
427 out.append(";\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000428
Ben Wagner561c1b02017-01-09 15:54:34 -0500429 out.append("static struct {\n");
430 out.append(" void (*fun)();\n");
431 out.append(" const char* str;\n");
432 out.append("} tests[] = {\n");
433 out.append(" TEST(");
434 appendTestName(nameSuffix, out);
435 out.append("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000436#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000437}
438
reed086eea92016-05-04 17:12:46 -0700439SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700440
caryclark@google.com66089e42013-04-10 15:55:37 +0000441bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
442 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000443 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
444 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700445 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000446 if (!Simplify(path, &out)) {
447 SkDebugf("%s did not expect failure\n", __FUNCTION__);
448 REPORTER_ASSERT(state.fReporter, 0);
449 return false;
450 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000451 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000452 return true;
453 }
halcanary96fcdcc2015-08-27 07:41:13 -0700454 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700455 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000456 SkAutoMutexAcquire autoM(simplifyDebugOut);
Cary Clark389c5572017-04-14 07:46:07 -0400457 std::string str;
halcanary96fcdcc2015-08-27 07:41:13 -0700458 const char* pathPrefix = nullptr;
459 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000460 if (fillType == SkPath::kEvenOdd_FillType) {
461 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
462 nameSuffix = "x";
463 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000464 const char testFunction[] = "testSimplify(reporter, path);";
Ben Wagner561c1b02017-01-09 15:54:34 -0500465 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
Mike Reedff80c2a2017-01-07 11:16:28 -0500466 SkDebugf("%s", str.c_str());
caryclark@google.com66089e42013-04-10 15:55:37 +0000467 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000468 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000469 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000470 return result == 0;
471}
472
caryclark54359292015-03-26 07:52:43 -0700473static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700474 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700475#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000476 showPathData(path);
477#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000478 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700479 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
480 SkDEBUGPARAMS(testName))) {
481 if (ExpectSuccess::kYes == expectSuccess) {
482 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
483 REPORTER_ASSERT(reporter, 0);
484 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000485 return false;
caryclark55888e42016-07-18 10:01:36 -0700486 } else {
487 if (ExpectSuccess::kNo == expectSuccess) {
488 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
489 REPORTER_ASSERT(reporter, 0);
490 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000491 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000492 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700493 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700494 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700495 if (!errors) {
496 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
497 REPORTER_ASSERT(reporter, 0);
498 return false;
499 }
caryclarkd5b91732016-08-09 05:04:29 -0700500 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000501 REPORTER_ASSERT(reporter, 0);
502 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000503 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700504 return errors == 0;
505}
506
507bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700508 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
509 ExpectMatch::kYes);
510}
511
caryclark30b9fdd2016-08-31 14:36:29 -0700512bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
513 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
514 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700515}
516
517bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
518 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700519 return inner_simplify(reporter, path, filename, checkFail ?
520 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000521}
522
caryclark@google.coma5e55922013-05-07 18:51:31 +0000523#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000524static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
525 SkDebugf("\n");
526 showPathData(a);
527 showOp(shapeOp);
528 showPathData(b);
529}
530#endif
531
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000532static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700533 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
534 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700535#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000536 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000537#endif
538 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700539 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700540 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700541 if (ExpectSuccess::kYes == expectSuccess) {
542 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700543 REPORTER_ASSERT(reporter, 0);
544 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000545 return false;
caryclark55888e42016-07-18 10:01:36 -0700546 } else {
547 if (ExpectSuccess::kNo == expectSuccess) {
548 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
549 REPORTER_ASSERT(reporter, 0);
550 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000551 }
caryclark54359292015-03-26 07:52:43 -0700552 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000553 return true;
554 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000555 SkPath pathOut, scaledPathOut;
556 SkRegion rgnA, rgnB, openClip, rgnOut;
557 openClip.setRect(-16000, -16000, 16000, 16000);
558 rgnA.setPath(a, openClip);
559 rgnB.setPath(b, openClip);
560 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
561 rgnOut.getBoundaryPath(&pathOut);
562
563 SkMatrix scale;
564 scaleMatrix(a, b, scale);
565 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
566 SkPath scaledA, scaledB;
567 scaledA.addPath(a, scale);
568 scaledA.setFillType(a.getFillType());
569 scaledB.addPath(b, scale);
570 scaledB.setFillType(b.getFillType());
571 scaledRgnA.setPath(scaledA, openClip);
572 scaledRgnB.setPath(scaledB, openClip);
573 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
574 scaledRgnOut.getBoundaryPath(&scaledPathOut);
575 SkBitmap bitmap;
576 SkPath scaledOut;
577 scaledOut.addPath(out, scale);
578 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000579 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700580 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000581 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000582 return result == 0;
583}
584
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000585bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800586 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700587 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
588 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800589}
590
591bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
592 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700593 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
594 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000595}
596
caryclark30b9fdd2016-08-31 14:36:29 -0700597bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700598 const SkPathOp shapeOp, const char* testName) {
599 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
600 ExpectMatch::kFlaky);
601}
602
Cary Clark59d5a0e2017-01-23 14:38:52 +0000603bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
604 const SkPathOp shapeOp, const char* testName) {
605#if DEBUG_SHOW_TEST_NAME
606 showName(a, b, shapeOp);
607#endif
608 SkPath orig;
609 orig.lineTo(54, 43);
610 SkPath out = orig;
611 if (Op(a, b, shapeOp, &out) ) {
612 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
613 REPORTER_ASSERT(reporter, 0);
614 return false;
615 }
616 SkASSERT(out == orig);
617 return true;
618}
619
reed086eea92016-05-04 17:12:46 -0700620SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000621
mtklein406654b2014-09-03 15:34:37 -0700622void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000623 if (reporter->verbose()) {
624 SkAutoMutexAcquire lock(gMutex);
625 testName = test;
626 size_t testNameSize = strlen(test);
627 SkFILEStream inFile("../../experimental/Intersection/op.htm");
628 if (inFile.isValid()) {
629 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000630 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000631 size_t inLen = inData.count();
632 inFile.read(inData.begin(), inLen);
Ben Wagner4d1955c2017-03-10 13:08:15 -0500633 inFile.close();
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000634 char* insert = strstr(inData.begin(), marker);
635 if (insert) {
636 insert += sizeof(marker) - 1;
637 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
638 testNumber = atoi(numLoc) + 1;
639 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000640 }
641 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000642}
643
Mike Reedff80c2a2017-01-07 11:16:28 -0500644void PathOpsThreadState::outputProgress(const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000645 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700646 const char* pathPrefix = nullptr;
647 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000648 if (pathFillType == SkPath::kEvenOdd_FillType) {
649 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
650 nameSuffix = "x";
651 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500652 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000653}
654
Mike Reedff80c2a2017-01-07 11:16:28 -0500655void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000656 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000657 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000658 const char* nameSuffix = opSuffixes[op];
Ben Wagner561c1b02017-01-09 15:54:34 -0500659 appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000660}
661
662void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000663 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700664 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000665 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000666 size_t index;
667 if (firstTest) {
668 index = count - 1;
669 while (index > 0 && tests[index].fun != firstTest) {
670 --index;
671 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000672#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700673 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000674#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000675 (*tests[index].fun)(reporter, tests[index].str);
676 if (tests[index].fun == stopTest) {
677 return;
678 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000679 }
680 index = reverse ? count - 1 : 0;
681 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700682 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000683 do {
caryclark54359292015-03-26 07:52:43 -0700684 if (tests[index].fun == skipTest) {
685 foundSkip = true;
686 }
687 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000688 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700689 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000690 #endif
caryclark30b9fdd2016-08-31 14:36:29 -0700691 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000692 }
caryclark03b03ca2015-04-23 09:13:37 -0700693 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000694 break;
695 }
696 index += reverse ? -1 : 1;
697 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700698#if DEBUG_SHOW_TEST_NAME
699 SkDebugf(
700 "\n"
701 "</div>\n"
702 "\n"
703 "<script type=\"text/javascript\">\n"
704 "\n"
705 "var testDivs = [\n"
706 );
707 index = reverse ? count - 1 : 0;
708 last = reverse ? 0 : count - 1;
709 foundSkip = !skipTest;
710 do {
711 if (tests[index].fun == skipTest) {
712 foundSkip = true;
713 }
714 if (foundSkip && tests[index].fun != firstTest) {
715 SkDebugf(" %s,\n", tests[index].str);
716 }
717 if (tests[index].fun == stopTest || index == last) {
718 break;
719 }
720 index += reverse ? -1 : 1;
721 } while (true);
722#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000723}