blob: f7fa014ea45880a78ab9dd66d43b80ccc8750a4b [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.");
12
13namespace DM {
14
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000015// Splits off the last N suffixes of name (splitting on _) and appends them to out.
16// Returns the total number of characters consumed.
17static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000018 SkTArray<SkString> split;
19 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000020 int consumed = 0;
21 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000022 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000023 out->push_back(split[split.count()-i-1]);
24 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000025 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000026 return consumed;
27}
28
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000029WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : CpuTask(parent), fBitmap(bitmap) {
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000030 const int suffixes = parent.depth() + 1;
31 const SkString& name = parent.name();
32 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes);
33 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000034}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000035
rmistry@google.comd6bab022013-12-02 13:50:38 +000036void WriteTask::makeDirOrFail(SkString dir) {
37 if (!sk_mkdir(dir.c_str())) {
38 this->fail();
39 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000040}
41
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000042namespace {
43
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000044// One file that first contains a .png of an SkBitmap, then its raw pixels.
45// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
46struct PngAndRaw {
47 static bool Encode(SkBitmap bitmap, const char* path) {
48 SkFILEWStream stream(path);
49 if (!stream.isValid()) {
50 SkDebugf("Can't write %s.\n", path);
51 return false;
52 }
53
54 // Write a PNG first for humans and other tools to look at.
55 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
56 SkDebugf("Can't encode a PNG.\n");
57 return false;
58 }
59
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000060 // Pad out so the raw pixels start 4-byte aligned.
61 const uint32_t maxPadding = 0;
62 const size_t pos = stream.bytesWritten();
63 stream.write(&maxPadding, SkAlign4(pos) - pos);
64
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000065 // Then write our secret raw pixels that only DM reads.
66 SkAutoLockPixels lock(bitmap);
67 return stream.write(bitmap.getPixels(), bitmap.getSize());
68 }
69
70 // This assumes bitmap already has allocated pixels of the correct size.
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000071 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
72 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
73 if (!data) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000074 SkDebugf("Can't read %s.\n", path);
75 return false;
76 }
77
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000078 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
79 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
80 const size_t bitmapBytes = info.getSafeSize(rowBytes);
81 if (data->size() < bitmapBytes) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000082 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
83 return false;
84 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000085
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000086 const size_t offset = data->size() - bitmapBytes;
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000087 SkAutoTUnref<SkData> subset(
88 SkData::NewSubset(data, offset, bitmapBytes));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000089 SkAutoTUnref<SkPixelRef> pixels(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000090 SkMallocPixelRef::NewWithData(
91 info, rowBytes, NULL/*ctable*/, subset));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000092 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000093
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000094 bitmap->setConfig(info, rowBytes);
95 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000096 return true;
97 }
98};
99
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000100} // namespace
101
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000102void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000103 SkString dir(FLAGS_writePath[0]);
104 this->makeDirOrFail(dir);
105 for (int i = 0; i < fSuffixes.count(); i++) {
106 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
107 this->makeDirOrFail(dir);
108 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000109 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
110 path.append(".png");
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000111 if (!PngAndRaw::Encode(fBitmap, path.c_str())) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000112 this->fail();
113 }
114}
115
116SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000117 SkString name("writing ");
118 for (int i = 0; i < fSuffixes.count(); i++) {
119 name.appendf("%s/", fSuffixes[i].c_str());
120 }
121 name.append(fGmName.c_str());
122 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000123}
124
125bool WriteTask::shouldSkip() const {
126 return FLAGS_writePath.isEmpty();
127}
128
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000129static SkString path_to_expected_image(const char* root, const Task& task) {
130 SkString filename = task.name();
131
132 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
133 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
134 SkTArray<SkString> suffixes;
135 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
136 SkASSERT(1 == suffixes.count());
137
138 // We'll look in root/suffix for images.
139 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
140
141 // Remove the suffix and tack on a .png.
142 filename.remove(filename.size() - suffixLength, suffixLength);
143 filename.append(".png");
144
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000145 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
146}
147
148bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000149 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
150 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
151 return false;
152 }
153
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000154 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000155 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000156 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000157 return false;
158 }
159
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000160 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000161}
162
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000163} // namespace DM