1 #include "DMWriteTask.h"
4 #include "SkColorPriv.h"
5 #include "SkCommonFlags.h"
6 #include "SkImageEncoder.h"
7 #include "SkMallocPixelRef.h"
11 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs.");
12 DEFINE_bool(writePngOnly, false, "If true, don't encode raw bitmap after .png data. "
13 "This means -r won't work, but skdiff will still work fine.");
17 // Splits off the last N suffixes of name (splitting on _) and appends them to out.
18 // Returns the total number of characters consumed.
19 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
20 SkTArray<SkString> split;
21 SkStrSplit(name, "_", &split);
23 for (int i = 0; i < N; i++) {
24 // We're splitting off suffixes from the back to front.
25 out->push_back(split[split.count()-i-1]);
26 consumed += out->back().size() + 1; // Add one for the _.
31 inline static SkString find_gm_name(const Task& parent, SkTArray<SkString>* suffixList) {
32 const int suffixes = parent.depth() + 1;
33 const SkString& name = parent.name();
34 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList);
35 return SkString(name.c_str(), name.size() - totalSuffixLength);
38 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
40 , fGmName(find_gm_name(parent, &fSuffixes))
43 , fExtension(".png") {}
45 WriteTask::WriteTask(const Task& parent, SkData *data, const char* ext)
47 , fGmName(find_gm_name(parent, &fSuffixes))
51 void WriteTask::makeDirOrFail(SkString dir) {
52 if (!sk_mkdir(dir.c_str())) {
59 // One file that first contains a .png of an SkBitmap, then its raw pixels.
60 // We use this custom format to avoid premultiplied/unpremultiplied pixel conversions.
62 static bool Encode(SkBitmap bitmap, const char* path) {
63 SkFILEWStream stream(path);
64 if (!stream.isValid()) {
65 SkDebugf("Can't write %s.\n", path);
69 // Write a PNG first for humans and other tools to look at.
70 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_Type, 100)) {
71 SkDebugf("Can't encode a PNG.\n");
74 if (FLAGS_writePngOnly) {
78 // Pad out so the raw pixels start 4-byte aligned.
79 const uint32_t maxPadding = 0;
80 const size_t pos = stream.bytesWritten();
81 stream.write(&maxPadding, SkAlign4(pos) - pos);
83 // Then write our secret raw pixels that only DM reads.
84 SkAutoLockPixels lock(bitmap);
85 return stream.write(bitmap.getPixels(), bitmap.getSize());
88 // This assumes bitmap already has allocated pixels of the correct size.
89 static bool Decode(const char* path, SkImageInfo info, SkBitmap* bitmap) {
90 SkAutoTUnref<SkData> data(SkData::NewFromFileName(path));
92 SkDebugf("Can't read %s.\n", path);
96 // The raw pixels are at the end of the file. We'll skip the encoded PNG at the front.
97 const size_t rowBytes = info.minRowBytes(); // Assume densely packed.
98 const size_t bitmapBytes = info.getSafeSize(rowBytes);
99 if (data->size() < bitmapBytes) {
100 SkDebugf("%s is too small to contain the bitmap we're looking for.\n", path);
104 const size_t offset = data->size() - bitmapBytes;
105 SkAutoTUnref<SkData> subset(
106 SkData::NewSubset(data, offset, bitmapBytes));
107 SkAutoTUnref<SkPixelRef> pixels(
108 SkMallocPixelRef::NewWithData(
109 info, rowBytes, NULL/*ctable*/, subset));
112 bitmap->setInfo(info, rowBytes);
113 bitmap->setPixelRef(pixels);
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);
130 void WriteTask::draw() {
131 SkString dir(FLAGS_writePath[0]);
133 if (dir.equals("@")) {
134 dir.set(FLAGS_resourcePath[0]);
137 this->makeDirOrFail(dir);
138 for (int i = 0; i < fSuffixes.count(); i++) {
139 dir = SkOSPath::SkPathJoin(dir.c_str(), fSuffixes[i].c_str());
140 this->makeDirOrFail(dir);
143 SkString path = SkOSPath::SkPathJoin(dir.c_str(), fGmName.c_str());
144 path.append(fExtension);
146 const bool ok = fData.get() ? save_data_to_file(fData, path.c_str())
147 : PngAndRaw::Encode(fBitmap, path.c_str());
153 SkString WriteTask::name() const {
154 SkString name("writing ");
155 for (int i = 0; i < fSuffixes.count(); i++) {
156 name.appendf("%s/", fSuffixes[i].c_str());
158 name.append(fGmName.c_str());
162 bool WriteTask::shouldSkip() const {
163 return FLAGS_writePath.isEmpty();
166 static SkString path_to_expected_image(const char* root, const Task& task) {
167 SkString filename = task.name();
169 // We know that all names passed in here belong to top-level Tasks, which have a single suffix
170 // (8888, 565, gpu, etc.) indicating what subdirectory to look in.
171 SkTArray<SkString> suffixes;
172 const int suffixLength = split_suffixes(1, filename.c_str(), &suffixes);
173 SkASSERT(1 == suffixes.count());
175 // We'll look in root/suffix for images.
176 const SkString dir = SkOSPath::SkPathJoin(root, suffixes[0].c_str());
178 // Remove the suffix and tack on a .png.
179 filename.remove(filename.size() - suffixLength, suffixLength);
180 filename.append(".png");
182 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
185 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
186 if (!FLAGS_writePath.isEmpty() && 0 == strcmp(FLAGS_writePath[0], fRoot)) {
187 SkDebugf("We seem to be reading and writing %s concurrently. This won't work.\n", fRoot);
191 const SkString path = path_to_expected_image(fRoot, task);
193 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
197 return BitmapsEqual(expected, bitmap);