1 #include "PathOpsExtendedTest.h" 2 #include "PathOpsThreadedCommon.h" 3 #include "SkBitmap.h" 4 #include "SkDevice.h" 5 #include "SkCanvas.h" 6 #include "SkImageDecoder.h" 7 #include "SkImageEncoder.h" 8 #include "SkStream.h" 9 #include "SkOSFile.h" 10 #include "SkPicture.h" 11 #include "SkString.h" 12 13 #ifdef SK_BUILD_FOR_WIN 14 #define PATH_SLASH "\\" 15 #define IN_DIR "D:" PATH_SLASH "skp" 16 #define OUT_DIR "D:" PATH_SLASH 17 #else 18 #define PATH_SLASH "/" 19 #define IN_DIR "/Volumes/Untitled" PATH_SLASH 20 #define OUT_DIR PATH_SLASH 21 #endif 22 23 static const char pictDir[] = IN_DIR ; 24 static const char outSkpClipDir[] = OUT_DIR "skpClip"; 25 static const char outOldClipDir[] = OUT_DIR "oldClip"; 26 27 static void make_filepath(SkString* path, const char* dir, const SkString& name) { 28 size_t len = strlen(dir); 29 path->set(dir); 30 if (len > 0 && dir[len - 1] != PATH_SLASH[0]) { 31 path->append(PATH_SLASH); 32 } 33 path->append(name); 34 } 35 36 static void testOne(const SkString& filename) { 37 #if DEBUG_SHOW_TEST_NAME 38 SkString testName(filename); 39 const char http[] = "http"; 40 if (testName.startsWith(http)) { 41 testName.remove(0, sizeof(http) - 1); 42 } 43 while (testName.startsWith("_")) { 44 testName.remove(0, 1); 45 } 46 const char dotSkp[] = ".skp"; 47 if (testName.endsWith(dotSkp)) { 48 size_t len = testName.size(); 49 testName.remove(len - (sizeof(dotSkp) - 1), sizeof(dotSkp) - 1); 50 } 51 testName.prepend("skp"); 52 testName.append("1"); 53 strncpy(DEBUG_FILENAME_STRING, testName.c_str(), DEBUG_FILENAME_STRING_LENGTH); 54 #endif 55 SkString path; 56 make_filepath(&path, pictDir, filename); 57 SkFILEStream stream(path.c_str()); 58 if (!stream.isValid()) { 59 return; 60 } 61 SkPicture* pic = SkPicture::CreateFromStream(&stream, &SkImageDecoder::DecodeMemory); 62 if (!pic) { 63 SkDebugf("unable to decode %s\n", filename.c_str()); 64 return; 65 } 66 int width = pic->width(); 67 int height = pic->height(); 68 SkBitmap bitmap; 69 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 70 bool success = bitmap.allocPixels(); 71 if (!success) { 72 SkDebugf("unable to allocate bitmap for %s\n", filename.c_str()); 73 return; 74 } 75 SkCanvas canvas(bitmap); 76 SkString pngName(filename); 77 pngName.remove(pngName.size() - 3, 3); 78 pngName.append("png"); 79 for (int i = 0; i < 2; ++i) { 80 bool useOp = i ? true : false; 81 canvas.setAllowSimplifyClip(useOp); 82 pic->draw(&canvas); 83 SkString outFile; 84 make_filepath(&outFile, useOp ? outSkpClipDir : outOldClipDir, pngName); 85 SkImageEncoder::EncodeFile(outFile.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); 86 } 87 SkDELETE(pic); 88 } 89 90 const char skipBefore[] = "http___kkiste_to.skp"; 91 92 static void PathOpsSkpClipTest(skiatest::Reporter* reporter) { 93 SkOSFile::Iter iter(pictDir, "skp"); 94 SkString filename; 95 int testCount = 0; 96 while (iter.next(&filename)) { 97 if (strcmp(filename.c_str(), skipBefore) < 0) { 98 continue; 99 } 100 testOne(filename); 101 if (reporter->verbose()) { 102 SkDebugf("."); 103 if (++testCount % 100 == 0) { 104 SkDebugf("\n"); 105 } 106 } 107 reporter->bumpTestCount(); 108 } 109 } 110 111 static void testSkpClipMain(PathOpsThreadState* data) { 112 SkString str(data->fSerialNo); 113 testOne(str); 114 if (data->fReporter->verbose()) { 115 SkDebugf("."); 116 static int threadTestCount; 117 sk_atomic_inc(&threadTestCount); 118 if (threadTestCount % 100 == 0) { 119 SkDebugf("\n"); 120 } 121 } 122 } 123 124 static void PathOpsSkpClipThreadedTest(skiatest::Reporter* reporter) { 125 int threadCount = initializeTests(reporter, "skpClipThreadedTest"); 126 PathOpsThreadedTestRunner testRunner(reporter, threadCount); 127 SkOSFile::Iter iter(pictDir, "skp"); 128 SkString filename; 129 while (iter.next(&filename)) { 130 if (strcmp(filename.c_str(), skipBefore) < 0) { 131 continue; 132 } 133 *testRunner.fRunnables.append() = SkNEW_ARGS(PathOpsThreadedRunnable, 134 (&testSkpClipMain, filename.c_str(), &testRunner)); 135 reporter->bumpTestCount(); 136 } 137 testRunner.render(); 138 } 139 140 static void PathOpsSkpClipOneOffTest(skiatest::Reporter* reporter) { 141 SkString filename(skipBefore); 142 testOne(filename); 143 } 144 145 #include "TestClassDef.h" 146 DEFINE_TESTCLASS_SHORT(PathOpsSkpClipTest) 147 148 DEFINE_TESTCLASS_SHORT(PathOpsSkpClipOneOffTest) 149 150 DEFINE_TESTCLASS_SHORT(PathOpsSkpClipThreadedTest) 151