blob: 326d454150946f9584333e2977f964796568caa7 [file] [log] [blame]
mtklein@google.coma7a9f372013-10-18 20:52:44 +00001#include "DMWriteTask.h"
2
3#include "DMUtil.h"
4#include "SkCommandLineFlags.h"
commit-bot@chromium.org99589af2013-12-10 14:53:16 +00005#include "SkImageDecoder.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00006#include "SkImageEncoder.h"
rmistry@google.comd6bab022013-12-02 13:50:38 +00007#include "SkString.h"
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +00008#include "SkUnPreMultiply.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
28WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) {
29 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
41void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000042 SkString dir(FLAGS_writePath[0]);
43 this->makeDirOrFail(dir);
44 for (int i = 0; i < fSuffixes.count(); i++) {
45 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
46 this->makeDirOrFail(dir);
47 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000048 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
49 path.append(".png");
50 if (!SkImageEncoder::EncodeFile(path.c_str(),
mtklein@google.coma7a9f372013-10-18 20:52:44 +000051 fBitmap,
52 SkImageEncoder::kPNG_Type,
rmistry@google.comd6bab022013-12-02 13:50:38 +000053 100/*quality*/)) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +000054 this->fail();
55 }
56}
57
58SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000059 SkString name("writing ");
60 for (int i = 0; i < fSuffixes.count(); i++) {
61 name.appendf("%s/", fSuffixes[i].c_str());
62 }
63 name.append(fGmName.c_str());
64 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +000065}
66
67bool WriteTask::shouldSkip() const {
68 return FLAGS_writePath.isEmpty();
69}
70
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000071static SkString path_to_expected_image(const char* root, const Task& task) {
72 SkString filename = task.name();
73
74 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
75 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
76 SkTArray<SkString> suffixes;
77 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
78 SkASSERT(1 == suffixes.count());
79
80 // We'll look in root/suffix for images.
81 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
82
83 // Remove the suffix and tack on a .png.
84 filename.remove(filename.size() - suffixLength, suffixLength);
85 filename.append(".png");
86
87 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str());
88
89 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
90}
91
92bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +000093 // PNG is stored unpremultiplied, and going from premul to unpremul to premul is lossy. To
94 // skirt this problem, we decode the PNG into an unpremul bitmap, convert our bitmap to unpremul
95 // if needed, and compare those. Each image goes once from premul to unpremul, never back.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000096 const SkString path = path_to_expected_image(fRoot, task);
97
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +000098 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path.c_str()));
99 if (NULL == stream.get()) {
100 SkDebugf("Could not read %s.\n", path.c_str());
101 return false;
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000102 }
103
commit-bot@chromium.org69a0d7a2014-01-06 20:24:21 +0000104 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
105 if (NULL == decoder.get()) {
106 SkDebugf("Could not find a decoder for %s.\n", path.c_str());
107 return false;
108 }
109
110 SkImageInfo info;
111 SkAssertResult(bitmap.asImageInfo(&info));
112
113 SkBitmap expected;
114 expected.setConfig(info);
115 expected.allocPixels();
116
117 // expected will be unpremultiplied.
118 decoder->setRequireUnpremultipliedColors(true);
119 if (!decoder->decode(stream, &expected, SkImageDecoder::kDecodePixels_Mode)) {
120 SkDebugf("Could not decode %s.\n", path.c_str());
121 return false;
122 }
123
124 // We always seem to decode to 8888. This puts 565 back in 565.
125 if (expected.config() != bitmap.config()) {
126 SkBitmap converted;
127 SkAssertResult(expected.copyTo(&converted, bitmap.config()));
128 expected.swap(converted);
129 }
130 SkASSERT(expected.config() == bitmap.config());
131
132 // Manually unpremultiply 8888 bitmaps to match expected.
133 // Their pixels are shared, concurrently even, so we must copy them.
134 if (info.fColorType == kPMColor_SkColorType) {
135 SkBitmap unpremul;
136 unpremul.setConfig(info);
137 unpremul.allocPixels();
138
139 SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul);
140 const SkPMColor* src = (SkPMColor*)bitmap.getPixels();
141 SkColor* dst = (SkColor*)unpremul.getPixels();
142
143 for (size_t i = 0; i < bitmap.getSize()/4; i++) {
144 dst[i] = SkUnPreMultiply::PMColorToColor(src[i]);
145 }
146 bitmap.swap(unpremul);
147 }
148
149 return BitmapsEqual(expected, bitmap);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +0000150}
151
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000152} // namespace DM