1 #include "DMWriteTask.h"
2
3 #include "DMUtil.h"
4 #include "SkCommandLineFlags.h"
5 #include "SkImageDecoder.h"
6 #include "SkImageEncoder.h"
7 #include "SkString.h"
8
9 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
10
11 namespace DM {
12
13 // Splits off the last N suffixes of name (splitting on _) and appends them to out.
14 // Returns the total number of characters consumed.
split_suffixes(int N,const char * name,SkTArray<SkString> * out)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
WriteTask(const Task & parent,SkBitmap bitmap)27 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap) : Task(parent), fBitmap(bitmap) {
28 const int suffixes = parent.depth() + 1;
29 const SkString& name = parent.name();
30 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffixes);
31 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
32 }
33
makeDirOrFail(SkString dir)34 void WriteTask::makeDirOrFail(SkString dir) {
35 if (!sk_mkdir(dir.c_str())) {
36 this->fail();
37 }
38 }
39
draw()40 void WriteTask::draw() {
41 SkString dir(FLAGS_writePath[0]);
42 this->makeDirOrFail(dir);
43 for (int i = 0; i < fSuffixes.count(); i++) {
44 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
45 this->makeDirOrFail(dir);
46 }
47 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
48 path.append(".png");
49 if (!SkImageEncoder::EncodeFile(path.c_str(),
50 fBitmap,
51 SkImageEncoder::kPNG_Type,
52 100/*quality*/)) {
53 this->fail();
54 }
55 }
56
name() const57 SkString WriteTask::name() const {
58 SkString name("writing ");
59 for (int i = 0; i < fSuffixes.count(); i++) {
60 name.appendf("%s/", fSuffixes[i].c_str());
61 }
62 name.append(fGmName.c_str());
63 return name;
64 }
65
shouldSkip() const66 bool WriteTask::shouldSkip() const {
67 return FLAGS_writePath.isEmpty();
68 }
69
path_to_expected_image(const char * root,const Task & task)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 have 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
check(const Task & task,SkBitmap bitmap) const91 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
110 } // namespace DM
111