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..b7e2794ec35bbee54acb709a5dbefe60d6a4ea8b 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" |
@@ -17,6 +25,20 @@ |
#include "third_party/skia/include/core/SkUnPreMultiply.h" |
namespace { |
+ |
+// TODO(nyquist): Add support for changing this from the client. |
+static base::LazyInstance<std::set<std::string>> g_client_cache_contents = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+SkData* BlobCacheImageProtoAsSkData( |
+ std::unique_ptr<blimp::BlobCacheImage> proto) { |
Kevin M
2016/04/14 20:08:30
pass as const ref
nyquist
2016/04/15 01:27:12
Done.
|
+ 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 +47,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 +70,26 @@ class WebPImageEncoder : public SkPixelSerializer { |
return nullptr; |
picture.height = pixmap.height(); |
+ // Each pixel is using 4 bytes (RGBA/BGRA), so hash over the whole input. |
+ size_t pixel_size = picture.width * picture.height * 4; |
+ std::vector<unsigned char> sha1_bytes = |
+ blimp::ToSHA1HashBytes(pixmap.addr(), pixel_size); |
+ std::string sha1 = blimp::ToSHA1HexString(sha1_bytes.data()); |
Kevin M
2016/04/14 20:08:30
Ditto my previous comment in another file about us
nyquist
2016/04/15 01:27:12
Done.
|
+ |
+ // Create proto with all requires information. |
+ std::unique_ptr<blimp::BlobCacheImage> proto(new blimp::BlobCacheImage); |
+ proto->set_id(sha1_bytes.data(), sha1_bytes.size()); |
Kevin M
2016/04/14 20:08:30
.swap() the vectors for great efficiencies.
nyquist
2016/04/15 01:27:12
So now, I use the sha1_bytes below when looking up
|
+ proto->set_width(pixmap.width()); |
+ proto->set_height(pixmap.height()); |
+ |
+ if (g_client_cache_contents.Get().find(sha1) != |
+ g_client_cache_contents.Get().end()) { |
+ // Found image in client cache, so skip sending decoded payload. |
+ SkData* sk_data = BlobCacheImageProtoAsSkData(std::move(proto)); |
+ DVLOG(2) << "Sending cached: " << sha1 << " 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 +123,15 @@ 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(sha1); |
+ proto->set_payload(&data.front(), data.size()); |
+ |
+ // Copy proto into SkData. |
+ SkData* sk_data = BlobCacheImageProtoAsSkData(std::move(proto)); |
+ DVLOG(2) << "Sending image: " << sha1 << " size = " << sk_data->size(); |
+ return sk_data; |
} |
private: |