blob: 5a07e4669d472dd05eb3d113f156fc9946a41982 [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"
caryclark17f0b6d2014-07-22 10:15:34 -07005#include "SkCommonFlags.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00006#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
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000011DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png data. "
12 "This means -r won't work, but skdiff will still work fine.");
mtklein@google.coma7a9f372013-10-18 20:52:44 +000013
14namespace DM {
15
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000016// Splits off the last N suffixes of name (splitting on _) and appends them to out.
17// Returns the total number of characters consumed.
18static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000019 SkTArray<SkString> split;
20 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000021 int consumed = 0;
22 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000023 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000024 out->push_back(split[split.count()-i-1]);
25 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000026 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000027 return consumed;
28}
29
mtklein30bf3e22014-06-03 13:57:14 -070030inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suffixList) {
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000031 const int suffixes = parent.depth() + 1;
32 const SkString& name = parent.name();
mtklein30bf3e22014-06-03 13:57:14 -070033 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList);
34 return SkString(name.c_str(), name.size() - totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000035}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000036
mtklein30bf3e22014-06-03 13:57:14 -070037WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
38 : CpuTask(parent)
39 , fGmName(find_gm_name(parent, &fSuffixes))
40 , fBitmap(bitmap)
41 , fData(NULL)
42 , fExtension(".png") {}
43
halcanarya4c60942014-08-26 10:38:07 -070044WriteTask::WriteTask(const Task& parent, SkStreamAsset *data, const char* ext)
mtklein30bf3e22014-06-03 13:57:14 -070045 : CpuTask(parent)
46 , fGmName(find_gm_name(parent, &fSuffixes))
halcanarya4c60942014-08-26 10:38:07 -070047 , fData(data)
48 , fExtension(ext) {
49 SkASSERT(fData.get());
50 SkASSERT(fData->unique());
51}
mtklein30bf3e22014-06-03 13:57:14 -070052
rmistry@google.comd6bab022013-12-02 13:50:38 +000053void WriteTask::makeDirOrFail(SkString dir) {
54 if (!sk_mkdir(dir.c_str())) {
55 this->fail();
56 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000057}
58
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000059namespace {
60
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000061// One file that first contains a .png of an SkBitmap, then its raw pixels.
62// We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
63struct PngAndRaw {
64 static bool Encode(SkBitmap bitmap, const char* path) {
65 SkFILEWStream stream(path);
66 if (!stream.isValid()) {
67 SkDebugf("Can't write %s.\n", path);
68 return false;
69 }
70
71 // Write a PNG first for humans and other tools to look at.
72 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
73 SkDebugf("Can't encode a PNG.\n");
74 return false;
75 }
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000076 if (FLAGS_writePngOnly) {
77 return true;
78 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000079
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000080 // Pad out so the raw pixels start 4-byte aligned.
81 const uint32_t maxPadding = 0;
82 const size_t pos = stream.bytesWritten();
83 stream.write(&maxPadding, SkAlign4(pos) - pos);
84
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000085 // Then write our secret raw pixels that only DM reads.
86 SkAutoLockPixels lock(bitmap);
87 return stream.write(bitmap.getPixels(), bitmap.getSize());
88 }
89
90 // This assumes bitmap already has allocated pixels of the correct size.
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000091 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
92 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
93 if (!data) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000094 SkDebugf("Can't read %s.\n", path);
95 return false;
96 }
97
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000098 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
99 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
100 const size_t bitmapBytes = info.getSafeSize(rowBytes);
101 if (data->size() < bitmapBytes) {
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000102 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
103 return false;
104 }
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000105
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000106 const size_t offset = data->size() - bitmapBytes;
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000107 SkAutoTUnref<SkData> subset(
108 SkData::NewSubset(data, offset, bitmapBytes));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000109 SkAutoTUnref<SkPixelRef> pixels(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000110 SkMallocPixelRef::NewWithData(
111 info, rowBytes, NULL/*ctable*/, subset));
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000112 SkASSERT(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000113
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000114 bitmap->setInfo(info, rowBytes);
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000115 bitmap->setPixelRef(pixels);
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +0000116 return true;
117 }
118};
119
mtklein30bf3e22014-06-03 13:57:14 -0700120// Does not take ownership of data.
halcanarya4c60942014-08-26 10:38:07 -0700121bool save_data_to_file(SkStreamAsset* data, const char* path) {
122 data->rewind();
mtklein30bf3e22014-06-03 13:57:14 -0700123 SkFILEWStream stream(path);
halcanarya4c60942014-08-26 10:38:07 -0700124 if (!stream.isValid() || !stream.writeStream(data, data->getLength())) {
mtklein30bf3e22014-06-03 13:57:14 -0700125 SkDebugf("Can't write %s.\n", path);
126 return false;
127 }
128 return true;
129}
130
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000131} // namespace
132
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000133void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000134 SkString dir(FLAGS_writePath[0]);
caryclark17f0b6d2014-07-22 10:15:34 -0700135#if SK_BUILD_FOR_IOS
136 if (dir.equals("@")) {
137 dir.set(FLAGS_resourcePath[0]);
138 }
139#endif
rmistry@google.comd6bab022013-12-02 13:50:38 +0000140 this->makeDirOrFail(dir);
141 for (int i = 0; i < fSuffixes.count(); i++) {
tfarinaa8e2e152014-07-28 19:26:58 -0700142 dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
rmistry@google.comd6bab022013-12-02 13:50:38 +0000143 this->makeDirOrFail(dir);
144 }
mtklein30bf3e22014-06-03 13:57:14 -0700145
tfarinaa8e2e152014-07-28 19:26:58 -0700146 SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str());
mtklein30bf3e22014-06-03 13:57:14 -0700147 path.append(fExtension);
148
halcanarya4c60942014-08-26 10:38:07 -0700149 const bool ok = fData.get() ? save_data_to_file(fData.get(), path.c_str())
mtklein30bf3e22014-06-03 13:57:14 -0700150 : PngAndRaw::Encode(fBitmap, path.c_str());
151 if (!ok) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000152 this->fail();
153 }
154}
155
156SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000157 SkString name("writing ");
158 for (int i = 0; i < fSuffixes.count(); i++) {
159 name.appendf("%s/", fSuffixes[i].c_str());
160 }
161 name.append(fGmName.c_str());
162 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000163}
164
165bool WriteTask::shouldSkip() const {
166 return FLAGS_writePath.isEmpty();
167}
168
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000169static SkString path_to_expected_image(const char* root, const Task& task) {
170 SkString filename = task.name();
171
172 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
173 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
174 SkTArray<SkString> suffixes;
175 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
176 SkASSERT(1 == suffixes.count());
177
178 // We'll look in root/suffix for images.
tfarinaa8e2e152014-07-28 19:26:58 -0700179 const SkString dir = SkOSPath::Join(root, suffixes[0].c_str());
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000180
181 // Remove the suffix and tack on a .png.
182 filename.remove(filename.size() - suffixLength, suffixLength);
183 filename.append(".png");
184
tfarinaa8e2e152014-07-28 19:26:58 -0700185 return SkOSPath::Join(dir.c_str(), filename.c_str());
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000186}
187
188bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +0000189 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
190 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
191 return false;
192 }
193
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000194 const SkString path = path_to_expected_image(fRoot, task);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000195 SkBitmap expected;
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +0000196 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000197 return false;
198 }
199
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000200 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000201}
202
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000203} // namespace DM