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/common/compositor/webp_decoder.h" | 5 #include "blimp/common/compositor/webp_decoder.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/numerics/safe_conversions.h" | |
10 #include "base/sha1.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "blimp/common/blob_cache/blob_cache.h" | |
14 #include "blimp/common/blob_cache/infinite_blob_cache.h" | |
15 #include "blimp/common/blob_cache/sha1_util.h" | |
16 #include "blimp/common/proto/blob_cache.pb.h" | |
8 #include "third_party/libwebp/webp/decode.h" | 17 #include "third_party/libwebp/webp/decode.h" |
9 #include "third_party/libwebp/webp/demux.h" | 18 #include "third_party/libwebp/webp/demux.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
11 | 20 |
12 namespace blimp { | 21 namespace blimp { |
22 namespace { | |
23 | |
24 // TODO(nyquist): Make this not be infinite size. | |
25 static base::LazyInstance<InfiniteBlobCache> g_blob_cache = | |
26 LAZY_INSTANCE_INITIALIZER; | |
27 | |
28 } // namespace | |
13 | 29 |
14 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | 30 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
15 DCHECK(bitmap); | 31 DCHECK(bitmap); |
16 | 32 |
17 // Initialize an empty WebPDecoderConfig. | 33 // Initialize an empty WebPDecoderConfig. |
18 WebPDecoderConfig config; | 34 WebPDecoderConfig config; |
19 if (!WebPInitDecoderConfig(&config)) { | 35 if (!WebPInitDecoderConfig(&config)) { |
20 LOG(WARNING) << "Failed to initialize WebP config."; | 36 LOG(WARNING) << "Failed to initialize WebP config."; |
21 return false; | 37 return false; |
22 } | 38 } |
23 | 39 |
24 // Treat the input as uint8_t. | 40 std::unique_ptr<BlobCacheImage> deserialized(new BlobCacheImage); |
25 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; | 41 int signed_size = base::checked_cast<int>(input_size); |
42 if (!deserialized->ParseFromArray(input, signed_size)) { | |
43 LOG(WARNING) << "Failed to parse BlobCacheImage data"; | |
44 return false; | |
45 } | |
46 | |
47 // Used later to decode the image and is initialized either based on a cached | |
48 // item or from the |payload| of the proto. | |
49 WebPData webp_data; | |
50 | |
51 // Declared here to still be in scope while decoding WebP data. | |
52 scoped_refptr<RefCountedVector> cached(new RefCountedVector); | |
53 | |
54 // The cache uses the SHA1 hex string of the ID. | |
55 DCHECK(deserialized->id().length() == base::kSHA1Length); | |
Kevin M
2016/04/14 20:08:30
DCHECK_EQ
nyquist
2016/04/15 01:27:12
Done.
| |
56 std::string hex_id = blimp::ToSHA1HexString(deserialized->id().c_str()); | |
Kevin M
2016/04/14 20:08:30
Why are we using a different encoding of |id| for
nyquist
2016/04/15 01:27:12
Done.
| |
57 | |
58 bool found_in_cache = false; | |
59 if (g_blob_cache.Get().Contains(hex_id)) { | |
60 // The image was found in the cache, so decode using cached data. | |
61 cached = g_blob_cache.Get().Get(hex_id); | |
62 webp_data.bytes = cached->data.data(); | |
63 webp_data.size = cached->data.size(); | |
64 DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size; | |
65 found_in_cache = true; | |
66 } else { | |
67 // The image was not found in the cache, so decode using the payload. | |
68 DCHECK(deserialized->has_payload()); | |
69 webp_data.bytes = | |
70 reinterpret_cast<const uint8_t*>(deserialized->payload().c_str()); | |
71 webp_data.size = deserialized->payload().size(); | |
72 } | |
26 | 73 |
27 // Read WebP feature information into |config.input|, which is a | 74 // Read WebP feature information into |config.input|, which is a |
28 // WebPBitstreamFeatures. It contains information such as width, height and | 75 // WebPBitstreamFeatures. It contains information such as width, height and |
29 // whether the WebP image has an alpha channel or not. | 76 // whether the WebP image has an alpha channel or not. |
30 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { | 77 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != |
78 VP8_STATUS_OK) { | |
31 LOG(WARNING) << "Failed to get WebP features."; | 79 LOG(WARNING) << "Failed to get WebP features."; |
32 return false; | 80 return false; |
33 } | 81 } |
34 // Animations are not supported. | 82 // Animations are not supported. |
35 DCHECK_EQ(0, config.input.has_animation); | 83 DCHECK_EQ(0, config.input.has_animation); |
36 | 84 |
37 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. | 85 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. |
38 bitmap->allocN32Pixels(config.input.width, config.input.height); | 86 bitmap->allocN32Pixels(config.input.width, config.input.height); |
39 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); | 87 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); |
40 | 88 |
(...skipping 11 matching lines...) Expand all Loading... | |
52 // Instead of using the default WebPDecBuffer output, make WebPDecode directly | 100 // Instead of using the default WebPDecBuffer output, make WebPDecode directly |
53 // write into the SkBitmap. | 101 // write into the SkBitmap. |
54 decoderBuffer.is_external_memory = 1; | 102 decoderBuffer.is_external_memory = 1; |
55 decoderBuffer.u.RGBA.rgba = | 103 decoderBuffer.u.RGBA.rgba = |
56 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | 104 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); |
57 | 105 |
58 // Set the config up to use the decoding buffer we created. | 106 // Set the config up to use the decoding buffer we created. |
59 config.output = decoderBuffer; | 107 config.output = decoderBuffer; |
60 | 108 |
61 // Decode the input data into the bitmap buffer. | 109 // Decode the input data into the bitmap buffer. |
62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; | 110 bool success = |
111 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK; | |
63 | 112 |
64 // Now free the buffer. It is safe to call this even when the buffer is | 113 // Now free the buffer. It is safe to call this even when the buffer is |
65 // external and not allocated by WebPDecode. | 114 // external and not allocated by WebPDecode. |
66 WebPFreeDecBuffer(&config.output); | 115 WebPFreeDecBuffer(&config.output); |
67 | 116 |
68 if (!success) { | 117 if (!success) { |
69 LOG(WARNING) << "Failed to decode WebP data."; | 118 LOG(WARNING) << "Failed to decode WebP data."; |
70 return false; | 119 return false; |
71 } | 120 } |
121 | |
122 if (!found_in_cache) { | |
123 DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id | |
124 << " size: " << webp_data.size; | |
125 scoped_refptr<RefCountedVector> to_cache(new RefCountedVector); | |
126 to_cache->data.assign(webp_data.bytes, webp_data.bytes + webp_data.size); | |
127 g_blob_cache.Get().Put(hex_id, std::move(to_cache)); | |
128 } | |
129 | |
72 return true; | 130 return true; |
73 } | 131 } |
74 | 132 |
75 } // namespace blimp | 133 } // namespace blimp |
OLD | NEW |