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> | |
9 #include <string> | |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/sha1.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_util.h" | |
11 #include "blimp/common/compositor/webp_decoder.h" | 16 #include "blimp/common/compositor/webp_decoder.h" |
17 #include "blimp/common/proto/blob_cache.pb.h" | |
12 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
13 #include "third_party/libwebp/webp/encode.h" | 19 #include "third_party/libwebp/webp/encode.h" |
14 #include "third_party/skia/include/core/SkData.h" | 20 #include "third_party/skia/include/core/SkData.h" |
15 #include "third_party/skia/include/core/SkPicture.h" | 21 #include "third_party/skia/include/core/SkPicture.h" |
16 #include "third_party/skia/include/core/SkPixelSerializer.h" | 22 #include "third_party/skia/include/core/SkPixelSerializer.h" |
17 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 23 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
18 | 24 |
19 namespace { | 25 namespace { |
26 | |
27 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.
| |
28 unsigned char sha1_hash[base::kSHA1Length]; | |
29 base::SHA1HashBytes(static_cast<const unsigned char*>(data), data_size, | |
30 sha1_hash); | |
31 return base::ToLowerASCII(base::HexEncode(sha1_hash, base::kSHA1Length)); | |
32 } | |
33 | |
34 // TODO(nyquist): Add support for changing this from the client. | |
35 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.
| |
36 | |
20 // TODO(nyquist): Make sure encoder does not serialize images more than once. | 37 // TODO(nyquist): Make sure encoder does not serialize images more than once. |
21 // See crbug.com/548434. | 38 // See crbug.com/548434. |
22 class WebPImageEncoder : public SkPixelSerializer { | 39 class WebPImageEncoder : public SkPixelSerializer { |
23 public: | 40 public: |
24 WebPImageEncoder() {} | 41 WebPImageEncoder() {} |
25 ~WebPImageEncoder() override{}; | 42 ~WebPImageEncoder() override{}; |
26 | 43 |
27 bool onUseEncodedData(const void* data, size_t len) override { | 44 bool onUseEncodedData(const void* data, size_t len) override { |
28 const unsigned char* cast_data = static_cast<const unsigned char*>(data); | 45 // Always encode even WebP data, to enable caching. |
29 if (len < 14) | 46 return false; |
30 return false; | |
31 return !memcmp(cast_data, "RIFF", 4) && !memcmp(cast_data + 8, "WEBPVP", 6); | |
32 } | 47 } |
33 | 48 |
34 SkData* onEncode(const SkPixmap& pixmap) override { | 49 SkData* onEncode(const SkPixmap& pixmap) override { |
35 // Initialize an empty WebPConfig. | 50 // Initialize an empty WebPConfig. |
36 WebPConfig config; | 51 WebPConfig config; |
37 if (!WebPConfigInit(&config)) | 52 if (!WebPConfigInit(&config)) |
38 return nullptr; | 53 return nullptr; |
39 | 54 |
40 // Initialize an empty WebPPicture. | 55 // Initialize an empty WebPPicture. |
41 WebPPicture picture; | 56 WebPPicture picture; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 // Encode the picture using the given configuration. | 91 // Encode the picture using the given configuration. |
77 bool success = WebPEncode(&config, &picture); | 92 bool success = WebPEncode(&config, &picture); |
78 | 93 |
79 // Release the memory allocated by WebPPictureImport*(). This does not free | 94 // Release the memory allocated by WebPPictureImport*(). This does not free |
80 // the memory used by the picture object itself. | 95 // the memory used by the picture object itself. |
81 WebPPictureFree(&picture); | 96 WebPPictureFree(&picture); |
82 | 97 |
83 if (!success) | 98 if (!success) |
84 return nullptr; | 99 return nullptr; |
85 | 100 |
86 // Copy WebP data into SkData. |data| is allocated only on the stack, so | 101 std::string sha1 = ToSHA1HexString(&data.front(), data.size()); |
87 // it is automatically deleted after this. | 102 if (g_client_cache_contents.find(sha1) == g_client_cache_contents.end()) { |
88 return SkData::NewWithCopy(&data.front(), data.size()); | 103 g_client_cache_contents.insert(sha1); |
104 // 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.
| |
105 // it is automatically deleted after this. | |
106 VLOG(2) << "Sending image: " << sha1 << " size = " << data.size(); | |
107 return SkData::NewWithCopy(&data.front(), data.size()); | |
108 } else { | |
Kevin M
2016/04/13 23:29:53
Add comment about cache hit
nyquist
2016/04/14 19:33:23
Done.
| |
109 std::unique_ptr<blimp::BlobCacheImageIdentifier> proto( | |
110 new blimp::BlobCacheImageIdentifier); | |
111 proto->set_id(sha1); | |
112 proto->set_width(picture.width); | |
113 proto->set_height(picture.height); | |
114 int signed_size = proto->ByteSize(); | |
115 size_t unsigned_size = base::checked_cast<size_t>(signed_size); | |
116 std::vector<uint8_t> serialized(unsigned_size); | |
117 proto->SerializeToArray(serialized.data(), signed_size); | |
118 // 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.
| |
119 VLOG(2) << "Sending cached: " << sha1 << " size = " << serialized.size(); | |
120 return SkData::NewWithCopy(serialized.data(), serialized.size()); | |
121 } | |
89 } | 122 } |
90 | 123 |
91 private: | 124 private: |
92 // WebPWriterFunction implementation. | 125 // WebPWriterFunction implementation. |
93 static int WriteOutput(const uint8_t* data, | 126 static int WriteOutput(const uint8_t* data, |
94 size_t size, | 127 size_t size, |
95 const WebPPicture* const picture) { | 128 const WebPPicture* const picture) { |
96 std::vector<unsigned char>* dest = | 129 std::vector<unsigned char>* dest = |
97 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); | 130 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); |
98 dest->insert(dest->end(), data, data + size); | 131 dest->insert(dest->end(), data, data + size); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 return pixel_serializer_.get(); | 203 return pixel_serializer_.get(); |
171 } | 204 } |
172 | 205 |
173 SkPicture::InstallPixelRefProc | 206 SkPicture::InstallPixelRefProc |
174 EngineImageSerializationProcessor::GetPixelDeserializer() { | 207 EngineImageSerializationProcessor::GetPixelDeserializer() { |
175 return nullptr; | 208 return nullptr; |
176 } | 209 } |
177 | 210 |
178 } // namespace engine | 211 } // namespace engine |
179 } // namespace blimp | 212 } // namespace blimp |
OLD | NEW |