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/client/feature/compositor/blimp_image_decoder.h" | 5 #include "blimp/client/feature/compositor/blimp_image_decoder.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "blimp/common/blob_cache/blob_cache.h" | 11 #include "blimp/common/blob_cache/blob_cache.h" |
12 #include "blimp/common/blob_cache/id_util.h" | 12 #include "blimp/common/blob_cache/id_util.h" |
13 #include "blimp/common/blob_cache/in_memory_blob_cache.h" | 13 #include "blimp/common/blob_cache/in_memory_blob_cache.h" |
14 #include "blimp/common/proto/blob_cache.pb.h" | 14 #include "blimp/common/proto/blob_cache.pb.h" |
15 #include "third_party/libwebp/webp/decode.h" | 15 #include "third_party/libwebp/webp/decode.h" |
16 #include "third_party/libwebp/webp/demux.h" | 16 #include "third_party/libwebp/webp/demux.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
18 | 18 |
19 namespace blimp { | 19 namespace blimp { |
20 namespace { | |
21 | |
22 // TODO(nyquist): Make this not be infinite size. | |
23 static base::LazyInstance<InMemoryBlobCache> g_blob_cache = | |
24 LAZY_INSTANCE_INITIALIZER; | |
25 | |
26 } // namespace | |
27 | |
28 namespace client { | 20 namespace client { |
29 | 21 |
30 bool BlimpImageDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | 22 bool BlimpImageDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
31 DCHECK(bitmap); | 23 DCHECK(bitmap); |
32 | 24 |
33 // Initialize an empty WebPDecoderConfig. | 25 // Initialize an empty WebPDecoderConfig. |
34 WebPDecoderConfig config; | 26 WebPDecoderConfig config; |
35 if (!WebPInitDecoderConfig(&config)) { | 27 if (!WebPInitDecoderConfig(&config)) { |
36 LOG(WARNING) << "Failed to initialize WebP config."; | 28 LOG(WARNING) << "Failed to initialize WebP config."; |
37 return false; | 29 return false; |
38 } | 30 } |
39 | 31 |
40 BlobCacheImageMetadata deserialized; | |
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 if (!IsValidBlobId(BlobId(deserialized.id()))) { | |
48 LOG(WARNING) << "Length of ID is not correct " | |
49 << deserialized.id().length(); | |
50 return false; | |
51 } | |
52 std::string hex_id = BlobIdToString(deserialized.id()); | |
53 | |
54 // Declared here to still be in scope while decoding WebP data. | |
55 BlobDataPtr cached; | |
56 | |
57 // Set to true if the client already has the data in its cache. If it does not | |
58 // keeping |found_in_cache| as false will trigger caching the input in the | |
59 // end of this function. | |
60 bool found_in_cache = false; | |
61 | |
62 // Used later to decode the image and is initialized either based on a cached | |
63 // item or from the |payload| of the proto. | |
64 WebPData webp_data; | 32 WebPData webp_data; |
65 | 33 webp_data.bytes = reinterpret_cast<const uint8_t*>(input); |
66 if (g_blob_cache.Get().Contains(deserialized.id())) { | 34 webp_data.size = input_size; |
67 // The image was found in the cache, so decode using cached data. | |
68 cached = g_blob_cache.Get().Get(deserialized.id()); | |
69 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data()); | |
70 webp_data.size = cached->data.size(); | |
71 DVLOG(2) << "Found id " << hex_id << " with size = " << webp_data.size; | |
72 found_in_cache = true; | |
73 } else { | |
74 // The image was not found in the cache, so decode using the payload. | |
75 DCHECK(deserialized.has_payload()); | |
76 webp_data.bytes = | |
77 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str()); | |
78 webp_data.size = deserialized.payload().size(); | |
79 } | |
80 | 35 |
81 // Read WebP feature information into |config.input|, which is a | 36 // Read WebP feature information into |config.input|, which is a |
82 // WebPBitstreamFeatures. It contains information such as width, height and | 37 // WebPBitstreamFeatures. It contains information such as width, height and |
83 // whether the WebP image has an alpha channel or not. | 38 // whether the WebP image has an alpha channel or not. |
84 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != | 39 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != |
85 VP8_STATUS_OK) { | 40 VP8_STATUS_OK) { |
86 LOG(WARNING) << "Failed to get WebP features."; | 41 LOG(WARNING) << "Failed to get WebP features."; |
87 return false; | 42 return false; |
88 } | 43 } |
89 // Animations are not supported. | 44 // Animations are not supported. |
(...skipping 29 matching lines...) Expand all Loading... |
119 | 74 |
120 // Now free the buffer. It is safe to call this even when the buffer is | 75 // Now free the buffer. It is safe to call this even when the buffer is |
121 // external and not allocated by WebPDecode. | 76 // external and not allocated by WebPDecode. |
122 WebPFreeDecBuffer(&config.output); | 77 WebPFreeDecBuffer(&config.output); |
123 | 78 |
124 if (!success) { | 79 if (!success) { |
125 LOG(WARNING) << "Failed to decode WebP data."; | 80 LOG(WARNING) << "Failed to decode WebP data."; |
126 return false; | 81 return false; |
127 } | 82 } |
128 | 83 |
129 if (!found_in_cache) { | |
130 DVLOG(2) << "Inserting image to cache with id: " << hex_id | |
131 << " size: " << webp_data.size; | |
132 BlobDataPtr to_cache(new BlobData(std::string( | |
133 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size))); | |
134 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache)); | |
135 } | |
136 | |
137 return true; | 84 return true; |
138 } | 85 } |
139 | 86 |
140 } // namespace client | 87 } // namespace client |
141 } // namespace blimp | 88 } // namespace blimp |
OLD | NEW |