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" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 5 | #include "SkCommonFlags.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | d6dcacd | 2014-05-14 20:26:00 +0000 | [diff] [blame] | 11 | DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png data. " |
| 12 | "This means -r won't work, but skdiff will still work fine."); |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 13 | |
| 14 | namespace DM { |
| 15 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 16 | // Splits off the last N suffixes of name (splitting on _) and appends them to out. |
| 17 | // Returns the total number of characters consumed. |
| 18 | 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] | 19 | SkTArray<SkString> split; |
| 20 | SkStrSplit(name, "_", &split); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 21 | int consumed = 0; |
| 22 | for (int i = 0; i < N; i++) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 23 | // We're splitting off suffixes from the back to front. |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 24 | out->push_back(split[split.count()-i-1]); |
| 25 | consumed += out->back().size() + 1; // Add one for the _. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 26 | } |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 27 | return consumed; |
| 28 | } |
| 29 | |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 30 | inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suffixList) { |
commit-bot@chromium.org | d6dcacd | 2014-05-14 20:26:00 +0000 | [diff] [blame] | 31 | const int suffixes = parent.depth() + 1; |
| 32 | const SkString& name = parent.name(); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 33 | const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList); |
| 34 | return SkString(name.c_str(), name.size() - totalSuffixLength); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 35 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 36 | |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 37 | WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) |
| 38 | : CpuTask(parent) |
| 39 | , fGmName(find_gm_name(parent, &fSuffixes)) |
| 40 | , fBitmap(bitmap) |
| 41 | , fData(NULL) |
| 42 | , fExtension(".png") {} |
| 43 | |
| 44 | WriteTask::WriteTask(const Task& parent, SkData *data, const char* ext) |
| 45 | : CpuTask(parent) |
| 46 | , fGmName(find_gm_name(parent, &fSuffixes)) |
| 47 | , fData(SkRef(data)) |
| 48 | , fExtension(ext) {} |
| 49 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 50 | void WriteTask::makeDirOrFail(SkString dir) { |
| 51 | if (!sk_mkdir(dir.c_str())) { |
| 52 | this->fail(); |
| 53 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 56 | namespace { |
| 57 | |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 58 | // One file that first contains a .png of an SkBitmap, then its raw pixels. |
| 59 | // We use this custom format to avoid premultiplied/unpremultiplied pixel conversions. |
| 60 | struct PngAndRaw { |
| 61 | static bool Encode(SkBitmap bitmap, const char* path) { |
| 62 | SkFILEWStream stream(path); |
| 63 | if (!stream.isValid()) { |
| 64 | SkDebugf("Can't write %s.\n", path); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | // Write a PNG first for humans and other tools to look at. |
| 69 | if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) { |
| 70 | SkDebugf("Can't encode a PNG.\n"); |
| 71 | return false; |
| 72 | } |
commit-bot@chromium.org | d6dcacd | 2014-05-14 20:26:00 +0000 | [diff] [blame] | 73 | if (FLAGS_writePngOnly) { |
| 74 | return true; |
| 75 | } |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 77 | // Pad out so the raw pixels start 4-byte aligned. |
| 78 | const uint32_t maxPadding = 0; |
| 79 | const size_t pos = stream.bytesWritten(); |
| 80 | stream.write(&maxPadding, SkAlign4(pos) - pos); |
| 81 | |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 82 | // Then write our secret raw pixels that only DM reads. |
| 83 | SkAutoLockPixels lock(bitmap); |
| 84 | return stream.write(bitmap.getPixels(), bitmap.getSize()); |
| 85 | } |
| 86 | |
| 87 | // 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] | 88 | static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) { |
| 89 | SkAutoTUnref<SkData> data(SkData::NewFromFileName(path)); |
| 90 | if (!data) { |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 91 | SkDebugf("Can't read %s.\n", path); |
| 92 | return false; |
| 93 | } |
| 94 | |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 95 | // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front. |
| 96 | const size_t rowBytes = info.minRowBytes(); // Assume densely packed. |
| 97 | const size_t bitmapBytes = info.getSafeSize(rowBytes); |
| 98 | if (data->size() < bitmapBytes) { |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 99 | SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path); |
| 100 | return false; |
| 101 | } |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 103 | const size_t offset = data->size() - bitmapBytes; |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 104 | SkAutoTUnref<SkData> subset( |
| 105 | SkData::NewSubset(data, offset, bitmapBytes)); |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 106 | SkAutoTUnref<SkPixelRef> pixels( |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 107 | SkMallocPixelRef::NewWithData( |
| 108 | info, rowBytes, NULL/*ctable*/, subset)); |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 109 | SkASSERT(pixels); |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 111 | bitmap->setInfo(info, rowBytes); |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 112 | bitmap->setPixelRef(pixels); |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | }; |
| 116 | |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 117 | // Does not take ownership of data. |
| 118 | bool save_data_to_file(const SkData* data, const char* path) { |
| 119 | SkFILEWStream stream(path); |
| 120 | if (!stream.isValid() || !stream.write(data->data(), data->size())) { |
| 121 | SkDebugf("Can't write %s.\n", path); |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 127 | } // namespace |
| 128 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 129 | void WriteTask::draw() { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 130 | SkString dir(FLAGS_writePath[0]); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 131 | #if SK_BUILD_FOR_IOS |
| 132 | if (dir.equals("@")) { |
| 133 | dir.set(FLAGS_resourcePath[0]); |
| 134 | } |
| 135 | #endif |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 136 | this->makeDirOrFail(dir); |
| 137 | for (int i = 0; i < fSuffixes.count(); i++) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 138 | dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str()); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 139 | this->makeDirOrFail(dir); |
| 140 | } |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 141 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 142 | SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str()); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 143 | path.append(fExtension); |
| 144 | |
| 145 | const bool ok = fData.get() ? save_data_to_file(fData, path.c_str()) |
| 146 | : PngAndRaw::Encode(fBitmap, path.c_str()); |
| 147 | if (!ok) { |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 148 | this->fail(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | SkString WriteTask::name() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 153 | SkString name("writing "); |
| 154 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 155 | name.appendf("%s/", fSuffixes[i].c_str()); |
| 156 | } |
| 157 | name.append(fGmName.c_str()); |
| 158 | return name; |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool WriteTask::shouldSkip() const { |
| 162 | return FLAGS_writePath.isEmpty(); |
| 163 | } |
| 164 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 165 | static SkString path_to_expected_image(const char* root, const Task& task) { |
| 166 | SkString filename = task.name(); |
| 167 | |
| 168 | // We know that all names passed in here belong to top-level Tasks, which have a single suffix |
| 169 | // (8888, 565, gpu, etc.) indicating what subdirectory to look in. |
| 170 | SkTArray<SkString> suffixes; |
| 171 | const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes); |
| 172 | SkASSERT(1 == suffixes.count()); |
| 173 | |
| 174 | // We'll look in root/suffix for images. |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 175 | const SkString dir = SkOSPath::Join(root, suffixes[0].c_str()); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 176 | |
| 177 | // Remove the suffix and tack on a .png. |
| 178 | filename.remove(filename.size() - suffixLength, suffixLength); |
| 179 | filename.append(".png"); |
| 180 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 181 | return SkOSPath::Join(dir.c_str(), filename.c_str()); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
commit-bot@chromium.org | 0888b75 | 2014-02-10 16:39:40 +0000 | [diff] [blame] | 185 | if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) { |
| 186 | SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot); |
| 187 | return false; |
| 188 | } |
| 189 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 190 | const SkString path = path_to_expected_image(fRoot, task); |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 191 | SkBitmap expected; |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 192 | if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) { |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
commit-bot@chromium.org | 69a0d7a | 2014-01-06 20:24:21 +0000 | [diff] [blame] | 196 | return BitmapsEqual(expected, bitmap); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 197 | } |
| 198 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 199 | } // namespace DM |