blob: c86381f78af12485fcd1bf4d1dcbc4cc08acb119 [file] [log] [blame]
mtklein@google.coma7a9f372013-10-18 20:52:44 +00001#include "DMWriteTask.h"
2
3#include "DMUtil.h"
4#include "SkCommandLineFlags.h"
5#include "SkImageEncoder.h"
rmistry@google.comd6bab022013-12-02 13:50:38 +00006#include "SkString.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00007
8DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
9
10namespace DM {
11
rmistry@google.comd6bab022013-12-02 13:50:38 +000012WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) {
13 const int suffixes = parent.depth() + 1;
14 const char* name = parent.name().c_str();
15 SkTArray<SkString> split;
16 SkStrSplit(name, "_", &split);
17 int totalSuffixLength = 0;
18 for (int i = 0; i < suffixes; i++) {
19 // We're splitting off suffixes from the back to front.
20 fSuffixes.push_back(split[split.count()-i-1]);
21 totalSuffixLength += fSuffixes.back().size() + 1;
22 }
23 fGmName.set(name, strlen(name)-totalSuffixLength);
24}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000025
rmistry@google.comd6bab022013-12-02 13:50:38 +000026void WriteTask::makeDirOrFail(SkString dir) {
27 if (!sk_mkdir(dir.c_str())) {
28 this->fail();
29 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000030}
31
32void WriteTask::draw() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000033 SkString dir(FLAGS_writePath[0]);
34 this->makeDirOrFail(dir);
35 for (int i = 0; i < fSuffixes.count(); i++) {
36 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
37 this->makeDirOrFail(dir);
38 }
39 if (!SkImageEncoder::EncodeFile(Png(SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str())).c_str(),
mtklein@google.coma7a9f372013-10-18 20:52:44 +000040 fBitmap,
41 SkImageEncoder::kPNG_Type,
rmistry@google.comd6bab022013-12-02 13:50:38 +000042 100/*quality*/)) {
mtklein@google.coma7a9f372013-10-18 20:52:44 +000043 this->fail();
44 }
45}
46
47SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000048 SkString name("writing ");
49 for (int i = 0; i < fSuffixes.count(); i++) {
50 name.appendf("%s/", fSuffixes[i].c_str());
51 }
52 name.append(fGmName.c_str());
53 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +000054}
55
56bool WriteTask::shouldSkip() const {
57 return FLAGS_writePath.isEmpty();
58}
59
60} // namespace DM