Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: dm/DMWriteTask.cpp

Issue 288823002: DM: tweaks (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: oops Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dm/DMWriteTask.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); 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 da ta. "
13 "This means -r won't work, but skdiff will stil l work fine.");
12 14
13 namespace DM { 15 namespace DM {
14 16
15 // Splits off the last N suffixes of name (splitting on _) and appends them to o ut. 17 // Splits off the last N suffixes of name (splitting on _) and appends them to o ut.
16 // Returns the total number of characters consumed. 18 // Returns the total number of characters consumed.
17 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { 19 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
18 SkTArray<SkString> split; 20 SkTArray<SkString> split;
19 SkStrSplit(name, "_", &split); 21 SkStrSplit(name, "_", &split);
20 int consumed = 0; 22 int consumed = 0;
21 for (int i = 0; i < N; i++) { 23 for (int i = 0; i < N; i++) {
22 // We're splitting off suffixes from the back to front. 24 // We're splitting off suffixes from the back to front.
23 out->push_back(split[split.count()-i-1]); 25 out->push_back(split[split.count()-i-1]);
24 consumed += out->back().size() + 1; // Add one for the _. 26 consumed += out->back().size() + 1; // Add one for the _.
25 } 27 }
26 return consumed; 28 return consumed;
27 } 29 }
28 30
29 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap, Mode mode) 31 WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
30 : CpuTask(parent), fBitmap(bitmap) { 32 : CpuTask(parent), fBitmap(bitmap) {
31 if (mode == kVerbatim_Mode) { 33 const int suffixes = parent.depth() + 1;
32 fGmName.set(parent.name()); 34 const SkString& name = parent.name();
33 } else { 35 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fSuffi xes);
34 const int suffixes = parent.depth() + 1; 36 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
35 const SkString& name = parent.name();
36 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), &fS uffixes);
37 fGmName.set(name.c_str(), name.size()-totalSuffixLength);
38 }
39 } 37 }
40 38
41 void WriteTask::makeDirOrFail(SkString dir) { 39 void WriteTask::makeDirOrFail(SkString dir) {
42 if (!sk_mkdir(dir.c_str())) { 40 if (!sk_mkdir(dir.c_str())) {
43 this->fail(); 41 this->fail();
44 } 42 }
45 } 43 }
46 44
47 namespace { 45 namespace {
48 46
49 // One file that first contains a .png of an SkBitmap, then its raw pixels. 47 // One file that first contains a .png of an SkBitmap, then its raw pixels.
50 // We use this custom format to avoid premultiplied/unpremultiplied pixel conver sions. 48 // We use this custom format to avoid premultiplied/unpremultiplied pixel conver sions.
51 struct PngAndRaw { 49 struct PngAndRaw {
52 static bool Encode(SkBitmap bitmap, const char* path) { 50 static bool Encode(SkBitmap bitmap, const char* path) {
53 SkFILEWStream stream(path); 51 SkFILEWStream stream(path);
54 if (!stream.isValid()) { 52 if (!stream.isValid()) {
55 SkDebugf("Can't write %s.\n", path); 53 SkDebugf("Can't write %s.\n", path);
56 return false; 54 return false;
57 } 55 }
58 56
59 // Write a PNG first for humans and other tools to look at. 57 // Write a PNG first for humans and other tools to look at.
60 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_ Type, 100)) { 58 if (!SkImageEncoder::EncodeStream(&stream, bitmap, SkImageEncoder::kPNG_ Type, 100)) {
61 SkDebugf("Can't encode a PNG.\n"); 59 SkDebugf("Can't encode a PNG.\n");
62 return false; 60 return false;
63 } 61 }
62 if (FLAGS_writePngOnly) {
63 return true;
64 }
64 65
65 // Pad out so the raw pixels start 4-byte aligned. 66 // Pad out so the raw pixels start 4-byte aligned.
66 const uint32_t maxPadding = 0; 67 const uint32_t maxPadding = 0;
67 const size_t pos = stream.bytesWritten(); 68 const size_t pos = stream.bytesWritten();
68 stream.write(&maxPadding, SkAlign4(pos) - pos); 69 stream.write(&maxPadding, SkAlign4(pos) - pos);
69 70
70 // Then write our secret raw pixels that only DM reads. 71 // Then write our secret raw pixels that only DM reads.
71 SkAutoLockPixels lock(bitmap); 72 SkAutoLockPixels lock(bitmap);
72 return stream.write(bitmap.getPixels(), bitmap.getSize()); 73 return stream.write(bitmap.getPixels(), bitmap.getSize());
73 } 74 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 const SkString path = path_to_expected_image(fRoot, task); 160 const SkString path = path_to_expected_image(fRoot, task);
160 SkBitmap expected; 161 SkBitmap expected;
161 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) { 162 if (!PngAndRaw::Decode(path.c_str(), bitmap.info(), &expected)) {
162 return false; 163 return false;
163 } 164 }
164 165
165 return BitmapsEqual(expected, bitmap); 166 return BitmapsEqual(expected, bitmap);
166 } 167 }
167 168
168 } // namespace DM 169 } // namespace DM
OLDNEW
« no previous file with comments | « dm/DMWriteTask.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698