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