mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 1 | #include "DMWriteTask.h" |
| 2 | |
| 3 | #include "DMUtil.h" |
| 4 | #include "SkCommandLineFlags.h" |
| 5 | #include "SkImageEncoder.h" |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 6 | #include "SkString.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 7 | |
| 8 | DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); |
| 9 | |
| 10 | namespace DM { |
| 11 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 12 | WriteTask::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.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 25 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 26 | void WriteTask::makeDirOrFail(SkString dir) { |
| 27 | if (!sk_mkdir(dir.c_str())) { |
| 28 | this->fail(); |
| 29 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void WriteTask::draw() { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 33 | 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.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 40 | fBitmap, |
| 41 | SkImageEncoder::kPNG_Type, |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 42 | 100/*quality*/)) { |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 43 | this->fail(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | SkString WriteTask::name() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 48 | 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.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool WriteTask::shouldSkip() const { |
| 57 | return FLAGS_writePath.isEmpty(); |
| 58 | } |
| 59 | |
| 60 | } // namespace DM |