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"
18 #include "SkThreadPool.h"
20 #ifdef SK_BUILD_FOR_MAC
21 #include <sys/sysctl.h>
24 __SK_FORCE_IMAGE_DECODER_LINKING;
26 static const char marker[] =
29 "<script type=\"text/javascript\">\n"
33 static const char* opStrs[] = {
38 "kReverseDifference_PathOp",
41 static const char* opSuffixes[] = {
48 static bool gShowPath = false;
49 static bool gComparePathsAssert = true;
50 static bool gPathStrAssert = true;
52 #if DEBUG_SHOW_TEST_NAME
53 static void showPathData(const SkPath& path) {
54 SkPath::RawIter iter(path);
57 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
58 bool firstPtSet = false;
59 bool lastPtSet = true;
60 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
62 case SkPath::kMove_Verb:
63 if (firstPtSet && lastPtSet && firstPt != lastPt) {
64 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
65 firstPt.fX, firstPt.fY);
71 case SkPath::kLine_Verb:
72 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
73 pts[1].fX, pts[1].fY);
77 case SkPath::kQuad_Verb:
78 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
79 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
83 case SkPath::kCubic_Verb:
84 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
85 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
86 pts[3].fX, pts[3].fY);
90 case SkPath::kClose_Verb:
91 if (firstPtSet && lastPtSet && firstPt != lastPt) {
92 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
93 firstPt.fX, firstPt.fY);
95 firstPtSet = lastPtSet = false;
98 SkDEBUGFAIL("bad verb");
102 if (firstPtSet && lastPtSet && firstPt != lastPt) {
103 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
104 firstPt.fX, firstPt.fY);
109 void showOp(const SkPathOp op) {
111 case kDifference_PathOp:
112 SkDebugf("op difference\n");
114 case kIntersect_PathOp:
115 SkDebugf("op intersect\n");
118 SkDebugf("op union\n");
121 SkDebugf("op xor\n");
123 case kReverseDifference_PathOp:
124 SkDebugf("op reverse difference\n");
131 #if DEBUG_SHOW_TEST_NAME
132 static char hexorator(int x) {
142 void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
143 #if DEBUG_SHOW_TEST_NAME
144 state->fSerialNo[0] = hexorator(state->fA);
145 state->fSerialNo[1] = hexorator(state->fB);
146 state->fSerialNo[2] = hexorator(state->fC);
147 state->fSerialNo[3] = hexorator(state->fD);
148 state->fSerialNo[4] = hexorator(a);
149 state->fSerialNo[5] = hexorator(b);
150 state->fSerialNo[6] = hexorator(c);
151 state->fSerialNo[7] = hexorator(d);
152 state->fSerialNo[8] = '\0';
153 SkDebugf("%s\n", state->fSerialNo);
154 if (strcmp(state->fSerialNo, state->fKey) == 0) {
155 SkDebugf("%s\n", state->fPathStr);
160 const int bitWidth = 64;
161 const int bitHeight = 64;
163 static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
164 SkRect larger = one.getBounds();
165 larger.join(two.getBounds());
166 SkScalar largerWidth = larger.width();
167 if (largerWidth < 4) {
170 SkScalar largerHeight = larger.height();
171 if (largerHeight < 4) {
174 SkScalar hScale = (bitWidth - 2) / largerWidth;
175 SkScalar vScale = (bitHeight - 2) / largerHeight;
177 scale.preScale(hScale, vScale);
180 static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
182 if (bits.width() == 0) {
183 bits.allocN32Pixels(bitWidth * 2, bitHeight);
185 SkCanvas canvas(bits);
186 canvas.drawColor(SK_ColorWHITE);
189 const SkRect& bounds1 = scaledOne.getBounds();
190 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
191 canvas.drawPath(scaledOne, paint);
194 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
195 canvas.drawPath(scaledTwo, paint);
199 for (int y = 0; y < bitHeight - 1; ++y) {
200 uint32_t* addr1 = bits.getAddr32(0, y);
201 uint32_t* addr2 = bits.getAddr32(0, y + 1);
202 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
203 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
204 for (int x = 0; x < bitWidth - 1; ++x) {
206 bool err = addr1[x] != addr3[x];
208 errors2 += addr1[x + 1] != addr3[x + 1]
209 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
218 static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
219 SkPath& scaledTwo, int& error2x2) {
221 scaleMatrix(one, two, scale);
222 one.transform(scale, &scaledOne);
223 two.transform(scale, &scaledTwo);
224 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
227 bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
231 const SkRect& bounds1 = one.getBounds();
232 const SkRect& bounds2 = two.getBounds();
233 SkRect larger = bounds1;
234 larger.join(bounds2);
237 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
238 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
241 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
242 if (bitHeight >= (int) sizeof(out)) {
245 bits.allocN32Pixels(bitWidth * 2, bitHeight);
246 SkCanvas canvas(bits);
247 canvas.drawColor(SK_ColorWHITE);
250 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
251 canvas.drawPath(one, paint);
254 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
255 canvas.drawPath(two, paint);
257 for (int y = 0; y < bitHeight; ++y) {
258 uint32_t* addr1 = bits.getAddr32(0, y);
261 for (x = 0; x < bitWidth; ++x) {
262 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
265 for (x = bitWidth; x < bitWidth * 2; ++x) {
266 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
269 SkDebugf("%s\n", out);
274 static int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
275 const SkPath& two, SkBitmap& bitmap) {
277 SkPath scaledOne, scaledTwo;
278 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
279 if (errors2x2 == 0) {
282 const int MAX_ERRORS = 9;
283 REPORTER_ASSERT(reporter, errors2x2 <= MAX_ERRORS || !gComparePathsAssert);
284 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
287 const int gTestFirst = 4;
288 static int gTestNo = gTestFirst;
289 static SkTDArray<SkPathOp> gTestOp;
291 static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
292 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
293 const SkPathOp shapeOp, const SkMatrix& scale) {
294 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
295 SkString defaultTestName;
297 defaultTestName.printf("xOp%d%s", gTestNo, opSuffixes[shapeOp]);
298 testName = defaultTestName.c_str();
300 SkDebugf("static void %s(skiatest::Reporter* reporter, const char* filename) {\n", testName);
301 *gTestOp.append() = shapeOp;
303 SkDebugf(" SkPath path, pathB;\n");
304 #if DEBUG_SHOW_TEST_NAME
305 SkPathOpsDebug::ShowOnePath(a, "path", false);
306 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
308 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
310 drawAsciiPaths(scaledOne, scaledTwo, true);
313 void ShowTestArray() {
314 for (int x = gTestFirst; x < gTestNo; ++x) {
315 SkDebugf(" TEST(xOp%d%s),\n", x, opSuffixes[gTestOp[x - gTestFirst]]);
319 SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
320 SK_DECLARE_STATIC_MUTEX(compareDebugOut4);
321 static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
322 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
323 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale) {
325 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
326 if (errors2x2 == 0) {
328 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
332 const int MAX_ERRORS = 8;
333 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
334 SkAutoMutexAcquire autoM(compareDebugOut3);
335 SkDebugf("\n*** this test fails ***\n");
336 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
337 REPORTER_ASSERT(reporter, 0);
338 } else if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
339 SkAutoMutexAcquire autoM(compareDebugOut4);
340 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
342 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
345 // Default values for when reporter->verbose() is false.
346 static int testNumber = 55;
347 static const char* testName = "pathOpTest";
349 static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
350 outFile.writeText(testName);
351 outFile.writeDecAsText(testNumber);
354 outFile.writeText(nameSuffix);
358 static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
359 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
361 outFile.writeText("<div id=\"");
362 writeTestName(nameSuffix, outFile);
363 outFile.writeText("\">\n");
365 outFile.writeText(pathPrefix);
367 outFile.writeText(pathStr);
368 outFile.writeText("</div>\n\n");
370 outFile.writeText(marker);
371 outFile.writeText(" ");
372 writeTestName(nameSuffix, outFile);
373 outFile.writeText(",\n\n\n");
375 outFile.writeText("static void ");
376 writeTestName(nameSuffix, outFile);
377 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
379 outFile.writeText(", pathB");
381 outFile.writeText(";\n");
383 outFile.writeText(pathPrefix);
385 outFile.writeText(pathStr);
386 outFile.writeText(" ");
387 outFile.writeText(testFunction);
388 outFile.writeText("\n}\n\n");
390 outFile.writeText("static void (*firstTest)() = ");
391 writeTestName(nameSuffix, outFile);
392 outFile.writeText(";\n\n");
394 outFile.writeText("static struct {\n");
395 outFile.writeText(" void (*fun)();\n");
396 outFile.writeText(" const char* str;\n");
397 outFile.writeText("} tests[] = {\n");
398 outFile.writeText(" TEST(");
399 writeTestName(nameSuffix, outFile);
400 outFile.writeText("),\n");
405 SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
406 bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
407 const char* pathStr) {
408 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
409 path.setFillType(fillType);
410 #if DEBUG_SHOW_TEST_NAME
412 SkPathOpsDebug::ShowOnePath(path, "path", false);
415 if (!Simplify(path, &out)) {
416 SkDebugf("%s did not expect failure\n", __FUNCTION__);
417 REPORTER_ASSERT(state.fReporter, 0);
420 if (!state.fReporter->verbose()) {
423 int result = comparePaths(state.fReporter, NULL, path, out, *state.fBitmap);
424 if (result && gPathStrAssert) {
425 SkAutoMutexAcquire autoM(simplifyDebugOut);
427 sk_bzero(temp, sizeof(temp));
428 SkMemoryWStream stream(temp, sizeof(temp));
429 const char* pathPrefix = NULL;
430 const char* nameSuffix = NULL;
431 if (fillType == SkPath::kEvenOdd_FillType) {
432 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
435 const char testFunction[] = "testSimplify(reporter, path);";
436 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
438 REPORTER_ASSERT(state.fReporter, 0);
440 state.fReporter->bumpTestCount();
444 bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
445 #if DEBUG_SHOW_TEST_NAME
449 if (!Simplify(path, &out)) {
450 SkDebugf("%s did not expect failure\n", __FUNCTION__);
451 REPORTER_ASSERT(reporter, 0);
455 int result = comparePaths(reporter, filename, path, out, bitmap);
456 if (result && gPathStrAssert) {
457 REPORTER_ASSERT(reporter, 0);
459 reporter->bumpTestCount();
463 #if DEBUG_SHOW_TEST_NAME
464 static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
472 static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
473 const SkPathOp shapeOp, const char* testName, bool threaded) {
474 #if DEBUG_SHOW_TEST_NAME
475 showName(a, b, shapeOp);
478 if (!Op(a, b, shapeOp, &out) ) {
479 SkDebugf("%s did not expect failure\n", __FUNCTION__);
480 REPORTER_ASSERT(reporter, 0);
483 if (threaded && !reporter->verbose()) {
486 SkPath pathOut, scaledPathOut;
487 SkRegion rgnA, rgnB, openClip, rgnOut;
488 openClip.setRect(-16000, -16000, 16000, 16000);
489 rgnA.setPath(a, openClip);
490 rgnB.setPath(b, openClip);
491 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
492 rgnOut.getBoundaryPath(&pathOut);
495 scaleMatrix(a, b, scale);
496 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
497 SkPath scaledA, scaledB;
498 scaledA.addPath(a, scale);
499 scaledA.setFillType(a.getFillType());
500 scaledB.addPath(b, scale);
501 scaledB.setFillType(b.getFillType());
502 scaledRgnA.setPath(scaledA, openClip);
503 scaledRgnB.setPath(scaledB, openClip);
504 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
505 scaledRgnOut.getBoundaryPath(&scaledPathOut);
508 scaledOut.addPath(out, scale);
509 scaledOut.setFillType(out.getFillType());
510 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
511 a, b, shapeOp, scale);
512 if (result && gPathStrAssert) {
513 REPORTER_ASSERT(reporter, 0);
515 reporter->bumpTestCount();
519 bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
520 const SkPathOp shapeOp, const char* testName) {
521 return innerPathOp(reporter, a, b, shapeOp, testName, false);
524 bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
525 const SkPathOp shapeOp, const char* testName) {
526 #if DEBUG_SHOW_TEST_NAME
527 showName(a, b, shapeOp);
530 if (Op(a, b, shapeOp, &out) ) {
531 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
532 REPORTER_ASSERT(reporter, 0);
538 bool testThreadedPathOp(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);
543 SK_DECLARE_STATIC_MUTEX(gMutex);
545 int initializeTests(skiatest::Reporter* reporter, const char* test) {
546 #if 0 // doesn't work yet
547 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
548 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
550 if (reporter->verbose()) {
551 SkAutoMutexAcquire lock(gMutex);
553 size_t testNameSize = strlen(test);
554 SkFILEStream inFile("../../experimental/Intersection/op.htm");
555 if (inFile.isValid()) {
556 SkTDArray<char> inData;
557 inData.setCount((int) inFile.getLength());
558 size_t inLen = inData.count();
559 inFile.read(inData.begin(), inLen);
560 inFile.setPath(NULL);
561 char* insert = strstr(inData.begin(), marker);
563 insert += sizeof(marker) - 1;
564 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
565 testNumber = atoi(numLoc) + 1;
569 return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 1;
572 void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
573 const char testFunction[] = "testSimplify(path);";
574 const char* pathPrefix = NULL;
575 const char* nameSuffix = NULL;
576 if (pathFillType == SkPath::kEvenOdd_FillType) {
577 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
580 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
581 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
584 void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
585 const char testFunction[] = "testOp(path);";
586 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
587 const char* nameSuffix = opSuffixes[op];
588 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
589 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
592 void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
593 void (*firstTest)(skiatest::Reporter* , const char* filename),
594 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
598 while (index > 0 && tests[index].fun != firstTest) {
601 #if DEBUG_SHOW_TEST_NAME
602 SkDebugf("<div id=\"%s\">\n", tests[index].str);
603 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
605 (*tests[index].fun)(reporter, tests[index].str);
606 if (tests[index].fun == stopTest) {
610 index = reverse ? count - 1 : 0;
611 size_t last = reverse ? 0 : count - 1;
613 if (tests[index].fun != firstTest) {
614 #if DEBUG_SHOW_TEST_NAME
615 SkDebugf("<div id=\"%s\">\n", tests[index].str);
616 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
618 (*tests[index].fun)(reporter, tests[index].str);
620 if (tests[index].fun == stopTest) {
621 SkDebugf("lastTest\n");
627 index += reverse ? -1 : 1;