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