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..88f13eb4a7883c119c8320cbc69ec9ad8b4539a8 100644 |
--- a/blimp/common/compositor/webp_decoder.cc |
+++ b/blimp/common/compositor/webp_decoder.cc |
@@ -4,12 +4,28 @@ |
#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/in_memory_blob_cache.h" |
+#include "blimp/common/blob_cache/sha1_util.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 { |
+ |
+// TODO(nyquist): Make this not be infinite size. |
+static base::LazyInstance<InMemoryBlobCache> g_blob_cache = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
DCHECK(bitmap); |
@@ -21,13 +37,46 @@ bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
return false; |
} |
- // Treat the input as uint8_t. |
- WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; |
+ std::unique_ptr<BlobCacheImageMetadata> deserialized( |
vmpstr
2016/04/15 18:18:54
BlobCacheImageMetadata deserialized;
nyquist
2016/04/16 00:25:30
Done.
|
+ new BlobCacheImageMetadata); |
+ int signed_size = base::checked_cast<int>(input_size); |
+ if (!deserialized->ParseFromArray(input, signed_size)) { |
+ LOG(WARNING) << "Failed to parse BlobCacheImageMetadata"; |
+ return false; |
+ } |
+ |
+ // Used later to decode the image and is initialized either based on a cached |
+ // item or from the |payload| of the proto. |
+ WebPData webp_data; |
vmpstr
2016/04/15 18:18:54
Define this closer to where it's used please
nyquist
2016/04/16 00:25:30
Done.
|
+ |
+ // Declared here to still be in scope while decoding WebP data. |
+ scoped_refptr<RefCountedVector> cached(new RefCountedVector); |
+ |
+ // The cache uses the SHA1 hex string of the ID. |
+ DCHECK_EQ(base::kSHA1Length, deserialized->id().length()); |
Kevin M
2016/04/15 17:28:51
This is network-originating data, so DCHECK isn't
nyquist
2016/04/16 00:25:30
Done.
|
+ std::string hex_id = blimp::FormatSHA1Hash(deserialized->id().c_str()); |
+ |
+ bool found_in_cache = false; |
+ if (g_blob_cache.Get().Contains(deserialized->id())) { |
+ // The image was found in the cache, so decode using cached data. |
+ cached = g_blob_cache.Get().Get(deserialized->id()); |
+ webp_data.bytes = cached->data.data(); |
+ webp_data.size = cached->data.size(); |
+ DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size; |
+ found_in_cache = true; |
+ } else { |
+ // The image was not found in the cache, so decode using the payload. |
+ DCHECK(deserialized->has_payload()); |
+ webp_data.bytes = |
+ reinterpret_cast<const uint8_t*>(deserialized->payload().c_str()); |
+ webp_data.size = deserialized->payload().size(); |
+ } |
// 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. |
- if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { |
+ if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != |
+ VP8_STATUS_OK) { |
LOG(WARNING) << "Failed to get WebP features."; |
return false; |
} |
@@ -59,7 +108,8 @@ bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
config.output = decoderBuffer; |
// Decode the input data into the bitmap buffer. |
- bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; |
+ bool success = |
+ WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK; |
// Now free the buffer. It is safe to call this even when the buffer is |
// external and not allocated by WebPDecode. |
@@ -69,6 +119,15 @@ bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
LOG(WARNING) << "Failed to decode WebP data."; |
return false; |
} |
+ |
+ if (!found_in_cache) { |
+ DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id |
+ << " size: " << webp_data.size; |
+ scoped_refptr<RefCountedVector> to_cache(new RefCountedVector); |
+ to_cache->data.assign(webp_data.bytes, webp_data.bytes + webp_data.size); |
+ g_blob_cache.Get().Put(deserialized->id(), std::move(to_cache)); |
+ } |
+ |
return true; |
} |