aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dm/DMWriteTask.cpp32
-rw-r--r--gyp/tests.gypi1
-rw-r--r--tests/UnpremultiplyTest.cpp38
3 files changed, 12 insertions, 59 deletions
diff --git a/dm/DMWriteTask.cpp b/dm/DMWriteTask.cpp
index 95f162d313..fd8396cce1 100644
--- a/dm/DMWriteTask.cpp
+++ b/dm/DMWriteTask.cpp
@@ -64,18 +64,9 @@ void WriteTask::makeDirOrFail(SkString dir) {
}
}
-static SkStreamAsset* encode_to_png(const SkBitmap& bitmap) {
- SkDynamicMemoryWStream png;
- if (!SkImageEncoder::EncodeStream(&png, bitmap, SkImageEncoder::kPNG_Type, 100)) {
- return NULL;
- }
- png.copyToData()->unref(); // Forces detachAsStream() to be contiguous.
- return png.detachAsStream();
-}
-
-static SkString get_md5(SkStreamAsset* src) {
+static SkString get_md5(const void* ptr, size_t len) {
SkMD5 hasher;
- hasher.write(src->getMemoryBase(), src->getLength());
+ hasher.write(ptr, len);
SkMD5::Digest digest;
hasher.finish(digest);
@@ -96,14 +87,14 @@ SkTArray<JsonData> gJsonData;
SK_DECLARE_STATIC_MUTEX(gJsonDataLock);
void WriteTask::draw() {
- if (!fData.get()) {
- fData.reset(encode_to_png(fBitmap));
- if (!fData.get()) {
- this->fail("Can't encode to PNG.");
- }
+ SkString md5;
+ {
+ SkAutoLockPixels lock(fBitmap);
+ md5 = fData ? get_md5(fData->getMemoryBase(), fData->getLength())
+ : get_md5(fBitmap.getPixels(), fBitmap.getSize());
}
- JsonData entry = { fBaseName, fSuffixes[0], fSourceType, get_md5(fData) };
+ JsonData entry = { fBaseName, fSuffixes[0], fSourceType, md5 };
{
SkAutoMutexAcquire lock(&gJsonDataLock);
gJsonData.push_back(entry);
@@ -120,7 +111,7 @@ void WriteTask::draw() {
SkString path;
if (FLAGS_nameByHash) {
// Flat directory of hash-named files.
- path = SkOSPath::Join(dir.c_str(), entry.md5.c_str());
+ path = SkOSPath::Join(dir.c_str(), 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
@@ -145,8 +136,9 @@ void WriteTask::draw() {
return this->fail("Can't open file.");
}
- fData->rewind();
- if (!file.writeStream(fData, fData->getLength())) {
+ bool ok = fData ? file.writeStream(fData, fData->getLength())
+ : SkImageEncoder::EncodeStream(&file, fBitmap, SkImageEncoder::kPNG_Type, 100);
+ if (!ok) {
return this->fail("Can't write to file.");
}
}
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index 15f5d0445b..cf78e41f63 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -205,7 +205,6 @@
'../tests/TracingTest.cpp',
'../tests/TypefaceTest.cpp',
'../tests/UnicodeTest.cpp',
- '../tests/UnpremultiplyTest.cpp',
'../tests/UtilsTest.cpp',
'../tests/WArrayTest.cpp',
'../tests/WritePixelsTest.cpp',
diff --git a/tests/UnpremultiplyTest.cpp b/tests/UnpremultiplyTest.cpp
deleted file mode 100644
index 7846a1b370..0000000000
--- a/tests/UnpremultiplyTest.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkColorPriv.h"
-#include "SkUnPreMultiply.h"
-#include "Test.h"
-
-DEF_TEST(Unpremultiply, reporter) {
- // Here we test that unpremultiplication is injective:
- // no two distinct premul colors map to the same unpremul color.
-
- // DM exploits this fact to safely hash .pngs instead of the original bitmaps.
-
- // It is sufficient to test red. Green and blue follow the same rules.
- // This means we have at most 256*256 possible colors to deal with.
- int hits[256*256];
- for (size_t i = 0; i < SK_ARRAY_COUNT(hits); i++) {
- hits[i] = 0;
- }
-
- for (int a = 0; a < 256; a++) {
- for (int r = 0; r <= a; r++) {
- SkPMColor pm = SkPackARGB32(a, r, 0, 0);
- SkColor upm = SkUnPreMultiply::PMColorToColor(pm);
-
- // ARGB -> AR
- hits[upm >> 16]++;
- }
- }
-
- for (size_t i = 0; i < SK_ARRAY_COUNT(hits); i++) {
- REPORTER_ASSERT(reporter, hits[i] < 2);
- }
-}