Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #include "DMWriteTask.h" | 1 #include "DMWriteTask.h" |
| 2 | 2 |
| 3 #include "DMUtil.h" | 3 #include "DMUtil.h" |
| 4 #include "SkColorPriv.h" | 4 #include "SkColorPriv.h" |
| 5 #include "SkCommonFlags.h" | 5 #include "SkCommonFlags.h" |
| 6 #include "SkImageEncoder.h" | 6 #include "SkImageEncoder.h" |
| 7 #include "SkMD5.h" | |
| 7 #include "SkMallocPixelRef.h" | 8 #include "SkMallocPixelRef.h" |
| 9 #include "SkOSFile.h" | |
| 8 #include "SkStream.h" | 10 #include "SkStream.h" |
| 9 #include "SkString.h" | 11 #include "SkString.h" |
| 10 | 12 |
| 11 DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png da ta. " | |
| 12 "This means -r won't work, but skdiff will stil l work fine."); | |
| 13 | |
| 14 namespace DM { | 13 namespace DM { |
| 15 | 14 |
| 16 // Splits off the last N suffixes of name (splitting on _) and appends them to o ut. | 15 // Splits off the last N suffixes of name (splitting on _) and appends them to o ut. |
| 17 // Returns the total number of characters consumed. | 16 // Returns the total number of characters consumed. |
| 18 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { | 17 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { |
| 19 SkTArray<SkString> split; | 18 SkTArray<SkString> split; |
| 20 SkStrSplit(name, "_", &split); | 19 SkStrSplit(name, "_", &split); |
| 21 int consumed = 0; | 20 int consumed = 0; |
| 22 for (int i = 0; i < N; i++) { | 21 for (int i = 0; i < N; i++) { |
| 23 // We're splitting off suffixes from the back to front. | 22 // We're splitting off suffixes from the back to front. |
| 24 out->push_back(split[split.count()-i-1]); | 23 out->push_back(split[split.count()-i-1]); |
| 25 consumed += out->back().size() + 1; // Add one for the _. | 24 consumed += out->back().size() + 1; // Add one for the _. |
| 26 } | 25 } |
| 27 return consumed; | 26 return consumed; |
| 28 } | 27 } |
| 29 | 28 |
| 30 inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suff ixList) { | 29 inline static SkString find_base_name(const Task& parent, SkTArray<SkString>* su ffixList) { |
| 31 const int suffixes = parent.depth() + 1; | 30 const int suffixes = parent.depth() + 1; |
| 32 const SkString& name = parent.name(); | 31 const SkString& name = parent.name(); |
| 33 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixL ist); | 32 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixL ist); |
| 34 return SkString(name.c_str(), name.size() - totalSuffixLength); | 33 return SkString(name.c_str(), name.size() - totalSuffixLength); |
| 35 } | 34 } |
| 36 | 35 |
| 36 struct JsonData { | |
| 37 SkString name; | |
| 38 SkMD5::Digest md5; | |
| 39 }; | |
| 40 SkTArray<JsonData> gJsonData; | |
| 41 SK_DECLARE_STATIC_MUTEX(gJsonDataLock); | |
| 42 | |
| 37 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) | 43 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) |
| 38 : CpuTask(parent) | 44 : CpuTask(parent) |
| 39 , fGmName(find_gm_name(parent, &fSuffixes)) | 45 , fFullName(parent.name()) |
| 46 , fBaseName(find_base_name(parent, &fSuffixes)) | |
| 40 , fBitmap(bitmap) | 47 , fBitmap(bitmap) |
| 41 , fData(NULL) | 48 , fData(NULL) |
| 42 , fExtension(".png") {} | 49 , fExtension(".png") { |
| 50 } | |
| 43 | 51 |
| 44 WriteTask::WriteTask(const Task& parent, SkStreamAsset *data, const char* ext) | 52 WriteTask::WriteTask(const Task& parent, SkStreamAsset *data, const char* ext) |
| 45 : CpuTask(parent) | 53 : CpuTask(parent) |
| 46 , fGmName(find_gm_name(parent, &fSuffixes)) | 54 , fFullName(parent.name()) |
| 55 , fBaseName(find_base_name(parent, &fSuffixes)) | |
| 47 , fData(data) | 56 , fData(data) |
| 48 , fExtension(ext) { | 57 , fExtension(ext) { |
| 49 SkASSERT(fData.get()); | 58 SkASSERT(fData.get()); |
| 50 SkASSERT(fData->unique()); | 59 SkASSERT(fData->unique()); |
| 51 } | 60 } |
| 52 | 61 |
| 53 void WriteTask::makeDirOrFail(SkString dir) { | 62 void WriteTask::makeDirOrFail(SkString dir) { |
| 54 if (!sk_mkdir(dir.c_str())) { | 63 if (!sk_mkdir(dir.c_str())) { |
| 55 this->fail(); | 64 this->fail(); |
| 56 } | 65 } |
| 57 } | 66 } |
| 58 | 67 |
| 59 namespace { | 68 static bool save_bitmap_to_file(SkBitmap bitmap, const char* path) { |
| 60 | |
| 61 // One file that first contains a .png of an SkBitmap, then its raw pixels. | |
| 62 // We use this custom format to avoid premultiplied/unpremultiplied pixel conver sions. | |
| 63 struct PngAndRaw { | |
| 64 static bool Encode(SkBitmap bitmap, const char* path) { | |
| 65 SkFILEWStream stream(path); | |
| 66 if (!stream.isValid()) { | |
| 67 SkDebugf("Can't write %s.\n", path); | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 // Write a PNG first for humans and other tools to look at. | |
| 72 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_ Type, 100)) { | |
| 73 SkDebugf("Can't encode a PNG.\n"); | |
| 74 return false; | |
| 75 } | |
| 76 if (FLAGS_writePngOnly) { | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 // Pad out so the raw pixels start 4-byte aligned. | |
| 81 const uint32_t maxPadding = 0; | |
| 82 const size_t pos = stream.bytesWritten(); | |
| 83 stream.write(&maxPadding, SkAlign4(pos) - pos); | |
| 84 | |
| 85 // Then write our secret raw pixels that only DM reads. | |
| 86 SkAutoLockPixels lock(bitmap); | |
| 87 return stream.write(bitmap.getPixels(), bitmap.getSize()); | |
| 88 } | |
| 89 | |
| 90 // This assumes bitmap already has allocated pixels of the correct size. | |
| 91 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) { | |
| 92 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path)); | |
| 93 if (!data) { | |
| 94 SkDebugf("Can't read %s.\n", path); | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 // The raw pixels are at the end of the file. We'll skip the encoded PN G at the front. | |
| 99 const size_t rowBytes = info.minRowBytes(); // Assume densely packed. | |
| 100 const size_t bitmapBytes = info.getSafeSize(rowBytes); | |
| 101 if (data->size() < bitmapBytes) { | |
| 102 SkDebugf("%s is too small to contain the bitmap we're looking for.\n ", path); | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 const size_t offset = data->size() - bitmapBytes; | |
| 107 SkAutoTUnref<SkData> subset( | |
| 108 SkData::NewSubset(data, offset, bitmapBytes)); | |
| 109 SkAutoTUnref<SkPixelRef> pixels( | |
| 110 SkMallocPixelRef::NewWithData( | |
| 111 info, rowBytes, NULL/*ctable*/, subset)); | |
| 112 SkASSERT(pixels); | |
| 113 | |
| 114 bitmap->setInfo(info, rowBytes); | |
| 115 bitmap->setPixelRef(pixels); | |
| 116 return true; | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 // Does not take ownership of data. | |
| 121 bool save_data_to_file(SkStreamAsset* data, const char* path) { | |
| 122 data->rewind(); | |
| 123 SkFILEWStream stream(path); | 69 SkFILEWStream stream(path); |
| 124 if (!stream.isValid() || !stream.writeStream(data, data->getLength())) { | 70 if (!stream.isValid() || |
| 125 SkDebugf("Can't write %s.\n", path); | 71 !SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type , 100)) { |
| 72 SkDebugf("Can't write a PNG to %s.\n", path); | |
| 126 return false; | 73 return false; |
| 127 } | 74 } |
| 128 return true; | 75 return true; |
| 129 } | 76 } |
| 130 | 77 |
| 131 } // namespace | 78 // Does not take ownership of data. |
| 79 static bool save_data_to_file(SkStreamAsset* data, const char* path) { | |
| 80 data->rewind(); | |
| 81 SkFILEWStream stream(path); | |
| 82 if (!stream.isValid() || !stream.writeStream(data, data->getLength())) { | |
| 83 SkDebugf("Can't write data to %s.\n", path); | |
| 84 return false; | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 132 | 88 |
| 133 void WriteTask::draw() { | 89 void WriteTask::draw() { |
| 134 SkString dir(FLAGS_writePath[0]); | 90 SkString dir(FLAGS_writePath[0]); |
| 135 #if SK_BUILD_FOR_IOS | 91 #if SK_BUILD_FOR_IOS |
| 136 if (dir.equals("@")) { | 92 if (dir.equals("@")) { |
| 137 dir.set(FLAGS_resourcePath[0]); | 93 dir.set(FLAGS_resourcePath[0]); |
| 138 } | 94 } |
| 139 #endif | 95 #endif |
| 140 this->makeDirOrFail(dir); | 96 this->makeDirOrFail(dir); |
| 141 for (int i = 0; i < fSuffixes.count(); i++) { | 97 for (int i = 0; i < fSuffixes.count(); i++) { |
| 142 dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str()); | 98 dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str()); |
| 143 this->makeDirOrFail(dir); | 99 this->makeDirOrFail(dir); |
| 144 } | 100 } |
| 145 | 101 |
| 146 SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str()); | 102 // FIXME: MD5 is really slow. Let's use a different hash. |
| 103 SkMD5 hasher; | |
| 104 if (fData.get()) { | |
| 105 hasher.write(fData->getMemoryBase(), fData->getLength()); | |
| 106 } else { | |
| 107 SkAutoLockPixels lock(fBitmap); | |
| 108 hasher.write(fBitmap.getPixels(), fBitmap.getSize()); | |
| 109 } | |
| 110 | |
| 111 JsonData entry; | |
| 112 entry.name = fFullName; | |
| 113 hasher.finish(entry.md5); | |
| 114 | |
| 115 { | |
| 116 SkAutoMutexAcquire lock(&gJsonDataLock); | |
| 117 gJsonData.push_back(entry); | |
| 118 } | |
| 119 | |
| 120 SkString path = SkOSPath::Join(dir.c_str(), fBaseName.c_str()); | |
| 147 path.append(fExtension); | 121 path.append(fExtension); |
| 148 | 122 |
| 149 const bool ok = fData.get() ? save_data_to_file(fData.get(), path.c_str()) | 123 const bool ok = fData.get() ? save_data_to_file(fData.get(), path.c_str()) |
| 150 : PngAndRaw::Encode(fBitmap, path.c_str()); | 124 : save_bitmap_to_file(fBitmap, path.c_str()); |
| 151 if (!ok) { | 125 if (!ok) { |
| 152 this->fail(); | 126 this->fail(); |
| 153 } | 127 } |
| 154 } | 128 } |
| 155 | 129 |
| 156 SkString WriteTask::name() const { | 130 SkString WriteTask::name() const { |
| 157 SkString name("writing "); | 131 SkString name("writing "); |
| 158 for (int i = 0; i < fSuffixes.count(); i++) { | 132 for (int i = 0; i < fSuffixes.count(); i++) { |
| 159 name.appendf("%s/", fSuffixes[i].c_str()); | 133 name.appendf("%s/", fSuffixes[i].c_str()); |
| 160 } | 134 } |
| 161 name.append(fGmName.c_str()); | 135 name.append(fBaseName.c_str()); |
| 162 return name; | 136 return name; |
| 163 } | 137 } |
| 164 | 138 |
| 165 bool WriteTask::shouldSkip() const { | 139 bool WriteTask::shouldSkip() const { |
| 166 return FLAGS_writePath.isEmpty(); | 140 return FLAGS_writePath.isEmpty(); |
| 167 } | 141 } |
| 168 | 142 |
| 169 static SkString path_to_expected_image(const char* root, const Task& task) { | 143 WriteTask::Expectations* WriteTask::Expectations::Create(const char* path) { |
| 170 SkString filename = task.name(); | 144 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], path)) { |
| 145 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", path); | |
| 146 return NULL; | |
| 147 } | |
| 171 | 148 |
| 172 // We know that all names passed in here belong to top-level Tasks, which ha ve a single suffix | 149 SkString jsonPath; |
| 173 // (8888, 565, gpu, etc.) indicating what subdirectory to look in. | 150 if (sk_isdir(path)) { |
| 174 SkTArray<SkString> suffixes; | 151 jsonPath = SkOSPath::Join(path, "dm.json"); |
| 175 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes); | 152 } else { |
| 176 SkASSERT(1 == suffixes.count()); | 153 jsonPath.set(path); |
| 154 } | |
| 177 | 155 |
| 178 // We'll look in root/suffix for images. | 156 SkAutoDataUnref json(SkData::NewFromFileName(jsonPath.c_str())); |
| 179 const SkString dir = SkOSPath::Join(root, suffixes[0].c_str()); | 157 if (NULL == json.get()) { |
| 158 SkDebugf("Can't read %s!\n", jsonPath.c_str()); | |
| 159 return NULL; | |
| 160 } | |
| 180 | 161 |
| 181 // Remove the suffix and tack on a .png. | 162 SkAutoTDelete<Expectations> expectations(SkNEW(Expectations)); |
| 182 filename.remove(filename.size() - suffixLength, suffixLength); | 163 Json::Reader reader; |
| 183 filename.append(".png"); | 164 const char* begin = (const char*)json->bytes(); |
| 184 | 165 const char* end = begin + json->size(); |
| 185 return SkOSPath::Join(dir.c_str(), filename.c_str()); | 166 if (!reader.parse(begin, end, expectations->fJson)) { |
| 167 SkDebugf("Can't read %s as JSON!\n", jsonPath.c_str()); | |
| 168 return NULL; | |
| 169 } | |
| 170 return expectations.detach(); | |
| 186 } | 171 } |
| 187 | 172 |
| 188 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { | 173 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
| 189 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) { | 174 const SkString name = task.name(); |
| 190 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot); | 175 if (fJson[name.c_str()].isNull()) { |
| 191 return false; | 176 return true; // No expectations. |
| 192 } | 177 } |
| 193 | 178 |
| 194 const SkString path = path_to_expected_image(fRoot, task); | 179 const char* md5Ascii = fJson[name.c_str()].asCString(); |
| 195 SkBitmap expected; | 180 uint8_t md5[16]; |
| 196 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) { | 181 |
| 197 return false; | 182 for (int j = 0; j < 16; j++) { |
| 183 sscanf(md5Ascii + (j*2), "%02hhx", md5 + j); | |
| 198 } | 184 } |
| 199 | 185 |
| 200 return BitmapsEqual(expected, bitmap); | 186 SkMD5 hasher; |
| 187 { | |
| 188 SkAutoLockPixels lock(bitmap); | |
| 189 hasher.write(bitmap.getPixels(), bitmap.getSize()); | |
| 190 } | |
| 191 SkMD5::Digest digest; | |
| 192 hasher.finish(digest); | |
| 193 | |
| 194 return 0 == memcmp(md5, digest.data, 16); | |
| 195 } | |
| 196 | |
| 197 void WriteTask::DumpJson() { | |
| 198 if (FLAGS_writePath.isEmpty()) { | |
| 199 return; | |
| 200 } | |
| 201 | |
| 202 // FIXME: This JSON format is a complete MVP strawman. | |
| 203 Json::Value root; | |
| 204 { | |
| 205 SkAutoMutexAcquire lock(&gJsonDataLock); | |
| 206 for (int i = 0; i < gJsonData.count(); i++) { | |
| 207 char md5Ascii[32]; | |
| 208 for (int j = 0; j < 16; j++) { | |
| 209 sprintf(md5Ascii + (j*2), "%02x", gJsonData[i].md5.data[j]); | |
| 210 } | |
| 211 root[gJsonData[i].name.c_str()] = md5Ascii; | |
|
jcgregorio
2014/09/05 17:54:21
Can we make this an array of hashes, or do you wan
| |
| 212 } | |
| 213 } | |
| 214 | |
| 215 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); | |
| 216 SkFILEWStream stream(path.c_str()); | |
| 217 stream.writeText(Json::StyledWriter().write(root).c_str()); | |
| 218 stream.flush(); | |
| 201 } | 219 } |
| 202 | 220 |
| 203 } // namespace DM | 221 } // namespace DM |
| OLD | NEW |