blob: 13f25d0fc25d0f9358dc9a716cb3315ad12157a8 [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"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00005#include "SkCommandLineFlags.h"
6#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
11DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000012DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png data. "
13 "This means -r won't work, but skdiff will still work fine.");
mtklein@google.coma7a9f372013-10-18 20:52:44 +000014
15namespace DM {
16
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000017// Splits off the last N suffixes of name (splitting on _) and appends them to out.
18// Returns the total number of characters consumed.
19static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000020 SkTArray<SkString> split;
21 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000022 int consumed = 0;
23 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000024 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000025 out->push_back(split[split.count()-i-1]);
26 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000027 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000028 return consumed;
29}
30
mtklein30bf3e22014-06-03 13:57:14 -070031inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suffixList) {
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000032 const int suffixes = parent.depth() + 1;
33 const SkString& name = parent.name();
mtklein30bf3e22014-06-03 13:57:14 -070034 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList);
35 return SkString(name.c_str(), name.size() - totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000036}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000037
mtklein30bf3e22014-06-03 13:57:14 -070038WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
39 : CpuTask(parent)
40 , fGmName(find_gm_name(parent, &fSuffixes))
41 , fBitmap(bitmap)
42 , fData(NULL)
43 , fExtension(".png") {}
44
45WriteTask::WriteTask(const Task& parent, SkData *data, const char* ext)
46 : CpuTask(parent)
47 , fGmName(find_gm_name(parent, &fSuffixes))
48 , fData(SkRef(data))
49 , fExtension(ext) {}
50
rmistry@google.comd6bab022013-12-02 13:50:38 +000051void WriteTask::makeDirOrFail(SkString dir) {
52 if (!sk_mkdir(dir.c_str())) {
53 this->fail();
54 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000055}
56
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000057namespace {
58
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000059// One file that first contains a .png of an SkBitmap, then its raw pixels.
60// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
61struct PngAndRaw {
62 static bool Encode(SkBitmap bitmap, const char* path) {
63 SkFILEWStream stream(path);
64 if (!stream.isValid()) {
65 SkDebugf("Can't write %s.\n", path);
66 return false;
67 }
68
69 // Write a PNG first for humans and other tools to look at.
70 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
71 SkDebugf("Can't encode a PNG.\n");
72 return false;
73 }
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000074 if (FLAGS_writePngOnly) {
75 return true;
76 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000077
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000078 // Pad out so the raw pixels start 4-byte aligned.
79 const uint32_t maxPadding = 0;
80 const size_t pos = stream.bytesWritten();
81 stream.write(&maxPadding, SkAlign4(pos) - pos);
82
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000083 // Then write our secret raw pixels that only DM reads.
84 SkAutoLockPixels lock(bitmap);
85 return stream.write(bitmap.getPixels(), bitmap.getSize());
86 }
87
88 // This assumes bitmap already has allocated pixels of the correct size.
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000089 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
90 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
91 if (!data) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000092 SkDebugf("Can't read %s.\n", path);
93 return false;
94 }
95
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000096 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
97 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
98 const size_t bitmapBytes = info.getSafeSize(rowBytes);
99 if (data->size() < bitmapBytes) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000100 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
101 return false;
102 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000103
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000104 const size_t offset = data->size() - bitmapBytes;
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000105 SkAutoTUnref<SkData> subset(
106 SkData::NewSubset(data, offset, bitmapBytes));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000107 SkAutoTUnref<SkPixelRef> pixels(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000108 SkMallocPixelRef::NewWithData(
109 info, rowBytes, NULL/*ctable*/, subset));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000110 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000111
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000112 bitmap->setInfo(info, rowBytes);
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000113 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000114 return true;
115 }
116};
117
mtklein30bf3e22014-06-03 13:57:14 -0700118// Does not take ownership of data.
119bool save_data_to_file(const SkData* data, const char* path) {
120 SkFILEWStream stream(path);
121 if (!stream.isValid() || !stream.write(data->data(), data->size())) {
122 SkDebugf("Can't write %s.\n", path);
123 return false;
124 }
125 return true;
126}
127
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000128} // namespace
129
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000130void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000131 SkString dir(FLAGS_writePath[0]);
132 this->makeDirOrFail(dir);
133 for (int i = 0; i < fSuffixes.count(); i++) {
134 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
135 this->makeDirOrFail(dir);
136 }
mtklein30bf3e22014-06-03 13:57:14 -0700137
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000138 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
mtklein30bf3e22014-06-03 13:57:14 -0700139 path.append(fExtension);
140
141 const bool ok = fData.get() ? save_data_to_file(fData, path.c_str())
142 : PngAndRaw::Encode(fBitmap, path.c_str());
143 if (!ok) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000144 this->fail();
145 }
146}
147
148SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000149 SkString name("writing ");
150 for (int i = 0; i < fSuffixes.count(); i++) {
151 name.appendf("%s/", fSuffixes[i].c_str());
152 }
153 name.append(fGmName.c_str());
154 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000155}
156
157bool WriteTask::shouldSkip() const {
158 return FLAGS_writePath.isEmpty();
159}
160
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000161static SkString path_to_expected_image(const char* root, const Task& task) {
162 SkString filename = task.name();
163
164 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
165 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
166 SkTArray<SkString> suffixes;
167 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
168 SkASSERT(1 == suffixes.count());
169
170 // We'll look in root/suffix for images.
171 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
172
173 // Remove the suffix and tack on a .png.
174 filename.remove(filename.size() - suffixLength, suffixLength);
175 filename.append(".png");
176
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000177 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
178}
179
180bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000181 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
182 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
183 return false;
184 }
185
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000186 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000187 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000188 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000189 return false;
190 }
191
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000192 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000193}
194
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000195} // namespace DM