Home | History | Annotate | Download | only in Intersection
      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 "DataTypes.h"
      9 #include "EdgeWalker_Test.h"
     10 #include "Intersection_Tests.h"
     11 #include "SkBitmap.h"
     12 #include "SkCanvas.h"
     13 #include "SkMatrix.h"
     14 #include "SkPaint.h"
     15 #include "SkStream.h"
     16 
     17 #include <algorithm>
     18 #include <errno.h>
     19 #include <pthread.h>
     20 #include <unistd.h>
     21 #include <sys/types.h>
     22 #include <sys/sysctl.h>
     23 
     24 #undef SkASSERT
     25 #define SkASSERT(cond) while (!(cond)) { sk_throw(); }
     26 
     27 static const char marker[] =
     28     "</div>\n"
     29     "\n"
     30     "<script type=\"text/javascript\">\n"
     31     "\n"
     32     "var testDivs = [\n";
     33 
     34 static const char* opStrs[] = {
     35     "kDifference_Op",
     36     "kIntersect_Op",
     37     "kUnion_Op",
     38     "kXor_Op",
     39 };
     40 
     41 static const char* opSuffixes[] = {
     42     "d",
     43     "i",
     44     "u",
     45     "x",
     46 };
     47 
     48 static const char preferredFilename[] = "/flash/debug/XX.txt";
     49 static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt";
     50 
     51 static bool gShowPath = false;
     52 static bool gComparePaths = true;
     53 static bool gShowOutputProgress = false;
     54 static bool gComparePathsAssert = true;
     55 static bool gPathStrAssert = true;
     56 static bool gUsePhysicalFiles = false;
     57 
     58 static void showPathContour(SkPath::Iter& iter) {
     59     uint8_t verb;
     60     SkPoint pts[4];
     61     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
     62         switch (verb) {
     63             case SkPath::kMove_Verb:
     64                 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", pts[0].fX, pts[0].fY);
     65                 continue;
     66             case SkPath::kLine_Verb:
     67                 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", pts[1].fX, pts[1].fY);
     68                 break;
     69             case SkPath::kQuad_Verb:
     70                 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
     71                     pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
     72                 break;
     73             case SkPath::kCubic_Verb:
     74                 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
     75                     pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
     76                 break;
     77             case SkPath::kClose_Verb:
     78                 SkDebugf("path.close();\n");
     79                 break;
     80             default:
     81                 SkDEBUGFAIL("bad verb");
     82                 return;
     83         }
     84     }
     85 }
     86 
     87 void showPath(const SkPath& path, const char* str) {
     88     SkDebugf("%s\n", !str ? "original:" : str);
     89     showPath(path);
     90 }
     91 
     92 void showPath(const SkPath& path) {
     93     SkPath::Iter iter(path, true);
     94     int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0;
     95     if (rectCount > 0) {
     96         SkTDArray<SkRect> rects;
     97         SkTDArray<SkPath::Direction> directions;
     98         rects.setCount(rectCount);
     99         directions.setCount(rectCount);
    100         path.rectContours(rects.begin(), directions.begin());
    101         for (int contour = 0; contour < rectCount; ++contour) {
    102             const SkRect& rect = rects[contour];
    103             SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
    104                     rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
    105                     ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
    106         }
    107         return;
    108     }
    109     iter.setPath(path, true);
    110     showPathContour(iter);
    111 }
    112 
    113 void showPathData(const SkPath& path) {
    114     SkPath::Iter iter(path, true);
    115     uint8_t verb;
    116     SkPoint pts[4];
    117     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
    118         switch (verb) {
    119             case SkPath::kMove_Verb:
    120                 continue;
    121             case SkPath::kLine_Verb:
    122                 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
    123                 break;
    124             case SkPath::kQuad_Verb:
    125                 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
    126                     pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
    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, pts[3].fX, pts[3].fY);
    131                 break;
    132             case SkPath::kClose_Verb:
    133                 break;
    134             default:
    135                 SkDEBUGFAIL("bad verb");
    136                 return;
    137         }
    138     }
    139 }
    140 
    141 void showOp(const ShapeOp op) {
    142     switch (op) {
    143         case kDifference_Op:
    144             SkDebugf("op difference\n");
    145             break;
    146         case kIntersect_Op:
    147             SkDebugf("op intersect\n");
    148             break;
    149         case kUnion_Op:
    150             SkDebugf("op union\n");
    151             break;
    152         case kXor_Op:
    153             SkDebugf("op xor\n");
    154             break;
    155         default:
    156             SkASSERT(0);
    157     }
    158 }
    159 
    160 static void showPath(const SkPath& path, const char* str, const SkMatrix& scale) {
    161     SkPath scaled;
    162     SkMatrix inverse;
    163     bool success = scale.invert(&inverse);
    164     if (!success) SkASSERT(0);
    165     path.transform(inverse, &scaled);
    166     showPath(scaled, str);
    167 }
    168 
    169 const int bitWidth = 64;
    170 const int bitHeight = 64;
    171 
    172 static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
    173     SkRect larger = one.getBounds();
    174     larger.join(two.getBounds());
    175     SkScalar largerWidth = larger.width();
    176     if (largerWidth < 4) {
    177         largerWidth = 4;
    178     }
    179     SkScalar largerHeight = larger.height();
    180     if (largerHeight < 4) {
    181         largerHeight = 4;
    182     }
    183     SkScalar hScale = (bitWidth - 2) / largerWidth;
    184     SkScalar vScale = (bitHeight - 2) / largerHeight;
    185     scale.reset();
    186     scale.preScale(hScale, vScale);
    187 }
    188 
    189 static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
    190         int& error2x2) {
    191     if (bits.width() == 0) {
    192         bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
    193         bits.allocPixels();
    194     }
    195     SkCanvas canvas(bits);
    196     canvas.drawColor(SK_ColorWHITE);
    197     SkPaint paint;
    198     canvas.save();
    199     const SkRect& bounds1 = scaledOne.getBounds();
    200     canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
    201     canvas.drawPath(scaledOne, paint);
    202     canvas.restore();
    203     canvas.save();
    204     canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
    205     canvas.drawPath(scaledTwo, paint);
    206     canvas.restore();
    207     int errors2 = 0;
    208     int errors = 0;
    209     for (int y = 0; y < bitHeight - 1; ++y) {
    210         uint32_t* addr1 = bits.getAddr32(0, y);
    211         uint32_t* addr2 = bits.getAddr32(0, y + 1);
    212         uint32_t* addr3 = bits.getAddr32(bitWidth, y);
    213         uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
    214         for (int x = 0; x < bitWidth - 1; ++x) {
    215             // count 2x2 blocks
    216             bool err = addr1[x] != addr3[x];
    217             if (err) {
    218                 errors2 += addr1[x + 1] != addr3[x + 1]
    219                         && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
    220                 errors++;
    221             }
    222         }
    223     }
    224     if (errors2 >= 6 || errors > 160) {
    225         SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
    226     }
    227     error2x2 = errors2;
    228     return errors;
    229 }
    230 
    231 static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
    232         SkPath& scaledTwo, int& error2x2) {
    233     SkMatrix scale;
    234     scaleMatrix(one, two, scale);
    235     one.transform(scale, &scaledOne);
    236     two.transform(scale, &scaledTwo);
    237     return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
    238 }
    239 
    240 bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
    241     if (!drawPaths) {
    242         return true;
    243     }
    244     const SkRect& bounds1 = one.getBounds();
    245     const SkRect& bounds2 = two.getBounds();
    246     SkRect larger = bounds1;
    247     larger.join(bounds2);
    248     SkBitmap bits;
    249     char out[256];
    250     int bitWidth = SkScalarCeil(larger.width()) + 2;
    251     if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
    252         return false;
    253     }
    254     int bitHeight = SkScalarCeil(larger.height()) + 2;
    255     if (bitHeight >= (int) sizeof(out)) {
    256         return false;
    257     }
    258     bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
    259     bits.allocPixels();
    260     SkCanvas canvas(bits);
    261     canvas.drawColor(SK_ColorWHITE);
    262     SkPaint paint;
    263     canvas.save();
    264     canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
    265     canvas.drawPath(one, paint);
    266     canvas.restore();
    267     canvas.save();
    268     canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
    269     canvas.drawPath(two, paint);
    270     canvas.restore();
    271     for (int y = 0; y < bitHeight; ++y) {
    272         uint32_t* addr1 = bits.getAddr32(0, y);
    273         int x;
    274         char* outPtr = out;
    275         for (x = 0; x < bitWidth; ++x) {
    276             *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
    277         }
    278         *outPtr++ = '|';
    279         for (x = bitWidth; x < bitWidth * 2; ++x) {
    280             *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
    281         }
    282         *outPtr++ = '\0';
    283         SkDebugf("%s\n", out);
    284     }
    285     return true;
    286 }
    287 
    288 static void showSimplifiedPath(const SkPath& one, const SkPath& two,
    289         const SkPath& scaledOne, const SkPath& scaledTwo) {
    290     showPath(one, "original:");
    291     showPath(two, "simplified:");
    292     drawAsciiPaths(scaledOne, scaledTwo, true);
    293 }
    294 
    295 int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) {
    296     int errors2x2;
    297     SkPath scaledOne, scaledTwo;
    298     int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
    299     if (errors2x2 == 0) {
    300         return 0;
    301     }
    302     const int MAX_ERRORS = 9;
    303     if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
    304         showSimplifiedPath(one, two, scaledOne, scaledTwo);
    305     }
    306     if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
    307         SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
    308         showSimplifiedPath(one, two, scaledOne, scaledTwo);
    309         SkASSERT(0);
    310     }
    311     return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
    312 }
    313 
    314 static void showShapeOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
    315         const SkPath& scaledOne, const SkPath& scaledTwo, const ShapeOp shapeOp,
    316         const SkMatrix& scale) {
    317     SkASSERT((unsigned) shapeOp < sizeof(opStrs) / sizeof(opStrs[0]));
    318     showPath(a, "minuend:");
    319     SkDebugf("op: %s\n", opStrs[shapeOp]);
    320     showPath(b, "subtrahend:");
    321     // the region often isn't very helpful since it approximates curves with a lot of line-tos
    322     if (0) showPath(scaledOne, "region:", scale);
    323     showPath(two, "op result:");
    324     drawAsciiPaths(scaledOne, scaledTwo, true);
    325 }
    326 
    327 static int comparePaths(const SkPath& one, const SkPath& scaledOne, const SkPath& two,
    328         const SkPath& scaledTwo,
    329         SkBitmap& bitmap, const SkPath& a, const SkPath& b, const ShapeOp shapeOp,
    330         const SkMatrix& scale) {
    331     int errors2x2;
    332     int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
    333     if (errors2x2 == 0) {
    334         return 0;
    335     }
    336     const int MAX_ERRORS = 8;
    337     if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
    338         showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
    339     }
    340     if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
    341         SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
    342         showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
    343         SkASSERT(0);
    344     }
    345     return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
    346 }
    347 
    348 // doesn't work yet
    349 void comparePathsTiny(const SkPath& one, const SkPath& two) {
    350     const SkRect& bounds1 = one.getBounds();
    351     const SkRect& bounds2 = two.getBounds();
    352     SkRect larger = bounds1;
    353     larger.join(bounds2);
    354     SkBitmap bits;
    355     int bitWidth = SkScalarCeil(larger.width()) + 2;
    356     int bitHeight = SkScalarCeil(larger.height()) + 2;
    357     bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight);
    358     bits.allocPixels();
    359     SkCanvas canvas(bits);
    360     canvas.drawColor(SK_ColorWHITE);
    361     SkPaint paint;
    362     canvas.save();
    363     canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
    364     canvas.drawPath(one, paint);
    365     canvas.restore();
    366     canvas.save();
    367     canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1);
    368     canvas.drawPath(two, paint);
    369     canvas.restore();
    370     for (int y = 0; y < bitHeight; ++y) {
    371         uint8_t* addr1 = bits.getAddr1(0, y);
    372         uint8_t* addr2 = bits.getAddr1(bitWidth, y);
    373         for (unsigned x = 0; x < bits.rowBytes(); ++x) {
    374             SkASSERT(addr1[x] == addr2[x]);
    375         }
    376     }
    377 }
    378 
    379 bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) {
    380     if (gShowPath) {
    381         showPath(path);
    382     }
    383     simplify(path, fill, out);
    384     if (!gComparePaths) {
    385         return true;
    386     }
    387     return comparePaths(path, out, bitmap) == 0;
    388 }
    389 
    390 bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state,
    391         const char* pathStr) {
    392     SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
    393     path.setFillType(fillType);
    394     if (gShowPath) {
    395         showPath(path);
    396     }
    397     simplifyx(path, out);
    398     if (!gComparePaths) {
    399         return true;
    400     }
    401     int result = comparePaths(path, out, state.bitmap);
    402     if (result && gPathStrAssert) {
    403         SkDebugf("addTest %s\n", state.filename);
    404         char temp[8192];
    405         bzero(temp, sizeof(temp));
    406         SkMemoryWStream stream(temp, sizeof(temp));
    407         const char* pathPrefix = NULL;
    408         const char* nameSuffix = NULL;
    409         if (fillType == SkPath::kEvenOdd_FillType) {
    410             pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
    411             nameSuffix = "x";
    412         }
    413         const char testFunction[] = "testSimplifyx(path);";
    414         outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, stream);
    415         SkDebugf(temp);
    416         SkASSERT(0);
    417     }
    418     return result == 0;
    419 }
    420 
    421 bool testSimplifyx(const SkPath& path) {
    422     SkPath out;
    423     simplifyx(path, out);
    424     SkBitmap bitmap;
    425     int result = comparePaths(path, out, bitmap);
    426     if (result && gPathStrAssert) {
    427         SkASSERT(0);
    428     }
    429     return result == 0;
    430 }
    431 
    432 bool testShapeOp(const SkPath& a, const SkPath& b, const ShapeOp shapeOp) {
    433 #if FORCE_RELEASE == 0
    434     showPathData(a);
    435     showOp(shapeOp);
    436     showPathData(b);
    437 #endif
    438     SkPath out;
    439     operate(a, b, shapeOp, out);
    440     SkPath pathOut, scaledPathOut;
    441     SkRegion rgnA, rgnB, openClip, rgnOut;
    442     openClip.setRect(-16000, -16000, 16000, 16000);
    443     rgnA.setPath(a, openClip);
    444     rgnB.setPath(b, openClip);
    445     rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
    446     rgnOut.getBoundaryPath(&pathOut);
    447 
    448     SkMatrix scale;
    449     scaleMatrix(a, b, scale);
    450     SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
    451     SkPath scaledA, scaledB;
    452     scaledA.addPath(a, scale);
    453     scaledA.setFillType(a.getFillType());
    454     scaledB.addPath(b, scale);
    455     scaledB.setFillType(b.getFillType());
    456     scaledRgnA.setPath(scaledA, openClip);
    457     scaledRgnB.setPath(scaledB, openClip);
    458     scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
    459     scaledRgnOut.getBoundaryPath(&scaledPathOut);
    460     SkBitmap bitmap;
    461     SkPath scaledOut;
    462     scaledOut.addPath(out, scale);
    463     scaledOut.setFillType(out.getFillType());
    464     int result = comparePaths(pathOut, scaledPathOut, out, scaledOut, bitmap, a, b, shapeOp, scale);
    465     if (result && gPathStrAssert) {
    466         SkASSERT(0);
    467     }
    468     return result == 0;
    469 }
    470 
    471 const int maxThreadsAllocated = 64;
    472 static int maxThreads = 1;
    473 static int threadIndex;
    474 State4 threadState[maxThreadsAllocated];
    475 static int testNumber;
    476 static const char* testName;
    477 static bool debugThreads = false;
    478 
    479 State4* State4::queue = NULL;
    480 pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER;
    481 pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER;
    482 
    483 State4::State4() {
    484     bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100);
    485     bitmap.allocPixels();
    486 }
    487 
    488 void createThread(State4* statePtr, void* (*testFun)(void* )) {
    489     int threadError = pthread_create(&statePtr->threadID, NULL, testFun,
    490             (void*) statePtr);
    491     SkASSERT(!threadError);
    492 }
    493 
    494 int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) {
    495     int testsRun = 0;
    496     State4* statePtr;
    497     if (!gRunTestsInOneThread) {
    498         pthread_mutex_lock(&State4::addQueue);
    499         if (threadIndex < maxThreads) {
    500             statePtr = &threadState[threadIndex];
    501             statePtr->testsRun = 0;
    502             statePtr->a = a;
    503             statePtr->b = b;
    504             statePtr->c = c;
    505             statePtr->d = d;
    506             statePtr->done = false;
    507             statePtr->index = threadIndex;
    508             statePtr->last = false;
    509             if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__,
    510                     statePtr->index, statePtr->done, statePtr->last);
    511             pthread_cond_init(&statePtr->initialized, NULL);
    512             ++threadIndex;
    513             createThread(statePtr, testFun);
    514         } else {
    515             while (!State4::queue) {
    516                 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
    517                 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
    518             }
    519             statePtr = State4::queue;
    520             testsRun += statePtr->testsRun;
    521             statePtr->testsRun = 0;
    522             statePtr->a = a;
    523             statePtr->b = b;
    524             statePtr->c = c;
    525             statePtr->d = d;
    526             statePtr->done = false;
    527             State4::queue = NULL;
    528             for (int index = 0; index < maxThreads; ++index) {
    529                 if (threadState[index].done) {
    530                     State4::queue = &threadState[index];
    531                 }
    532             }
    533             if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__,
    534                     statePtr->index, statePtr->done, statePtr->last,
    535                     State4::queue ? State4::queue->index : -1);
    536             pthread_cond_signal(&statePtr->initialized);
    537         }
    538         pthread_mutex_unlock(&State4::addQueue);
    539     } else {
    540         statePtr = &threadState[0];
    541         testsRun += statePtr->testsRun;
    542         statePtr->testsRun = 0;
    543         statePtr->a = a;
    544         statePtr->b = b;
    545         statePtr->c = c;
    546         statePtr->d = d;
    547         statePtr->done = false;
    548         statePtr->index = threadIndex;
    549         statePtr->last = false;
    550         (*testFun)(statePtr);
    551     }
    552     return testsRun;
    553 }
    554 
    555 void initializeTests(const char* test, size_t testNameSize) {
    556     testName = test;
    557     if (!gRunTestsInOneThread) {
    558         int threads = -1;
    559         size_t size = sizeof(threads);
    560         sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0);
    561         if (threads > 0) {
    562             maxThreads = threads;
    563         } else {
    564             maxThreads = 8;
    565         }
    566     }
    567     SkFILEStream inFile("../../experimental/Intersection/op.htm");
    568     if (inFile.isValid()) {
    569         SkTDArray<char> inData;
    570         inData.setCount(inFile.getLength());
    571         size_t inLen = inData.count();
    572         inFile.read(inData.begin(), inLen);
    573         inFile.setPath(NULL);
    574         char* insert = strstr(inData.begin(), marker);
    575         if (insert) {
    576             insert += sizeof(marker) - 1;
    577             const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
    578             testNumber = atoi(numLoc) + 1;
    579         }
    580     }
    581     const char* filename = preferredFilename;
    582     SkFILEWStream preferredTest(filename);
    583     if (!preferredTest.isValid()) {
    584         filename = backupFilename;
    585         SkFILEWStream backupTest(filename);
    586         SkASSERT(backupTest.isValid());
    587     }
    588     for (int index = 0; index < maxThreads; ++index) {
    589         State4* statePtr = &threadState[index];
    590         strcpy(statePtr->filename, filename);
    591         size_t len = strlen(filename);
    592         SkASSERT(statePtr->filename[len - 6] == 'X');
    593         SkASSERT(statePtr->filename[len - 5] == 'X');
    594         statePtr->filename[len - 6] = '0' + index / 10;
    595         statePtr->filename[len - 5] = '0' + index % 10;
    596     }
    597     threadIndex = 0;
    598 }
    599 
    600 void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) {
    601     if (gRunTestsInOneThread && gShowOutputProgress) {
    602         if (pathFillType == SkPath::kEvenOdd_FillType) {
    603             SkDebugf("    path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr);
    604         }
    605         SkDebugf("%s\n", pathStr);
    606     }
    607     const char testFunction[] = "testSimplifyx(path);";
    608     const char* pathPrefix = NULL;
    609     const char* nameSuffix = NULL;
    610     if (pathFillType == SkPath::kEvenOdd_FillType) {
    611         pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
    612         nameSuffix = "x";
    613     }
    614     if (gUsePhysicalFiles) {
    615         SkFILEWStream outFile(state.filename);
    616         if (!outFile.isValid()) {
    617             SkASSERT(0);
    618             return;
    619         }
    620         outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outFile);
    621         return;
    622     }
    623     SkFILEWStream outRam(state.filename);
    624     outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outRam);
    625 }
    626 
    627 void outputProgress(const State4& state, const char* pathStr, ShapeOp op) {
    628     SkString testFunc("testShapeOp(path, pathB, ");
    629     testFunc += opStrs[op];
    630     testFunc += ");";
    631     const char* testFunction = testFunc.c_str();
    632     if (gRunTestsInOneThread && gShowOutputProgress) {
    633         SkDebugf("%s\n", pathStr);
    634         SkDebugf("    %s\n", testFunction);
    635     }
    636     const char* nameSuffix = opSuffixes[op];
    637     if (gUsePhysicalFiles) {
    638         SkFILEWStream outFile(state.filename);
    639         if (!outFile.isValid()) {
    640             SkASSERT(0);
    641             return;
    642         }
    643         outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outFile);
    644         return;
    645     }
    646     SkFILEWStream outRam(state.filename);
    647     outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outRam);
    648 }
    649 
    650 static void writeTestName(const char* nameSuffix, SkWStream& outFile) {
    651     outFile.writeText(testName);
    652     outFile.writeDecAsText(testNumber);
    653     if (nameSuffix) {
    654         outFile.writeText(nameSuffix);
    655     }
    656 }
    657 
    658 void outputToStream(const State4& state, const char* pathStr, const char* pathPrefix,
    659         const char* nameSuffix,
    660         const char* testFunction, SkWStream& outFile) {
    661     outFile.writeText("<div id=\"");
    662     writeTestName(nameSuffix, outFile);
    663     outFile.writeText("\">\n");
    664     if (pathPrefix) {
    665         outFile.writeText(pathPrefix);
    666     }
    667     outFile.writeText(pathStr);
    668     outFile.writeText("</div>\n\n");
    669 
    670     outFile.writeText(marker);
    671     outFile.writeText("    ");
    672     writeTestName(nameSuffix, outFile);
    673     outFile.writeText(",\n\n\n");
    674 
    675     outFile.writeText("static void ");
    676     writeTestName(nameSuffix, outFile);
    677     outFile.writeText("() {\n    SkPath path");
    678     if (!pathPrefix) {
    679         outFile.writeText(", pathB");
    680     }
    681     outFile.writeText(";\n");
    682     if (pathPrefix) {
    683         outFile.writeText(pathPrefix);
    684     }
    685     outFile.writeText(pathStr);
    686     outFile.writeText("    ");
    687     outFile.writeText(testFunction);
    688     outFile.writeText("\n}\n\n");
    689     outFile.writeText("static void (*firstTest)() = ");
    690     writeTestName(nameSuffix, outFile);
    691     outFile.writeText(";\n\n");
    692 
    693     outFile.writeText("static struct {\n");
    694     outFile.writeText("    void (*fun)();\n");
    695     outFile.writeText("    const char* str;\n");
    696     outFile.writeText("} tests[] = {\n");
    697     outFile.writeText("    TEST(");
    698     writeTestName(nameSuffix, outFile);
    699     outFile.writeText("),\n");
    700     outFile.flush();
    701 }
    702 
    703 bool runNextTestSet(State4& state) {
    704     if (gRunTestsInOneThread) {
    705         return false;
    706     }
    707     pthread_mutex_lock(&State4::addQueue);
    708     state.done = true;
    709     State4::queue = &state;
    710     if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index,
    711         state.done, state.last);
    712     pthread_cond_signal(&State4::checkQueue);
    713     while (state.done && !state.last) {
    714         if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last);
    715         pthread_cond_wait(&state.initialized, &State4::addQueue);
    716     }
    717     pthread_mutex_unlock(&State4::addQueue);
    718     return !state.last;
    719 }
    720 
    721 int waitForCompletion() {
    722     int testsRun = 0;
    723     if (!gRunTestsInOneThread) {
    724         pthread_mutex_lock(&State4::addQueue);
    725         int runningThreads = maxThreads;
    726         int index;
    727         while (runningThreads > 0) {
    728             while (!State4::queue) {
    729                 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
    730                 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
    731             }
    732             while (State4::queue) {
    733                 --runningThreads;
    734                 SkDebugf("");
    735                 State4::queue->last = true;
    736                 State4* next = NULL;
    737                 for (index = 0; index < maxThreads; ++index) {
    738                     State4& test = threadState[index];
    739                     if (test.done && !test.last) {
    740                         next = &test;
    741                     }
    742                 }
    743                 if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__,
    744                     State4::queue->index, next ? next->index : -1);
    745                 pthread_cond_signal(&State4::queue->initialized);
    746                 State4::queue = next;
    747             }
    748         }
    749         pthread_mutex_unlock(&State4::addQueue);
    750         for (index = 0; index < maxThreads; ++index) {
    751             pthread_join(threadState[index].threadID, NULL);
    752             testsRun += threadState[index].testsRun;
    753         }
    754         SkDebugf("\n");
    755     }
    756 #ifdef SK_DEBUG
    757     gDebugMaxWindSum = SK_MaxS32;
    758     gDebugMaxWindValue = SK_MaxS32;
    759 #endif
    760     return testsRun;
    761 }
    762