Index: blimp/engine/renderer/engine_image_serialization_processor.cc |
diff --git a/blimp/engine/renderer/engine_image_serialization_processor.cc b/blimp/engine/renderer/engine_image_serialization_processor.cc |
index ffecf3576df9c7f9e89fe49a67294ffaee7a530f..68f48d4e339ba45ae28c4619ca4b510b8df1c8f8 100644 |
--- a/blimp/engine/renderer/engine_image_serialization_processor.cc |
+++ b/blimp/engine/renderer/engine_image_serialization_processor.cc |
@@ -5,10 +5,18 @@ |
#include "blimp/engine/renderer/engine_image_serialization_processor.h" |
#include <stddef.h> |
+#include <set> |
+#include <string> |
#include <vector> |
+#include "base/lazy_instance.h" |
#include "base/logging.h" |
+#include "base/sha1.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "blimp/common/blob_cache/sha1_util.h" |
#include "blimp/common/compositor/webp_decoder.h" |
+#include "blimp/common/proto/blob_cache.pb.h" |
#include "content/public/renderer/render_frame.h" |
#include "third_party/libwebp/webp/encode.h" |
#include "third_party/skia/include/core/SkData.h" |
@@ -16,7 +24,22 @@ |
#include "third_party/skia/include/core/SkPixelSerializer.h" |
#include "third_party/skia/include/core/SkUnPreMultiply.h" |
+namespace blimp { |
namespace { |
+ |
+// TODO(nyquist): Add support for changing this from the client. |
+static base::LazyInstance<std::set<std::string>> g_client_cache_contents = |
nyquist
2016/04/16 00:25:30
with base::LazyInstance<std::set<BlobID>> this fai
vmpstr
2016/04/16 00:52:47
What do ou mean address() being declared twice? Is
nyquist
2016/04/16 01:10:50
Sent link to compile error on chat, but basically
|
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+SkData* BlobCacheImageMetadataProtoAsSkData( |
+ const BlobCacheImageMetadata& proto) { |
+ int signed_size = proto.ByteSize(); |
+ size_t unsigned_size = base::checked_cast<size_t>(signed_size); |
+ std::vector<uint8_t> serialized(unsigned_size); |
+ proto.SerializeToArray(serialized.data(), signed_size); |
+ return SkData::NewWithCopy(serialized.data(), serialized.size()); |
+} |
+ |
// TODO(nyquist): Make sure encoder does not serialize images more than once. |
// See crbug.com/548434. |
class WebPImageEncoder : public SkPixelSerializer { |
@@ -25,10 +48,8 @@ class WebPImageEncoder : public SkPixelSerializer { |
~WebPImageEncoder() override{}; |
bool onUseEncodedData(const void* data, size_t len) override { |
- const unsigned char* cast_data = static_cast<const unsigned char*>(data); |
- if (len < 14) |
- return false; |
- return !memcmp(cast_data, "RIFF", 4) && !memcmp(cast_data + 8, "WEBPVP", 6); |
+ // Always encode even WebP data, to enable caching. |
+ return false; |
} |
SkData* onEncode(const SkPixmap& pixmap) override { |
@@ -50,6 +71,24 @@ class WebPImageEncoder : public SkPixelSerializer { |
return nullptr; |
picture.height = pixmap.height(); |
+ BlobId blob_id = CalculateBlobId(pixmap.addr(), pixmap.getSafeSize()); |
+ std::string blob_id_hex = FormatBlobId(blob_id); |
+ |
+ // Create proto with all requires information. |
+ BlobCacheImageMetadata proto; |
+ proto.set_id(blob_id); |
+ proto.set_width(pixmap.width()); |
+ proto.set_height(pixmap.height()); |
+ |
+ if (g_client_cache_contents.Get().find(blob_id) != |
+ g_client_cache_contents.Get().end()) { |
+ // Found image in client cache, so skip sending decoded payload. |
+ SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); |
+ DVLOG(2) << "Sending cached: " << blob_id_hex |
+ << " size = " << sk_data->size(); |
+ return sk_data; |
+ } |
+ |
DVLOG(2) << "Encoding image color_type=" << pixmap.colorType() |
<< ", alpha_type=" << pixmap.alphaType() << " " << pixmap.width() |
<< "x" << pixmap.height(); |
@@ -83,9 +122,16 @@ class WebPImageEncoder : public SkPixelSerializer { |
if (!success) |
return nullptr; |
- // Copy WebP data into SkData. |data| is allocated only on the stack, so |
- // it is automatically deleted after this. |
- return SkData::NewWithCopy(&data.front(), data.size()); |
+ // Did not find item in cache, so add it to client cache representation |
+ // and send full item. |
+ g_client_cache_contents.Get().insert(blob_id); |
+ proto.set_payload(&data.front(), data.size()); |
+ |
+ // Copy proto into SkData. |
+ SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); |
+ DVLOG(2) << "Sending image: " << blob_id_hex |
+ << " size = " << sk_data->size(); |
+ return sk_data; |
} |
private: |
@@ -149,7 +195,6 @@ class WebPImageEncoder : public SkPixelSerializer { |
} // namespace |
-namespace blimp { |
namespace engine { |
EngineImageSerializationProcessor::EngineImageSerializationProcessor( |