blob: 5adb1d043780979686b1a66d81fd9474067abc4e [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"
commit-bot@chromium.org99589af2013-12-10 14:53:16 +00006#include "SkImageDecoder.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00007#include "SkImageEncoder.h"
rmistry@google.comd6bab022013-12-02 13:50:38 +00008#include "SkString.h"
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +00009#include "SkUnPreMultiply.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
29WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) {
30 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
42void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000043 SkString dir(FLAGS_writePath[0]);
44 this->makeDirOrFail(dir);
45 for (int i = 0; i < fSuffixes.count(); i++) {
46 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
47 this->makeDirOrFail(dir);
48 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000049 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
50 path.append(".png");
51 if (!SkImageEncoder::EncodeFile(path.c_str(),
mtklein@google.coma7a9f372013-10-18 20:52:44 +000052 fBitmap,
53 SkImageEncoder::kPNG_Type,
rmistry@google.comd6bab022013-12-02 13:50:38 +000054 100/*quality*/)) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +000055 this->fail();
56 }
57}
58
59SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000060 SkString name("writing ");
61 for (int i = 0; i < fSuffixes.count(); i++) {
62 name.appendf("%s/", fSuffixes[i].c_str());
63 }
64 name.append(fGmName.c_str());
65 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +000066}
67
68bool WriteTask::shouldSkip() const {
69 return FLAGS_writePath.isEmpty();
70}
71
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000072static SkString path_to_expected_image(const char* root, const Task& task) {
73 SkString filename = task.name();
74
75 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
76 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
77 SkTArray<SkString> suffixes;
78 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
79 SkASSERT(1 == suffixes.count());
80
81 // We'll look in root/suffix for images.
82 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
83
84 // Remove the suffix and tack on a .png.
85 filename.remove(filename.size() - suffixLength, suffixLength);
86 filename.append(".png");
87
88 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str());
89
90 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
91}
92
93bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org0888b752014-02-10 16:39:40 +000094 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
95 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
96 return false;
97 }
98
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +000099 // PNG is stored unpremultiplied, and going from premul to unpremul to premul is lossy. To
100 // skirt this problem, we decode the PNG into an unpremul bitmap, convert our bitmap to unpremul
101 // if needed, and compare those. Each image goes once from premul to unpremul, never back.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000102 const SkString path = path_to_expected_image(fRoot, task);
103
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000104 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path.c_str()));
105 if (NULL == stream.get()) {
106 SkDebugf("Could not read %s.\n", path.c_str());
107 return false;
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000108 }
109
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000110 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
111 if (NULL == decoder.get()) {
112 SkDebugf("Could not find a decoder for %s.\n", path.c_str());
113 return false;
114 }
115
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000116 const SkImageInfo info = bitmap.info();
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000117
118 SkBitmap expected;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000119 expected.allocPixels(info);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000120
121 // expected will be unpremultiplied.
122 decoder->setRequireUnpremultipliedColors(true);
123 if (!decoder->decode(stream, &expected, SkImageDecoder::kDecodePixels_Mode)) {
124 SkDebugf("Could not decode %s.\n", path.c_str());
125 return false;
126 }
127
128 // We always seem to decode to 8888. This puts 565 back in 565.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000129 if (expected.colorType() != bitmap.colorType()) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000130 SkBitmap converted;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000131 SkAssertResult(expected.copyTo(&converted, bitmap.colorType()));
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000132 expected.swap(converted);
133 }
134 SkASSERT(expected.config() == bitmap.config());
135
136 // Manually unpremultiply 8888 bitmaps to match expected.
137 // Their pixels are shared, concurrently even, so we must copy them.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000138 if (info.colorType() == kPMColor_SkColorType) {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000139 SkBitmap unpremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000140 unpremul.allocPixels(info);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000141
142 SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul);
143 const SkPMColor* src = (SkPMColor*)bitmap.getPixels();
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +0000144 uint32_t* dst = (uint32_t*)unpremul.getPixels();
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000145 for (size_t i = 0; i < bitmap.getSize()/4; i++) {
commit-bot@chromium.orgb06faac2014-01-15 22:24:58 +0000146 dst[i] = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(src[i]);
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000147 }
148 bitmap.swap(unpremul);
149 }
150
151 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000152}
153
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000154} // namespace DM