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_proxy.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 // For each pixel, un-premultiplies the alpha-channel for each of the RGB |
42 // See crbug.com/548434. | 40 // channels. As an example, for a channel value that before multiplication was |
43 class WebPImageEncoder : public SkPixelSerializer { | 41 // 255, and after applying an alpha of 128, the premultiplied pixel would be |
44 public: | 42 // 128. The un-premultiply step uses the alpha-channel to get back to 255. The |
45 WebPImageEncoder() {} | 43 // alpha channel is kept unchanged. |
46 ~WebPImageEncoder() override{}; | 44 void UnPremultiply(const unsigned char* in_pixels, |
47 | 45 unsigned char* out_pixels, |
48 bool onUseEncodedData(const void* data, size_t len) override { | 46 size_t pixel_count) { |
49 TRACE_EVENT1("blimp", "WebPImageEncoded::UsingEncodedData", | 47 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
50 "OriginalImageSize", len); | 48 for (; pixel_count-- > 0; in_pixels += 4) { |
51 // Encode all images regardless of their format, including WebP images. | 49 unsigned char alpha = in_pixels[3]; |
52 return false; | 50 if (alpha == 255) { // Full opacity, just blindly copy. |
| 51 *out_pixels++ = in_pixels[0]; |
| 52 *out_pixels++ = in_pixels[1]; |
| 53 *out_pixels++ = in_pixels[2]; |
| 54 *out_pixels++ = alpha; |
| 55 } else { |
| 56 SkUnPreMultiply::Scale scale = table[alpha]; |
| 57 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[0]); |
| 58 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[1]); |
| 59 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[2]); |
| 60 *out_pixels++ = alpha; |
| 61 } |
53 } | 62 } |
54 | |
55 SkData* onEncode(const SkPixmap& pixmap) override { | |
56 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 | |
67 // Ensure width and height are valid dimensions. | |
68 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) | |
69 return nullptr; | |
70 picture.width = pixmap.width(); | |
71 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) | |
72 return nullptr; | |
73 picture.height = pixmap.height(); | |
74 | |
75 const BlobId blob_id = CalculateBlobId(pixmap.addr(), pixmap.getSafeSize()); | |
76 std::string blob_id_hex = BlobIdToString(blob_id); | |
77 | |
78 // Create proto with all requires information. | |
79 BlobCacheImageMetadata proto; | |
80 proto.set_id(blob_id); | |
81 proto.set_width(pixmap.width()); | |
82 proto.set_height(pixmap.height()); | |
83 | |
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); | |
143 DVLOG(2) << "Sending image: " << blob_id_hex | |
144 << " size = " << sk_data->size(); | |
145 return sk_data; | |
146 } | |
147 | |
148 private: | |
149 // WebPWriterFunction implementation. | |
150 static int WriteOutput(const uint8_t* data, | |
151 size_t size, | |
152 const WebPPicture* const picture) { | |
153 std::vector<unsigned char>* dest = | |
154 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); | |
155 dest->insert(dest->end(), data, data + size); | |
156 return 1; | |
157 } | |
158 | |
159 // For each pixel, un-premultiplies the alpha-channel for each of the RGB | |
160 // channels. As an example, for a channel value that before multiplication was | |
161 // 255, and after applying an alpha of 128, the premultiplied pixel would be | |
162 // 128. The un-premultiply step uses the alpha-channel to get back to 255. The | |
163 // alpha channel is kept unchanged. | |
164 void UnPremultiply(const unsigned char* in_pixels, | |
165 unsigned char* out_pixels, | |
166 size_t pixel_count) { | |
167 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); | |
168 for (; pixel_count-- > 0; in_pixels += 4) { | |
169 unsigned char alpha = in_pixels[3]; | |
170 if (alpha == 255) { // Full opacity, just blindly copy. | |
171 *out_pixels++ = in_pixels[0]; | |
172 *out_pixels++ = in_pixels[1]; | |
173 *out_pixels++ = in_pixels[2]; | |
174 *out_pixels++ = alpha; | |
175 } else { | |
176 SkUnPreMultiply::Scale scale = table[alpha]; | |
177 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[0]); | |
178 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[1]); | |
179 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[2]); | |
180 *out_pixels++ = alpha; | |
181 } | |
182 } | |
183 } | 63 } |
184 | 64 |
185 bool PlatformPictureImport(const unsigned char* pixels, | 65 bool PlatformPictureImport(const unsigned char* pixels, |
186 WebPPicture* picture, | 66 WebPPicture* picture, |
187 SkAlphaType alphaType) { | 67 SkAlphaType alphaType) { |
188 // Each pixel uses 4 bytes (RGBA) which affects the stride per row. | 68 // Each pixel uses 4 bytes (RGBA) which affects the stride per row. |
189 int row_stride = picture->width * 4; | 69 int row_stride = picture->width * 4; |
190 if (alphaType == kPremul_SkAlphaType) { | 70 if (alphaType == kPremul_SkAlphaType) { |
191 // Need to unpremultiply each pixel, each pixel using 4 bytes (RGBA). | 71 // Need to unpremultiply each pixel, each pixel using 4 bytes (RGBA). |
192 size_t pixel_count = picture->height * picture->width; | 72 size_t pixel_count = picture->height * picture->width; |
193 std::vector<unsigned char> unpremul_pixels(pixel_count * 4); | 73 std::vector<unsigned char> unpremul_pixels(pixel_count * 4); |
194 UnPremultiply(pixels, unpremul_pixels.data(), pixel_count); | 74 UnPremultiply(pixels, unpremul_pixels.data(), pixel_count); |
195 if (SK_B32_SHIFT) // Android | 75 if (SK_B32_SHIFT) // Android |
196 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), | 76 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), |
197 row_stride); | 77 row_stride); |
198 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); | 78 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); |
199 } | 79 } |
200 | 80 |
201 if (SK_B32_SHIFT) // Android | 81 if (SK_B32_SHIFT) // Android |
202 return WebPPictureImportRGBA(picture, pixels, row_stride); | 82 return WebPPictureImportRGBA(picture, pixels, row_stride); |
203 return WebPPictureImportBGRA(picture, pixels, row_stride); | 83 return WebPPictureImportBGRA(picture, pixels, row_stride); |
204 } | 84 } |
205 }; | |
206 | 85 |
207 } // namespace | 86 } // namespace |
208 | 87 |
209 namespace engine { | 88 EngineImageSerializationProcessor::EngineImageSerializationProcessor( |
210 | 89 std::unique_ptr<BlobChannelSenderProxy> blob_channel) |
211 EngineImageSerializationProcessor::EngineImageSerializationProcessor( | 90 : blob_channel_(std::move(blob_channel)) { |
212 mojom::BlobChannelPtr blob_channel) | 91 DCHECK(blob_channel_); |
213 : blob_channel_(std::move(blob_channel)) { | 92 WebPMemoryWriterInit(&writer_state_); |
214 DCHECK(blob_channel_); | |
215 | |
216 pixel_serializer_.reset(new WebPImageEncoder); | |
217 | |
218 // Dummy BlobChannel command. | |
219 // TODO(nyquist): Remove this after integrating BlobChannel. | |
220 blob_channel_->Push("foo"); | |
221 } | 93 } |
222 | 94 |
223 EngineImageSerializationProcessor::~EngineImageSerializationProcessor() {} | 95 EngineImageSerializationProcessor::~EngineImageSerializationProcessor() { |
| 96 WebPMemoryWriterClear(&writer_state_); |
| 97 } |
224 | 98 |
225 SkPixelSerializer* EngineImageSerializationProcessor::GetPixelSerializer() { | 99 SkPixelSerializer* EngineImageSerializationProcessor::GetPixelSerializer() { |
226 return pixel_serializer_.get(); | 100 return this; |
227 } | 101 } |
228 | 102 |
229 SkPicture::InstallPixelRefProc | 103 SkPicture::InstallPixelRefProc |
230 EngineImageSerializationProcessor::GetPixelDeserializer() { | 104 EngineImageSerializationProcessor::GetPixelDeserializer() { |
231 return nullptr; | 105 return nullptr; |
232 } | 106 } |
233 | 107 |
| 108 bool EngineImageSerializationProcessor::onUseEncodedData(const void* data, |
| 109 size_t len) { |
| 110 TRACE_EVENT1("blimp", "WebPImageEncoded::UsingEncodedData", |
| 111 "OriginalImageSize", len); |
| 112 // Encode all images regardless of their format, including WebP images. |
| 113 return false; |
| 114 } |
| 115 |
| 116 SkData* EngineImageSerializationProcessor::onEncode(const SkPixmap& pixmap) { |
| 117 TRACE_EVENT0("blimp", "WebImageEncoder::onEncode"); |
| 118 |
| 119 // Ensure width and height are valid dimensions. |
| 120 DCHECK_GT(pixmap.width(), 0); |
| 121 DCHECK_LE(pixmap.width(), WEBP_MAX_DIMENSION); |
| 122 DCHECK_GT(pixmap.height(), 0); |
| 123 DCHECK_LE(pixmap.height(), WEBP_MAX_DIMENSION); |
| 124 |
| 125 // Send the image to the client via the BlobChannel if we know that the |
| 126 // client doesn't have it. |
| 127 const BlobId blob_id = CalculateBlobId(pixmap.addr(), pixmap.getSafeSize()); |
| 128 if (!blob_channel_->IsInEngineCache(blob_id)) { |
| 129 blob_channel_->PutBlob(blob_id, EncodeImageAsBlob(pixmap)); |
| 130 DCHECK(blob_channel_->IsInEngineCache(blob_id)); |
| 131 } |
| 132 |
| 133 // Push the blob to the client, if the client doesn't already have it. |
| 134 if (!blob_channel_->IsInClientCache(blob_id)) { |
| 135 blob_channel_->DeliverBlob(blob_id); |
| 136 } |
| 137 |
| 138 // Construct a proto with the image metadata. |
| 139 BlobCacheImageMetadata proto; |
| 140 proto.set_id(blob_id); |
| 141 proto.set_width(pixmap.width()); |
| 142 proto.set_height(pixmap.height()); |
| 143 |
| 144 SkData* sk_data = BlobCacheImageMetadataProtoAsSkData(proto); |
| 145 DVLOG(3) << "Returning image ID " << BlobIdToString(blob_id); |
| 146 return sk_data; |
| 147 } |
| 148 |
| 149 scoped_refptr<BlobData> EngineImageSerializationProcessor::EncodeImageAsBlob( |
| 150 const SkPixmap& pixmap) { |
| 151 DVLOG(2) << "Encoding image color_type=" << pixmap.colorType() |
| 152 << ", alpha_type=" << pixmap.alphaType() << " " << pixmap.width() |
| 153 << "x" << pixmap.height(); |
| 154 |
| 155 WebPConfig config; |
| 156 CHECK(WebPConfigInit(&config)); |
| 157 |
| 158 WebPPicture picture; |
| 159 CHECK(WebPPictureInit(&picture)); |
| 160 picture.width = pixmap.width(); |
| 161 picture.height = pixmap.height(); |
| 162 |
| 163 // Import picture from raw pixels. |
| 164 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); |
| 165 CHECK(PlatformPictureImport(pixel_chars, &picture, pixmap.alphaType())); |
| 166 |
| 167 // Set up the writer parameters. |
| 168 writer_state_.size = 0; |
| 169 picture.custom_ptr = &writer_state_; |
| 170 picture.writer = &WebPMemoryWrite; |
| 171 |
| 172 // Setup the configuration for the output WebP picture. This is currently |
| 173 // the same as the default configuration for WebP, but since any change in |
| 174 // the WebP defaults would invalidate all caches they are hard coded. |
| 175 config.lossless = 0; |
| 176 config.quality = 75.0; // between 0 (smallest file) and 100 (biggest). |
| 177 |
| 178 // TODO(nyquist): Move image encoding to a different thread when |
| 179 // asynchronous loading of images is possible. The encode work currently |
| 180 // blocks the render thread so we are dropping the method down to 0. |
| 181 // crbug.com/603643. |
| 182 config.method = 0; // quality/speed trade-off (0=fast, 6=slower-better). |
| 183 |
| 184 TRACE_EVENT_BEGIN0("blimp", "WebPImageEncoder::onEncode WebPEncode"); |
| 185 // Encode the picture using the given configuration. |
| 186 bool success = WebPEncode(&config, &picture); |
| 187 TRACE_EVENT_END1("blimp", "WebPImageEncoder::onEncode WebPEncode", |
| 188 "EncodedImageSize", static_cast<int>(writer_state_.size)); |
| 189 |
| 190 // Release the memory allocated by WebPPictureImport*(). This does not |
| 191 // free the memory used by the picture object itself. |
| 192 WebPPictureFree(&picture); |
| 193 |
| 194 if (!success) { |
| 195 LOG(FATAL) << "WebPPictureEncode() failed."; |
| 196 return nullptr; |
| 197 } |
| 198 |
| 199 scoped_refptr<BlobData> blob_data(new BlobData); |
| 200 blob_data->data.assign(reinterpret_cast<const char*>(writer_state_.mem), |
| 201 writer_state_.size); |
| 202 return blob_data; |
| 203 } |
| 204 |
234 } // namespace engine | 205 } // namespace engine |
235 } // namespace blimp | 206 } // namespace blimp |
OLD | NEW |