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