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

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

Issue 1955493002: Move BlimpImageSerializationProcessor and WebPDecoder to //blimp/client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments, moved to blimp::client namespace, renamed webp_decoder.[cc|h] according to func… Created 4 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "blimp/common/compositor/webp_decoder.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "blimp/common/blob_cache/blob_cache.h"
12 #include "blimp/common/blob_cache/id_util.h"
13 #include "blimp/common/blob_cache/in_memory_blob_cache.h"
14 #include "blimp/common/proto/blob_cache.pb.h"
15 #include "third_party/libwebp/webp/decode.h"
16 #include "third_party/libwebp/webp/demux.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18
19 namespace blimp {
20 namespace {
21
22 // TODO(nyquist): Make this not be infinite size.
23 static base::LazyInstance<InMemoryBlobCache> g_blob_cache =
24 LAZY_INSTANCE_INITIALIZER;
25
26 } // namespace
27
28 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) {
29 DCHECK(bitmap);
30
31 // Initialize an empty WebPDecoderConfig.
32 WebPDecoderConfig config;
33 if (!WebPInitDecoderConfig(&config)) {
34 LOG(WARNING) << "Failed to initialize WebP config.";
35 return false;
36 }
37
38 BlobCacheImageMetadata deserialized;
39 int signed_size = base::checked_cast<int>(input_size);
40 if (!deserialized.ParseFromArray(input, signed_size)) {
41 LOG(WARNING) << "Failed to parse BlobCacheImageMetadata";
42 return false;
43 }
44
45 if (!IsValidBlobId(BlobId(deserialized.id()))) {
46 LOG(WARNING) << "Length of ID is not correct "
47 << deserialized.id().length();
48 return false;
49 }
50 std::string hex_id = BlobIdToString(deserialized.id());
51
52 // Declared here to still be in scope while decoding WebP data.
53 BlobDataPtr cached;
54
55 // Set to true if the client already has the data in its cache. If it does not
56 // keeping |found_in_cache| as false will trigger caching the input in the
57 // end of this function.
58 bool found_in_cache = false;
59
60 // Used later to decode the image and is initialized either based on a cached
61 // item or from the |payload| of the proto.
62 WebPData webp_data;
63
64 if (g_blob_cache.Get().Contains(deserialized.id())) {
65 // The image was found in the cache, so decode using cached data.
66 cached = g_blob_cache.Get().Get(deserialized.id());
67 webp_data.bytes = reinterpret_cast<const uint8_t*>(cached->data.data());
68 webp_data.size = cached->data.size();
69 DVLOG(2) << "Found id " << hex_id << " with size = " << webp_data.size;
70 found_in_cache = true;
71 } else {
72 // The image was not found in the cache, so decode using the payload.
73 DCHECK(deserialized.has_payload());
74 webp_data.bytes =
75 reinterpret_cast<const uint8_t*>(deserialized.payload().c_str());
76 webp_data.size = deserialized.payload().size();
77 }
78
79 // Read WebP feature information into |config.input|, which is a
80 // WebPBitstreamFeatures. It contains information such as width, height and
81 // whether the WebP image has an alpha channel or not.
82 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) !=
83 VP8_STATUS_OK) {
84 LOG(WARNING) << "Failed to get WebP features.";
85 return false;
86 }
87 // Animations are not supported.
88 DCHECK_EQ(0, config.input.has_animation);
89
90 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures.
91 bitmap->allocN32Pixels(config.input.width, config.input.height);
92 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType());
93
94 // Setup the decoder buffer based on the WebPBitstreamFeatures.
95 WebPDecBuffer decoderBuffer;
96
97 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android).
98 decoderBuffer.colorspace = MODE_rgbA;
99 #else // Output little-endian BGRA pixels.
100 decoderBuffer.colorspace = MODE_bgrA;
101 #endif
102 decoderBuffer.u.RGBA.stride = config.input.width * 4;
103 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height;
104
105 // Instead of using the default WebPDecBuffer output, make WebPDecode directly
106 // write into the SkBitmap.
107 decoderBuffer.is_external_memory = 1;
108 decoderBuffer.u.RGBA.rgba =
109 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0));
110
111 // Set the config up to use the decoding buffer we created.
112 config.output = decoderBuffer;
113
114 // Decode the input data into the bitmap buffer.
115 bool success =
116 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK;
117
118 // Now free the buffer. It is safe to call this even when the buffer is
119 // external and not allocated by WebPDecode.
120 WebPFreeDecBuffer(&config.output);
121
122 if (!success) {
123 LOG(WARNING) << "Failed to decode WebP data.";
124 return false;
125 }
126
127 if (!found_in_cache) {
128 DVLOG(2) << "Inserting image to cache with id: " << hex_id
129 << " size: " << webp_data.size;
130 BlobDataPtr to_cache(new BlobData(std::string(
131 reinterpret_cast<const char*>(webp_data.bytes), webp_data.size)));
132 g_blob_cache.Get().Put(deserialized.id(), std::move(to_cache));
133 }
134
135 return true;
136 }
137
138 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/common/compositor/webp_decoder.h ('k') | blimp/engine/renderer/blimp_content_renderer_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698