PathOpsSkpClipTest.cpp revision deee496cd30070e52556dcb538c2e5eb39b66b81
1 2#include "SkBitmap.h" 3#include "SkCanvas.h" 4#include "SkColor.h" 5#include "SkColorPriv.h" 6#include "SkDevice.h" 7#include "SkGraphics.h" 8#include "SkImageDecoder.h" 9#include "SkImageEncoder.h" 10#include "SkOSFile.h" 11#include "SkPathOpsDebug.h" 12#include "SkPicture.h" 13#include "SkRTConf.h" 14#include "SkStream.h" 15#include "SkString.h" 16#include "SkTArray.h" 17#include "SkTDArray.h" 18#include "SkThreadPool.h" 19#include "SkTime.h" 20#include "Test.h" 21 22#ifdef SK_BUILD_FOR_WIN 23 #define PATH_SLASH "\\" 24 #define IN_DIR "D:\\9-30-13\\" 25 #define OUT_DIR "D:\\opSkpClip\\1\\" 26#else 27 #define PATH_SLASH "/" 28 #ifdef SK_BUILD_FOR_MAC 29 #define IN_DIR "/Volumes/tera/9-30-13/skp" 30 #define OUT_DIR "/Volumes/tera/out/9-30-13/1/" 31 #else 32 #define IN_DIR "/usr/local/google/home/caryclark/skps/9-30-13/skp" 33 #define OUT_DIR "/mnt/skia/opSkpClip/1/" 34 #endif 35#endif 36 37const struct { 38 int directory; 39 const char* filename; 40} skipOverSept[] = { 41 {9, "http___www_symptome_ch_.skp"}, // triangle clip with corner at x.999 42 {11, "http___www_menly_fr_.skp"}, 43 {12, "http___www_banrasdr_com_.skp"}, 44}; 45 46size_t skipOverSeptCount = sizeof(skipOverSept) / sizeof(skipOverSept[0]); 47 48enum TestStep { 49 kCompareBits, 50 kEncodeFiles, 51}; 52 53enum { 54 kMaxLength = 128, 55 kMaxFiles = 128, 56 kSmallLimit = 1000, 57}; 58 59struct TestResult { 60 void init(int dirNo) { 61 fDirNo = dirNo; 62 sk_bzero(fFilename, sizeof(fFilename)); 63 fTestStep = kCompareBits; 64 fScaleOversized = true; 65 } 66 67 SkString status() { 68 SkString outStr; 69 outStr.printf("%s %d %d\n", fFilename, fPixelError, fTime); 70 return outStr; 71 } 72 73 static void Test(int dirNo, const char* filename, TestStep testStep) { 74 TestResult test; 75 test.init(dirNo); 76 test.fTestStep = testStep; 77 strcpy(test.fFilename, filename); 78 test.testOne(); 79 } 80 81 void test(int dirNo, const SkString& filename) { 82 init(dirNo); 83 strcpy(fFilename, filename.c_str()); 84 testOne(); 85 } 86 87 void testOne(); 88 89 char fFilename[kMaxLength]; 90 TestStep fTestStep; 91 int fDirNo; 92 int fPixelError; 93 int fTime; 94 bool fScaleOversized; 95}; 96 97struct TestState { 98 void init(int dirNo, skiatest::Reporter* reporter) { 99 fReporter = reporter; 100 fResult.init(dirNo); 101 fFoundCount = 0; 102 TestState::fSmallCount = 0; 103 fSmallestError = 0; 104 sk_bzero(fFilesFound, sizeof(fFilesFound)); 105 sk_bzero(fDirsFound, sizeof(fDirsFound)); 106 sk_bzero(fError, sizeof(fError)); 107 } 108 109 static bool bumpSmallCount() { 110 sk_atomic_inc(&fSmallCount); 111 return fSmallCount > kSmallLimit; 112 } 113 114 static void clearSmallCount() { 115 if (fSmallCount < kSmallLimit) { 116 fSmallCount = 0; 117 } 118 } 119 120 char fFilesFound[kMaxFiles][kMaxLength]; 121 int fDirsFound[kMaxFiles]; 122 int fError[kMaxFiles]; 123 int fFoundCount; 124 static int fSmallCount; 125 int fSmallestError; 126 skiatest::Reporter* fReporter; 127 TestResult fResult; 128}; 129 130int TestState::fSmallCount; 131 132struct TestRunner { 133 TestRunner(skiatest::Reporter* reporter, int threadCount) 134 : fNumThreads(threadCount) 135 , fReporter(reporter) { 136 } 137 138 ~TestRunner(); 139 void render(); 140 int fNumThreads; 141 SkTDArray<class TestRunnable*> fRunnables; 142 skiatest::Reporter* fReporter; 143}; 144 145class TestRunnable : public SkRunnable { 146public: 147 TestRunnable(void (*testFun)(TestState*), int dirNo, TestRunner* runner) { 148 fState.init(dirNo, runner->fReporter); 149 fTestFun = testFun; 150 } 151 152 virtual void run() SK_OVERRIDE { 153 SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024); 154 (*fTestFun)(&fState); 155 } 156 157 TestState fState; 158 void (*fTestFun)(TestState*); 159}; 160 161TestRunner::~TestRunner() { 162 for (int index = 0; index < fRunnables.count(); index++) { 163 SkDELETE(fRunnables[index]); 164 } 165} 166 167void TestRunner::render() { 168 SkThreadPool pool(fNumThreads); 169 for (int index = 0; index < fRunnables.count(); ++ index) { 170 pool.add(fRunnables[index]); 171 } 172} 173 174//////////////////////////////////////////////// 175 176static const char outOpDir[] = OUT_DIR "opClip"; 177static const char outOldDir[] = OUT_DIR "oldClip"; 178static const char outSkpDir[] = OUT_DIR "skpTest"; 179static const char outDiffDir[] = OUT_DIR "outTest"; 180static const char outStatusDir[] = OUT_DIR "statusTest"; 181 182static SkString make_filepath(int dirNo, const char* dir, const char* name) { 183 SkString path(dir); 184 if (dirNo) { 185 path.appendf("%d", dirNo); 186 } 187 path.append(PATH_SLASH); 188 path.append(name); 189 return path; 190} 191 192static SkString make_in_dir_name(int dirNo) { 193 SkString dirName(IN_DIR); 194 dirName.appendf("%d", dirNo); 195 if (!sk_exists(dirName.c_str())) { 196 SkDebugf("could not read dir %s\n", dirName.c_str()); 197 return SkString(); 198 } 199 return dirName; 200} 201 202static bool make_one_out_dir(const char* outDirStr) { 203 SkString outDir = make_filepath(0, outDirStr, ""); 204 if (!sk_exists(outDir.c_str())) { 205 if (!sk_mkdir(outDir.c_str())) { 206 SkDebugf("could not create dir %s\n", outDir.c_str()); 207 return false; 208 } 209 } 210 return true; 211} 212 213static bool make_out_dirs() { 214 SkString outDir = make_filepath(0, OUT_DIR, ""); 215 if (!sk_exists(outDir.c_str())) { 216 if (!sk_mkdir(outDir.c_str())) { 217 SkDebugf("could not create dir %s\n", outDir.c_str()); 218 return false; 219 } 220 } 221 return make_one_out_dir(outOldDir) 222 && make_one_out_dir(outOpDir) 223 && make_one_out_dir(outSkpDir) 224 && make_one_out_dir(outDiffDir) 225 && make_one_out_dir(outStatusDir); 226} 227 228static SkString make_png_name(const char* filename) { 229 SkString pngName = SkString(filename); 230 pngName.remove(pngName.size() - 3, 3); 231 pngName.append("png"); 232 return pngName; 233} 234 235static int similarBits(const SkBitmap& gr, const SkBitmap& sk) { 236 const int kRowCount = 3; 237 const int kThreshold = 3; 238 int width = SkTMin(gr.width(), sk.width()); 239 if (width < kRowCount) { 240 return true; 241 } 242 int height = SkTMin(gr.height(), sk.height()); 243 if (height < kRowCount) { 244 return true; 245 } 246 int errorTotal = 0; 247 SkTArray<int, true> errorRows; 248 errorRows.push_back_n(width * kRowCount); 249 SkAutoLockPixels autoGr(gr); 250 SkAutoLockPixels autoSk(sk); 251 for (int y = 0; y < height; ++y) { 252 SkPMColor* grRow = gr.getAddr32(0, y); 253 SkPMColor* skRow = sk.getAddr32(0, y); 254 int* base = &errorRows[0]; 255 int* cOut = &errorRows[y % kRowCount]; 256 for (int x = 0; x < width; ++x) { 257 SkPMColor grColor = grRow[x]; 258 SkPMColor skColor = skRow[x]; 259 int dr = SkGetPackedR32(grColor) - SkGetPackedR32(skColor); 260 int dg = SkGetPackedG32(grColor) - SkGetPackedG32(skColor); 261 int db = SkGetPackedB32(grColor) - SkGetPackedB32(skColor); 262 int error = cOut[x] = SkTMax(SkAbs32(dr), SkTMax(SkAbs32(dg), SkAbs32(db))); 263 if (error < kThreshold || x < 2) { 264 continue; 265 } 266 if (base[x - 2] < kThreshold 267 || base[width + x - 2] < kThreshold 268 || base[width * 2 + x - 2] < kThreshold 269 || base[x - 1] < kThreshold 270 || base[width + x - 1] < kThreshold 271 || base[width * 2 + x - 1] < kThreshold 272 || base[x] < kThreshold 273 || base[width + x] < kThreshold 274 || base[width * 2 + x] < kThreshold) { 275 continue; 276 } 277 errorTotal += error; 278 } 279 } 280 return errorTotal; 281} 282 283static bool addError(TestState* data, const TestResult& testResult) { 284 bool foundSmaller = false; 285 int dCount = data->fFoundCount; 286 int pixelError = testResult.fPixelError; 287 if (data->fFoundCount < kMaxFiles) { 288 data->fError[dCount] = pixelError; 289 strcpy(data->fFilesFound[dCount], testResult.fFilename); 290 data->fDirsFound[dCount] = testResult.fDirNo; 291 ++data->fFoundCount; 292 } else if (pixelError > data->fSmallestError) { 293 int smallest = SK_MaxS32; 294 int smallestIndex = 0; 295 for (int index = 0; index < kMaxFiles; ++index) { 296 if (smallest > data->fError[index]) { 297 smallest = data->fError[index]; 298 smallestIndex = index; 299 } 300 } 301 data->fError[smallestIndex] = pixelError; 302 strcpy(data->fFilesFound[smallestIndex], testResult.fFilename); 303 data->fDirsFound[smallestIndex] = testResult.fDirNo; 304 data->fSmallestError = SK_MaxS32; 305 for (int index = 0; index < kMaxFiles; ++index) { 306 if (data->fSmallestError > data->fError[index]) { 307 data->fSmallestError = data->fError[index]; 308 } 309 } 310 SkDebugf("*%d*", data->fSmallestError); 311 foundSmaller = true; 312 } 313 return foundSmaller; 314} 315 316 317 318static SkMSec timePict(SkPicture* pic, SkCanvas* canvas) { 319 canvas->save(); 320 int pWidth = pic->width(); 321 int pHeight = pic->height(); 322 const int maxDimension = 1000; 323 const int slices = 3; 324 int xInterval = SkTMax(pWidth - maxDimension, 0) / (slices - 1); 325 int yInterval = SkTMax(pHeight - maxDimension, 0) / (slices - 1); 326 SkRect rect = {0, 0, SkIntToScalar(SkTMin(maxDimension, pWidth)), 327 SkIntToScalar(SkTMin(maxDimension, pHeight))}; 328 canvas->clipRect(rect); 329 SkMSec start = SkTime::GetMSecs(); 330 for (int x = 0; x < slices; ++x) { 331 for (int y = 0; y < slices; ++y) { 332 pic->draw(canvas); 333 canvas->translate(0, SkIntToScalar(yInterval)); 334 } 335 canvas->translate(SkIntToScalar(xInterval), SkIntToScalar(-yInterval * slices)); 336 } 337 SkMSec end = SkTime::GetMSecs(); 338 canvas->restore(); 339 return end - start; 340} 341 342static void drawPict(SkPicture* pic, SkCanvas* canvas, int scale) { 343 canvas->clear(SK_ColorWHITE); 344 if (scale != 1) { 345 canvas->save(); 346 canvas->scale(1.0f / scale, 1.0f / scale); 347 } 348 pic->draw(canvas); 349 if (scale != 1) { 350 canvas->restore(); 351 } 352} 353 354static void writePict(const SkBitmap& bitmap, const char* outDir, const char* pngName) { 355 SkString outFile = make_filepath(0, outDir, pngName); 356 if (!SkImageEncoder::EncodeFile(outFile.c_str(), bitmap, 357 SkImageEncoder::kPNG_Type, 100)) { 358 SkDebugf("unable to encode gr %s (width=%d height=%d)\n", pngName, 359 bitmap.width(), bitmap.height()); 360 } 361} 362 363void TestResult::testOne() { 364 SkPicture* pic = NULL; 365 { 366 #if DEBUG_SHOW_TEST_NAME 367 if (fTestStep == kCompareBits) { 368 SkString testName(fFilename); 369 const char http[] = "http"; 370 if (testName.startsWith(http)) { 371 testName.remove(0, sizeof(http) - 1); 372 } 373 while (testName.startsWith("_")) { 374 testName.remove(0, 1); 375 } 376 const char dotSkp[] = ".skp"; 377 if (testName.endsWith(dotSkp)) { 378 size_t len = testName.size(); 379 testName.remove(len - (sizeof(dotSkp) - 1), sizeof(dotSkp) - 1); 380 } 381 testName.prepend("skp"); 382 testName.append("1"); 383 strncpy(DEBUG_FILENAME_STRING, testName.c_str(), DEBUG_FILENAME_STRING_LENGTH); 384 } else if (fTestStep == kEncodeFiles) { 385 strncpy(DEBUG_FILENAME_STRING, "", DEBUG_FILENAME_STRING_LENGTH); 386 } 387 #endif 388 SkString path = make_filepath(fDirNo, IN_DIR, fFilename); 389 SkFILEStream stream(path.c_str()); 390 if (!stream.isValid()) { 391 SkDebugf("invalid stream %s\n", path.c_str()); 392 goto finish; 393 } 394 SkPicture* pic = SkPicture::CreateFromStream(&stream, &SkImageDecoder::DecodeMemory); 395 if (!pic) { 396 SkDebugf("unable to decode %s\n", fFilename); 397 goto finish; 398 } 399 int width = pic->width(); 400 int height = pic->height(); 401 SkBitmap oldBitmap, opBitmap; 402 int scale = 1; 403 do { 404 int dimX = (width + scale - 1) / scale; 405 int dimY = (height + scale - 1) / scale; 406 if (oldBitmap.allocN32Pixels(dimX, dimY) && 407 opBitmap.allocN32Pixels(dimX, dimY)) { 408 break; 409 } 410 SkDebugf("-%d-", scale); 411 } while ((scale *= 2) < 256); 412 if (scale >= 256) { 413 SkDebugf("unable to allocate bitmap for %s (w=%d h=%d)\n", fFilename, 414 width, height); 415 return; 416 } 417 oldBitmap.eraseColor(SK_ColorWHITE); 418 SkCanvas oldCanvas(oldBitmap); 419 oldCanvas.setAllowSimplifyClip(false); 420 opBitmap.eraseColor(SK_ColorWHITE); 421 SkCanvas opCanvas(opBitmap); 422 opCanvas.setAllowSimplifyClip(true); 423 drawPict(pic, &oldCanvas, fScaleOversized ? scale : 1); 424 drawPict(pic, &opCanvas, fScaleOversized ? scale : 1); 425 if (fTestStep == kCompareBits) { 426 fPixelError = similarBits(oldBitmap, opBitmap); 427 int oldTime = timePict(pic, &oldCanvas); 428 int opTime = timePict(pic, &opCanvas); 429 fTime = oldTime - opTime; 430 } else if (fTestStep == kEncodeFiles) { 431 SkString pngStr = make_png_name(fFilename); 432 const char* pngName = pngStr.c_str(); 433 writePict(oldBitmap, outOldDir, pngName); 434 writePict(opBitmap, outOpDir, pngName); 435 } 436 } 437finish: 438 SkDELETE(pic); 439} 440 441static SkString makeStatusString(int dirNo) { 442 SkString statName; 443 statName.printf("stats%d.txt", dirNo); 444 SkString statusFile = make_filepath(0, outStatusDir, statName.c_str()); 445 return statusFile; 446} 447 448class PreParser { 449public: 450 PreParser(int dirNo) 451 : fDirNo(dirNo) 452 , fIndex(0) { 453 SkString statusPath = makeStatusString(dirNo); 454 if (!sk_exists(statusPath.c_str())) { 455 return; 456 } 457 SkFILEStream reader; 458 reader.setPath(statusPath.c_str()); 459 while (fetch(reader, &fResults.push_back())) 460 ; 461 fResults.pop_back(); 462 } 463 464 bool fetch(SkFILEStream& reader, TestResult* result) { 465 char c; 466 int i = 0; 467 result->init(fDirNo); 468 result->fPixelError = 0; 469 result->fTime = 0; 470 do { 471 bool readOne = reader.read(&c, 1) != 0; 472 if (!readOne) { 473 SkASSERT(i == 0); 474 return false; 475 } 476 if (c == ' ') { 477 result->fFilename[i++] = '\0'; 478 break; 479 } 480 result->fFilename[i++] = c; 481 SkASSERT(i < kMaxLength); 482 } while (true); 483 do { 484 SkAssertResult(reader.read(&c, 1)); 485 if (c == ' ') { 486 break; 487 } 488 SkASSERT(c >= '0' && c <= '9'); 489 result->fPixelError = result->fPixelError * 10 + (c - '0'); 490 } while (true); 491 bool minus = false; 492 do { 493 SkAssertResult(reader.read(&c, 1)); 494 if (c == '\n') { 495 break; 496 } 497 if (c == '-') { 498 minus = true; 499 continue; 500 } 501 SkASSERT(c >= '0' && c <= '9'); 502 result->fTime = result->fTime * 10 + (c - '0'); 503 } while (true); 504 if (minus) { 505 result->fTime = -result->fTime; 506 } 507 return true; 508 } 509 510 bool match(const SkString& filename, SkFILEWStream* stream, TestResult* result) { 511 if (fIndex < fResults.count()) { 512 *result = fResults[fIndex++]; 513 SkASSERT(filename.equals(result->fFilename)); 514 SkString outStr(result->status()); 515 stream->write(outStr.c_str(), outStr.size()); 516 return true; 517 } 518 return false; 519 } 520 521private: 522 int fDirNo; 523 int fIndex; 524 SkTArray<TestResult, true> fResults; 525}; 526 527static bool doOneDir(TestState* state) { 528 int dirNo = state->fResult.fDirNo; 529 skiatest::Reporter* reporter = state->fReporter; 530 SkString dirName = make_in_dir_name(dirNo); 531 SkASSERT(dirName.size()); 532 SkOSFile::Iter iter(dirName.c_str(), "skp"); 533 SkString filename; 534 int testCount = 0; 535 PreParser preParser(dirNo); 536 SkFILEWStream statusStream(makeStatusString(dirNo).c_str()); 537 while (iter.next(&filename)) { 538 for (size_t index = 0; index < skipOverSeptCount; ++index) { 539 if (skipOverSept[index].directory == dirNo 540 && strcmp(filename.c_str(), skipOverSept[index].filename) == 0) { 541 goto skipOver; 542 } 543 } 544 if (preParser.match(filename, &statusStream, &state->fResult)) { 545 addError(state, state->fResult); 546 ++testCount; 547 goto checkEarlyExit; 548 } 549 if (state->fSmallestError > 5000000) { 550 return false; 551 } 552 { 553 TestResult& result = state->fResult; 554 result.test(dirNo, filename); 555 SkString outStr(result.status()); 556 statusStream.write(outStr.c_str(), outStr.size()); 557 statusStream.flush(); 558 if (1) { 559 SkDebugf("%s", outStr.c_str()); 560 } 561 bool noMatch = addError(state, state->fResult); 562 if (noMatch) { 563 state->clearSmallCount(); 564 } else if (state->bumpSmallCount()) { 565 return false; 566 } 567 } 568 ++testCount; 569 if (reporter->verbose()) { 570 SkDebugf("."); 571 if (++testCount % 100 == 0) { 572 SkDebugf("%d\n", testCount); 573 } 574 } 575skipOver: 576 if (reporter->verbose()) { 577 static int threadTestCount; 578 SkDebugf("."); 579 sk_atomic_inc(&threadTestCount); 580 if (threadTestCount % 100 == 0) { 581 SkDebugf("%d\n", threadTestCount); 582 } 583 } 584checkEarlyExit: 585 if (1 && testCount == 20) { 586 return true; 587 } 588 } 589 return true; 590} 591 592static bool initTest() { 593#if !defined SK_BUILD_FOR_WIN && !defined SK_BUILD_FOR_MAC 594 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true); 595 SK_CONF_SET("images.png.suppressDecoderWarnings", true); 596#endif 597 return make_out_dirs(); 598} 599 600static void encodeFound(skiatest::Reporter* reporter, TestState& state) { 601 if (reporter->verbose()) { 602 for (int index = 0; index < state.fFoundCount; ++index) { 603 SkDebugf("%d %s %d\n", state.fDirsFound[index], state.fFilesFound[index], 604 state.fError[index]); 605 } 606 } 607 for (int index = 0; index < state.fFoundCount; ++index) { 608 TestResult::Test(state.fDirsFound[index], state.fFilesFound[index], kEncodeFiles); 609 if (state.fReporter->verbose()) SkDebugf("+"); 610 } 611} 612 613DEF_TEST(PathOpsSkpClip, reporter) { 614 if (!initTest()) { 615 return; 616 } 617 SkTArray<TestResult, true> errors; 618 TestState state; 619 state.init(0, reporter); 620 for (int dirNo = 1; dirNo <= 100; ++dirNo) { 621 if (reporter->verbose()) { 622 SkDebugf("dirNo=%d\n", dirNo); 623 } 624 state.fResult.fDirNo = dirNo; 625 if (!doOneDir(&state)) { 626 break; 627 } 628 } 629 encodeFound(reporter, state); 630} 631 632static void testSkpClipMain(TestState* data) { 633 (void) doOneDir(data); 634} 635 636DEF_TEST(PathOpsSkpClipThreaded, reporter) { 637 if (!initTest()) { 638 return; 639 } 640 int threadCount = reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 1; 641 TestRunner testRunner(reporter, threadCount); 642 for (int dirNo = 1; dirNo <= 100; ++dirNo) { 643 *testRunner.fRunnables.append() = SkNEW_ARGS(TestRunnable, 644 (&testSkpClipMain, dirNo, &testRunner)); 645 } 646 testRunner.render(); 647 TestState state; 648 state.init(0, reporter); 649 for (int dirNo = 1; dirNo <= 100; ++dirNo) { 650 TestState& testState = testRunner.fRunnables[dirNo - 1]->fState; 651 for (int inner = 0; inner < testState.fFoundCount; ++inner) { 652 TestResult& testResult = testState.fResult; 653 SkASSERT(testResult.fDirNo == dirNo); 654 testResult.fPixelError = testState.fError[inner]; 655 strcpy(testResult.fFilename, testState.fFilesFound[inner]); 656 addError(&state, testResult); 657 } 658 } 659 encodeFound(reporter, state); 660} 661 662DEF_TEST(PathOpsSkpClipOneOff, reporter) { 663 if (!initTest()) { 664 return; 665 } 666 const int testIndex = 43 - 41; 667 int dirNo = skipOverSept[testIndex].directory; 668 SkAssertResult(make_in_dir_name(dirNo).size()); 669 SkString filename(skipOverSept[testIndex].filename); 670 TestResult state; 671 state.test(dirNo, filename); 672 if (reporter->verbose()) { 673 SkDebugf("%s", state.status().c_str()); 674 } 675 state.fTestStep = kEncodeFiles; 676 state.testOne(); 677} 678