2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "PathOpsExtendedTest.h"
9 #include "PathOpsThreadedCommon.h"
12 #include "SkForceLinking.h"
17 #include "SkTaskGroup.h"
20 #ifdef SK_BUILD_FOR_MAC
21 #include <sys/sysctl.h>
24 __SK_FORCE_IMAGE_DECODER_LINKING;
26 DEFINE_bool2(runFail, f, false, "run tests known to fail.");
28 static const char marker[] =
31 "<script type=\"text/javascript\">\n"
35 static const char* opStrs[] = {
40 "kReverseDifference_PathOp",
43 static const char* opSuffixes[] = {
50 static bool gShowPath = false;
51 static bool gComparePathsAssert = true;
52 static bool gPathStrAssert = true;
54 #if DEBUG_SHOW_TEST_NAME
55 static void showPathData(const SkPath& path) {
56 SkPath::RawIter iter(path);
59 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
60 bool firstPtSet = false;
61 bool lastPtSet = true;
62 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
64 case SkPath::kMove_Verb:
65 if (firstPtSet && lastPtSet && firstPt != lastPt) {
66 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
67 firstPt.fX, firstPt.fY);
73 case SkPath::kLine_Verb:
74 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
75 pts[1].fX, pts[1].fY);
79 case SkPath::kQuad_Verb:
80 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
81 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
85 case SkPath::kCubic_Verb:
86 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
87 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
88 pts[3].fX, pts[3].fY);
92 case SkPath::kClose_Verb:
93 if (firstPtSet && lastPtSet && firstPt != lastPt) {
94 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
95 firstPt.fX, firstPt.fY);
97 firstPtSet = lastPtSet = false;
100 SkDEBUGFAIL("bad verb");
104 if (firstPtSet && lastPtSet && firstPt != lastPt) {
105 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
106 firstPt.fX, firstPt.fY);
111 void showOp(const SkPathOp op) {
113 case kDifference_PathOp:
114 SkDebugf("op difference\n");
116 case kIntersect_PathOp:
117 SkDebugf("op intersect\n");
120 SkDebugf("op union\n");
123 SkDebugf("op xor\n");
125 case kReverseDifference_PathOp:
126 SkDebugf("op reverse difference\n");
133 #if DEBUG_SHOW_TEST_NAME
134 static char hexorator(int x) {
144 void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
145 #if DEBUG_SHOW_TEST_NAME
146 state->fSerialNo[0] = hexorator(state->fA);
147 state->fSerialNo[1] = hexorator(state->fB);
148 state->fSerialNo[2] = hexorator(state->fC);
149 state->fSerialNo[3] = hexorator(state->fD);
150 state->fSerialNo[4] = hexorator(a);
151 state->fSerialNo[5] = hexorator(b);
152 state->fSerialNo[6] = hexorator(c);
153 state->fSerialNo[7] = hexorator(d);
154 state->fSerialNo[8] = '\0';
155 SkDebugf("%s\n", state->fSerialNo);
156 if (strcmp(state->fSerialNo, state->fKey) == 0) {
157 SkDebugf("%s\n", state->fPathStr);
162 const int bitWidth = 64;
163 const int bitHeight = 64;
165 static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
166 SkRect larger = one.getBounds();
167 larger.join(two.getBounds());
168 SkScalar largerWidth = larger.width();
169 if (largerWidth < 4) {
172 SkScalar largerHeight = larger.height();
173 if (largerHeight < 4) {
176 SkScalar hScale = (bitWidth - 2) / largerWidth;
177 SkScalar vScale = (bitHeight - 2) / largerHeight;
179 scale.preScale(hScale, vScale);
182 static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
184 if (bits.width() == 0) {
185 bits.allocN32Pixels(bitWidth * 2, bitHeight);
187 SkCanvas canvas(bits);
188 canvas.drawColor(SK_ColorWHITE);
191 const SkRect& bounds1 = scaledOne.getBounds();
192 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
193 canvas.drawPath(scaledOne, paint);
196 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
197 canvas.drawPath(scaledTwo, paint);
201 for (int y = 0; y < bitHeight - 1; ++y) {
202 uint32_t* addr1 = bits.getAddr32(0, y);
203 uint32_t* addr2 = bits.getAddr32(0, y + 1);
204 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
205 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
206 for (int x = 0; x < bitWidth - 1; ++x) {
208 bool err = addr1[x] != addr3[x];
210 errors2 += addr1[x + 1] != addr3[x + 1]
211 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
220 static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
221 SkPath& scaledTwo, int& error2x2) {
223 scaleMatrix(one, two, scale);
224 one.transform(scale, &scaledOne);
225 two.transform(scale, &scaledTwo);
226 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
229 bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
233 const SkRect& bounds1 = one.getBounds();
234 const SkRect& bounds2 = two.getBounds();
235 SkRect larger = bounds1;
236 larger.join(bounds2);
239 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
240 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
243 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
244 if (bitHeight >= (int) sizeof(out)) {
247 bits.allocN32Pixels(bitWidth * 2, bitHeight);
248 SkCanvas canvas(bits);
249 canvas.drawColor(SK_ColorWHITE);
252 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
253 canvas.drawPath(one, paint);
256 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
257 canvas.drawPath(two, paint);
259 for (int y = 0; y < bitHeight; ++y) {
260 uint32_t* addr1 = bits.getAddr32(0, y);
263 for (x = 0; x < bitWidth; ++x) {
264 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
267 for (x = bitWidth; x < bitWidth * 2; ++x) {
268 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
271 SkDebugf("%s\n", out);
276 static int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
277 const SkPath& two, SkBitmap& bitmap) {
279 SkPath scaledOne, scaledTwo;
280 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
281 if (errors2x2 == 0) {
284 const int MAX_ERRORS = 9;
285 REPORTER_ASSERT(reporter, errors2x2 <= MAX_ERRORS || !gComparePathsAssert);
286 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
289 const int gTestFirst = 4;
290 static int gTestNo = gTestFirst;
291 static SkTDArray<SkPathOp> gTestOp;
293 static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
294 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
295 const SkPathOp shapeOp, const SkMatrix& scale) {
296 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
297 SkString defaultTestName;
299 defaultTestName.printf("xOp%d%s", gTestNo, opSuffixes[shapeOp]);
300 testName = defaultTestName.c_str();
302 SkDebugf("static void %s(skiatest::Reporter* reporter, const char* filename) {\n", testName);
303 *gTestOp.append() = shapeOp;
305 SkDebugf(" SkPath path, pathB;\n");
306 #if DEBUG_SHOW_TEST_NAME
307 SkPathOpsDebug::ShowOnePath(a, "path", false);
308 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
310 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
312 drawAsciiPaths(scaledOne, scaledTwo, true);
315 void ShowTestArray() {
316 for (int x = gTestFirst; x < gTestNo; ++x) {
317 SkDebugf(" TEST(xOp%d%s),\n", x, opSuffixes[gTestOp[x - gTestFirst]]);
321 SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
322 SK_DECLARE_STATIC_MUTEX(compareDebugOut4);
323 static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
324 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
325 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale) {
327 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
328 if (errors2x2 == 0) {
330 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
334 const int MAX_ERRORS = 8;
335 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
336 SkAutoMutexAcquire autoM(compareDebugOut3);
337 SkDebugf("\n*** this test fails ***\n");
338 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
339 REPORTER_ASSERT(reporter, 0);
340 } else if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
341 SkAutoMutexAcquire autoM(compareDebugOut4);
342 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
344 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
347 // Default values for when reporter->verbose() is false.
348 static int testNumber = 55;
349 static const char* testName = "pathOpTest";
351 static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
352 outFile.writeText(testName);
353 outFile.writeDecAsText(testNumber);
356 outFile.writeText(nameSuffix);
360 static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
361 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
363 outFile.writeText("<div id=\"");
364 writeTestName(nameSuffix, outFile);
365 outFile.writeText("\">\n");
367 outFile.writeText(pathPrefix);
369 outFile.writeText(pathStr);
370 outFile.writeText("</div>\n\n");
372 outFile.writeText(marker);
373 outFile.writeText(" ");
374 writeTestName(nameSuffix, outFile);
375 outFile.writeText(",\n\n\n");
377 outFile.writeText("static void ");
378 writeTestName(nameSuffix, outFile);
379 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
381 outFile.writeText(", pathB");
383 outFile.writeText(";\n");
385 outFile.writeText(pathPrefix);
387 outFile.writeText(pathStr);
388 outFile.writeText(" ");
389 outFile.writeText(testFunction);
390 outFile.writeText("\n}\n\n");
392 outFile.writeText("static void (*firstTest)() = ");
393 writeTestName(nameSuffix, outFile);
394 outFile.writeText(";\n\n");
396 outFile.writeText("static struct {\n");
397 outFile.writeText(" void (*fun)();\n");
398 outFile.writeText(" const char* str;\n");
399 outFile.writeText("} tests[] = {\n");
400 outFile.writeText(" TEST(");
401 writeTestName(nameSuffix, outFile);
402 outFile.writeText("),\n");
407 SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
408 bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
409 const char* pathStr) {
410 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
411 path.setFillType(fillType);
412 #if DEBUG_SHOW_TEST_NAME
414 SkPathOpsDebug::ShowOnePath(path, "path", false);
417 if (!Simplify(path, &out)) {
418 SkDebugf("%s did not expect failure\n", __FUNCTION__);
419 REPORTER_ASSERT(state.fReporter, 0);
422 if (!state.fReporter->verbose()) {
425 int result = comparePaths(state.fReporter, NULL, path, out, *state.fBitmap);
426 if (result && gPathStrAssert) {
427 SkAutoMutexAcquire autoM(simplifyDebugOut);
429 sk_bzero(temp, sizeof(temp));
430 SkMemoryWStream stream(temp, sizeof(temp));
431 const char* pathPrefix = NULL;
432 const char* nameSuffix = NULL;
433 if (fillType == SkPath::kEvenOdd_FillType) {
434 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
437 const char testFunction[] = "testSimplify(reporter, path);";
438 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
440 REPORTER_ASSERT(state.fReporter, 0);
442 state.fReporter->bumpTestCount();
446 bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
447 #if DEBUG_SHOW_TEST_NAME
451 if (!Simplify(path, &out)) {
452 SkDebugf("%s did not expect failure\n", __FUNCTION__);
453 REPORTER_ASSERT(reporter, 0);
457 int result = comparePaths(reporter, filename, path, out, bitmap);
458 if (result && gPathStrAssert) {
459 REPORTER_ASSERT(reporter, 0);
461 reporter->bumpTestCount();
465 #if DEBUG_SHOW_TEST_NAME
466 static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
474 static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
475 const SkPathOp shapeOp, const char* testName, bool threaded) {
476 #if DEBUG_SHOW_TEST_NAME
477 showName(a, b, shapeOp);
480 if (!Op(a, b, shapeOp, &out) ) {
481 SkDebugf("%s did not expect failure\n", __FUNCTION__);
482 REPORTER_ASSERT(reporter, 0);
485 if (threaded && !reporter->verbose()) {
488 SkPath pathOut, scaledPathOut;
489 SkRegion rgnA, rgnB, openClip, rgnOut;
490 openClip.setRect(-16000, -16000, 16000, 16000);
491 rgnA.setPath(a, openClip);
492 rgnB.setPath(b, openClip);
493 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
494 rgnOut.getBoundaryPath(&pathOut);
497 scaleMatrix(a, b, scale);
498 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
499 SkPath scaledA, scaledB;
500 scaledA.addPath(a, scale);
501 scaledA.setFillType(a.getFillType());
502 scaledB.addPath(b, scale);
503 scaledB.setFillType(b.getFillType());
504 scaledRgnA.setPath(scaledA, openClip);
505 scaledRgnB.setPath(scaledB, openClip);
506 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
507 scaledRgnOut.getBoundaryPath(&scaledPathOut);
510 scaledOut.addPath(out, scale);
511 scaledOut.setFillType(out.getFillType());
512 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
513 a, b, shapeOp, scale);
514 if (result && gPathStrAssert) {
515 REPORTER_ASSERT(reporter, 0);
517 reporter->bumpTestCount();
521 bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
522 const SkPathOp shapeOp, const char* testName) {
523 return innerPathOp(reporter, a, b, shapeOp, testName, false);
526 bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
527 const SkPathOp shapeOp, const char* testName) {
528 #if DEBUG_SHOW_TEST_NAME
529 showName(a, b, shapeOp);
532 if (Op(a, b, shapeOp, &out) ) {
533 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
534 REPORTER_ASSERT(reporter, 0);
540 bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
541 const SkPathOp shapeOp, const char* testName) {
542 return innerPathOp(reporter, a, b, shapeOp, testName, true);
545 SK_DECLARE_STATIC_MUTEX(gMutex);
547 void initializeTests(skiatest::Reporter* reporter, const char* test) {
548 #if 0 // doesn't work yet
549 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
550 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
552 if (reporter->verbose()) {
553 SkAutoMutexAcquire lock(gMutex);
555 size_t testNameSize = strlen(test);
556 SkFILEStream inFile("../../experimental/Intersection/op.htm");
557 if (inFile.isValid()) {
558 SkTDArray<char> inData;
559 inData.setCount((int) inFile.getLength());
560 size_t inLen = inData.count();
561 inFile.read(inData.begin(), inLen);
562 inFile.setPath(NULL);
563 char* insert = strstr(inData.begin(), marker);
565 insert += sizeof(marker) - 1;
566 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
567 testNumber = atoi(numLoc) + 1;
573 void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
574 const char testFunction[] = "testSimplify(path);";
575 const char* pathPrefix = NULL;
576 const char* nameSuffix = NULL;
577 if (pathFillType == SkPath::kEvenOdd_FillType) {
578 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
581 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
582 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
585 void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
586 const char testFunction[] = "testOp(path);";
587 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
588 const char* nameSuffix = opSuffixes[op];
589 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
590 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
593 void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
594 void (*firstTest)(skiatest::Reporter* , const char* filename),
595 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
599 while (index > 0 && tests[index].fun != firstTest) {
602 #if DEBUG_SHOW_TEST_NAME
603 SkDebugf("<div id=\"%s\">\n", tests[index].str);
604 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
606 (*tests[index].fun)(reporter, tests[index].str);
607 if (tests[index].fun == stopTest) {
611 index = reverse ? count - 1 : 0;
612 size_t last = reverse ? 0 : count - 1;
614 if (tests[index].fun != firstTest) {
615 #if DEBUG_SHOW_TEST_NAME
616 SkDebugf("<div id=\"%s\">\n", tests[index].str);
617 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
619 (*tests[index].fun)(reporter, tests[index].str);
621 if (tests[index].fun == stopTest) {
622 SkDebugf("lastTest\n");
628 index += reverse ? -1 : 1;