1 /*
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"
9 #include "PathOpsThreadedCommon.h"
10 #include "SkBitmap.h"
11 #include "SkCanvas.h"
12 #include "SkMatrix.h"
13 #include "SkMutex.h"
14 #include "SkPaint.h"
15 #include "SkRegion.h"
16 #include "SkStream.h"
17
18 #include <stdlib.h>
19
20 #ifdef SK_BUILD_FOR_MAC
21 #include <sys/sysctl.h>
22 #endif
23
24 bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
25 SkDEBUGPARAMS(bool skipAssert)
26 SkDEBUGPARAMS(const char* testName));
27
28 bool SimplifyDebug(const SkPath& one, SkPath* result
29 SkDEBUGPARAMS(bool skipAssert)
30 SkDEBUGPARAMS(const char* testName));
31
32 static const char marker[] =
33 "</div>\n"
34 "\n"
35 "<script type=\"text/javascript\">\n"
36 "\n"
37 "var testDivs = [\n";
38
39 static const char* opStrs[] = {
40 "kDifference_SkPathOp",
41 "kIntersect_SkPathOp",
42 "kUnion_SkPathOp",
43 "kXOR_PathOp",
44 "kReverseDifference_SkPathOp",
45 };
46
47 static const char* opSuffixes[] = {
48 "d",
49 "i",
50 "u",
51 "o",
52 "r",
53 };
54
55 enum class ExpectSuccess {
56 kNo,
57 kYes,
58 kFlaky
59 };
60
61 enum class SkipAssert {
62 kNo,
63 kYes
64 };
65
66 enum class ExpectMatch {
67 kNo,
68 kYes,
69 kFlaky
70 };
71
72 #if DEBUG_SHOW_TEST_NAME
showPathData(const SkPath & path)73 static void showPathData(const SkPath& path) {
74 SkPath::RawIter iter(path);
75 uint8_t verb;
76 SkPoint pts[4];
77 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
78 bool firstPtSet = false;
79 bool lastPtSet = true;
80 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
81 switch (verb) {
82 case SkPath::kMove_Verb:
83 if (firstPtSet && lastPtSet && firstPt != lastPt) {
84 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
85 firstPt.fX, firstPt.fY);
86 lastPtSet = false;
87 }
88 firstPt = pts[0];
89 firstPtSet = true;
90 continue;
91 case SkPath::kLine_Verb:
92 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
93 pts[1].fX, pts[1].fY);
94 lastPt = pts[1];
95 lastPtSet = true;
96 break;
97 case SkPath::kQuad_Verb:
98 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
99 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
100 lastPt = pts[2];
101 lastPtSet = true;
102 break;
103 case SkPath::kConic_Verb:
104 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
105 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
106 iter.conicWeight());
107 lastPt = pts[2];
108 lastPtSet = true;
109 break;
110 case SkPath::kCubic_Verb:
111 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
112 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
113 pts[3].fX, pts[3].fY);
114 lastPt = pts[3];
115 lastPtSet = true;
116 break;
117 case SkPath::kClose_Verb:
118 if (firstPtSet && lastPtSet && firstPt != lastPt) {
119 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
120 firstPt.fX, firstPt.fY);
121 }
122 firstPtSet = lastPtSet = false;
123 break;
124 default:
125 SkDEBUGFAIL("bad verb");
126 return;
127 }
128 }
129 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 }
134 #endif
135
showOp(const SkPathOp op)136 void showOp(const SkPathOp op) {
137 switch (op) {
138 case kDifference_SkPathOp:
139 SkDebugf("op difference\n");
140 break;
141 case kIntersect_SkPathOp:
142 SkDebugf("op intersect\n");
143 break;
144 case kUnion_SkPathOp:
145 SkDebugf("op union\n");
146 break;
147 case kXOR_SkPathOp:
148 SkDebugf("op xor\n");
149 break;
150 case kReverseDifference_SkPathOp:
151 SkDebugf("op reverse difference\n");
152 break;
153 default:
154 SkASSERT(0);
155 }
156 }
157
158 #if DEBUG_SHOW_TEST_NAME
hexorator(int x)159 static char hexorator(int x) {
160 if (x < 10) {
161 return x + '0';
162 }
163 x -= 10;
164 SkASSERT(x < 26);
165 return x + 'A';
166 }
167 #endif
168
ShowTestName(PathOpsThreadState * state,int a,int b,int c,int d)169 void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
170 #if DEBUG_SHOW_TEST_NAME
171 state->fSerialNo[0] = hexorator(state->fA);
172 state->fSerialNo[1] = hexorator(state->fB);
173 state->fSerialNo[2] = hexorator(state->fC);
174 state->fSerialNo[3] = hexorator(state->fD);
175 state->fSerialNo[4] = hexorator(a);
176 state->fSerialNo[5] = hexorator(b);
177 state->fSerialNo[6] = hexorator(c);
178 state->fSerialNo[7] = hexorator(d);
179 state->fSerialNo[8] = '\0';
180 SkDebugf("%s\n", state->fSerialNo);
181 if (strcmp(state->fSerialNo, state->fKey) == 0) {
182 SkDebugf("%s\n", state->fPathStr);
183 }
184 #endif
185 }
186
187 const int bitWidth = 64;
188 const int bitHeight = 64;
189
scaleMatrix(const SkPath & one,const SkPath & two,SkMatrix & scale)190 static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
191 SkRect larger = one.getBounds();
192 larger.join(two.getBounds());
193 SkScalar largerWidth = larger.width();
194 if (largerWidth < 4) {
195 largerWidth = 4;
196 }
197 SkScalar largerHeight = larger.height();
198 if (largerHeight < 4) {
199 largerHeight = 4;
200 }
201 SkScalar hScale = (bitWidth - 2) / largerWidth;
202 SkScalar vScale = (bitHeight - 2) / largerHeight;
203 scale.reset();
204 scale.preScale(hScale, vScale);
205 larger.fLeft *= hScale;
206 larger.fRight *= hScale;
207 larger.fTop *= vScale;
208 larger.fBottom *= vScale;
209 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
210 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
211 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
212 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
213 scale.postTranslate(dx, dy);
214 }
215
pathsDrawTheSame(SkBitmap & bits,const SkPath & scaledOne,const SkPath & scaledTwo,int & error2x2)216 static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
217 int& error2x2) {
218 if (bits.width() == 0) {
219 bits.allocN32Pixels(bitWidth * 2, bitHeight);
220 }
221 SkCanvas canvas(bits);
222 canvas.drawColor(SK_ColorWHITE);
223 SkPaint paint;
224 canvas.save();
225 const SkRect& bounds1 = scaledOne.getBounds();
226 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
227 canvas.drawPath(scaledOne, paint);
228 canvas.restore();
229 canvas.save();
230 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
231 canvas.drawPath(scaledTwo, paint);
232 canvas.restore();
233 int errors2 = 0;
234 int errors = 0;
235 for (int y = 0; y < bitHeight - 1; ++y) {
236 uint32_t* addr1 = bits.getAddr32(0, y);
237 uint32_t* addr2 = bits.getAddr32(0, y + 1);
238 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
239 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
240 for (int x = 0; x < bitWidth - 1; ++x) {
241 // count 2x2 blocks
242 bool err = addr1[x] != addr3[x];
243 if (err) {
244 errors2 += addr1[x + 1] != addr3[x + 1]
245 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
246 errors++;
247 }
248 }
249 }
250 error2x2 = errors2;
251 return errors;
252 }
253
pathsDrawTheSame(const SkPath & one,const SkPath & two,SkBitmap & bits,SkPath & scaledOne,SkPath & scaledTwo,int & error2x2)254 static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
255 SkPath& scaledTwo, int& error2x2) {
256 SkMatrix scale;
257 scaleMatrix(one, two, scale);
258 one.transform(scale, &scaledOne);
259 two.transform(scale, &scaledTwo);
260 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
261 }
262
drawAsciiPaths(const SkPath & one,const SkPath & two,bool drawPaths)263 bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
264 if (!drawPaths) {
265 return true;
266 }
267 const SkRect& bounds1 = one.getBounds();
268 const SkRect& bounds2 = two.getBounds();
269 SkRect larger = bounds1;
270 larger.join(bounds2);
271 SkBitmap bits;
272 char out[256];
273 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
274 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
275 return false;
276 }
277 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
278 if (bitHeight >= (int) sizeof(out)) {
279 return false;
280 }
281 bits.allocN32Pixels(bitWidth * 2, bitHeight);
282 SkCanvas canvas(bits);
283 canvas.drawColor(SK_ColorWHITE);
284 SkPaint paint;
285 canvas.save();
286 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
287 canvas.drawPath(one, paint);
288 canvas.restore();
289 canvas.save();
290 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
291 canvas.drawPath(two, paint);
292 canvas.restore();
293 for (int y = 0; y < bitHeight; ++y) {
294 uint32_t* addr1 = bits.getAddr32(0, y);
295 int x;
296 char* outPtr = out;
297 for (x = 0; x < bitWidth; ++x) {
298 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
299 }
300 *outPtr++ = '|';
301 for (x = bitWidth; x < bitWidth * 2; ++x) {
302 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
303 }
304 *outPtr++ = '\0';
305 SkDebugf("%s\n", out);
306 }
307 return true;
308 }
309
comparePaths(skiatest::Reporter * reporter,const char * filename,const SkPath & one,const SkPath & two,SkBitmap & bitmap)310 int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
311 const SkPath& two, SkBitmap& bitmap) {
312 int errors2x2;
313 SkPath scaledOne, scaledTwo;
314 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
315 if (errors2x2 == 0) {
316 return 0;
317 }
318 const int MAX_ERRORS = 9;
319 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
320 }
321
322 static SkTDArray<SkPathOp> gTestOp;
323
showPathOpPath(const char * testName,const SkPath & one,const SkPath & two,const SkPath & a,const SkPath & b,const SkPath & scaledOne,const SkPath & scaledTwo,const SkPathOp shapeOp,const SkMatrix & scale)324 static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
325 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
326 const SkPathOp shapeOp, const SkMatrix& scale) {
327 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
328 if (!testName) {
329 testName = "xOp";
330 }
331 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
332 testName, opSuffixes[shapeOp]);
333 *gTestOp.append() = shapeOp;
334 SkDebugf(" SkPath path, pathB;\n");
335 SkPathOpsDebug::ShowOnePath(a, "path", false);
336 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
337 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
338 SkDebugf("}\n");
339 drawAsciiPaths(scaledOne, scaledTwo, true);
340 }
341
342 SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
343
comparePaths(skiatest::Reporter * reporter,const char * testName,const SkPath & one,const SkPath & scaledOne,const SkPath & two,const SkPath & scaledTwo,SkBitmap & bitmap,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const SkMatrix & scale,ExpectMatch expectMatch)344 static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
345 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
346 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
347 ExpectMatch expectMatch) {
348 int errors2x2;
349 const int MAX_ERRORS = 8;
350 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
351 if (ExpectMatch::kNo == expectMatch) {
352 if (errors2x2 < MAX_ERRORS) {
353 REPORTER_ASSERT(reporter, 0);
354 }
355 return 0;
356 }
357 if (errors2x2 == 0) {
358 return 0;
359 }
360 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
361 SkAutoMutexAcquire autoM(compareDebugOut3);
362 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
363 SkDebugf("\n/*");
364 REPORTER_ASSERT(reporter, 0);
365 SkDebugf(" */\n");
366 }
367 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
368 }
369
370 // Default values for when reporter->verbose() is false.
371 static int testNumber = 55;
372 static const char* testName = "pathOpTest";
373
appendTestName(const char * nameSuffix,SkString & out)374 static void appendTestName(const char* nameSuffix, SkString& out) {
375 out.appendf("%s%d", testName, testNumber);
376 ++testNumber;
377 if (nameSuffix) {
378 out.append(nameSuffix);
379 }
380 }
381
appendTest(const char * pathStr,const char * pathPrefix,const char * nameSuffix,const char * testFunction,bool twoPaths,SkString & out)382 static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
383 const char* testFunction, bool twoPaths, SkString& out) {
384 #if 0
385 out.append("\n<div id=\"");
386 appendTestName(nameSuffix, out);
387 out.append("\">\n");
388 if (pathPrefix) {
389 out.append(pathPrefix);
390 }
391 out.append(pathStr);
392 out.append("</div>\n\n");
393
394 out.append(marker);
395 out.append(" ");
396 appendTestName(nameSuffix, out);
397 out.append(",\n\n\n");
398 #endif
399 out.append("static void ");
400 appendTestName(nameSuffix, out);
401 out.append("(skiatest::Reporter* reporter) {\n SkPath path");
402 if (twoPaths) {
403 out.append(", pathB");
404 }
405 out.append(";\n");
406 if (pathPrefix) {
407 out.append(pathPrefix);
408 }
409 out.appendf("%s %s\n}\n\n", pathStr, testFunction);
410 #if 0
411 out.append("static void (*firstTest)() = ");
412 appendTestName(nameSuffix, out);
413 out.append(";\n\n");
414
415 out.append("static struct {\n");
416 out.append(" void (*fun)();\n");
417 out.append(" const char* str;\n");
418 out.append("} tests[] = {\n");
419 out.append(" TEST(");
420 appendTestName(nameSuffix, out);
421 out.append("),\n");
422 #endif
423 }
424
425 SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
426
testSimplify(SkPath & path,bool useXor,SkPath & out,PathOpsThreadState & state,const char * pathStr)427 bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
428 const char* pathStr) {
429 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
430 path.setFillType(fillType);
431 state.fReporter->bumpTestCount();
432 if (!Simplify(path, &out)) {
433 SkDebugf("%s did not expect failure\n", __FUNCTION__);
434 REPORTER_ASSERT(state.fReporter, 0);
435 return false;
436 }
437 if (!state.fReporter->verbose()) {
438 return true;
439 }
440 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
441 if (result) {
442 SkAutoMutexAcquire autoM(simplifyDebugOut);
443 SkString str;
444 const char* pathPrefix = nullptr;
445 const char* nameSuffix = nullptr;
446 if (fillType == SkPath::kEvenOdd_FillType) {
447 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
448 nameSuffix = "x";
449 }
450 const char testFunction[] = "testSimplify(reporter, path);";
451 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
452 SkDebugf("%s", str.c_str());
453 REPORTER_ASSERT(state.fReporter, 0);
454 }
455 state.fReporter->bumpTestCount();
456 return result == 0;
457 }
458
inner_simplify(skiatest::Reporter * reporter,const SkPath & path,const char * filename,ExpectSuccess expectSuccess,SkipAssert skipAssert,ExpectMatch expectMatch)459 static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
460 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
461 #if 0 && DEBUG_SHOW_TEST_NAME
462 showPathData(path);
463 #endif
464 SkPath out;
465 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
466 SkDEBUGPARAMS(testName))) {
467 if (ExpectSuccess::kYes == expectSuccess) {
468 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
469 REPORTER_ASSERT(reporter, 0);
470 }
471 return false;
472 } else {
473 if (ExpectSuccess::kNo == expectSuccess) {
474 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
475 REPORTER_ASSERT(reporter, 0);
476 }
477 }
478 SkBitmap bitmap;
479 int errors = comparePaths(reporter, filename, path, out, bitmap);
480 if (ExpectMatch::kNo == expectMatch) {
481 if (!errors) {
482 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
483 REPORTER_ASSERT(reporter, 0);
484 return false;
485 }
486 } else if (ExpectMatch::kYes == expectMatch && errors) {
487 REPORTER_ASSERT(reporter, 0);
488 }
489 reporter->bumpTestCount();
490 return errors == 0;
491 }
492
testSimplify(skiatest::Reporter * reporter,const SkPath & path,const char * filename)493 bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
494 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
495 ExpectMatch::kYes);
496 }
497
testSimplifyFuzz(skiatest::Reporter * reporter,const SkPath & path,const char * filename)498 bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
499 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
500 ExpectMatch::kFlaky);
501 }
502
testSimplifyCheck(skiatest::Reporter * reporter,const SkPath & path,const char * filename,bool checkFail)503 bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
504 bool checkFail) {
505 return inner_simplify(reporter, path, filename, checkFail ?
506 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
507 }
508
509 #if DEBUG_SHOW_TEST_NAME
showName(const SkPath & a,const SkPath & b,const SkPathOp shapeOp)510 static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
511 SkDebugf("\n");
512 showPathData(a);
513 showOp(shapeOp);
514 showPathData(b);
515 }
516 #endif
517
innerPathOp(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName,ExpectSuccess expectSuccess,SkipAssert skipAssert,ExpectMatch expectMatch)518 static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
519 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
520 SkipAssert skipAssert, ExpectMatch expectMatch) {
521 #if 0 && DEBUG_SHOW_TEST_NAME
522 showName(a, b, shapeOp);
523 #endif
524 SkPath out;
525 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
526 SkDEBUGPARAMS(testName))) {
527 if (ExpectSuccess::kYes == expectSuccess) {
528 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
529 REPORTER_ASSERT(reporter, 0);
530 }
531 return false;
532 } else {
533 if (ExpectSuccess::kNo == expectSuccess) {
534 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
535 REPORTER_ASSERT(reporter, 0);
536 }
537 }
538 if (!reporter->verbose()) {
539 return true;
540 }
541 SkPath pathOut, scaledPathOut;
542 SkRegion rgnA, rgnB, openClip, rgnOut;
543 openClip.setRect(-16000, -16000, 16000, 16000);
544 rgnA.setPath(a, openClip);
545 rgnB.setPath(b, openClip);
546 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
547 rgnOut.getBoundaryPath(&pathOut);
548
549 SkMatrix scale;
550 scaleMatrix(a, b, scale);
551 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
552 SkPath scaledA, scaledB;
553 scaledA.addPath(a, scale);
554 scaledA.setFillType(a.getFillType());
555 scaledB.addPath(b, scale);
556 scaledB.setFillType(b.getFillType());
557 scaledRgnA.setPath(scaledA, openClip);
558 scaledRgnB.setPath(scaledB, openClip);
559 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
560 scaledRgnOut.getBoundaryPath(&scaledPathOut);
561 SkBitmap bitmap;
562 SkPath scaledOut;
563 scaledOut.addPath(out, scale);
564 scaledOut.setFillType(out.getFillType());
565 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
566 a, b, shapeOp, scale, expectMatch);
567 reporter->bumpTestCount();
568 return result == 0;
569 }
570
testPathOp(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)571 bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
572 const SkPathOp shapeOp, const char* testName) {
573 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
574 ExpectMatch::kYes);
575 }
576
testPathOpCheck(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName,bool checkFail)577 bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
578 const SkPathOp shapeOp, const char* testName, bool checkFail) {
579 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
580 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
581 }
582
testPathOpFuzz(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)583 bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
584 const SkPathOp shapeOp, const char* testName) {
585 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
586 ExpectMatch::kFlaky);
587 }
588
testPathOpFail(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)589 bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
590 const SkPathOp shapeOp, const char* testName) {
591 #if DEBUG_SHOW_TEST_NAME
592 showName(a, b, shapeOp);
593 #endif
594 SkPath orig;
595 orig.lineTo(54, 43);
596 SkPath out = orig;
597 if (Op(a, b, shapeOp, &out) ) {
598 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
599 REPORTER_ASSERT(reporter, 0);
600 return false;
601 }
602 SkASSERT(out == orig);
603 return true;
604 }
605
606 SK_DECLARE_STATIC_MUTEX(gMutex);
607
initializeTests(skiatest::Reporter * reporter,const char * test)608 void initializeTests(skiatest::Reporter* reporter, const char* test) {
609 if (reporter->verbose()) {
610 SkAutoMutexAcquire lock(gMutex);
611 testName = test;
612 size_t testNameSize = strlen(test);
613 SkFILEStream inFile("../../experimental/Intersection/op.htm");
614 if (inFile.isValid()) {
615 SkTDArray<char> inData;
616 inData.setCount((int) inFile.getLength());
617 size_t inLen = inData.count();
618 inFile.read(inData.begin(), inLen);
619 inFile.close();
620 char* insert = strstr(inData.begin(), marker);
621 if (insert) {
622 insert += sizeof(marker) - 1;
623 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
624 testNumber = atoi(numLoc) + 1;
625 }
626 }
627 }
628 }
629
outputProgress(const char * pathStr,SkPath::FillType pathFillType)630 void PathOpsThreadState::outputProgress(const char* pathStr, SkPath::FillType pathFillType) {
631 const char testFunction[] = "testSimplify(path);";
632 const char* pathPrefix = nullptr;
633 const char* nameSuffix = nullptr;
634 if (pathFillType == SkPath::kEvenOdd_FillType) {
635 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
636 nameSuffix = "x";
637 }
638 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
639 }
640
outputProgress(const char * pathStr,SkPathOp op)641 void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
642 const char testFunction[] = "testOp(path);";
643 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
644 const char* nameSuffix = opSuffixes[op];
645 appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
646 }
647
RunTestSet(skiatest::Reporter * reporter,TestDesc tests[],size_t count,void (* firstTest)(skiatest::Reporter *,const char * filename),void (* skipTest)(skiatest::Reporter *,const char * filename),void (* stopTest)(skiatest::Reporter *,const char * filename),bool reverse)648 void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
649 void (*firstTest)(skiatest::Reporter* , const char* filename),
650 void (*skipTest)(skiatest::Reporter* , const char* filename),
651 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
652 size_t index;
653 if (firstTest) {
654 index = count - 1;
655 while (index > 0 && tests[index].fun != firstTest) {
656 --index;
657 }
658 #if DEBUG_SHOW_TEST_NAME
659 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
660 #endif
661 (*tests[index].fun)(reporter, tests[index].str);
662 if (tests[index].fun == stopTest) {
663 return;
664 }
665 }
666 index = reverse ? count - 1 : 0;
667 size_t last = reverse ? 0 : count - 1;
668 bool foundSkip = !skipTest;
669 do {
670 if (tests[index].fun == skipTest) {
671 foundSkip = true;
672 }
673 if (foundSkip && tests[index].fun != firstTest) {
674 #if DEBUG_SHOW_TEST_NAME
675 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
676 #endif
677 (*tests[index].fun)(reporter, tests[index].str);
678 }
679 if (tests[index].fun == stopTest || index == last) {
680 break;
681 }
682 index += reverse ? -1 : 1;
683 } while (true);
684 #if DEBUG_SHOW_TEST_NAME
685 SkDebugf(
686 "\n"
687 "</div>\n"
688 "\n"
689 "<script type=\"text/javascript\">\n"
690 "\n"
691 "var testDivs = [\n"
692 );
693 index = reverse ? count - 1 : 0;
694 last = reverse ? 0 : count - 1;
695 foundSkip = !skipTest;
696 do {
697 if (tests[index].fun == skipTest) {
698 foundSkip = true;
699 }
700 if (foundSkip && tests[index].fun != firstTest) {
701 SkDebugf(" %s,\n", tests[index].str);
702 }
703 if (tests[index].fun == stopTest || index == last) {
704 break;
705 }
706 index += reverse ? -1 : 1;
707 } while (true);
708 #endif
709 }
710