blob: 146c42ade793d562811d9b31ea4fdbe6ab9119da [file] [log] [blame]
caryclark@google.com07e97fc2013-07-08 17:17:02 +00001#include "PathOpsExtendedTest.h"
2#include "PathOpsThreadedCommon.h"
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +00003#include "SkBitmap.h"
4#include "SkDevice.h"
5#include "SkCanvas.h"
caryclark@google.comcffbcc32013-06-04 17:59:42 +00006#include "SkImageDecoder.h"
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +00007#include "SkImageEncoder.h"
8#include "SkStream.h"
9#include "SkOSFile.h"
10#include "SkPicture.h"
11#include "SkString.h"
caryclark@google.com07e97fc2013-07-08 17:17:02 +000012
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
23static const char pictDir[] = IN_DIR ;
24static const char outSkpClipDir[] = OUT_DIR "skpClip";
25static const char outOldClipDir[] = OUT_DIR "oldClip";
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000026
27static void make_filepath(SkString* path, const char* dir, const SkString& name) {
28 size_t len = strlen(dir);
29 path->set(dir);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000030 if (len > 0 && dir[len - 1] != PATH_SLASH[0]) {
31 path->append(PATH_SLASH);
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000032 }
33 path->append(name);
34}
35
caryclark@google.com07e97fc2013-07-08 17:17:02 +000036static 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 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +000061 SkPicture* pic = SkPicture::CreateFromStream(&stream, &SkImageDecoder::DecodeMemory);
62 if (!pic) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000063 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);
caryclark@google.com4fdbb222013-07-23 15:27:41 +000070 bool success = bitmap.allocPixels();
caryclark@google.com07e97fc2013-07-08 17:17:02 +000071 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
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000090const char skipBefore[] = "http___kkiste_to.skp";
caryclark@google.com07e97fc2013-07-08 17:17:02 +000091
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000092static void PathOpsSkpClipTest(skiatest::Reporter* reporter) {
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000093 SkOSFile::Iter iter(pictDir, "skp");
94 SkString filename;
caryclark@google.com07e97fc2013-07-08 17:17:02 +000095 int testCount = 0;
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000096 while (iter.next(&filename)) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000097 if (strcmp(filename.c_str(), skipBefore) < 0) {
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000098 continue;
99 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000100 testOne(filename);
101 if (reporter->verbose()) {
102 SkDebugf(".");
103 if (++testCount % 100 == 0) {
104 SkDebugf("\n");
105 }
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +0000106 }
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +0000107 reporter->bumpTestCount();
108 }
109}
110
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000111static 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
124static 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
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000140static void PathOpsSkpClipOneOffTest(skiatest::Reporter* reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000141 SkString filename(skipBefore);
142 testOne(filename);
143}
144
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +0000145#include "TestClassDef.h"
146DEFINE_TESTCLASS_SHORT(PathOpsSkpClipTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000147
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000148DEFINE_TESTCLASS_SHORT(PathOpsSkpClipOneOffTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000149
150DEFINE_TESTCLASS_SHORT(PathOpsSkpClipThreadedTest)