Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Unified Diff: blimp/engine/renderer/engine_image_serialization_processor.cc

Issue 1867653002: Initial version of Blimp BlobCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed BlobId from 'const std::string' to 'std::string' Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« blimp/common/compositor/webp_decoder.cc ('K') | « blimp/engine/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..db12dc7cb3424d7f6eefc278a2b13c18b443a36b 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<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.SerializeToArray(serialized.data(), signed_size);
Kevin M 2016/04/18 18:13:14 optimization nit: SerializeWithCachedSizesToArray?
nyquist 2016/04/18 20:12:02 Done.
+ 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.
Kevin M 2016/04/18 18:13:14 "even" ? "Encode all images regardless of their f
nyquist 2016/04/18 20:12:02 Done.
+ 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();
@@ -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(
« blimp/common/compositor/webp_decoder.cc ('K') | « blimp/engine/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698