blob: 890a4bb052b719e096fd51bd15eb2212a22ce6b6 [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;
87 SkAutoTUnref<SkPixelRef> pixels(
88 SkMallocPixelRef::NewWithData(info, rowBytes, NULL/*ctable*/, data, offset));
89 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000090
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000091 bitmap->setConfig(info, rowBytes);
92 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000093 return true;
94 }
95};
96
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000097} // namespace
98
mtklein@google.coma7a9f372013-10-18 20:52:44 +000099void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000100 SkString dir(FLAGS_writePath[0]);
101 this->makeDirOrFail(dir);
102 for (int i = 0; i < fSuffixes.count(); i++) {
103 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
104 this->makeDirOrFail(dir);
105 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000106 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
107 path.append(".png");
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000108 if (!PngAndRaw::Encode(fBitmap, path.c_str())) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000109 this->fail();
110 }
111}
112
113SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000114 SkString name("writing ");
115 for (int i = 0; i < fSuffixes.count(); i++) {
116 name.appendf("%s/", fSuffixes[i].c_str());
117 }
118 name.append(fGmName.c_str());
119 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000120}
121
122bool WriteTask::shouldSkip() const {
123 return FLAGS_writePath.isEmpty();
124}
125
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000126static SkString path_to_expected_image(const char* root, const Task& task) {
127 SkString filename = task.name();
128
129 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
130 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
131 SkTArray<SkString> suffixes;
132 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
133 SkASSERT(1 == suffixes.count());
134
135 // We'll look in root/suffix for images.
136 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
137
138 // Remove the suffix and tack on a .png.
139 filename.remove(filename.size() - suffixLength, suffixLength);
140 filename.append(".png");
141
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000142 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
143}
144
145bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000146 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
147 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
148 return false;
149 }
150
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000151 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000152 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000153 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000154 return false;
155 }
156
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000157 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000158}
159
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000160} // namespace DM