blob: 11b9fd3470738acaa865f0c2685d7c377cc87213 [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"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00008
9DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
10
11namespace DM {
12
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000013// Splits off the last N suffixes of name (splitting on _) and appends them to out.
14// Returns the total number of characters consumed.
15static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000016 SkTArray<SkString> split;
17 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000018 int consumed = 0;
19 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000020 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000021 out->push_back(split[split.count()-i-1]);
22 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000023 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000024 return consumed;
25}
26
27WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) {
28 const int suffixes = parent.depth() + 1;
29 const SkString& name = parent.name();
30 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes);
31 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000032}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000033
rmistry@google.comd6bab022013-12-02 13:50:38 +000034void WriteTask::makeDirOrFail(SkString dir) {
35 if (!sk_mkdir(dir.c_str())) {
36 this->fail();
37 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000038}
39
40void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000041 SkString dir(FLAGS_writePath[0]);
42 this->makeDirOrFail(dir);
43 for (int i = 0; i < fSuffixes.count(); i++) {
44 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
45 this->makeDirOrFail(dir);
46 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000047 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
48 path.append(".png");
49 if (!SkImageEncoder::EncodeFile(path.c_str(),
mtklein@google.coma7a9f372013-10-18 20:52:44 +000050 fBitmap,
51 SkImageEncoder::kPNG_Type,
rmistry@google.comd6bab022013-12-02 13:50:38 +000052 100/*quality*/)) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +000053 this->fail();
54 }
55}
56
57SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000058 SkString name("writing ");
59 for (int i = 0; i < fSuffixes.count(); i++) {
60 name.appendf("%s/", fSuffixes[i].c_str());
61 }
62 name.append(fGmName.c_str());
63 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +000064}
65
66bool WriteTask::shouldSkip() const {
67 return FLAGS_writePath.isEmpty();
68}
69
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000070static SkString path_to_expected_image(const char* root, const Task& task) {
71 SkString filename = task.name();
72
73 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
74 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
75 SkTArray<SkString> suffixes;
76 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
77 SkASSERT(1 == suffixes.count());
78
79 // We'll look in root/suffix for images.
80 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
81
82 // Remove the suffix and tack on a .png.
83 filename.remove(filename.size() - suffixLength, suffixLength);
84 filename.append(".png");
85
86 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str());
87
88 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
89}
90
91bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
92 const SkString path = path_to_expected_image(fRoot, task);
93
94 SkBitmap expected;
95 if (SkImageDecoder::DecodeFile(path.c_str(), &expected)) {
96 if (expected.config() != bitmap.config()) {
97 SkBitmap converted;
98 SkAssertResult(expected.copyTo(&converted, bitmap.config()));
99 expected.swap(converted);
100 }
101 SkASSERT(expected.config() == bitmap.config());
102 return BitmapsEqual(expected, bitmap);
103 }
104
105 // Couldn't read the file, etc.
106 SkDebugf("Problem decoding %s to SkBitmap.\n", path.c_str());
107 return false;
108}
109
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000110} // namespace DM