| 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 "SkCommandLineFlags.h" | 5 #include "SkCommandLineFlags.h" |
| 6 #include "SkImageEncoder.h" | 6 #include "SkImageEncoder.h" |
| 7 #include "SkMallocPixelRef.h" | 7 #include "SkMallocPixelRef.h" |
| 8 #include "SkStream.h" | 8 #include "SkStream.h" |
| 9 #include "SkString.h" | 9 #include "SkString.h" |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 SkStrSplit(name, "_", &split); | 21 SkStrSplit(name, "_", &split); |
| 22 int consumed = 0; | 22 int consumed = 0; |
| 23 for (int i = 0; i < N; i++) { | 23 for (int i = 0; i < N; i++) { |
| 24 // We're splitting off suffixes from the back to front. | 24 // We're splitting off suffixes from the back to front. |
| 25 out->push_back(split[split.count()-i-1]); | 25 out->push_back(split[split.count()-i-1]); |
| 26 consumed += out->back().size() + 1; // Add one for the _. | 26 consumed += out->back().size() + 1; // Add one for the _. |
| 27 } | 27 } |
| 28 return consumed; | 28 return consumed; |
| 29 } | 29 } |
| 30 | 30 |
| 31 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) | 31 inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suff
ixList) { |
| 32 : CpuTask(parent), fBitmap(bitmap) { | |
| 33 const int suffixes = parent.depth() + 1; | 32 const int suffixes = parent.depth() + 1; |
| 34 const SkString& name = parent.name(); | 33 const SkString& name = parent.name(); |
| 35 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffi
xes); | 34 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixL
ist); |
| 36 fGmName.set(name.c_str(), name.size()-totalSuffixLength); | 35 return SkString(name.c_str(), name.size() - totalSuffixLength); |
| 37 } | 36 } |
| 38 | 37 |
| 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 |
| 39 void WriteTask::makeDirOrFail(SkString dir) { | 51 void WriteTask::makeDirOrFail(SkString dir) { |
| 40 if (!sk_mkdir(dir.c_str())) { | 52 if (!sk_mkdir(dir.c_str())) { |
| 41 this->fail(); | 53 this->fail(); |
| 42 } | 54 } |
| 43 } | 55 } |
| 44 | 56 |
| 45 namespace { | 57 namespace { |
| 46 | 58 |
| 47 // One file that first contains a .png of an SkBitmap, then its raw pixels. | 59 // One file that first contains a .png of an SkBitmap, then its raw pixels. |
| 48 // We use this custom format to avoid premultiplied/unpremultiplied pixel conver
sions. | 60 // We use this custom format to avoid premultiplied/unpremultiplied pixel conver
sions. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 SkMallocPixelRef::NewWithData( | 108 SkMallocPixelRef::NewWithData( |
| 97 info, rowBytes, NULL/*ctable*/, subset)); | 109 info, rowBytes, NULL/*ctable*/, subset)); |
| 98 SkASSERT(pixels); | 110 SkASSERT(pixels); |
| 99 | 111 |
| 100 bitmap->setInfo(info, rowBytes); | 112 bitmap->setInfo(info, rowBytes); |
| 101 bitmap->setPixelRef(pixels); | 113 bitmap->setPixelRef(pixels); |
| 102 return true; | 114 return true; |
| 103 } | 115 } |
| 104 }; | 116 }; |
| 105 | 117 |
| 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 |
| 106 } // namespace | 128 } // namespace |
| 107 | 129 |
| 108 void WriteTask::draw() { | 130 void WriteTask::draw() { |
| 109 SkString dir(FLAGS_writePath[0]); | 131 SkString dir(FLAGS_writePath[0]); |
| 110 this->makeDirOrFail(dir); | 132 this->makeDirOrFail(dir); |
| 111 for (int i = 0; i < fSuffixes.count(); i++) { | 133 for (int i = 0; i < fSuffixes.count(); i++) { |
| 112 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); | 134 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); |
| 113 this->makeDirOrFail(dir); | 135 this->makeDirOrFail(dir); |
| 114 } | 136 } |
| 137 |
| 115 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); | 138 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); |
| 116 path.append(".png"); | 139 path.append(fExtension); |
| 117 if (!PngAndRaw::Encode(fBitmap, path.c_str())) { | 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) { |
| 118 this->fail(); | 144 this->fail(); |
| 119 } | 145 } |
| 120 } | 146 } |
| 121 | 147 |
| 122 SkString WriteTask::name() const { | 148 SkString WriteTask::name() const { |
| 123 SkString name("writing "); | 149 SkString name("writing "); |
| 124 for (int i = 0; i < fSuffixes.count(); i++) { | 150 for (int i = 0; i < fSuffixes.count(); i++) { |
| 125 name.appendf("%s/", fSuffixes[i].c_str()); | 151 name.appendf("%s/", fSuffixes[i].c_str()); |
| 126 } | 152 } |
| 127 name.append(fGmName.c_str()); | 153 name.append(fGmName.c_str()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 const SkString path = path_to_expected_image(fRoot, task); | 186 const SkString path = path_to_expected_image(fRoot, task); |
| 161 SkBitmap expected; | 187 SkBitmap expected; |
| 162 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) { | 188 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) { |
| 163 return false; | 189 return false; |
| 164 } | 190 } |
| 165 | 191 |
| 166 return BitmapsEqual(expected, bitmap); | 192 return BitmapsEqual(expected, bitmap); |
| 167 } | 193 } |
| 168 | 194 |
| 169 } // namespace DM | 195 } // namespace DM |
| OLD | NEW |