Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: blimp/common/compositor/webp_decoder.cc

Issue 1867653002: Initial version of Blimp BlobCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed BlobId from 'const std::string' to 'std::string' Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/in_memory_blob_cache.h"
15 #include "blimp/common/blob_cache/sha1_util.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, but "
vmpstr 2016/04/18 19:38:11 Log base::kSHA1Length as well for posterity?
nyquist 2016/04/18 20:12:02 Done.
50 << deserialized.id().length();
51 return false;
52 }
53 std::string hex_id = FormatBlobId(deserialized.id());
54
55 // Declared here to still be in scope while decoding WebP data.
56 BlobData cached;
57
58 // Set to true if the client already has the data in its cache. If it does not
59 // keeping |found_in_cache| as false will trigger caching the input in the
60 // end of this function.
61 bool found_in_cache = false;
62
63 // Used later to decode the image and is initialized either based on a cached
64 // item or from the |payload| of the proto.
65 WebPData webp_data;
66
67 if (g_blob_cache.Get().Contains(deserialized.id())) {
68 // The image was found in the cache, so decode using cached data.
69 cached = g_blob_cache.Get().Get(deserialized.id());
vmpstr 2016/04/18 19:38:11 Honestly, I'm not sure how I feel about BlobData b
nyquist 2016/04/18 20:12:02 Okay. I'll keep that in mind. If we realize going
70 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data());
71 webp_data.size = cached->data.size();
72 DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size;
73 found_in_cache = true;
74 } else {
75 // The image was not found in the cache, so decode using the payload.
76 DCHECK(deserialized.has_payload());
77 webp_data.bytes =
78 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str());
79 webp_data.size = deserialized.payload().size();
80 }
26 81
27 // Read WebP feature information into |config.input|, which is a 82 // Read WebP feature information into |config.input|, which is a
28 // WebPBitstreamFeatures. It contains information such as width, height and 83 // WebPBitstreamFeatures. It contains information such as width, height and
29 // whether the WebP image has an alpha channel or not. 84 // whether the WebP image has an alpha channel or not.
30 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { 85 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) !=
86 VP8_STATUS_OK) {
31 LOG(WARNING) << "Failed to get WebP features."; 87 LOG(WARNING) << "Failed to get WebP features.";
32 return false; 88 return false;
33 } 89 }
34 // Animations are not supported. 90 // Animations are not supported.
35 DCHECK_EQ(0, config.input.has_animation); 91 DCHECK_EQ(0, config.input.has_animation);
36 92
37 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. 93 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures.
38 bitmap->allocN32Pixels(config.input.width, config.input.height); 94 bitmap->allocN32Pixels(config.input.width, config.input.height);
39 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); 95 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType());
40 96
(...skipping 11 matching lines...) Expand all
52 // Instead of using the default WebPDecBuffer output, make WebPDecode directly 108 // Instead of using the default WebPDecBuffer output, make WebPDecode directly
53 // write into the SkBitmap. 109 // write into the SkBitmap.
54 decoderBuffer.is_external_memory = 1; 110 decoderBuffer.is_external_memory = 1;
55 decoderBuffer.u.RGBA.rgba = 111 decoderBuffer.u.RGBA.rgba =
56 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); 112 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0));
57 113
58 // Set the config up to use the decoding buffer we created. 114 // Set the config up to use the decoding buffer we created.
59 config.output = decoderBuffer; 115 config.output = decoderBuffer;
60 116
61 // Decode the input data into the bitmap buffer. 117 // Decode the input data into the bitmap buffer.
62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; 118 bool success =
119 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK;
63 120
64 // Now free the buffer. It is safe to call this even when the buffer is 121 // Now free the buffer. It is safe to call this even when the buffer is
65 // external and not allocated by WebPDecode. 122 // external and not allocated by WebPDecode.
66 WebPFreeDecBuffer(&config.output); 123 WebPFreeDecBuffer(&config.output);
67 124
68 if (!success) { 125 if (!success) {
69 LOG(WARNING) << "Failed to decode WebP data."; 126 LOG(WARNING) << "Failed to decode WebP data.";
70 return false; 127 return false;
71 } 128 }
129
130 if (!found_in_cache) {
131 DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id
132 << " size: " << webp_data.size;
133 BlobData to_cache(new base::RefCountedData<const std::string>(std::string(
134 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size)));
135 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache));
136 }
137
72 return true; 138 return true;
73 } 139 }
74 140
75 } // namespace blimp 141 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698