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

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: Addressed comments from kmarshall, except swap 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 std::unique_ptr<BlobCacheImageMetadata> deserialized(
vmpstr 2016/04/15 18:18:54 BlobCacheImageMetadata deserialized;
nyquist 2016/04/16 00:25:30 Done.
25 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; 41 new BlobCacheImageMetadata);
42 int signed_size = base::checked_cast<int>(input_size);
43 if (!deserialized->ParseFromArray(input, signed_size)) {
44 LOG(WARNING) << "Failed to parse BlobCacheImageMetadata";
45 return false;
46 }
47
48 // Used later to decode the image and is initialized either based on a cached
49 // item or from the |payload| of the proto.
50 WebPData webp_data;
vmpstr 2016/04/15 18:18:54 Define this closer to where it's used please
nyquist 2016/04/16 00:25:30 Done.
51
52 // Declared here to still be in scope while decoding WebP data.
53 scoped_refptr<RefCountedVector> cached(new RefCountedVector);
54
55 // The cache uses the SHA1 hex string of the ID.
56 DCHECK_EQ(base::kSHA1Length, deserialized->id().length());
Kevin M 2016/04/15 17:28:51 This is network-originating data, so DCHECK isn't
nyquist 2016/04/16 00:25:30 Done.
57 std::string hex_id = blimp::FormatSHA1Hash(deserialized->id().c_str());
58
59 bool found_in_cache = false;
60 if (g_blob_cache.Get().Contains(deserialized->id())) {
61 // The image was found in the cache, so decode using cached data.
62 cached = g_blob_cache.Get().Get(deserialized->id());
63 webp_data.bytes = cached->data.data();
64 webp_data.size = cached->data.size();
65 DVLOG(2) << "Found SHA1 " << hex_id << " with size = " << webp_data.size;
66 found_in_cache = true;
67 } else {
68 // The image was not found in the cache, so decode using the payload.
69 DCHECK(deserialized->has_payload());
70 webp_data.bytes =
71 reinterpret_cast<const uint8_t*>(deserialized->payload().c_str());
72 webp_data.size = deserialized->payload().size();
73 }
26 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(webp_data.bytes, webp_data.size, &config.input) !=
79 VP8_STATUS_OK) {
31 LOG(WARNING) << "Failed to get WebP features."; 80 LOG(WARNING) << "Failed to get WebP features.";
32 return false; 81 return false;
33 } 82 }
34 // Animations are not supported. 83 // Animations are not supported.
35 DCHECK_EQ(0, config.input.has_animation); 84 DCHECK_EQ(0, config.input.has_animation);
36 85
37 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. 86 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures.
38 bitmap->allocN32Pixels(config.input.width, config.input.height); 87 bitmap->allocN32Pixels(config.input.width, config.input.height);
39 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); 88 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType());
40 89
(...skipping 11 matching lines...) Expand all
52 // Instead of using the default WebPDecBuffer output, make WebPDecode directly 101 // Instead of using the default WebPDecBuffer output, make WebPDecode directly
53 // write into the SkBitmap. 102 // write into the SkBitmap.
54 decoderBuffer.is_external_memory = 1; 103 decoderBuffer.is_external_memory = 1;
55 decoderBuffer.u.RGBA.rgba = 104 decoderBuffer.u.RGBA.rgba =
56 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); 105 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0));
57 106
58 // Set the config up to use the decoding buffer we created. 107 // Set the config up to use the decoding buffer we created.
59 config.output = decoderBuffer; 108 config.output = decoderBuffer;
60 109
61 // Decode the input data into the bitmap buffer. 110 // Decode the input data into the bitmap buffer.
62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; 111 bool success =
112 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK;
63 113
64 // Now free the buffer. It is safe to call this even when the buffer is 114 // Now free the buffer. It is safe to call this even when the buffer is
65 // external and not allocated by WebPDecode. 115 // external and not allocated by WebPDecode.
66 WebPFreeDecBuffer(&config.output); 116 WebPFreeDecBuffer(&config.output);
67 117
68 if (!success) { 118 if (!success) {
69 LOG(WARNING) << "Failed to decode WebP data."; 119 LOG(WARNING) << "Failed to decode WebP data.";
70 return false; 120 return false;
71 } 121 }
122
123 if (!found_in_cache) {
124 DVLOG(2) << "Inserting image to cache with SHA1: " << hex_id
125 << " size: " << webp_data.size;
126 scoped_refptr<RefCountedVector> to_cache(new RefCountedVector);
127 to_cache->data.assign(webp_data.bytes, webp_data.bytes + webp_data.size);
128 g_blob_cache.Get().Put(deserialized->id(), std::move(to_cache));
129 }
130
72 return true; 131 return true;
73 } 132 }
74 133
75 } // namespace blimp 134 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698