blob: 11a71faed60e96695cd92c903cc1c633c497c820 [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.org69a0d7a2014-01-06 20:24:21 +000094 // PNG is stored unpremultiplied, and going from premul to unpremul to premul is lossy. To
95 // skirt this problem, we decode the PNG into an unpremul bitmap, convert our bitmap to unpremul
96 // if needed, and compare those. Each image goes once from premul to unpremul, never back.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000097 const SkString path = path_to_expected_image(fRoot, task);
98
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +000099 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path.c_str()));
100 if (NULL == stream.get()) {
101 SkDebugf("Could not read %s.\n", path.c_str());
102 return false;
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000103 }
104
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000105 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
106 if (NULL == decoder.get()) {
107 SkDebugf("Could not find a decoder for %s.\n", path.c_str());
108 return false;
109 }
110
111 SkImageInfo info;
112 SkAssertResult(bitmap.asImageInfo(&info));
113
114 SkBitmap expected;
115 expected.setConfig(info);
116 expected.allocPixels();
117
118 // expected will be unpremultiplied.
119 decoder->setRequireUnpremultipliedColors(true);
120 if (!decoder->decode(stream, &expected, SkImageDecoder::kDecodePixels_Mode)) {
121 SkDebugf("Could not decode %s.\n", path.c_str());
122 return false;
123 }
124
125 // We always seem to decode to 8888. This puts 565 back in 565.
126 if (expected.config() != bitmap.config()) {
127 SkBitmap converted;
128 SkAssertResult(expected.copyTo(&converted, bitmap.config()));
129 expected.swap(converted);
130 }
131 SkASSERT(expected.config() == bitmap.config());
132
133 // Manually unpremultiply 8888 bitmaps to match expected.
134 // Their pixels are shared, concurrently even, so we must copy them.
135 if (info.fColorType == kPMColor_SkColorType) {
136 SkBitmap unpremul;
137 unpremul.setConfig(info);
138 unpremul.allocPixels();
139
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +0000140 // Unpremultiply without changing native byte order.
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000141 SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul);
142 const SkPMColor* src = (SkPMColor*)bitmap.getPixels();
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +0000143 uint32_t* dst = (uint32_t*)unpremul.getPixels();
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000144 for (size_t i = 0; i < bitmap.getSize()/4; i++) {
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +0000145 const U8CPU a = SkGetPackedA32(src[i]);
146 const SkUnPreMultiply::Scale s = SkUnPreMultiply::GetScale(a);
147 dst[i] = SkPackARGB32NoCheck(a,
148 SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(src[i])),
149 SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(src[i])),
150 SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(src[i])));
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000151 }
152 bitmap.swap(unpremul);
153 }
154
155 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000156}
157
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000158} // namespace DM