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