Index: blimp/common/compositor/webp_decoder.cc |
diff --git a/blimp/common/compositor/webp_decoder.cc b/blimp/common/compositor/webp_decoder.cc |
index 7f4d3637d618eaed36ec202bc78e0aa0a6d9db8a..9293da20682d47a5609028f5a4d108222b485c2a 100644 |
--- a/blimp/common/compositor/webp_decoder.cc |
+++ b/blimp/common/compositor/webp_decoder.cc |
@@ -4,12 +4,33 @@ |
#include "blimp/common/compositor/webp_decoder.h" |
+#include "base/lazy_instance.h" |
#include "base/logging.h" |
+#include "base/numerics/safe_conversions.h" |
+#include "base/sha1.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "blimp/common/blob_cache/blob_cache.h" |
+#include "blimp/common/blob_cache/infinite_blob_cache.h" |
+#include "blimp/common/proto/blob_cache.pb.h" |
#include "third_party/libwebp/webp/decode.h" |
#include "third_party/libwebp/webp/demux.h" |
#include "third_party/skia/include/core/SkBitmap.h" |
namespace blimp { |
+namespace { |
+std::string ToSHA1HexString(const void* data, size_t data_size) { |
Kevin M
2016/04/13 23:29:52
What about using binary SHA1 for transmission and
nyquist
2016/04/14 19:33:23
Done. For now I also keep the cache in hex-format,
|
+ unsigned char sha1_hash[base::kSHA1Length]; |
+ base::SHA1HashBytes(static_cast<const unsigned char*>(data), data_size, |
+ sha1_hash); |
+ return base::ToLowerASCII(base::HexEncode(sha1_hash, base::kSHA1Length)); |
+} |
+ |
+// TODO(nyquist): Make this not be infinite size. |
+static base::LazyInstance<InfiniteBlobCache> g_blob_cache = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
DCHECK(bitmap); |
@@ -24,6 +45,33 @@ bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
// Treat the input as uint8_t. |
WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; |
Kevin M
2016/04/13 23:29:52
Brace initialization is blacklisted. Sowwy. :(
Kevin M
2016/04/13 23:29:53
Pick a better name?
vmpstr
2016/04/13 23:48:02
Soon though, right? I think pkasting might be tryi
nyquist
2016/04/14 19:33:23
Done.
|
+ if (input_size < 40) { |
+ LOG(WARNING) << "Throwing away tiny input with size " << input_size; |
Kevin M
2016/04/13 23:29:52
How about DLOG? Is this actionable for release bui
nyquist
2016/04/14 19:33:23
Done.
|
+ return false; |
+ } |
+ |
+ bool found_in_cache = false; |
Kevin M
2016/04/13 23:29:52
Move this declaration down, just above the ParseFr
nyquist
2016/04/14 19:33:23
Done.
|
+ // Declared here to still be in scope while decoding WebP data. |
+ scoped_refptr<RefCountedVector> cached(new RefCountedVector); |
+ |
+ std::unique_ptr<BlobCacheImageIdentifier> deserialized( |
+ new BlobCacheImageIdentifier); |
+ int signed_size = base::checked_cast<int>(input_size); |
+ if (deserialized->ParseFromArray(input, signed_size)) { |
+ if (g_blob_cache.Get().Contains(deserialized->id())) { |
+ cached = g_blob_cache.Get().Get(deserialized->id()); |
+ // Overwrite previously initialized WebPData. |
+ data.bytes = cached.get()->data.data(); |
+ data.size = cached.get()->data.size(); |
+ VLOG(2) << "Found SHA1 " << deserialized->id() |
+ << " with size = " << data.size; |
+ found_in_cache = true; |
+ } else { |
+ LOG(WARNING) << "Did not find cached image with SHA1: " |
+ << deserialized->id(); |
+ } |
+ } |
+ |
// Read WebP feature information into |config.input|, which is a |
// WebPBitstreamFeatures. It contains information such as width, height and |
// whether the WebP image has an alpha channel or not. |
@@ -69,6 +117,17 @@ bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
LOG(WARNING) << "Failed to decode WebP data."; |
return false; |
} |
+ |
+ if (!found_in_cache) { |
+ std::string sha1 = ToSHA1HexString(data.bytes, data.size); |
+ VLOG(2) << "Inserting image to cache with SHA1: " << sha1 |
+ << " size: " << data.size; |
+ scoped_refptr<RefCountedVector> to_cache(new RefCountedVector); |
+ for (size_t i = 0; i < data.size; ++i) |
Kevin M
2016/04/13 23:29:52
Use braces for single lines (consistent with rest
nyquist
2016/04/14 19:33:23
Done.
|
+ to_cache.get()->data.push_back(data.bytes[i]); |
+ g_blob_cache.Get().Put(sha1, to_cache); |
Kevin M
2016/04/13 23:29:53
std::move(to_cache)
nyquist
2016/04/14 19:33:23
Done.
|
+ } |
+ |
return true; |
} |