OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/engine/renderer/engine_image_serialization_processor.h" | 5 #include "blimp/engine/renderer/engine_image_serialization_processor.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
16 #include "blimp/common/blob_cache/id_util.h" | 16 #include "blimp/common/blob_cache/id_util.h" |
17 #include "blimp/common/proto/blob_cache.pb.h" | 17 #include "blimp/common/proto/blob_cache.pb.h" |
18 #include "blimp/engine/renderer/blob_channel_sender_host.h" | |
18 #include "content/public/renderer/render_frame.h" | 19 #include "content/public/renderer/render_frame.h" |
19 #include "third_party/libwebp/webp/encode.h" | 20 #include "third_party/libwebp/webp/encode.h" |
20 #include "third_party/skia/include/core/SkData.h" | 21 #include "third_party/skia/include/core/SkData.h" |
21 #include "third_party/skia/include/core/SkPicture.h" | 22 #include "third_party/skia/include/core/SkPicture.h" |
22 #include "third_party/skia/include/core/SkPixelSerializer.h" | 23 #include "third_party/skia/include/core/SkPixelSerializer.h" |
23 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 24 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
24 | 25 |
25 namespace blimp { | 26 namespace blimp { |
27 namespace engine { | |
26 namespace { | 28 namespace { |
27 | 29 |
28 // TODO(nyquist): Add support for changing this from the client. | |
29 static base::LazyInstance<std::set<BlobId>> g_client_cache_contents = | |
30 LAZY_INSTANCE_INITIALIZER; | |
31 | |
32 SkData* BlobCacheImageMetadataProtoAsSkData( | 30 SkData* BlobCacheImageMetadataProtoAsSkData( |
33 const BlobCacheImageMetadata& proto) { | 31 const BlobCacheImageMetadata& proto) { |
34 int signed_size = proto.ByteSize(); | 32 int signed_size = proto.ByteSize(); |
35 size_t unsigned_size = base::checked_cast<size_t>(signed_size); | 33 size_t unsigned_size = base::checked_cast<size_t>(signed_size); |
36 std::vector<uint8_t> serialized(unsigned_size); | 34 std::vector<uint8_t> serialized(unsigned_size); |
37 proto.SerializeWithCachedSizesToArray(serialized.data()); | 35 proto.SerializeWithCachedSizesToArray(serialized.data()); |
38 return SkData::NewWithCopy(serialized.data(), serialized.size()); | 36 return SkData::NewWithCopy(serialized.data(), serialized.size()); |
39 } | 37 } |
40 | 38 |
41 // TODO(nyquist): Make sure encoder does not serialize images more than once. | 39 // TODO(nyquist): Make sure encoder does not serialize images more than once. |
42 // See crbug.com/548434. | 40 // See crbug.com/548434. |
43 class WebPImageEncoder : public SkPixelSerializer { | 41 class WebPImageEncoder : public SkPixelSerializer { |
44 public: | 42 public: |
45 WebPImageEncoder() {} | 43 explicit WebPImageEncoder(BlobChannelSenderHost* blob_channel) |
46 ~WebPImageEncoder() override{}; | 44 : blob_channel_(blob_channel) { |
45 DCHECK(blob_channel_); | |
46 | |
47 WebPMemoryWriterInit(&writer_state_); | |
48 } | |
49 | |
50 ~WebPImageEncoder() override { WebPMemoryWriterClear(&writer_state_); }; | |
47 | 51 |
48 bool onUseEncodedData(const void* data, size_t len) override { | 52 bool onUseEncodedData(const void* data, size_t len) override { |
49 TRACE_EVENT1("blimp", "WebPImageEncoded::UsingEncodedData", | 53 TRACE_EVENT1("blimp", "WebPImageEncoded::UsingEncodedData", |
50 "OriginalImageSize", len); | 54 "OriginalImageSize", len); |
51 // Encode all images regardless of their format, including WebP images. | 55 // Encode all images regardless of their format, including WebP images. |
52 return false; | 56 return false; |
53 } | 57 } |
54 | 58 |
55 SkData* onEncode(const SkPixmap& pixmap) override { | 59 SkData* onEncode(const SkPixmap& pixmap) override { |
Wez
2016/05/21 01:08:04
This function has various error-handling (invalid
Kevin M
2016/05/27 22:35:31
Done.
| |
56 TRACE_EVENT0("blimp", "WebImageEncoder::onEncode"); | 60 TRACE_EVENT0("blimp", "WebImageEncoder::onEncode"); |
57 // Initialize an empty WebPConfig. | |
58 WebPConfig config; | |
59 if (!WebPConfigInit(&config)) | |
60 return nullptr; | |
61 | |
62 // Initialize an empty WebPPicture. | |
63 WebPPicture picture; | |
64 if (!WebPPictureInit(&picture)) | |
65 return nullptr; | |
66 | 61 |
67 // Ensure width and height are valid dimensions. | 62 // Ensure width and height are valid dimensions. |
68 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) | 63 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) |
69 return nullptr; | 64 return nullptr; |
70 picture.width = pixmap.width(); | |
71 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) | 65 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) |
72 return nullptr; | 66 return nullptr; |
73 picture.height = pixmap.height(); | |
74 | 67 |
68 // Send the image to the client via the BlobChannel if we know that the | |
69 // client doesn't have it. | |
75 const BlobId blob_id = CalculateBlobId(pixmap.addr(), pixmap.getSafeSize()); | 70 const BlobId blob_id = CalculateBlobId(pixmap.addr(), pixmap.getSafeSize()); |
76 std::string blob_id_hex = BlobIdToString(blob_id); | 71 if (!(blob_channel_->GetReplicationState(blob_id) & |
72 BlobChannelSenderHost::ReplicationState::ENGINE)) { | |
Wez
2016/05/21 01:08:05
nit: Would be more readable if you added a simple
Kevin M
2016/05/27 22:35:31
Done.
| |
73 DVLOG(2) << "Encoding image color_type=" << pixmap.colorType() | |
nyquist
2016/05/24 00:05:41
This method mixes low-level bits and bytes with hi
Kevin M
2016/05/27 22:35:31
How's this?
| |
74 << ", alpha_type=" << pixmap.alphaType() << " " << pixmap.width() | |
75 << "x" << pixmap.height(); | |
77 | 76 |
78 // Create proto with all requires information. | 77 // Initialize an empty WebPConfig. |
Wez
2016/05/21 01:08:04
nit: This comment and the init one below don't rea
Kevin M
2016/05/27 22:35:31
Done.
| |
78 WebPConfig config; | |
79 if (!WebPConfigInit(&config)) | |
80 return nullptr; | |
81 | |
82 // Initialize an empty WebPPicture. | |
83 WebPPicture picture; | |
84 if (!WebPPictureInit(&picture)) | |
85 return nullptr; | |
86 picture.width = pixmap.width(); | |
87 picture.height = pixmap.height(); | |
88 | |
89 // Import picture from raw pixels. | |
90 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); | |
91 if (!PlatformPictureImport(pixel_chars, &picture, pixmap.alphaType())) | |
92 return nullptr; | |
93 | |
94 // Set up the writer parameters. | |
95 writer_state_.size = 0; | |
96 picture.custom_ptr = &writer_state_; | |
97 picture.writer = &WebPMemoryWrite; | |
98 | |
99 // Setup the configuration for the output WebP picture. This is currently | |
100 // the same as the default configuration for WebP, but since any change in | |
101 // the WebP defaults would invalidate all caches they are hard coded. | |
102 config.lossless = 0; | |
103 config.quality = 75.0; // between 0 (smallest file) and 100 (biggest). | |
104 | |
105 // TODO(nyquist): Move image encoding to a different thread when | |
106 // asynchronous loading of images is possible. The encode work currently | |
107 // blocks the render thread so we are dropping the method down to 0. | |
108 // crbug.com/603643. | |
109 config.method = 0; // quality/speed trade-off (0=fast, 6=slower-better). | |
110 | |
111 TRACE_EVENT_BEGIN0("blimp", "WebPImageEncoder::onEncode WebPEncode"); | |
112 // Encode the picture using the given configuration. | |
113 bool success = WebPEncode(&config, &picture); | |
114 TRACE_EVENT_END1("blimp", "WebPImageEncoder::onEncode WebPEncode", | |
115 "EncodedImageSize", | |
116 static_cast<int>(writer_state_.size)); | |
117 | |
118 // Release the memory allocated by WebPPictureImport*(). This does not | |
119 // free | |
120 // the memory used by the picture object itself. | |
121 WebPPictureFree(&picture); | |
122 | |
123 if (!success) | |
124 return nullptr; | |
125 | |
126 DVLOG(2) << "Sending image " << BlobIdToString(blob_id) | |
127 << " (size = " << writer_state_.size << ")."; | |
128 | |
129 blob_channel_->Put( | |
130 blob_id, std::string(reinterpret_cast<const char*>(writer_state_.mem), | |
131 writer_state_.size)); | |
132 } | |
133 | |
134 DCHECK(blob_channel_->GetReplicationState(blob_id) & | |
135 BlobChannelSenderHost::ReplicationState::ENGINE); | |
136 | |
137 // Push the blob to the client, if the client doesn't already have it. | |
138 if (!(blob_channel_->GetReplicationState(blob_id) & | |
139 BlobChannelSenderHost::CLIENT)) { | |
140 blob_channel_->Push(blob_id); | |
141 } | |
142 | |
143 // Construct a proto with the image metadata. | |
79 BlobCacheImageMetadata proto; | 144 BlobCacheImageMetadata proto; |
80 proto.set_id(blob_id); | 145 proto.set_id(blob_id); |
81 proto.set_width(pixmap.width()); | 146 proto.set_width(pixmap.width()); |
82 proto.set_height(pixmap.height()); | 147 proto.set_height(pixmap.height()); |
83 | 148 |
84 if (g_client_cache_contents.Get().find(blob_id) != | |
85 g_client_cache_contents.Get().end()) { | |
86 // Found image in client cache, so skip sending decoded payload. | |
87 SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); | |
88 TRACE_EVENT1("blimp", "WebPImageEncoder::onEncode ImageFoundInCache", | |
89 "EncodedImageSize", sk_data->size()); | |
90 DVLOG(2) << "Sending cached: " << blob_id_hex | |
91 << " size = " << sk_data->size(); | |
92 return sk_data; | |
93 } | |
94 | |
95 DVLOG(2) << "Encoding image color_type=" << pixmap.colorType() | |
96 << ", alpha_type=" << pixmap.alphaType() << " " << pixmap.width() | |
97 << "x" << pixmap.height(); | |
98 | |
99 // Import picture from raw pixels. | |
100 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); | |
101 if (!PlatformPictureImport(pixel_chars, &picture, pixmap.alphaType())) | |
102 return nullptr; | |
103 | |
104 // Create a buffer for where to store the output data. | |
105 std::vector<unsigned char> encoded_data; | |
106 picture.custom_ptr = &encoded_data; | |
107 | |
108 // Use our own WebPWriterFunction implementation. | |
109 picture.writer = &WebPImageEncoder::WriteOutput; | |
110 | |
111 // Setup the configuration for the output WebP picture. This is currently | |
112 // the same as the default configuration for WebP, but since any change in | |
113 // the WebP defaults would invalidate all caches they are hard coded. | |
114 config.lossless = 0; | |
115 config.quality = 75.0; // between 0 (smallest file) and 100 (biggest). | |
116 | |
117 // TODO(nyquist): Move image encoding to a different thread when | |
118 // asynchronous loading of images is possible. The encode work currently | |
119 // blocks the render thread so we are dropping the method down to 0. | |
120 // crbug.com/603643. | |
121 config.method = 0; // quality/speed trade-off (0=fast, 6=slower-better). | |
122 | |
123 TRACE_EVENT_BEGIN0("blimp", "WebPImageEncoder::onEncode WebPEncode"); | |
124 // Encode the picture using the given configuration. | |
125 bool success = WebPEncode(&config, &picture); | |
126 TRACE_EVENT_END1("blimp", "WebPImageEncoder::onEncode WebPEncode", | |
127 "EncodedImageSize", encoded_data.size()); | |
128 | |
129 // Release the memory allocated by WebPPictureImport*(). This does not free | |
130 // the memory used by the picture object itself. | |
131 WebPPictureFree(&picture); | |
132 | |
133 if (!success) | |
134 return nullptr; | |
135 | |
136 // Did not find item in cache, so add it to client cache representation | |
137 // and send full item. | |
138 g_client_cache_contents.Get().insert(blob_id); | |
139 proto.set_payload(&encoded_data.front(), encoded_data.size()); | |
140 | |
141 // Copy proto into SkData. | |
142 SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); | 149 SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); |
143 DVLOG(2) << "Sending image: " << blob_id_hex | 150 DVLOG(3) << "Returning image ID " << BlobIdToString(blob_id); |
144 << " size = " << sk_data->size(); | |
145 return sk_data; | 151 return sk_data; |
146 } | 152 } |
147 | 153 |
148 private: | 154 private: |
149 // WebPWriterFunction implementation. | 155 // WebPWriterFunction implementation. |
150 static int WriteOutput(const uint8_t* data, | 156 static int WriteOutput(const uint8_t* data, |
151 size_t size, | 157 size_t size, |
152 const WebPPicture* const picture) { | 158 const WebPPicture* const picture) { |
153 std::vector<unsigned char>* dest = | 159 std::vector<unsigned char>* dest = |
154 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); | 160 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 if (SK_B32_SHIFT) // Android | 201 if (SK_B32_SHIFT) // Android |
196 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), | 202 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), |
197 row_stride); | 203 row_stride); |
198 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); | 204 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); |
199 } | 205 } |
200 | 206 |
201 if (SK_B32_SHIFT) // Android | 207 if (SK_B32_SHIFT) // Android |
202 return WebPPictureImportRGBA(picture, pixels, row_stride); | 208 return WebPPictureImportRGBA(picture, pixels, row_stride); |
203 return WebPPictureImportBGRA(picture, pixels, row_stride); | 209 return WebPPictureImportBGRA(picture, pixels, row_stride); |
204 } | 210 } |
211 | |
212 // Reusable output buffer for image encoding. | |
213 WebPMemoryWriter writer_state_; | |
214 | |
215 BlobChannelSenderHost* blob_channel_; | |
Wez
2016/05/21 01:08:04
nit: Brief comment to clarify purpose & ownership.
Kevin M
2016/05/27 22:35:31
Done.
| |
216 | |
217 DISALLOW_COPY_AND_ASSIGN(WebPImageEncoder); | |
205 }; | 218 }; |
206 | 219 |
207 } // namespace | 220 } // namespace |
208 | 221 |
209 namespace engine { | |
210 | |
211 EngineImageSerializationProcessor::EngineImageSerializationProcessor( | 222 EngineImageSerializationProcessor::EngineImageSerializationProcessor( |
212 mojom::BlobChannelPtr blob_channel) | 223 std::unique_ptr<BlobChannelSenderHost> blob_channel) |
213 : blob_channel_(std::move(blob_channel)) { | 224 : blob_channel_(std::move(blob_channel)) { |
214 DCHECK(blob_channel_); | 225 DCHECK(blob_channel_); |
215 | 226 |
216 pixel_serializer_.reset(new WebPImageEncoder); | 227 pixel_serializer_.reset(new WebPImageEncoder(blob_channel_.get())); |
Wez
2016/05/21 01:08:05
nit: Is there a reason why the EngineImageSerializ
Kevin M
2016/05/27 22:35:31
Done.
| |
217 | |
218 // Dummy BlobChannel command. | |
219 // TODO(nyquist): Remove this after integrating BlobChannel. | |
220 blob_channel_->Push("foo"); | |
221 } | 228 } |
222 | 229 |
223 EngineImageSerializationProcessor::~EngineImageSerializationProcessor() {} | 230 EngineImageSerializationProcessor::~EngineImageSerializationProcessor() {} |
224 | 231 |
225 SkPixelSerializer* EngineImageSerializationProcessor::GetPixelSerializer() { | 232 SkPixelSerializer* EngineImageSerializationProcessor::GetPixelSerializer() { |
226 return pixel_serializer_.get(); | 233 return pixel_serializer_.get(); |
227 } | 234 } |
228 | 235 |
229 SkPicture::InstallPixelRefProc | 236 SkPicture::InstallPixelRefProc |
230 EngineImageSerializationProcessor::GetPixelDeserializer() { | 237 EngineImageSerializationProcessor::GetPixelDeserializer() { |
231 return nullptr; | 238 return nullptr; |
232 } | 239 } |
233 | 240 |
234 } // namespace engine | 241 } // namespace engine |
235 } // namespace blimp | 242 } // namespace blimp |
OLD | NEW |