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" |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 5 | #include "SkImageDecoder.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 6 | #include "SkImageEncoder.h" |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 7 | #include "SkString.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 8 | |
| 9 | DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); |
| 10 | |
| 11 | namespace DM { |
| 12 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 13 | // Splits off the last N suffixes of name (splitting on _) and appends them to out. |
| 14 | // Returns the total number of characters consumed. |
| 15 | static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 16 | SkTArray<SkString> split; |
| 17 | SkStrSplit(name, "_", &split); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 18 | int consumed = 0; |
| 19 | for (int i = 0; i < N; i++) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 20 | // We're splitting off suffixes from the back to front. |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 21 | out->push_back(split[split.count()-i-1]); |
| 22 | consumed += out->back().size() + 1; // Add one for the _. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 23 | } |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 24 | return consumed; |
| 25 | } |
| 26 | |
| 27 | WriteTask::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.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 32 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 33 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 34 | void WriteTask::makeDirOrFail(SkString dir) { |
| 35 | if (!sk_mkdir(dir.c_str())) { |
| 36 | this->fail(); |
| 37 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void WriteTask::draw() { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 41 | 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.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 47 | SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); |
| 48 | path.append(".png"); |
| 49 | if (!SkImageEncoder::EncodeFile(path.c_str(), |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 50 | fBitmap, |
| 51 | SkImageEncoder::kPNG_Type, |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 52 | 100/*quality*/)) { |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 53 | this->fail(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | SkString WriteTask::name() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 58 | 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.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool WriteTask::shouldSkip() const { |
| 67 | return FLAGS_writePath.isEmpty(); |
| 68 | } |
| 69 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 70 | static 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 | |
| 91 | bool 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.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 110 | } // namespace DM |