Add --nameByHash for the bots, and refactor hashing a bit.

No diff in dm.json output.

Here's what we get when --nameByHash is on:

mtklein@mtklein ~/skia (dm)> ls bad4/
0077fefedcee39feae9d12751853758b.png  431921de1607b4e1ae6864cc684d9b8e.png  8a3e17eba9302498700a703bb286d1ef.png  c6448cb266f5e0b8d02b512ad484a9ad.png
01b0dcd515e846ea3b7f3b4c68120943.png  4372eb0fce0b0bcf5a582c502f90ecef.png  8a6acdd96cf6915fa01a9cea6d6291b0.png  c6db4e2c9860b900130af429373eafaa.png
02974ab0efa7aa325b3bf22515b33899.png  43bddc2e255a3501ce2728544b1d409d.png  8aa8763d11afd397c2768e54d5599181.png  c716a9a53513a16e0572c67863e3be08.png
02b7b9e5e95e69f89b6cec9c4539d972.png  43cf7adda66b3719e4abd19e92dda55e.png  8ade6bb4c9e2244e0dbd9436cf6e2132.png  c854bb6dd105644a71d0219025409039.png
02d29dbfd29260f9318fcd6177c6ba7b.png  4423958139a2b64185583dacb7379b13.png  8b039171593685112a9454879f40a965.png  c863ac2394e519690c57e1392b14b19f.png
...

BUG=skia:
R=jcgregorio@google.com, mtklein@google.com

Author: mtklein@chromium.org

Review URL: https://codereview.chromium.org/550283002
This commit is contained in:
mtklein 2014-09-08 11:33:48 -07:00 committed by Commit bot
parent 8067412d92
commit 858baf5b7d

View File

@ -10,6 +10,8 @@
#include "SkStream.h"
#include "SkString.h"
DEFINE_bool(nameByHash, false, "If true, write .../hash.png instead of .../mode/config/name.png");
namespace DM {
// Splits off the last N suffixes of name (splitting on _) and appends them to out.
@ -35,7 +37,7 @@ inline static SkString find_base_name(const Task& parent, SkTArray<SkString>* su
struct JsonData {
SkString name;
SkMD5::Digest md5;
SkString md5; // In ASCII, so 32 bytes long.
};
SkTArray<JsonData> gJsonData;
SK_DECLARE_STATIC_MUTEX(gJsonDataLock);
@ -86,7 +88,43 @@ static bool save_data_to_file(SkStreamAsset* data, const char* path) {
return true;
}
static SkString finish_hash(SkMD5* hasher) {
SkMD5::Digest digest;
hasher->finish(digest);
SkString out;
for (int i = 0; i < 16; i++) {
out.appendf("%02x", digest.data[i]);
}
return out;
}
static SkString hash(const SkBitmap& src) {
SkMD5 hasher;
{
SkAutoLockPixels lock(src);
hasher.write(src.getPixels(), src.getSize());
}
return finish_hash(&hasher);
}
static SkString hash(SkStreamAsset* src) {
SkMD5 hasher;
hasher.write(src->getMemoryBase(), src->getLength());
return finish_hash(&hasher);
}
void WriteTask::draw() {
JsonData entry = {
fFullName,
fData ? hash(fData) : hash(fBitmap),
};
{
SkAutoMutexAcquire lock(&gJsonDataLock);
gJsonData.push_back(entry);
}
SkString dir(FLAGS_writePath[0]);
#if SK_BUILD_FOR_IOS
if (dir.equals("@")) {
@ -94,31 +132,29 @@ void WriteTask::draw() {
}
#endif
this->makeDirOrFail(dir);
SkString path;
if (FLAGS_nameByHash) {
// Flat directory of hash-named files.
path = SkOSPath::Join(dir.c_str(), entry.md5.c_str());
path.append(fExtension);
// We're content-addressed, so it's possible two threads race to write
// this file. We let the first one win. This also means we won't
// overwrite identical files from previous runs.
if (sk_exists(path.c_str())) {
return;
}
} else {
// Nested by mode, config, etc.
for (int i = 0; i < fSuffixes.count(); i++) {
dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
this->makeDirOrFail(dir);
}
// FIXME: MD5 is really slow. Let's use a different hash.
SkMD5 hasher;
if (fData.get()) {
hasher.write(fData->getMemoryBase(), fData->getLength());
} else {
SkAutoLockPixels lock(fBitmap);
hasher.write(fBitmap.getPixels(), fBitmap.getSize());
}
JsonData entry;
entry.name = fFullName;
hasher.finish(entry.md5);
{
SkAutoMutexAcquire lock(&gJsonDataLock);
gJsonData.push_back(entry);
}
SkString path = SkOSPath::Join(dir.c_str(), fBaseName.c_str());
path = SkOSPath::Join(dir.c_str(), fBaseName.c_str());
path.append(fExtension);
// The path is unique, so two threads can't both write to the same file.
// If already present we overwrite here, since the content may have changed.
}
const bool ok = fData.get() ? save_data_to_file(fData.get(), path.c_str())
: save_bitmap_to_file(fBitmap, path.c_str());
@ -176,22 +212,9 @@ bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
return true; // No expectations.
}
const char* md5Ascii = fJson[name.c_str()].asCString();
uint8_t md5[16];
for (int j = 0; j < 16; j++) {
sscanf(md5Ascii + (j*2), "%02hhx", md5 + j);
}
SkMD5 hasher;
{
SkAutoLockPixels lock(bitmap);
hasher.write(bitmap.getPixels(), bitmap.getSize());
}
SkMD5::Digest digest;
hasher.finish(digest);
return 0 == memcmp(md5, digest.data, 16);
const char* expected = fJson[name.c_str()].asCString();
SkString actual = hash(bitmap);
return actual.equals(expected);
}
void WriteTask::DumpJson() {
@ -204,11 +227,7 @@ void WriteTask::DumpJson() {
{
SkAutoMutexAcquire lock(&gJsonDataLock);
for (int i = 0; i < gJsonData.count(); i++) {
char md5Ascii[32];
for (int j = 0; j < 16; j++) {
sprintf(md5Ascii + (j*2), "%02x", gJsonData[i].md5.data[j]);
}
root[gJsonData[i].name.c_str()] = md5Ascii;
root[gJsonData[i].name.c_str()] = gJsonData[i].md5.c_str();
}
}