Chromium Code Reviews| 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..2def239d91d42f021785787cbde5ab04184b274e 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::vector<unsigned char>>> |
| + g_client_cache_contents = LAZY_INSTANCE_INITIALIZER; |
| + |
| +SkData* BlobCacheImageMetadataProtoAsSkData( |
| + const blimp::BlobCacheImageMetadata& proto) { |
| + int signed_size = proto.ByteSize(); |
| + size_t unsigned_size = base::checked_cast<size_t>(signed_size); |
|
vmpstr
2016/04/15 18:18:54
This is super safe and all, but do you need this?
nyquist
2016/04/16 00:25:30
It returns an int, which could be negative, so we
|
| + 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,27 @@ class WebPImageEncoder : public SkPixelSerializer { |
| return nullptr; |
| picture.height = pixmap.height(); |
| + // Each pixel is using 4 bytes (RGBA/BGRA), so hash over the whole input. |
|
Kevin M
2016/04/15 17:28:51
Is there something we can check to validate the pi
vmpstr
2016/04/15 18:18:54
SkPixmap has a bunch of functions to tell you the
nyquist
2016/04/16 00:25:30
Doh... I thought they looked like row size :-/
th
|
| + 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::FormatSHA1Hash(sha1_bytes.data()); |
| + |
| + // Create proto with all requires information. |
| + std::unique_ptr<blimp::BlobCacheImageMetadata> proto( |
|
vmpstr
2016/04/15 18:18:54
Create this on the stack?
nyquist
2016/04/16 00:25:30
Done.
|
| + new blimp::BlobCacheImageMetadata); |
| + proto->set_id(sha1_bytes.data(), sha1_bytes.size()); |
| + proto->set_width(pixmap.width()); |
| + proto->set_height(pixmap.height()); |
| + |
| + if (g_client_cache_contents.Get().find(sha1_bytes) != |
| + g_client_cache_contents.Get().end()) { |
| + // Found image in client cache, so skip sending decoded payload. |
| + SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(*proto.get()); |
|
vmpstr
2016/04/15 18:18:54
If you create proto on the stack, then *proto.get(
nyquist
2016/04/16 00:25:30
Done.
|
| + 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 +124,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_bytes); |
| + proto->set_payload(&data.front(), data.size()); |
| + |
| + // Copy proto into SkData. |
| + SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(*proto.get()); |
| + DVLOG(2) << "Sending image: " << sha1 << " size = " << sk_data->size(); |
| + return sk_data; |
| } |
| private: |