| 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" |
| 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 | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 8 | #include "SkStream.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 | |
| commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 28 | WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : CpuTask(parent), fBitmap(bitmap) { |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 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 | |
| commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 41 | // One file that first contains a .png of an SkBitmap, then its raw pixels. |
| 42 | // We use this custom format to avoid premultiplied/unpremultiplied pixel conversions. |
| 43 | struct PngAndRaw { |
| 44 | static bool Encode(SkBitmap bitmap, const char* path) { |
| 45 | SkFILEWStream stream(path); |
| 46 | if (!stream.isValid()) { |
| 47 | SkDebugf("Can't write %s.\n", path); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | // Write a PNG first for humans and other tools to look at. |
| 52 | if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) { |
| 53 | SkDebugf("Can't encode a PNG.\n"); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // Then write our secret raw pixels that only DM reads. |
| 58 | SkAutoLockPixels lock(bitmap); |
| 59 | return stream.write(bitmap.getPixels(), bitmap.getSize()); |
| 60 | } |
| 61 | |
| 62 | // This assumes bitmap already has allocated pixels of the correct size. |
| 63 | static bool Decode(const char* path, SkBitmap* bitmap) { |
| 64 | SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path)); |
| 65 | if (NULL == stream.get()) { |
| 66 | SkDebugf("Can't read %s.\n", path); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // The raw pixels are at the end of the stream. Seek ahead to skip beyond the encoded PNG. |
| 71 | const size_t bitmapBytes = bitmap->getSize(); |
| 72 | if (stream->getLength() < bitmapBytes) { |
| 73 | SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path); |
| 74 | return false; |
| 75 | } |
| 76 | SkAssertResult(stream->seek(stream->getLength() - bitmapBytes)); |
| 77 | |
| 78 | // Read the raw pixels. |
| 79 | // TODO(mtklein): can we install a pixelref that just hangs onto the stream instead of |
| 80 | // copying into a malloc'd buffer? |
| 81 | SkAutoLockPixels lock(*bitmap); |
| 82 | if (bitmapBytes != stream->read(bitmap->getPixels(), bitmapBytes)) { |
| 83 | SkDebugf("Couldn't read raw bitmap from %s at %lu of %lu.\n", |
| 84 | path, stream->getPosition(), stream->getLength()); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | SkASSERT(stream->isAtEnd()); |
| 89 | return true; |
| 90 | } |
| 91 | }; |
| 92 | |
| mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 93 | void WriteTask::draw() { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 94 | SkString dir(FLAGS_writePath[0]); |
| 95 | this->makeDirOrFail(dir); |
| 96 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 97 | dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); |
| 98 | this->makeDirOrFail(dir); |
| 99 | } |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 100 | SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); |
| 101 | path.append(".png"); |
| commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 102 | if (!PngAndRaw::Encode(fBitmap, path.c_str())) { |
| mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 103 | this->fail(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | SkString WriteTask::name() const { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 108 | SkString name("writing "); |
| 109 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 110 | name.appendf("%s/", fSuffixes[i].c_str()); |
| 111 | } |
| 112 | name.append(fGmName.c_str()); |
| 113 | return name; |
| mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool WriteTask::shouldSkip() const { |
| 117 | return FLAGS_writePath.isEmpty(); |
| 118 | } |
| 119 | |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 120 | static SkString path_to_expected_image(const char* root, const Task& task) { |
| 121 | SkString filename = task.name(); |
| 122 | |
| 123 | // We know that all names passed in here belong to top-level Tasks, which have a single suffix |
| 124 | // (8888, 565, gpu, etc.) indicating what subdirectory to look in. |
| 125 | SkTArray<SkString> suffixes; |
| 126 | const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes); |
| 127 | SkASSERT(1 == suffixes.count()); |
| 128 | |
| 129 | // We'll look in root/suffix for images. |
| 130 | const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str()); |
| 131 | |
| 132 | // Remove the suffix and tack on a .png. |
| 133 | filename.remove(filename.size() - suffixLength, suffixLength); |
| 134 | filename.append(".png"); |
| 135 | |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 136 | return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str()); |
| 137 | } |
| 138 | |
| 139 | bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
| commit-bot@chromium.org | 0888b75 | 2014-02-10 16:39:40 +0000 | [diff] [blame] | 140 | if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) { |
| 141 | SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot); |
| 142 | return false; |
| 143 | } |
| 144 | |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 145 | const SkString path = path_to_expected_image(fRoot, task); |
| commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 146 | SkBitmap expected; |
| commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 147 | expected.allocPixels(bitmap.info()); |
| 148 | if (!PngAndRaw::Decode(path.c_str(), &expected)) { |
| commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
| commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 152 | return BitmapsEqual(expected, bitmap); |
| commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 155 | } // namespace DM |