mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 1 | #include "DMWriteTask.h" |
| 2 | |
| 3 | #include "DMUtil.h" |
commit-bot@chromium.org | 389fb7f | 2014-01-15 21:28:25 +0000 | [diff] [blame] | 4 | #include "SkColorPriv.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 5 | #include "SkCommandLineFlags.h" |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 6 | #include "SkImageDecoder.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 7 | #include "SkImageEncoder.h" |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 8 | #include "SkString.h" |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 9 | #include "SkUnPreMultiply.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 10 | |
| 11 | DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); |
| 12 | |
| 13 | namespace DM { |
| 14 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 15 | // Splits off the last N suffixes of name (splitting on _) and appends them to out. |
| 16 | // Returns the total number of characters consumed. |
| 17 | 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] | 18 | SkTArray<SkString> split; |
| 19 | SkStrSplit(name, "_", &split); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 20 | int consumed = 0; |
| 21 | for (int i = 0; i < N; i++) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 22 | // We're splitting off suffixes from the back to front. |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 23 | out->push_back(split[split.count()-i-1]); |
| 24 | consumed += out->back().size() + 1; // Add one for the _. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 25 | } |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 26 | return consumed; |
| 27 | } |
| 28 | |
| 29 | WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) { |
| 30 | const int suffixes = parent.depth() + 1; |
| 31 | const SkString& name = parent.name(); |
| 32 | const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes); |
| 33 | fGmName.set(name.c_str(), name.size()-totalSuffixLength); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 34 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 35 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 36 | void WriteTask::makeDirOrFail(SkString dir) { |
| 37 | if (!sk_mkdir(dir.c_str())) { |
| 38 | this->fail(); |
| 39 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void WriteTask::draw() { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 43 | SkString dir(FLAGS_writePath[0]); |
| 44 | this->makeDirOrFail(dir); |
| 45 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 46 | dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); |
| 47 | this->makeDirOrFail(dir); |
| 48 | } |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 49 | SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); |
| 50 | path.append(".png"); |
| 51 | if (!SkImageEncoder::EncodeFile(path.c_str(), |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 52 | fBitmap, |
| 53 | SkImageEncoder::kPNG_Type, |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 54 | 100/*quality*/)) { |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 55 | this->fail(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | SkString WriteTask::name() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 60 | SkString name("writing "); |
| 61 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 62 | name.appendf("%s/", fSuffixes[i].c_str()); |
| 63 | } |
| 64 | name.append(fGmName.c_str()); |
| 65 | return name; |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool WriteTask::shouldSkip() const { |
| 69 | return FLAGS_writePath.isEmpty(); |
| 70 | } |
| 71 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 72 | static SkString path_to_expected_image(const char* root, const Task& task) { |
| 73 | SkString filename = task.name(); |
| 74 | |
| 75 | // We know that all names passed in here belong to top-level Tasks, which have a single suffix |
| 76 | // (8888, 565, gpu, etc.) indicating what subdirectory to look in. |
| 77 | SkTArray<SkString> suffixes; |
| 78 | const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes); |
| 79 | SkASSERT(1 == suffixes.count()); |
| 80 | |
| 81 | // We'll look in root/suffix for images. |
| 82 | const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str()); |
| 83 | |
| 84 | // Remove the suffix and tack on a .png. |
| 85 | filename.remove(filename.size() - suffixLength, suffixLength); |
| 86 | filename.append(".png"); |
| 87 | |
| 88 | //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str()); |
| 89 | |
| 90 | return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str()); |
| 91 | } |
| 92 | |
| 93 | bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
commit-bot@chromium.org | 0888b75 | 2014-02-10 16:39:40 +0000 | [diff] [blame] | 94 | if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) { |
| 95 | SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot); |
| 96 | return false; |
| 97 | } |
| 98 | |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 99 | // PNG is stored unpremultiplied, and going from premul to unpremul to premul is lossy. To |
| 100 | // skirt this problem, we decode the PNG into an unpremul bitmap, convert our bitmap to unpremul |
| 101 | // 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] | 102 | const SkString path = path_to_expected_image(fRoot, task); |
| 103 | |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 104 | SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path.c_str())); |
| 105 | if (NULL == stream.get()) { |
| 106 | SkDebugf("Could not read %s.\n", path.c_str()); |
| 107 | return false; |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 108 | } |
| 109 | |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 110 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
| 111 | if (NULL == decoder.get()) { |
| 112 | SkDebugf("Could not find a decoder for %s.\n", path.c_str()); |
| 113 | return false; |
| 114 | } |
| 115 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 116 | const SkImageInfo info = bitmap.info(); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 117 | |
| 118 | SkBitmap expected; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 119 | expected.allocPixels(info); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 120 | |
| 121 | // expected will be unpremultiplied. |
| 122 | decoder->setRequireUnpremultipliedColors(true); |
| 123 | if (!decoder->decode(stream, &expected, SkImageDecoder::kDecodePixels_Mode)) { |
| 124 | SkDebugf("Could not decode %s.\n", path.c_str()); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // We always seem to decode to 8888. This puts 565 back in 565. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 129 | if (expected.colorType() != bitmap.colorType()) { |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 130 | SkBitmap converted; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 131 | SkAssertResult(expected.copyTo(&converted, bitmap.colorType())); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 132 | expected.swap(converted); |
| 133 | } |
| 134 | SkASSERT(expected.config() == bitmap.config()); |
| 135 | |
| 136 | // Manually unpremultiply 8888 bitmaps to match expected. |
| 137 | // Their pixels are shared, concurrently even, so we must copy them. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 138 | if (info.colorType() == kPMColor_SkColorType) { |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 139 | SkBitmap unpremul; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame^] | 140 | unpremul.allocPixels(info); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 141 | |
| 142 | SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul); |
| 143 | const SkPMColor* src = (SkPMColor*)bitmap.getPixels(); |
commit-bot@chromium.org | 389fb7f | 2014-01-15 21:28:25 +0000 | [diff] [blame] | 144 | uint32_t* dst = (uint32_t*)unpremul.getPixels(); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 145 | for (size_t i = 0; i < bitmap.getSize()/4; i++) { |
commit-bot@chromium.org | b06faac | 2014-01-15 22:24:58 +0000 | [diff] [blame] | 146 | dst[i] = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(src[i]); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 147 | } |
| 148 | bitmap.swap(unpremul); |
| 149 | } |
| 150 | |
| 151 | return BitmapsEqual(expected, bitmap); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 152 | } |
| 153 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 154 | } // namespace DM |