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/id_util.h" |
| 15 #include "blimp/common/blob_cache/in_memory_blob_cache.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<InMemoryBlobCache> 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 BlobCacheImageMetadata deserialized; |
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 BlobCacheImageMetadata"; |
| 44 return false; |
| 45 } |
| 46 |
| 47 // The cache uses the SHA1 hex string of the ID. |
| 48 if (deserialized.id().length() != base::kSHA1Length) { |
| 49 LOG(WARNING) << "Length of ID is not base::kSHA1Length (" |
| 50 << base::kSHA1Length << "), but " |
| 51 << deserialized.id().length(); |
| 52 return false; |
| 53 } |
| 54 std::string hex_id = FormatBlobId(deserialized.id()); |
| 55 |
| 56 // Declared here to still be in scope while decoding WebP data. |
| 57 BlobData cached; |
| 58 |
| 59 // Set to true if the client already has the data in its cache. If it does not |
| 60 // keeping |found_in_cache| as false will trigger caching the input in the |
| 61 // end of this function. |
| 62 bool found_in_cache = false; |
| 63 |
| 64 // Used later to decode the image and is initialized either based on a cached |
| 65 // item or from the |payload| of the proto. |
| 66 WebPData webp_data; |
| 67 |
| 68 if (g_blob_cache.Get().Contains(deserialized.id())) { |
| 69 // The image was found in the cache, so decode using cached data. |
| 70 cached = g_blob_cache.Get().Get(deserialized.id()); |
| 71 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data()); |
| 72 webp_data.size = cached->data.size(); |
| 73 DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size; |
| 74 found_in_cache = true; |
| 75 } else { |
| 76 // The image was not found in the cache, so decode using the payload. |
| 77 DCHECK(deserialized.has_payload()); |
| 78 webp_data.bytes = |
| 79 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str()); |
| 80 webp_data.size = deserialized.payload().size(); |
| 81 } |
26 | 82 |
27 // Read WebP feature information into |config.input|, which is a | 83 // Read WebP feature information into |config.input|, which is a |
28 // WebPBitstreamFeatures. It contains information such as width, height and | 84 // WebPBitstreamFeatures. It contains information such as width, height and |
29 // whether the WebP image has an alpha channel or not. | 85 // whether the WebP image has an alpha channel or not. |
30 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { | 86 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != |
| 87 VP8_STATUS_OK) { |
31 LOG(WARNING) << "Failed to get WebP features."; | 88 LOG(WARNING) << "Failed to get WebP features."; |
32 return false; | 89 return false; |
33 } | 90 } |
34 // Animations are not supported. | 91 // Animations are not supported. |
35 DCHECK_EQ(0, config.input.has_animation); | 92 DCHECK_EQ(0, config.input.has_animation); |
36 | 93 |
37 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. | 94 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. |
38 bitmap->allocN32Pixels(config.input.width, config.input.height); | 95 bitmap->allocN32Pixels(config.input.width, config.input.height); |
39 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); | 96 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); |
40 | 97 |
(...skipping 11 matching lines...) Expand all Loading... |
52 // Instead of using the default WebPDecBuffer output, make WebPDecode directly | 109 // Instead of using the default WebPDecBuffer output, make WebPDecode directly |
53 // write into the SkBitmap. | 110 // write into the SkBitmap. |
54 decoderBuffer.is_external_memory = 1; | 111 decoderBuffer.is_external_memory = 1; |
55 decoderBuffer.u.RGBA.rgba = | 112 decoderBuffer.u.RGBA.rgba = |
56 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | 113 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); |
57 | 114 |
58 // Set the config up to use the decoding buffer we created. | 115 // Set the config up to use the decoding buffer we created. |
59 config.output = decoderBuffer; | 116 config.output = decoderBuffer; |
60 | 117 |
61 // Decode the input data into the bitmap buffer. | 118 // Decode the input data into the bitmap buffer. |
62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; | 119 bool success = |
| 120 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK; |
63 | 121 |
64 // Now free the buffer. It is safe to call this even when the buffer is | 122 // Now free the buffer. It is safe to call this even when the buffer is |
65 // external and not allocated by WebPDecode. | 123 // external and not allocated by WebPDecode. |
66 WebPFreeDecBuffer(&config.output); | 124 WebPFreeDecBuffer(&config.output); |
67 | 125 |
68 if (!success) { | 126 if (!success) { |
69 LOG(WARNING) << "Failed to decode WebP data."; | 127 LOG(WARNING) << "Failed to decode WebP data."; |
70 return false; | 128 return false; |
71 } | 129 } |
| 130 |
| 131 if (!found_in_cache) { |
| 132 DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id |
| 133 << " size: " << webp_data.size; |
| 134 BlobData to_cache(new base::RefCountedData<const std::string>(std::string( |
| 135 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size))); |
| 136 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache)); |
| 137 } |
| 138 |
72 return true; | 139 return true; |
73 } | 140 } |
74 | 141 |
75 } // namespace blimp | 142 } // namespace blimp |
OLD | NEW |