blob: fa0d129d688628a4bfc69ce1a86af21f6172c512 [file] [log] [blame]
mtklein@google.coma7a9f372013-10-18 20:52:44 +00001#include "DMWriteTask.h"
2
scroggo7a10fb62014-11-04 07:21:10 -08003#include "DMJsonWriter.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00004#include "DMUtil.h"
commit-bot@chromium.org389fb7f2014-01-15 21:28:25 +00005#include "SkColorPriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -07006#include "SkCommonFlags.h"
mtklein197ceda2014-09-09 07:36:57 -07007#include "SkData.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +00008#include "SkImageEncoder.h"
mtklein1d0f1642014-09-08 08:05:18 -07009#include "SkMD5.h"
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000010#include "SkMallocPixelRef.h"
mtklein1d0f1642014-09-08 08:05:18 -070011#include "SkOSFile.h"
commit-bot@chromium.org1426c1e2014-03-03 15:43:56 +000012#include "SkStream.h"
commit-bot@chromium.orgeef834f2014-03-05 15:37:11 +000013#include "SkString.h"
mtklein@google.coma7a9f372013-10-18 20:52:44 +000014
mtklein858baf52014-09-08 11:33:48 -070015DEFINE_bool(nameByHash, false, "If true, write .../hash.png instead of .../mode/config/name.png");
16
mtklein@google.coma7a9f372013-10-18 20:52:44 +000017namespace DM {
18
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000019// Splits off the last N suffixes of name (splitting on _) and appends them to out.
20// Returns the total number of characters consumed.
21static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000022 SkTArray<SkString> split;
23 SkStrSplit(name, "_", &split);
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000024 int consumed = 0;
25 for (int i = 0; i < N; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000026 // We're splitting off suffixes from the back to front.
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000027 out->push_back(split[split.count()-i-1]);
28 consumed += out->back().size() + 1; // Add one for the _.
rmistry@google.comd6bab022013-12-02 13:50:38 +000029 }
commit-bot@chromium.org99589af2013-12-10 14:53:16 +000030 return consumed;
31}
32
mtklein1d0f1642014-09-08 08:05:18 -070033inline static SkString find_base_name(const Task& parent, SkTArray<SkString>* suffixList) {
commit-bot@chromium.orgd6dcacd2014-05-14 20:26:00 +000034 const int suffixes = parent.depth() + 1;
35 const SkString& name = parent.name();
mtklein30bf3e22014-06-03 13:57:14 -070036 const int totalSuffixLength = split_suffixes(suffixes, name.c_str(), suffixList);
37 return SkString(name.c_str(), name.size() - totalSuffixLength);
rmistry@google.comd6bab022013-12-02 13:50:38 +000038}
mtklein@google.coma7a9f372013-10-18 20:52:44 +000039
mtkleinea65bfa2014-09-09 07:59:46 -070040WriteTask::WriteTask(const Task& parent, const char* sourceType, SkBitmap bitmap)
mtklein30bf3e22014-06-03 13:57:14 -070041 : CpuTask(parent)
mtklein1d0f1642014-09-08 08:05:18 -070042 , fBaseName(find_base_name(parent, &fSuffixes))
mtkleinea65bfa2014-09-09 07:59:46 -070043 , fSourceType(sourceType)
mtklein30bf3e22014-06-03 13:57:14 -070044 , fBitmap(bitmap)
45 , fData(NULL)
mtklein1d0f1642014-09-08 08:05:18 -070046 , fExtension(".png") {
47}
mtklein30bf3e22014-06-03 13:57:14 -070048
mtkleinea65bfa2014-09-09 07:59:46 -070049WriteTask::WriteTask(const Task& parent,
50 const char* sourceType,
51 SkStreamAsset *data,
52 const char* ext)
mtklein30bf3e22014-06-03 13:57:14 -070053 : CpuTask(parent)
mtklein1d0f1642014-09-08 08:05:18 -070054 , fBaseName(find_base_name(parent, &fSuffixes))
mtkleinea65bfa2014-09-09 07:59:46 -070055 , fSourceType(sourceType)
halcanarya4c60942014-08-26 10:38:07 -070056 , fData(data)
57 , fExtension(ext) {
58 SkASSERT(fData.get());
59 SkASSERT(fData->unique());
60}
mtklein30bf3e22014-06-03 13:57:14 -070061
rmistry@google.comd6bab022013-12-02 13:50:38 +000062void WriteTask::makeDirOrFail(SkString dir) {
63 if (!sk_mkdir(dir.c_str())) {
64 this->fail();
65 }
mtklein@google.coma7a9f372013-10-18 20:52:44 +000066}
67
bungemand51ce442014-10-02 13:39:00 -070068static SkString get_md5_string(SkMD5* hasher) {
mtkleine2d4eb72014-09-08 12:42:23 -070069 SkMD5::Digest digest;
bungemand51ce442014-10-02 13:39:00 -070070 hasher->finish(digest);
mtkleine2d4eb72014-09-08 12:42:23 -070071
72 SkString md5;
73 for (int i = 0; i < 16; i++) {
74 md5.appendf("%02x", digest.data[i]);
75 }
76 return md5;
mtklein858baf52014-09-08 11:33:48 -070077}
78
bungemand51ce442014-10-02 13:39:00 -070079static SkString get_md5(const void* ptr, size_t len) {
80 SkMD5 hasher;
81 hasher.write(ptr, len);
82 return get_md5_string(&hasher);
83}
84
halcanarydaf36c12014-10-17 14:36:10 -070085static bool write_asset(SkStreamAsset* input, SkWStream* output) {
86 return input->rewind() && output->writeStream(input, input->getLength());
87}
88
bungemand51ce442014-10-02 13:39:00 -070089static SkString get_md5(SkStreamAsset* stream) {
90 SkMD5 hasher;
halcanarydaf36c12014-10-17 14:36:10 -070091 write_asset(stream, &hasher);
bungemand51ce442014-10-02 13:39:00 -070092 return get_md5_string(&hasher);
93}
94
mtklein@google.coma7a9f372013-10-18 20:52:44 +000095void WriteTask::draw() {
mtkleinc54056c2014-09-09 08:42:04 -070096 SkString md5;
97 {
98 SkAutoLockPixels lock(fBitmap);
bungemand51ce442014-10-02 13:39:00 -070099 md5 = fData ? get_md5(fData)
mtkleinc54056c2014-09-09 08:42:04 -0700100 : get_md5(fBitmap.getPixels(), fBitmap.getSize());
mtkleine2d4eb72014-09-08 12:42:23 -0700101 }
mtklein858baf52014-09-08 11:33:48 -0700102
mtklein87e24372014-09-19 10:35:07 -0700103 SkASSERT(fSuffixes.count() > 0);
104 SkString config = fSuffixes.back();
105 SkString mode("direct");
106 if (fSuffixes.count() > 1) {
107 mode = fSuffixes.fromBack(1);
108 }
109
mtklein858baf52014-09-08 11:33:48 -0700110 {
scroggo7a10fb62014-11-04 07:21:10 -0800111 const JsonWriter::BitmapResult entry = { fBaseName,
112 config,
113 mode,
114 fSourceType,
115 md5 };
116 JsonWriter::AddBitmapResult(entry);
mtklein858baf52014-09-08 11:33:48 -0700117 }
118
rmistry@google.comd6bab022013-12-02 13:50:38 +0000119 SkString dir(FLAGS_writePath[0]);
tfarina6b87df22014-10-06 10:46:50 -0700120#if defined(SK_BUILD_FOR_IOS)
caryclark17f0b6d2014-07-22 10:15:34 -0700121 if (dir.equals("@")) {
122 dir.set(FLAGS_resourcePath[0]);
123 }
124#endif
rmistry@google.comd6bab022013-12-02 13:50:38 +0000125 this->makeDirOrFail(dir);
mtklein30bf3e22014-06-03 13:57:14 -0700126
mtklein858baf52014-09-08 11:33:48 -0700127 SkString path;
128 if (FLAGS_nameByHash) {
129 // Flat directory of hash-named files.
mtkleinc54056c2014-09-09 08:42:04 -0700130 path = SkOSPath::Join(dir.c_str(), md5.c_str());
mtklein858baf52014-09-08 11:33:48 -0700131 path.append(fExtension);
132 // We're content-addressed, so it's possible two threads race to write
133 // this file. We let the first one win. This also means we won't
134 // overwrite identical files from previous runs.
135 if (sk_exists(path.c_str())) {
136 return;
137 }
mtklein1d0f1642014-09-08 08:05:18 -0700138 } else {
mtklein858baf52014-09-08 11:33:48 -0700139 // Nested by mode, config, etc.
140 for (int i = 0; i < fSuffixes.count(); i++) {
141 dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
142 this->makeDirOrFail(dir);
143 }
144 path = SkOSPath::Join(dir.c_str(), fBaseName.c_str());
145 path.append(fExtension);
146 // The path is unique, so two threads can't both write to the same file.
147 // If already present we overwrite here, since the content may have changed.
mtklein1d0f1642014-09-08 08:05:18 -0700148 }
149
mtkleine2d4eb72014-09-08 12:42:23 -0700150 SkFILEWStream file(path.c_str());
151 if (!file.isValid()) {
152 return this->fail("Can't open file.");
153 }
154
halcanarydaf36c12014-10-17 14:36:10 -0700155 bool ok = fData ? write_asset(fData, &file)
mtkleinc54056c2014-09-09 08:42:04 -0700156 : SkImageEncoder::EncodeStream(&file, fBitmap, SkImageEncoder::kPNG_Type, 100);
157 if (!ok) {
mtkleine2d4eb72014-09-08 12:42:23 -0700158 return this->fail("Can't write to file.");
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000159 }
160}
161
162SkString WriteTask::name() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000163 SkString name("writing ");
164 for (int i = 0; i < fSuffixes.count(); i++) {
165 name.appendf("%s/", fSuffixes[i].c_str());
166 }
mtklein1d0f1642014-09-08 08:05:18 -0700167 name.append(fBaseName.c_str());
rmistry@google.comd6bab022013-12-02 13:50:38 +0000168 return name;
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000169}
170
171bool WriteTask::shouldSkip() const {
172 return FLAGS_writePath.isEmpty();
173}
174
mtklein@google.coma7a9f372013-10-18 20:52:44 +0000175} // namespace DM