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