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 | 197ceda | 2014-09-09 07:36:57 -0700 | [diff] [blame] | 6 | #include "SkData.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 7 | #include "SkImageEncoder.h" |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 8 | #include "SkMD5.h" |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 9 | #include "SkMallocPixelRef.h" |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 10 | #include "SkOSFile.h" |
commit-bot@chromium.org | 1426c1e | 2014-03-03 15:43:56 +0000 | [diff] [blame] | 11 | #include "SkStream.h" |
commit-bot@chromium.org | eef834f | 2014-03-05 15:37:11 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 13 | |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 14 | DEFINE_bool(nameByHash, false, "If true, write .../hash.png instead of .../mode/config/name.png"); |
| 15 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 16 | namespace DM { |
| 17 | |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 18 | // Splits off the last N suffixes of name (splitting on _) and appends them to out. |
| 19 | // Returns the total number of characters consumed. |
| 20 | 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] | 21 | SkTArray<SkString> split; |
| 22 | SkStrSplit(name, "_", &split); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 23 | int consumed = 0; |
| 24 | for (int i = 0; i < N; i++) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 25 | // We're splitting off suffixes from the back to front. |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 26 | out->push_back(split[split.count()-i-1]); |
| 27 | consumed += out->back().size() + 1; // Add one for the _. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 28 | } |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 29 | return consumed; |
| 30 | } |
| 31 | |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 32 | inline static SkString find_base_name(const Task& parent, SkTArray<SkString>* suffixList) { |
commit-bot@chromium.org | d6dcacd | 2014-05-14 20:26:00 +0000 | [diff] [blame] | 33 | const int suffixes = parent.depth() + 1; |
| 34 | const SkString& name = parent.name(); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 35 | const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList); |
| 36 | return SkString(name.c_str(), name.size() - totalSuffixLength); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 37 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 38 | |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 39 | WriteTask::WriteTask(const Task& parent, const char* sourceType, SkBitmap bitmap) |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 40 | : CpuTask(parent) |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 41 | , fBaseName(find_base_name(parent, &fSuffixes)) |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 42 | , fSourceType(sourceType) |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 43 | , fBitmap(bitmap) |
| 44 | , fData(NULL) |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 45 | , fExtension(".png") { |
| 46 | } |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 47 | |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 48 | WriteTask::WriteTask(const Task& parent, |
| 49 | const char* sourceType, |
| 50 | SkStreamAsset *data, |
| 51 | const char* ext) |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 52 | : CpuTask(parent) |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 53 | , fBaseName(find_base_name(parent, &fSuffixes)) |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 54 | , fSourceType(sourceType) |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 55 | , fData(data) |
| 56 | , fExtension(ext) { |
| 57 | SkASSERT(fData.get()); |
| 58 | SkASSERT(fData->unique()); |
| 59 | } |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 60 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 61 | void WriteTask::makeDirOrFail(SkString dir) { |
| 62 | if (!sk_mkdir(dir.c_str())) { |
| 63 | this->fail(); |
| 64 | } |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
bungeman | d51ce44 | 2014-10-02 13:39:00 -0700 | [diff] [blame] | 67 | static SkString get_md5_string(SkMD5* hasher) { |
mtklein | e2d4eb7 | 2014-09-08 12:42:23 -0700 | [diff] [blame] | 68 | SkMD5::Digest digest; |
bungeman | d51ce44 | 2014-10-02 13:39:00 -0700 | [diff] [blame] | 69 | hasher->finish(digest); |
mtklein | e2d4eb7 | 2014-09-08 12:42:23 -0700 | [diff] [blame] | 70 | |
| 71 | SkString md5; |
| 72 | for (int i = 0; i < 16; i++) { |
| 73 | md5.appendf("%02x", digest.data[i]); |
| 74 | } |
| 75 | return md5; |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 76 | } |
| 77 | |
bungeman | d51ce44 | 2014-10-02 13:39:00 -0700 | [diff] [blame] | 78 | static SkString get_md5(const void* ptr, size_t len) { |
| 79 | SkMD5 hasher; |
| 80 | hasher.write(ptr, len); |
| 81 | return get_md5_string(&hasher); |
| 82 | } |
| 83 | |
| 84 | static SkString get_md5(SkStreamAsset* stream) { |
| 85 | SkMD5 hasher; |
| 86 | hasher.writeStream(stream, stream->getLength()); |
| 87 | return get_md5_string(&hasher); |
| 88 | } |
| 89 | |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 90 | struct JsonData { |
mtklein | 87e2437 | 2014-09-19 10:35:07 -0700 | [diff] [blame] | 91 | SkString name; // E.g. "ninepatch-stretch", "desk-gws_skp" |
| 92 | SkString config; // "gpu", "8888" |
| 93 | SkString mode; // "direct", "default-tilegrid", "pipe" |
| 94 | SkString sourceType; // "GM", "SKP" |
| 95 | SkString md5; // In ASCII, so 32 bytes long. |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 96 | }; |
| 97 | SkTArray<JsonData> gJsonData; |
| 98 | SK_DECLARE_STATIC_MUTEX(gJsonDataLock); |
| 99 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 100 | void WriteTask::draw() { |
mtklein | c54056c | 2014-09-09 08:42:04 -0700 | [diff] [blame] | 101 | SkString md5; |
| 102 | { |
| 103 | SkAutoLockPixels lock(fBitmap); |
bungeman | d51ce44 | 2014-10-02 13:39:00 -0700 | [diff] [blame] | 104 | md5 = fData ? get_md5(fData) |
mtklein | c54056c | 2014-09-09 08:42:04 -0700 | [diff] [blame] | 105 | : get_md5(fBitmap.getPixels(), fBitmap.getSize()); |
mtklein | e2d4eb7 | 2014-09-08 12:42:23 -0700 | [diff] [blame] | 106 | } |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 107 | |
mtklein | 87e2437 | 2014-09-19 10:35:07 -0700 | [diff] [blame] | 108 | SkASSERT(fSuffixes.count() > 0); |
| 109 | SkString config = fSuffixes.back(); |
| 110 | SkString mode("direct"); |
| 111 | if (fSuffixes.count() > 1) { |
| 112 | mode = fSuffixes.fromBack(1); |
| 113 | } |
| 114 | |
| 115 | JsonData entry = { fBaseName, config, mode, fSourceType, md5 }; |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 116 | { |
| 117 | SkAutoMutexAcquire lock(&gJsonDataLock); |
| 118 | gJsonData.push_back(entry); |
| 119 | } |
| 120 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 121 | SkString dir(FLAGS_writePath[0]); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 122 | #if SK_BUILD_FOR_IOS |
| 123 | if (dir.equals("@")) { |
| 124 | dir.set(FLAGS_resourcePath[0]); |
| 125 | } |
| 126 | #endif |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 127 | this->makeDirOrFail(dir); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 128 | |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 129 | SkString path; |
| 130 | if (FLAGS_nameByHash) { |
| 131 | // Flat directory of hash-named files. |
mtklein | c54056c | 2014-09-09 08:42:04 -0700 | [diff] [blame] | 132 | path = SkOSPath::Join(dir.c_str(), md5.c_str()); |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 133 | path.append(fExtension); |
| 134 | // We're content-addressed, so it's possible two threads race to write |
| 135 | // this file. We let the first one win. This also means we won't |
| 136 | // overwrite identical files from previous runs. |
| 137 | if (sk_exists(path.c_str())) { |
| 138 | return; |
| 139 | } |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 140 | } else { |
mtklein | 858baf5 | 2014-09-08 11:33:48 -0700 | [diff] [blame] | 141 | // Nested by mode, config, etc. |
| 142 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 143 | dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str()); |
| 144 | this->makeDirOrFail(dir); |
| 145 | } |
| 146 | path = SkOSPath::Join(dir.c_str(), fBaseName.c_str()); |
| 147 | path.append(fExtension); |
| 148 | // The path is unique, so two threads can't both write to the same file. |
| 149 | // If already present we overwrite here, since the content may have changed. |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 150 | } |
| 151 | |
mtklein | e2d4eb7 | 2014-09-08 12:42:23 -0700 | [diff] [blame] | 152 | SkFILEWStream file(path.c_str()); |
| 153 | if (!file.isValid()) { |
| 154 | return this->fail("Can't open file."); |
| 155 | } |
| 156 | |
mtklein | c54056c | 2014-09-09 08:42:04 -0700 | [diff] [blame] | 157 | bool ok = fData ? file.writeStream(fData, fData->getLength()) |
| 158 | : SkImageEncoder::EncodeStream(&file, fBitmap, SkImageEncoder::kPNG_Type, 100); |
| 159 | if (!ok) { |
mtklein | e2d4eb7 | 2014-09-08 12:42:23 -0700 | [diff] [blame] | 160 | return this->fail("Can't write to file."); |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | SkString WriteTask::name() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 165 | SkString name("writing "); |
| 166 | for (int i = 0; i < fSuffixes.count(); i++) { |
| 167 | name.appendf("%s/", fSuffixes[i].c_str()); |
| 168 | } |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 169 | name.append(fBaseName.c_str()); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 170 | return name; |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool WriteTask::shouldSkip() const { |
| 174 | return FLAGS_writePath.isEmpty(); |
| 175 | } |
| 176 | |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 177 | void WriteTask::DumpJson() { |
| 178 | if (FLAGS_writePath.isEmpty()) { |
| 179 | return; |
| 180 | } |
| 181 | |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 182 | Json::Value root; |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 183 | |
| 184 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 185 | root[FLAGS_properties[i-1]] = FLAGS_properties[i]; |
| 186 | } |
| 187 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 188 | root["key"][FLAGS_key[i-1]] = FLAGS_key[i]; |
| 189 | } |
| 190 | |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 191 | { |
| 192 | SkAutoMutexAcquire lock(&gJsonDataLock); |
| 193 | for (int i = 0; i < gJsonData.count(); i++) { |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 194 | Json::Value result; |
| 195 | result["key"]["name"] = gJsonData[i].name.c_str(); |
| 196 | result["key"]["config"] = gJsonData[i].config.c_str(); |
mtklein | 87e2437 | 2014-09-19 10:35:07 -0700 | [diff] [blame] | 197 | result["key"]["mode"] = gJsonData[i].mode.c_str(); |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 198 | result["options"]["source_type"] = gJsonData[i].sourceType.c_str(); |
| 199 | result["md5"] = gJsonData[i].md5.c_str(); |
| 200 | |
| 201 | root["results"].append(result); |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); |
| 206 | SkFILEWStream stream(path.c_str()); |
| 207 | stream.writeText(Json::StyledWriter().write(root).c_str()); |
| 208 | stream.flush(); |
commit-bot@chromium.org | 99589af | 2013-12-10 14:53:16 +0000 | [diff] [blame] | 209 | } |
| 210 | |
mtklein@google.com | a7a9f37 | 2013-10-18 20:52:44 +0000 | [diff] [blame] | 211 | } // namespace DM |