blob: b8ce3189a843ee6ce0be45d04d4b2ee37b917de8 [file] [log] [blame]
mtklein@google.coma7a9f372013-10-18 20:52:44 +00001#include "DMWriteTask.h"
2
3#include "DMUtil.h"
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +00004#include "SkColorPriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -07005#include "SkCommonFlags.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00006#include "SkImageEncoder.h"
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +00007#include "SkMallocPixelRef.h"
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +00008#include "SkStream.h"
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +00009#include "SkString.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +000010
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000011DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png data. "
12 "This means -r won't work, but skdiff will still work fine.");
mtklein@google.coma7a9f372013-10-18 20:52:44 +000013
14namespace DM {
15
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000016// Splits off the last N suffixes of name (splitting on _) and appends them to out.
17// Returns the total number of characters consumed.
18static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000019 SkTArray<SkString> split;
20 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000021 int consumed = 0;
22 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000023 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000024 out->push_back(split[split.count()-i-1]);
25 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000026 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000027 return consumed;
28}
29
mtklein30bf3e22014-06-03 13:57:14 -070030inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suffixList) {
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000031 const int suffixes = parent.depth() + 1;
32 const SkString& name = parent.name();
mtklein30bf3e22014-06-03 13:57:14 -070033 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList);
34 return SkString(name.c_str(), name.size() - totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000035}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000036
mtklein30bf3e22014-06-03 13:57:14 -070037WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
38 : CpuTask(parent)
39 , fGmName(find_gm_name(parent, &fSuffixes))
40 , fBitmap(bitmap)
41 , fData(NULL)
42 , fExtension(".png") {}
43
44WriteTask::WriteTask(const Task& parent, SkData *data, const char* ext)
45 : CpuTask(parent)
46 , fGmName(find_gm_name(parent, &fSuffixes))
47 , fData(SkRef(data))
48 , fExtension(ext) {}
49
rmistry@google.comd6bab022013-12-02 13:50:38 +000050void WriteTask::makeDirOrFail(SkString dir) {
51 if (!sk_mkdir(dir.c_str())) {
52 this->fail();
53 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000054}
55
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000056namespace {
57
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000058// One file that first contains a .png of an SkBitmap, then its raw pixels.
59// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
60struct PngAndRaw {
61 static bool Encode(SkBitmap bitmap, const char* path) {
62 SkFILEWStream stream(path);
63 if (!stream.isValid()) {
64 SkDebugf("Can't write %s.\n", path);
65 return false;
66 }
67
68 // Write a PNG first for humans and other tools to look at.
69 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
70 SkDebugf("Can't encode a PNG.\n");
71 return false;
72 }
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000073 if (FLAGS_writePngOnly) {
74 return true;
75 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000076
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000077 // Pad out so the raw pixels start 4-byte aligned.
78 const uint32_t maxPadding = 0;
79 const size_t pos = stream.bytesWritten();
80 stream.write(&maxPadding, SkAlign4(pos) - pos);
81
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000082 // Then write our secret raw pixels that only DM reads.
83 SkAutoLockPixels lock(bitmap);
84 return stream.write(bitmap.getPixels(), bitmap.getSize());
85 }
86
87 // This assumes bitmap already has allocated pixels of the correct size.
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000088 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
89 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
90 if (!data) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000091 SkDebugf("Can't read %s.\n", path);
92 return false;
93 }
94
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000095 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
96 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
97 const size_t bitmapBytes = info.getSafeSize(rowBytes);
98 if (data->size() < bitmapBytes) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000099 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
100 return false;
101 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000102
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000103 const size_t offset = data->size() - bitmapBytes;
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000104 SkAutoTUnref<SkData> subset(
105 SkData::NewSubset(data, offset, bitmapBytes));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000106 SkAutoTUnref<SkPixelRef> pixels(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000107 SkMallocPixelRef::NewWithData(
108 info, rowBytes, NULL/*ctable*/, subset));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000109 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000110
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000111 bitmap->setInfo(info, rowBytes);
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000112 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000113 return true;
114 }
115};
116
mtklein30bf3e22014-06-03 13:57:14 -0700117// Does not take ownership of data.
118bool save_data_to_file(const SkData* data, const char* path) {
119 SkFILEWStream stream(path);
120 if (!stream.isValid() || !stream.write(data->data(), data->size())) {
121 SkDebugf("Can't write %s.\n", path);
122 return false;
123 }
124 return true;
125}
126
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000127} // namespace
128
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000129void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000130 SkString dir(FLAGS_writePath[0]);
caryclark17f0b6d2014-07-22 10:15:34 -0700131#if SK_BUILD_FOR_IOS
132 if (dir.equals("@")) {
133 dir.set(FLAGS_resourcePath[0]);
134 }
135#endif
rmistry@google.comd6bab022013-12-02 13:50:38 +0000136 this->makeDirOrFail(dir);
137 for (int i = 0; i < fSuffixes.count(); i++) {
tfarinaa8e2e152014-07-28 19:26:58 -0700138 dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
rmistry@google.comd6bab022013-12-02 13:50:38 +0000139 this->makeDirOrFail(dir);
140 }
mtklein30bf3e22014-06-03 13:57:14 -0700141
tfarinaa8e2e152014-07-28 19:26:58 -0700142 SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str());
mtklein30bf3e22014-06-03 13:57:14 -0700143 path.append(fExtension);
144
145 const bool ok = fData.get() ? save_data_to_file(fData, path.c_str())
146 : PngAndRaw::Encode(fBitmap, path.c_str());
147 if (!ok) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000148 this->fail();
149 }
150}
151
152SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000153 SkString name("writing ");
154 for (int i = 0; i < fSuffixes.count(); i++) {
155 name.appendf("%s/", fSuffixes[i].c_str());
156 }
157 name.append(fGmName.c_str());
158 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000159}
160
161bool WriteTask::shouldSkip() const {
162 return FLAGS_writePath.isEmpty();
163}
164
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000165static SkString path_to_expected_image(const char* root, const Task& task) {
166 SkString filename = task.name();
167
168 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
169 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
170 SkTArray<SkString> suffixes;
171 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
172 SkASSERT(1 == suffixes.count());
173
174 // We'll look in root/suffix for images.
tfarinaa8e2e152014-07-28 19:26:58 -0700175 const SkString dir = SkOSPath::Join(root, suffixes[0].c_str());
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000176
177 // Remove the suffix and tack on a .png.
178 filename.remove(filename.size() - suffixLength, suffixLength);
179 filename.append(".png");
180
tfarinaa8e2e152014-07-28 19:26:58 -0700181 return SkOSPath::Join(dir.c_str(), filename.c_str());
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000182}
183
184bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000185 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
186 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
187 return false;
188 }
189
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000190 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000191 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000192 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000193 return false;
194 }
195
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000196 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000197}
198
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000199} // namespace DM