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