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/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/sha1.h" | |
11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
Wez
2016/04/23 00:08:52
Do we use string_util.h in here?
nyquist
2016/04/25 08:11:17
Done.
| |
13 #include "blimp/common/blob_cache/blob_cache.h" | 12 #include "blimp/common/blob_cache/blob_cache.h" |
14 #include "blimp/common/blob_cache/id_util.h" | 13 #include "blimp/common/blob_cache/id_util.h" |
15 #include "blimp/common/blob_cache/in_memory_blob_cache.h" | 14 #include "blimp/common/blob_cache/in_memory_blob_cache.h" |
16 #include "blimp/common/proto/blob_cache.pb.h" | 15 #include "blimp/common/proto/blob_cache.pb.h" |
17 #include "third_party/libwebp/webp/decode.h" | 16 #include "third_party/libwebp/webp/decode.h" |
18 #include "third_party/libwebp/webp/demux.h" | 17 #include "third_party/libwebp/webp/demux.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
20 | 19 |
21 namespace blimp { | 20 namespace blimp { |
22 namespace { | 21 namespace { |
(...skipping 14 matching lines...) Expand all Loading... | |
37 return false; | 36 return false; |
38 } | 37 } |
39 | 38 |
40 BlobCacheImageMetadata deserialized; | 39 BlobCacheImageMetadata deserialized; |
41 int signed_size = base::checked_cast<int>(input_size); | 40 int signed_size = base::checked_cast<int>(input_size); |
42 if (!deserialized.ParseFromArray(input, signed_size)) { | 41 if (!deserialized.ParseFromArray(input, signed_size)) { |
43 LOG(WARNING) << "Failed to parse BlobCacheImageMetadata"; | 42 LOG(WARNING) << "Failed to parse BlobCacheImageMetadata"; |
44 return false; | 43 return false; |
45 } | 44 } |
46 | 45 |
47 // The cache uses the SHA1 hex string of the ID. | 46 if (!BlobIdHasCorrectLength(BlobId(deserialized.id()))) { |
48 if (deserialized.id().length() != base::kSHA1Length) { | 47 LOG(WARNING) << "Length of ID is not correct " |
49 LOG(WARNING) << "Length of ID is not base::kSHA1Length (" | |
50 << base::kSHA1Length << "), but " | |
51 << deserialized.id().length(); | 48 << deserialized.id().length(); |
52 return false; | 49 return false; |
53 } | 50 } |
54 std::string hex_id = FormatBlobId(deserialized.id()); | 51 std::string hex_id = FormatBlobId(deserialized.id()); |
55 | 52 |
56 // Declared here to still be in scope while decoding WebP data. | 53 // Declared here to still be in scope while decoding WebP data. |
57 BlobDataPtr cached; | 54 BlobDataPtr cached; |
58 | 55 |
59 // Set to true if the client already has the data in its cache. If it does not | 56 // 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 | 57 // keeping |found_in_cache| as false will trigger caching the input in the |
61 // end of this function. | 58 // end of this function. |
62 bool found_in_cache = false; | 59 bool found_in_cache = false; |
63 | 60 |
64 // Used later to decode the image and is initialized either based on a cached | 61 // Used later to decode the image and is initialized either based on a cached |
65 // item or from the |payload| of the proto. | 62 // item or from the |payload| of the proto. |
66 WebPData webp_data; | 63 WebPData webp_data; |
67 | 64 |
68 if (g_blob_cache.Get().Contains(deserialized.id())) { | 65 if (g_blob_cache.Get().Contains(deserialized.id())) { |
69 // The image was found in the cache, so decode using cached data. | 66 // The image was found in the cache, so decode using cached data. |
70 cached = g_blob_cache.Get().Get(deserialized.id()); | 67 cached = g_blob_cache.Get().Get(deserialized.id()); |
71 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data()); | 68 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data()); |
72 webp_data.size = cached->data.size(); | 69 webp_data.size = cached->data.size(); |
73 DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size; | 70 DVLOG(2) << "Found id " << hex_id << " with size = " << webp_data.size; |
74 found_in_cache = true; | 71 found_in_cache = true; |
75 } else { | 72 } else { |
76 // The image was not found in the cache, so decode using the payload. | 73 // The image was not found in the cache, so decode using the payload. |
77 DCHECK(deserialized.has_payload()); | 74 DCHECK(deserialized.has_payload()); |
78 webp_data.bytes = | 75 webp_data.bytes = |
79 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str()); | 76 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str()); |
80 webp_data.size = deserialized.payload().size(); | 77 webp_data.size = deserialized.payload().size(); |
81 } | 78 } |
82 | 79 |
83 // Read WebP feature information into |config.input|, which is a | 80 // Read WebP feature information into |config.input|, which is a |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 // Now free the buffer. It is safe to call this even when the buffer is | 119 // Now free the buffer. It is safe to call this even when the buffer is |
123 // external and not allocated by WebPDecode. | 120 // external and not allocated by WebPDecode. |
124 WebPFreeDecBuffer(&config.output); | 121 WebPFreeDecBuffer(&config.output); |
125 | 122 |
126 if (!success) { | 123 if (!success) { |
127 LOG(WARNING) << "Failed to decode WebP data."; | 124 LOG(WARNING) << "Failed to decode WebP data."; |
128 return false; | 125 return false; |
129 } | 126 } |
130 | 127 |
131 if (!found_in_cache) { | 128 if (!found_in_cache) { |
132 DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id | 129 DVLOG(2) << "Inserting image to cache with id: " << hex_id |
133 << " size: " << webp_data.size; | 130 << " size: " << webp_data.size; |
134 BlobDataPtr to_cache(new BlobData(std::string( | 131 BlobDataPtr to_cache(new BlobData(std::string( |
135 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size))); | 132 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size))); |
136 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache)); | 133 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache)); |
137 } | 134 } |
138 | 135 |
139 return true; | 136 return true; |
140 } | 137 } |
141 | 138 |
142 } // namespace blimp | 139 } // namespace blimp |
OLD | NEW |