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 "SkForceLinking.h"
13 #include "SkMatrix.h"
14 #include "SkMutex.h"
15 #include "SkPaint.h"
16 #include "SkRTConf.h"
17 #include "SkStream.h"
18 
19 #include <stdlib.h>
20 
21 #ifdef SK_BUILD_FOR_MAC
22 #include <sys/sysctl.h>
23 #endif
24 
25 __SK_FORCE_IMAGE_DECODER_LINKING;
26 
27 DEFINE_bool2(runFail, f, false, "run tests known to fail.");
28 DEFINE_bool2(runBinary, f, false, "run tests known to fail binary sect.");
29 
30 static const char marker[] =
31     "</div>\n"
32     "\n"
33     "<script type=\"text/javascript\">\n"
34     "\n"
35     "var testDivs = [\n";
36 
37 static const char* opStrs[] = {
38     "kDifference_SkPathOp",
39     "kIntersect_SkPathOp",
40     "kUnion_SkPathOp",
41     "kXor_PathOp",
42     "kReverseDifference_SkPathOp",
43 };
44 
45 static const char* opSuffixes[] = {
46     "d",
47     "i",
48     "u",
49     "o",
50 };
51 
52 #if DEBUG_SHOW_TEST_NAME
showPathData(const SkPath & path)53 static void showPathData(const SkPath& path) {
54     SkPath::RawIter iter(path);
55     uint8_t verb;
56     SkPoint pts[4];
57     SkPoint firstPt = {0, 0}, lastPt = {0, 0};
58     bool firstPtSet = false;
59     bool lastPtSet = true;
60     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
61         switch (verb) {
62             case SkPath::kMove_Verb:
63                 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                 }
68                 firstPt = pts[0];
69                 firstPtSet = true;
70                 continue;
71             case SkPath::kLine_Verb:
72                 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
73                         pts[1].fX, pts[1].fY);
74                 lastPt = pts[1];
75                 lastPtSet = true;
76                 break;
77             case SkPath::kQuad_Verb:
78                 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
79                         pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
80                 lastPt = pts[2];
81                 lastPtSet = true;
82                 break;
83             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;
90             case SkPath::kCubic_Verb:
91                 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
92                         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);
94                 lastPt = pts[3];
95                 lastPtSet = true;
96                 break;
97             case SkPath::kClose_Verb:
98                 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;
103                 break;
104             default:
105                 SkDEBUGFAIL("bad verb");
106                 return;
107         }
108     }
109     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     }
113 }
114 #endif
115 
showOp(const SkPathOp op)116 void showOp(const SkPathOp op) {
117     switch (op) {
118         case kDifference_SkPathOp:
119             SkDebugf("op difference\n");
120             break;
121         case kIntersect_SkPathOp:
122             SkDebugf("op intersect\n");
123             break;
124         case kUnion_SkPathOp:
125             SkDebugf("op union\n");
126             break;
127         case kXOR_SkPathOp:
128             SkDebugf("op xor\n");
129             break;
130         case kReverseDifference_SkPathOp:
131             SkDebugf("op reverse difference\n");
132             break;
133         default:
134             SkASSERT(0);
135     }
136 }
137 
138 #if DEBUG_SHOW_TEST_NAME
hexorator(int x)139 static 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 
ShowTestName(PathOpsThreadState * state,int a,int b,int c,int d)149 void 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 
167 const int bitWidth = 64;
168 const int bitHeight = 64;
169 
scaleMatrix(const SkPath & one,const SkPath & two,SkMatrix & scale)170 static 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);
185     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);
194 }
195 
pathsDrawTheSame(SkBitmap & bits,const SkPath & scaledOne,const SkPath & scaledTwo,int & error2x2)196 static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
197         int& error2x2) {
198     if (bits.width() == 0) {
199         bits.allocN32Pixels(bitWidth * 2, bitHeight);
200     }
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     }
230     error2x2 = errors2;
231     return errors;
232 }
233 
pathsDrawTheSame(const SkPath & one,const SkPath & two,SkBitmap & bits,SkPath & scaledOne,SkPath & scaledTwo,int & error2x2)234 static 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 
drawAsciiPaths(const SkPath & one,const SkPath & two,bool drawPaths)243 bool 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];
253     int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
254     if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
255         return false;
256     }
257     int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
258     if (bitHeight >= (int) sizeof(out)) {
259         return false;
260     }
261     bits.allocN32Pixels(bitWidth * 2, bitHeight);
262     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 
comparePaths(skiatest::Reporter * reporter,const char * filename,const SkPath & one,const SkPath & two,SkBitmap & bitmap)290 int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
291         const SkPath& two, SkBitmap& bitmap) {
292     int errors2x2;
293     SkPath scaledOne, scaledTwo;
294     (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
295     if (errors2x2 == 0) {
296         return 0;
297     }
298     const int MAX_ERRORS = 9;
299     return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
300 }
301 
302 const int gTestFirst = 41;
303 static int gTestNo = gTestFirst;
304 static SkTDArray<SkPathOp> gTestOp;
305 
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)306 static 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) {
309     SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
310     if (!testName) {
311         testName = "xOp";
312     }
313     SkDebugf("static void %s%d%s(skiatest::Reporter* reporter, const char* filename) {\n",
314         testName, gTestNo, opSuffixes[shapeOp]);
315     *gTestOp.append() = shapeOp;
316     ++gTestNo;
317     SkDebugf("    SkPath path, pathB;\n");
318     SkPathOpsDebug::ShowOnePath(a, "path", false);
319     SkPathOpsDebug::ShowOnePath(b, "pathB", false);
320     SkDebugf("    testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
321     SkDebugf("}\n");
322     drawAsciiPaths(scaledOne, scaledTwo, true);
323 }
324 
ShowTestArray(const char * testName)325 void ShowTestArray(const char* testName) {
326     if (!testName) {
327         testName = "xOp";
328     }
329     for (int x = gTestFirst; x < gTestNo; ++x) {
330         SkDebugf("    TEST(%s%d%s),\n", testName, x, opSuffixes[gTestOp[x - gTestFirst]]);
331     }
332 }
333 
334 SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
335 
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,bool expectSuccess)336 static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
337         const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
338         const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
339         bool expectSuccess) {
340     int errors2x2;
341     const int MAX_ERRORS = 8;
342     (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
343     if (!expectSuccess) {
344         if (errors2x2 < MAX_ERRORS) {
345             REPORTER_ASSERT(reporter, 0);
346         }
347         return 0;
348     }
349     if (errors2x2 == 0) {
350         return 0;
351     }
352     if (errors2x2 >= MAX_ERRORS) {
353         SkAutoMutexAcquire autoM(compareDebugOut3);
354         showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
355         SkDebugf("\n/*");
356         REPORTER_ASSERT(reporter, 0);
357         SkDebugf(" */\n");
358     }
359     return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
360 }
361 
362 // Default values for when reporter->verbose() is false.
363 static int testNumber = 55;
364 static const char* testName = "pathOpTest";
365 
writeTestName(const char * nameSuffix,SkMemoryWStream & outFile)366 static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
367     outFile.writeText(testName);
368     outFile.writeDecAsText(testNumber);
369     ++testNumber;
370     if (nameSuffix) {
371         outFile.writeText(nameSuffix);
372     }
373 }
374 
outputToStream(const char * pathStr,const char * pathPrefix,const char * nameSuffix,const char * testFunction,bool twoPaths,SkMemoryWStream & outFile)375 static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
376         const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
377 #if 0
378     outFile.writeText("\n<div id=\"");
379     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");
391 #endif
392     outFile.writeText("static void ");
393     writeTestName(nameSuffix, outFile);
394     outFile.writeText("(skiatest::Reporter* reporter) {\n    SkPath path");
395     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");
406 #if 0
407     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");
418 #endif
419     outFile.flush();
420 }
421 
422 SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
423 
testSimplify(SkPath & path,bool useXor,SkPath & out,PathOpsThreadState & state,const char * pathStr)424 bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
425                   const char* pathStr) {
426     SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
427     path.setFillType(fillType);
428     state.fReporter->bumpTestCount();
429     if (!Simplify(path, &out)) {
430         SkDebugf("%s did not expect failure\n", __FUNCTION__);
431         REPORTER_ASSERT(state.fReporter, 0);
432         return false;
433     }
434     if (!state.fReporter->verbose()) {
435         return true;
436     }
437     int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
438     if (result) {
439         SkAutoMutexAcquire autoM(simplifyDebugOut);
440         char temp[8192];
441         sk_bzero(temp, sizeof(temp));
442         SkMemoryWStream stream(temp, sizeof(temp));
443         const char* pathPrefix = nullptr;
444         const char* nameSuffix = nullptr;
445         if (fillType == SkPath::kEvenOdd_FillType) {
446             pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
447             nameSuffix = "x";
448         }
449         const char testFunction[] = "testSimplify(reporter, path);";
450         outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
451         SkDebugf("%s", temp);
452         REPORTER_ASSERT(state.fReporter, 0);
453     }
454     state.fReporter->bumpTestCount();
455     return result == 0;
456 }
457 
inner_simplify(skiatest::Reporter * reporter,const SkPath & path,const char * filename,bool checkFail)458 static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
459         bool checkFail) {
460 #if 0 && DEBUG_SHOW_TEST_NAME
461     showPathData(path);
462 #endif
463     SkPath out;
464     if (!Simplify(path, &out)) {
465         SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
466         REPORTER_ASSERT(reporter, 0);
467         return false;
468     }
469     SkBitmap bitmap;
470     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) {
478         REPORTER_ASSERT(reporter, 0);
479     }
480     reporter->bumpTestCount();
481     return errors == 0;
482 }
483 
testSimplify(skiatest::Reporter * reporter,const SkPath & path,const char * filename)484 bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
485     return inner_simplify(reporter, path, filename, true);
486 }
487 
testSimplifyCheck(skiatest::Reporter * reporter,const SkPath & path,const char * filename,bool checkFail)488 bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
489         bool checkFail) {
490     return inner_simplify(reporter, path, filename, checkFail);
491 }
492 
493 #if DEBUG_SHOW_TEST_NAME
showName(const SkPath & a,const SkPath & b,const SkPathOp shapeOp)494 static 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 
502 bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result,
503              bool expectSuccess  SkDEBUGPARAMS(const char* testName));
504 
innerPathOp(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName,bool expectSuccess)505 static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
506         const SkPathOp shapeOp, const char* testName, bool expectSuccess) {
507 #if 0 && DEBUG_SHOW_TEST_NAME
508     showName(a, b, shapeOp);
509 #endif
510     SkPath out;
511     if (!OpDebug(a, b, shapeOp, &out, expectSuccess  SkDEBUGPARAMS(testName))) {
512         SkDebugf("%s did not expect failure\n", __FUNCTION__);
513         REPORTER_ASSERT(reporter, 0);
514         return false;
515     }
516     if (!reporter->verbose()) {
517         return true;
518     }
519     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());
543     int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
544             a, b, shapeOp, scale, expectSuccess);
545     reporter->bumpTestCount();
546     return result == 0;
547 }
548 
testPathOp(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)549 bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
550         const SkPathOp shapeOp, const char* testName) {
551     return innerPathOp(reporter, a, b, shapeOp, testName, true);
552 }
553 
testPathOpCheck(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName,bool checkFail)554 bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
555         const SkPathOp shapeOp, const char* testName, bool checkFail) {
556     return innerPathOp(reporter, a, b, shapeOp, testName, checkFail);
557 }
558 
testPathOpFailCheck(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)559 bool testPathOpFailCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
560         const SkPathOp shapeOp, const char* testName) {
561     return innerPathOp(reporter, a, b, shapeOp, testName, false);
562 }
563 
testPathFailOp(skiatest::Reporter * reporter,const SkPath & a,const SkPath & b,const SkPathOp shapeOp,const char * testName)564 bool 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
569     SkPath orig;
570     orig.lineTo(54, 43);
571     SkPath out = orig;
572     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     }
577     SkASSERT(out == orig);
578     return true;
579 }
580 
581 SK_DECLARE_STATIC_MUTEX(gMutex);
582 
initializeTests(skiatest::Reporter * reporter,const char * test)583 void initializeTests(skiatest::Reporter* reporter, const char* test) {
584 #if 0  // doesn't work yet
585     SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
586     SK_CONF_SET("images.png.suppressDecoderWarnings", true);
587 #endif
588     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;
595             inData.setCount((int) inFile.getLength());
596             size_t inLen = inData.count();
597             inFile.read(inData.begin(), inLen);
598             inFile.setPath(nullptr);
599             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             }
605         }
606     }
607 }
608 
outputProgress(char * ramStr,const char * pathStr,SkPath::FillType pathFillType)609 void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
610     const char testFunction[] = "testSimplify(path);";
611     const char* pathPrefix = nullptr;
612     const char* nameSuffix = nullptr;
613     if (pathFillType == SkPath::kEvenOdd_FillType) {
614         pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
615         nameSuffix = "x";
616     }
617     SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
618     outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
619 }
620 
outputProgress(char * ramStr,const char * pathStr,SkPathOp op)621 void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
622     const char testFunction[] = "testOp(path);";
623     SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
624     const char* nameSuffix = opSuffixes[op];
625     SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
626     outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
627 }
628 
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)629 void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
630                 void (*firstTest)(skiatest::Reporter* , const char* filename),
631                 void (*skipTest)(skiatest::Reporter* , const char* filename),
632                 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
633     size_t index;
634     if (firstTest) {
635         index = count - 1;
636         while (index > 0 && tests[index].fun != firstTest) {
637             --index;
638         }
639 #if DEBUG_SHOW_TEST_NAME
640         SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
641 #endif
642         (*tests[index].fun)(reporter, tests[index].str);
643         if (tests[index].fun == stopTest) {
644             return;
645         }
646     }
647     index = reverse ? count - 1 : 0;
648     size_t last = reverse ? 0 : count - 1;
649     bool foundSkip = !skipTest;
650     do {
651         if (tests[index].fun == skipTest) {
652             foundSkip = true;
653         }
654         if (foundSkip && tests[index].fun != firstTest) {
655     #if DEBUG_SHOW_TEST_NAME
656             SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
657     #endif
658             (*tests[index].fun)(reporter, tests[index].str);
659         }
660         if (tests[index].fun == stopTest || index == last) {
661             break;
662         }
663         index += reverse ? -1 : 1;
664     } while (true);
665 #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
690 }
691