blob: 7c9bb628a71cec1afa9128b95672e0b6613d8cac [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
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000031WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
commit-bot@chromium.org90b5a2a2014-05-14 17:55:32 +000032 : CpuTask(parent), fBitmap(bitmap) {
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000033 const int suffixes = parent.depth() + 1;
34 const SkString& name = parent.name();
35 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes);
36 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000037}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000038
rmistry@google.comd6bab022013-12-02 13:50:38 +000039void WriteTask::makeDirOrFail(SkString dir) {
40 if (!sk_mkdir(dir.c_str())) {
41 this->fail();
42 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000043}
44
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000045namespace {
46
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000047// One file that first contains a .png of an SkBitmap, then its raw pixels.
48// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
49struct PngAndRaw {
50 static bool Encode(SkBitmap bitmap, const char* path) {
51 SkFILEWStream stream(path);
52 if (!stream.isValid()) {
53 SkDebugf("Can't write %s.\n", path);
54 return false;
55 }
56
57 // Write a PNG first for humans and other tools to look at.
58 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
59 SkDebugf("Can't encode a PNG.\n");
60 return false;
61 }
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000062 if (FLAGS_writePngOnly) {
63 return true;
64 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000065
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000066 // Pad out so the raw pixels start 4-byte aligned.
67 const uint32_t maxPadding = 0;
68 const size_t pos = stream.bytesWritten();
69 stream.write(&maxPadding, SkAlign4(pos) - pos);
70
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000071 // Then write our secret raw pixels that only DM reads.
72 SkAutoLockPixels lock(bitmap);
73 return stream.write(bitmap.getPixels(), bitmap.getSize());
74 }
75
76 // This assumes bitmap already has allocated pixels of the correct size.
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000077 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
78 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
79 if (!data) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000080 SkDebugf("Can't read %s.\n", path);
81 return false;
82 }
83
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000084 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
85 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
86 const size_t bitmapBytes = info.getSafeSize(rowBytes);
87 if (data->size() < bitmapBytes) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000088 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
89 return false;
90 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000091
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000092 const size_t offset = data->size() - bitmapBytes;
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000093 SkAutoTUnref<SkData> subset(
94 SkData::NewSubset(data, offset, bitmapBytes));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000095 SkAutoTUnref<SkPixelRef> pixels(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000096 SkMallocPixelRef::NewWithData(
97 info, rowBytes, NULL/*ctable*/, subset));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000098 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000099
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000100 bitmap->setConfig(info, rowBytes);
101 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000102 return true;
103 }
104};
105
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000106} // namespace
107
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000108void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000109 SkString dir(FLAGS_writePath[0]);
110 this->makeDirOrFail(dir);
111 for (int i = 0; i < fSuffixes.count(); i++) {
112 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
113 this->makeDirOrFail(dir);
114 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000115 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
116 path.append(".png");
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000117 if (!PngAndRaw::Encode(fBitmap, path.c_str())) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000118 this->fail();
119 }
120}
121
122SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000123 SkString name("writing ");
124 for (int i = 0; i < fSuffixes.count(); i++) {
125 name.appendf("%s/", fSuffixes[i].c_str());
126 }
127 name.append(fGmName.c_str());
128 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000129}
130
131bool WriteTask::shouldSkip() const {
132 return FLAGS_writePath.isEmpty();
133}
134
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000135static SkString path_to_expected_image(const char* root, const Task& task) {
136 SkString filename = task.name();
137
138 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
139 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
140 SkTArray<SkString> suffixes;
141 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
142 SkASSERT(1 == suffixes.count());
143
144 // We'll look in root/suffix for images.
145 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
146
147 // Remove the suffix and tack on a .png.
148 filename.remove(filename.size() - suffixLength, suffixLength);
149 filename.append(".png");
150
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000151 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
152}
153
154bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000155 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
156 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
157 return false;
158 }
159
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000160 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000161 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000162 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000163 return false;
164 }
165
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000166 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000167}
168
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000169} // namespace DM