OLD | NEW |
1 #include "DMWriteTask.h" | 1 #include "DMWriteTask.h" |
2 | 2 |
3 #include "DMUtil.h" | 3 #include "DMUtil.h" |
4 #include "SkCommandLineFlags.h" | 4 #include "SkCommandLineFlags.h" |
| 5 #include "SkImageDecoder.h" |
5 #include "SkImageEncoder.h" | 6 #include "SkImageEncoder.h" |
6 #include "SkString.h" | 7 #include "SkString.h" |
7 | 8 |
8 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); | 9 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); |
9 | 10 |
10 namespace DM { | 11 namespace DM { |
11 | 12 |
| 13 // Splits off the last N suffixes of name (splitting on _) and appends them to o
ut. |
| 14 // Returns the total number of characters consumed. |
| 15 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { |
| 16 SkTArray<SkString> split; |
| 17 SkStrSplit(name, "_", &split); |
| 18 int consumed = 0; |
| 19 for (int i = 0; i < N; i++) { |
| 20 // We're splitting off suffixes from the back to front. |
| 21 out->push_back(split[split.count()-i-1]); |
| 22 consumed += out->back().size() + 1; // Add one for the _. |
| 23 } |
| 24 return consumed; |
| 25 } |
| 26 |
12 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitma
p(bitmap) { | 27 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitma
p(bitmap) { |
13 const int suffixes = parent.depth() + 1; | 28 const int suffixes = parent.depth() + 1; |
14 const char* name = parent.name().c_str(); | 29 const SkString& name = parent.name(); |
15 SkTArray<SkString> split; | 30 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffi
xes); |
16 SkStrSplit(name, "_", &split); | 31 fGmName.set(name.c_str(), name.size()-totalSuffixLength); |
17 int totalSuffixLength = 0; | |
18 for (int i = 0; i < suffixes; i++) { | |
19 // We're splitting off suffixes from the back to front. | |
20 fSuffixes.push_back(split[split.count()-i-1]); | |
21 totalSuffixLength += fSuffixes.back().size() + 1; | |
22 } | |
23 fGmName.set(name, strlen(name)-totalSuffixLength); | |
24 } | 32 } |
25 | 33 |
26 void WriteTask::makeDirOrFail(SkString dir) { | 34 void WriteTask::makeDirOrFail(SkString dir) { |
27 if (!sk_mkdir(dir.c_str())) { | 35 if (!sk_mkdir(dir.c_str())) { |
28 this->fail(); | 36 this->fail(); |
29 } | 37 } |
30 } | 38 } |
31 | 39 |
32 void WriteTask::draw() { | 40 void WriteTask::draw() { |
33 SkString dir(FLAGS_writePath[0]); | 41 SkString dir(FLAGS_writePath[0]); |
34 this->makeDirOrFail(dir); | 42 this->makeDirOrFail(dir); |
35 for (int i = 0; i < fSuffixes.count(); i++) { | 43 for (int i = 0; i < fSuffixes.count(); i++) { |
36 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); | 44 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str()); |
37 this->makeDirOrFail(dir); | 45 this->makeDirOrFail(dir); |
38 } | 46 } |
39 if (!SkImageEncoder::EncodeFile(Png(SkOSPath::SkPathJoin(dir.c_str(), fGmNam
e.c_str())).c_str(), | 47 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str()); |
| 48 path.append(".png"); |
| 49 if (!SkImageEncoder::EncodeFile(path.c_str(), |
40 fBitmap, | 50 fBitmap, |
41 SkImageEncoder::kPNG_Type, | 51 SkImageEncoder::kPNG_Type, |
42 100/*quality*/)) { | 52 100/*quality*/)) { |
43 this->fail(); | 53 this->fail(); |
44 } | 54 } |
45 } | 55 } |
46 | 56 |
47 SkString WriteTask::name() const { | 57 SkString WriteTask::name() const { |
48 SkString name("writing "); | 58 SkString name("writing "); |
49 for (int i = 0; i < fSuffixes.count(); i++) { | 59 for (int i = 0; i < fSuffixes.count(); i++) { |
50 name.appendf("%s/", fSuffixes[i].c_str()); | 60 name.appendf("%s/", fSuffixes[i].c_str()); |
51 } | 61 } |
52 name.append(fGmName.c_str()); | 62 name.append(fGmName.c_str()); |
53 return name; | 63 return name; |
54 } | 64 } |
55 | 65 |
56 bool WriteTask::shouldSkip() const { | 66 bool WriteTask::shouldSkip() const { |
57 return FLAGS_writePath.isEmpty(); | 67 return FLAGS_writePath.isEmpty(); |
58 } | 68 } |
59 | 69 |
| 70 static SkString path_to_expected_image(const char* root, const Task& task) { |
| 71 SkString filename = task.name(); |
| 72 |
| 73 // We know that all names passed in here belong to top-level Tasks, which ha
ve a single suffix |
| 74 // (8888, 565, gpu, etc.) indicating what subdirectory to look in. |
| 75 SkTArray<SkString> suffixes; |
| 76 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes); |
| 77 SkASSERT(1 == suffixes.count()); |
| 78 |
| 79 // We'll look in root/suffix for images. |
| 80 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str()); |
| 81 |
| 82 // Remove the suffix and tack on a .png. |
| 83 filename.remove(filename.size() - suffixLength, suffixLength); |
| 84 filename.append(".png"); |
| 85 |
| 86 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str()); |
| 87 |
| 88 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str()); |
| 89 } |
| 90 |
| 91 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
| 92 const SkString path = path_to_expected_image(fRoot, task); |
| 93 |
| 94 SkBitmap expected; |
| 95 if (SkImageDecoder::DecodeFile(path.c_str(), &expected)) { |
| 96 if (expected.config() != bitmap.config()) { |
| 97 SkBitmap converted; |
| 98 SkAssertResult(expected.copyTo(&converted, bitmap.config())); |
| 99 expected.swap(converted); |
| 100 } |
| 101 SkASSERT(expected.config() == bitmap.config()); |
| 102 return BitmapsEqual(expected, bitmap); |
| 103 } |
| 104 |
| 105 // Couldn't read the file, etc. |
| 106 SkDebugf("Problem decoding %s to SkBitmap.\n", path.c_str()); |
| 107 return false; |
| 108 } |
| 109 |
60 } // namespace DM | 110 } // namespace DM |
OLD | NEW |