| 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 "SkData.h" |
| 6 #include "SkImageEncoder.h" | 7 #include "SkImageEncoder.h" |
| 7 #include "SkMD5.h" | 8 #include "SkMD5.h" |
| 8 #include "SkMallocPixelRef.h" | 9 #include "SkMallocPixelRef.h" |
| 9 #include "SkOSFile.h" | 10 #include "SkOSFile.h" |
| 10 #include "SkStream.h" | 11 #include "SkStream.h" |
| 11 #include "SkString.h" | 12 #include "SkString.h" |
| 12 | 13 |
| 13 DEFINE_bool(nameByHash, false, "If true, write .../hash.png instead of .../mode/
config/name.png"); | 14 DEFINE_bool(nameByHash, false, "If true, write .../hash.png instead of .../mode/
config/name.png"); |
| 14 | 15 |
| 15 namespace DM { | 16 namespace DM { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 name.appendf("%s/", fSuffixes[i].c_str()); | 152 name.appendf("%s/", fSuffixes[i].c_str()); |
| 152 } | 153 } |
| 153 name.append(fBaseName.c_str()); | 154 name.append(fBaseName.c_str()); |
| 154 return name; | 155 return name; |
| 155 } | 156 } |
| 156 | 157 |
| 157 bool WriteTask::shouldSkip() const { | 158 bool WriteTask::shouldSkip() const { |
| 158 return FLAGS_writePath.isEmpty(); | 159 return FLAGS_writePath.isEmpty(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 WriteTask::Expectations* WriteTask::Expectations::Create(const char* path) { | |
| 162 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], path)) { | |
| 163 SkDebugf("We seem to be reading and writing %s concurrently. This won't
work.\n", path); | |
| 164 return NULL; | |
| 165 } | |
| 166 | |
| 167 SkString jsonPath; | |
| 168 if (sk_isdir(path)) { | |
| 169 jsonPath = SkOSPath::Join(path, "dm.json"); | |
| 170 } else { | |
| 171 jsonPath.set(path); | |
| 172 } | |
| 173 | |
| 174 SkAutoDataUnref json(SkData::NewFromFileName(jsonPath.c_str())); | |
| 175 if (NULL == json.get()) { | |
| 176 SkDebugf("Can't read %s!\n", jsonPath.c_str()); | |
| 177 return NULL; | |
| 178 } | |
| 179 | |
| 180 SkAutoTDelete<Expectations> expectations(SkNEW(Expectations)); | |
| 181 Json::Reader reader; | |
| 182 const char* begin = (const char*)json->bytes(); | |
| 183 const char* end = begin + json->size(); | |
| 184 if (!reader.parse(begin, end, expectations->fJson)) { | |
| 185 SkDebugf("Can't read %s as JSON!\n", jsonPath.c_str()); | |
| 186 return NULL; | |
| 187 } | |
| 188 return expectations.detach(); | |
| 189 } | |
| 190 | |
| 191 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { | |
| 192 const SkString name = task.name(); | |
| 193 if (fJson[name.c_str()].isNull()) { | |
| 194 return true; // No expectations. | |
| 195 } | |
| 196 | |
| 197 const char* expected = fJson[name.c_str()].asCString(); | |
| 198 SkAutoTDelete<SkStreamAsset> png(encode_to_png(bitmap)); | |
| 199 SkString actual = get_md5(png); | |
| 200 return actual.equals(expected); | |
| 201 } | |
| 202 | |
| 203 void WriteTask::DumpJson() { | 162 void WriteTask::DumpJson() { |
| 204 if (FLAGS_writePath.isEmpty()) { | 163 if (FLAGS_writePath.isEmpty()) { |
| 205 return; | 164 return; |
| 206 } | 165 } |
| 207 | 166 |
| 208 // FIXME: This JSON format is a complete MVP strawman. | 167 // FIXME: This JSON format is a complete MVP strawman. |
| 209 Json::Value root; | 168 Json::Value root; |
| 210 { | 169 { |
| 211 SkAutoMutexAcquire lock(&gJsonDataLock); | 170 SkAutoMutexAcquire lock(&gJsonDataLock); |
| 212 for (int i = 0; i < gJsonData.count(); i++) { | 171 for (int i = 0; i < gJsonData.count(); i++) { |
| 213 root[gJsonData[i].name.c_str()] = gJsonData[i].md5.c_str(); | 172 root[gJsonData[i].name.c_str()] = gJsonData[i].md5.c_str(); |
| 214 } | 173 } |
| 215 } | 174 } |
| 216 | 175 |
| 217 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); | 176 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); |
| 218 SkFILEWStream stream(path.c_str()); | 177 SkFILEWStream stream(path.c_str()); |
| 219 stream.writeText(Json::StyledWriter().write(root).c_str()); | 178 stream.writeText(Json::StyledWriter().write(root).c_str()); |
| 220 stream.flush(); | 179 stream.flush(); |
| 221 } | 180 } |
| 222 | 181 |
| 223 } // namespace DM | 182 } // namespace DM |
| OLD | NEW |