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 8b670197a35f70a28ab1230793c2117ea341751f..c3adf0595c91af0d54d232ddbad6a465d5109478 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/id_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<BlobId>> g_client_cache_contents = |
+ 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.SerializeWithCachedSizesToArray(serialized.data()); |
+ 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); |
+ // Encode all images regardless of their format, including WebP images. |
+ return false; |
} |
SkData* onEncode(const SkPixmap& pixmap) override { |
@@ -50,6 +71,24 @@ class WebPImageEncoder : public SkPixelSerializer { |
return nullptr; |
picture.height = pixmap.height(); |
+ const 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(); |
@@ -60,8 +99,8 @@ class WebPImageEncoder : public SkPixelSerializer { |
return nullptr; |
// Create a buffer for where to store the output data. |
- std::vector<unsigned char> data; |
- picture.custom_ptr = &data; |
+ std::vector<unsigned char> encoded_data; |
+ picture.custom_ptr = &encoded_data; |
// Use our own WebPWriterFunction implementation. |
picture.writer = &WebPImageEncoder::WriteOutput; |
@@ -88,9 +127,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(&encoded_data.front(), encoded_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: |
@@ -154,7 +200,6 @@ class WebPImageEncoder : public SkPixelSerializer { |
} // namespace |
-namespace blimp { |
namespace engine { |
EngineImageSerializationProcessor::EngineImageSerializationProcessor( |