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..56d1fd7eb3e31c88e849d9734fb7370a08b55fa4 100644 |
--- a/blimp/engine/renderer/engine_image_serialization_processor.cc |
+++ b/blimp/engine/renderer/engine_image_serialization_processor.cc |
@@ -5,10 +5,16 @@ |
#include "blimp/engine/renderer/engine_image_serialization_processor.h" |
#include <stddef.h> |
+#include <set> |
+#include <string> |
#include <vector> |
#include "base/logging.h" |
+#include "base/sha1.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_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 +23,17 @@ |
#include "third_party/skia/include/core/SkUnPreMultiply.h" |
namespace { |
+ |
+std::string ToSHA1HexString(const void* data, size_t data_size) { |
Kevin M
2016/04/13 23:29:53
Break this out into a library for sharing with cli
nyquist
2016/04/14 19:33:23
Done.
|
+ unsigned char sha1_hash[base::kSHA1Length]; |
+ base::SHA1HashBytes(static_cast<const unsigned char*>(data), data_size, |
+ sha1_hash); |
+ return base::ToLowerASCII(base::HexEncode(sha1_hash, base::kSHA1Length)); |
+} |
+ |
+// TODO(nyquist): Add support for changing this from the client. |
+std::set<std::string> g_client_cache_contents; |
Kevin M
2016/04/13 23:29:53
Use LazyInstance
nyquist
2016/04/14 19:33:23
Done.
|
+ |
// TODO(nyquist): Make sure encoder does not serialize images more than once. |
// See crbug.com/548434. |
class WebPImageEncoder : public SkPixelSerializer { |
@@ -25,10 +42,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 { |
@@ -83,9 +98,27 @@ 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()); |
+ std::string sha1 = ToSHA1HexString(&data.front(), data.size()); |
+ if (g_client_cache_contents.find(sha1) == g_client_cache_contents.end()) { |
+ g_client_cache_contents.insert(sha1); |
+ // Copy WebP data into SkData. |data| is allocated only on the stack, so |
Kevin M
2016/04/13 23:29:53
Move comment to top of block or add newline before
nyquist
2016/04/14 19:33:23
Done.
|
+ // it is automatically deleted after this. |
+ VLOG(2) << "Sending image: " << sha1 << " size = " << data.size(); |
+ return SkData::NewWithCopy(&data.front(), data.size()); |
+ } else { |
Kevin M
2016/04/13 23:29:53
Add comment about cache hit
nyquist
2016/04/14 19:33:23
Done.
|
+ std::unique_ptr<blimp::BlobCacheImageIdentifier> proto( |
+ new blimp::BlobCacheImageIdentifier); |
+ proto->set_id(sha1); |
+ proto->set_width(picture.width); |
+ proto->set_height(picture.height); |
+ 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); |
+ // Copy cache identifier proto into SkData. |
Kevin M
2016/04/13 23:29:53
Newline before comment.
nyquist
2016/04/14 19:33:23
Done.
|
+ VLOG(2) << "Sending cached: " << sha1 << " size = " << serialized.size(); |
+ return SkData::NewWithCopy(serialized.data(), serialized.size()); |
+ } |
} |
private: |