Chromium Code Reviews| 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/proto/blob_cache.pb.h" | |
| 8 #include "third_party/libwebp/webp/decode.h" | 16 #include "third_party/libwebp/webp/decode.h" |
| 9 #include "third_party/libwebp/webp/demux.h" | 17 #include "third_party/libwebp/webp/demux.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 | 19 |
| 12 namespace blimp { | 20 namespace blimp { |
| 21 namespace { | |
| 22 std::string ToSHA1HexString(const void* data, size_t data_size) { | |
|
Kevin M
2016/04/13 23:29:52
What about using binary SHA1 for transmission and
nyquist
2016/04/14 19:33:23
Done. For now I also keep the cache in hex-format,
| |
| 23 unsigned char sha1_hash[base::kSHA1Length]; | |
| 24 base::SHA1HashBytes(static_cast<const unsigned char*>(data), data_size, | |
| 25 sha1_hash); | |
| 26 return base::ToLowerASCII(base::HexEncode(sha1_hash, base::kSHA1Length)); | |
| 27 } | |
| 28 | |
| 29 // TODO(nyquist): Make this not be infinite size. | |
| 30 static base::LazyInstance<InfiniteBlobCache> g_blob_cache = | |
| 31 LAZY_INSTANCE_INITIALIZER; | |
| 32 | |
| 33 } // namespace | |
| 13 | 34 |
| 14 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | 35 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
| 15 DCHECK(bitmap); | 36 DCHECK(bitmap); |
| 16 | 37 |
| 17 // Initialize an empty WebPDecoderConfig. | 38 // Initialize an empty WebPDecoderConfig. |
| 18 WebPDecoderConfig config; | 39 WebPDecoderConfig config; |
| 19 if (!WebPInitDecoderConfig(&config)) { | 40 if (!WebPInitDecoderConfig(&config)) { |
| 20 LOG(WARNING) << "Failed to initialize WebP config."; | 41 LOG(WARNING) << "Failed to initialize WebP config."; |
| 21 return false; | 42 return false; |
| 22 } | 43 } |
| 23 | 44 |
| 24 // Treat the input as uint8_t. | 45 // Treat the input as uint8_t. |
| 25 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; | 46 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; |
|
Kevin M
2016/04/13 23:29:52
Brace initialization is blacklisted. Sowwy. :(
Kevin M
2016/04/13 23:29:53
Pick a better name?
vmpstr
2016/04/13 23:48:02
Soon though, right? I think pkasting might be tryi
nyquist
2016/04/14 19:33:23
Done.
| |
| 26 | 47 |
| 48 if (input_size < 40) { | |
| 49 LOG(WARNING) << "Throwing away tiny input with size " << input_size; | |
|
Kevin M
2016/04/13 23:29:52
How about DLOG? Is this actionable for release bui
nyquist
2016/04/14 19:33:23
Done.
| |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 bool found_in_cache = false; | |
|
Kevin M
2016/04/13 23:29:52
Move this declaration down, just above the ParseFr
nyquist
2016/04/14 19:33:23
Done.
| |
| 54 // Declared here to still be in scope while decoding WebP data. | |
| 55 scoped_refptr<RefCountedVector> cached(new RefCountedVector); | |
| 56 | |
| 57 std::unique_ptr<BlobCacheImageIdentifier> deserialized( | |
| 58 new BlobCacheImageIdentifier); | |
| 59 int signed_size = base::checked_cast<int>(input_size); | |
| 60 if (deserialized->ParseFromArray(input, signed_size)) { | |
| 61 if (g_blob_cache.Get().Contains(deserialized->id())) { | |
| 62 cached = g_blob_cache.Get().Get(deserialized->id()); | |
| 63 // Overwrite previously initialized WebPData. | |
| 64 data.bytes = cached.get()->data.data(); | |
| 65 data.size = cached.get()->data.size(); | |
| 66 VLOG(2) << "Found SHA1 " << deserialized->id() | |
| 67 << " with size = " << data.size; | |
| 68 found_in_cache = true; | |
| 69 } else { | |
| 70 LOG(WARNING) << "Did not find cached image with SHA1: " | |
| 71 << deserialized->id(); | |
| 72 } | |
| 73 } | |
| 74 | |
| 27 // Read WebP feature information into |config.input|, which is a | 75 // Read WebP feature information into |config.input|, which is a |
| 28 // WebPBitstreamFeatures. It contains information such as width, height and | 76 // WebPBitstreamFeatures. It contains information such as width, height and |
| 29 // whether the WebP image has an alpha channel or not. | 77 // whether the WebP image has an alpha channel or not. |
| 30 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { | 78 if (WebPGetFeatures(data.bytes, data.size, &config.input) != 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 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; | 110 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; |
| 63 | 111 |
| 64 // Now free the buffer. It is safe to call this even when the buffer is | 112 // Now free the buffer. It is safe to call this even when the buffer is |
| 65 // external and not allocated by WebPDecode. | 113 // external and not allocated by WebPDecode. |
| 66 WebPFreeDecBuffer(&config.output); | 114 WebPFreeDecBuffer(&config.output); |
| 67 | 115 |
| 68 if (!success) { | 116 if (!success) { |
| 69 LOG(WARNING) << "Failed to decode WebP data."; | 117 LOG(WARNING) << "Failed to decode WebP data."; |
| 70 return false; | 118 return false; |
| 71 } | 119 } |
| 120 | |
| 121 if (!found_in_cache) { | |
| 122 std::string sha1 = ToSHA1HexString(data.bytes, data.size); | |
| 123 VLOG(2) << "Inserting image to cache with SHA1: " << sha1 | |
| 124 << " size: " << data.size; | |
| 125 scoped_refptr<RefCountedVector> to_cache(new RefCountedVector); | |
| 126 for (size_t i = 0; i < data.size; ++i) | |
|
Kevin M
2016/04/13 23:29:52
Use braces for single lines (consistent with rest
nyquist
2016/04/14 19:33:23
Done.
| |
| 127 to_cache.get()->data.push_back(data.bytes[i]); | |
| 128 g_blob_cache.Get().Put(sha1, to_cache); | |
|
Kevin M
2016/04/13 23:29:53
std::move(to_cache)
nyquist
2016/04/14 19:33:23
Done.
| |
| 129 } | |
| 130 | |
| 72 return true; | 131 return true; |
| 73 } | 132 } |
| 74 | 133 |
| 75 } // namespace blimp | 134 } // namespace blimp |
| OLD | NEW |