blob: 04479e94fbcda3a89f2de0af4e32d6fef64fc1ef [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"
rmistry@google.comd6bab022013-12-02 13:50:38 +00007#include "SkString.h"
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +00008#include "SkStream.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00009
10DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
11
12namespace DM {
13
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000014// Splits off the last N suffixes of name (splitting on _) and appends them to out.
15// Returns the total number of characters consumed.
16static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000017 SkTArray<SkString> split;
18 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000019 int consumed = 0;
20 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000021 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000022 out->push_back(split[split.count()-i-1]);
23 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000024 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000025 return consumed;
26}
27
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000028WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : CpuTask(parent), fBitmap(bitmap) {
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000029 const int suffixes = parent.depth() + 1;
30 const SkString& name = parent.name();
31 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes);
32 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000033}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000034
rmistry@google.comd6bab022013-12-02 13:50:38 +000035void WriteTask::makeDirOrFail(SkString dir) {
36 if (!sk_mkdir(dir.c_str())) {
37 this->fail();
38 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000039}
40
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000041// One file that first contains a .png of an SkBitmap, then its raw pixels.
42// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
43struct PngAndRaw {
44 static bool Encode(SkBitmap bitmap, const char* path) {
45 SkFILEWStream stream(path);
46 if (!stream.isValid()) {
47 SkDebugf("Can't write %s.\n", path);
48 return false;
49 }
50
51 // Write a PNG first for humans and other tools to look at.
52 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
53 SkDebugf("Can't encode a PNG.\n");
54 return false;
55 }
56
57 // Then write our secret raw pixels that only DM reads.
58 SkAutoLockPixels lock(bitmap);
59 return stream.write(bitmap.getPixels(), bitmap.getSize());
60 }
61
62 // This assumes bitmap already has allocated pixels of the correct size.
63 static bool Decode(const char* path, SkBitmap* bitmap) {
64 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path));
65 if (NULL == stream.get()) {
66 SkDebugf("Can't read %s.\n", path);
67 return false;
68 }
69
70 // The raw pixels are at the end of the stream. Seek ahead to skip beyond the encoded PNG.
71 const size_t bitmapBytes = bitmap->getSize();
72 if (stream->getLength() < bitmapBytes) {
73 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
74 return false;
75 }
76 SkAssertResult(stream->seek(stream->getLength() - bitmapBytes));
77
78 // Read the raw pixels.
79 // TODO(mtklein): can we install a pixelref that just hangs onto the stream instead of
80 // copying into a malloc'd buffer?
81 SkAutoLockPixels lock(*bitmap);
82 if (bitmapBytes != stream->read(bitmap->getPixels(), bitmapBytes)) {
83 SkDebugf("Couldn't read raw bitmap from %s at %lu of %lu.\n",
84 path, stream->getPosition(), stream->getLength());
85 return false;
86 }
87
88 SkASSERT(stream->isAtEnd());
89 return true;
90 }
91};
92
mtklein@google.coma7a9f372013-10-18 20:52:44 +000093void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000094 SkString dir(FLAGS_writePath[0]);
95 this->makeDirOrFail(dir);
96 for (int i = 0; i < fSuffixes.count(); i++) {
97 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
98 this->makeDirOrFail(dir);
99 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000100 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
101 path.append(".png");
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000102 if (!PngAndRaw::Encode(fBitmap, path.c_str())) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000103 this->fail();
104 }
105}
106
107SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000108 SkString name("writing ");
109 for (int i = 0; i < fSuffixes.count(); i++) {
110 name.appendf("%s/", fSuffixes[i].c_str());
111 }
112 name.append(fGmName.c_str());
113 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000114}
115
116bool WriteTask::shouldSkip() const {
117 return FLAGS_writePath.isEmpty();
118}
119
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000120static SkString path_to_expected_image(const char* root, const Task& task) {
121 SkString filename = task.name();
122
123 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
124 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
125 SkTArray<SkString> suffixes;
126 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
127 SkASSERT(1 == suffixes.count());
128
129 // We'll look in root/suffix for images.
130 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
131
132 // Remove the suffix and tack on a .png.
133 filename.remove(filename.size() - suffixLength, suffixLength);
134 filename.append(".png");
135
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000136 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
137}
138
139bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000140 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
141 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
142 return false;
143 }
144
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000145 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000146 SkBitmap expected;
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000147 expected.allocPixels(bitmap.info());
148 if (!PngAndRaw::Decode(path.c_str(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000149 return false;
150 }
151
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000152 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000153}
154
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000155} // namespace DM