| OLD | NEW |
| (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/client/core/compositor/blimp_image_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 client { | |
| 21 | |
| 22 bool DecodeBlimpImage(const void* input, size_t input_size, SkBitmap* bitmap) { | |
| 23 DCHECK(bitmap); | |
| 24 | |
| 25 // Initialize an empty WebPDecoderConfig. | |
| 26 WebPDecoderConfig config; | |
| 27 if (!WebPInitDecoderConfig(&config)) { | |
| 28 LOG(WARNING) << "Failed to initialize WebP config."; | |
| 29 return false; | |
| 30 } | |
| 31 | |
| 32 WebPData webp_data; | |
| 33 webp_data.bytes = reinterpret_cast<const uint8_t*>(input); | |
| 34 webp_data.size = input_size; | |
| 35 | |
| 36 // Read WebP feature information into |config.input|, which is a | |
| 37 // WebPBitstreamFeatures. It contains information such as width, height and | |
| 38 // whether the WebP image has an alpha channel or not. | |
| 39 if (WebPGetFeatures(webp_data.bytes, webp_data.size, &config.input) != | |
| 40 VP8_STATUS_OK) { | |
| 41 LOG(WARNING) << "Failed to get WebP features."; | |
| 42 return false; | |
| 43 } | |
| 44 // Animations are not supported. | |
| 45 DCHECK_EQ(0, config.input.has_animation); | |
| 46 | |
| 47 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. | |
| 48 bitmap->allocN32Pixels(config.input.width, config.input.height); | |
| 49 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); | |
| 50 | |
| 51 // Setup the decoder buffer based on the WebPBitstreamFeatures. | |
| 52 WebPDecBuffer decoderBuffer; | |
| 53 | |
| 54 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android). | |
| 55 decoderBuffer.colorspace = MODE_rgbA; | |
| 56 #else // Output little-endian BGRA pixels. | |
| 57 decoderBuffer.colorspace = MODE_bgrA; | |
| 58 #endif | |
| 59 decoderBuffer.u.RGBA.stride = config.input.width * 4; | |
| 60 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height; | |
| 61 | |
| 62 // Instead of using the default WebPDecBuffer output, make WebPDecode directly | |
| 63 // write into the SkBitmap. | |
| 64 decoderBuffer.is_external_memory = 1; | |
| 65 decoderBuffer.u.RGBA.rgba = | |
| 66 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | |
| 67 | |
| 68 // Set the config up to use the decoding buffer we created. | |
| 69 config.output = decoderBuffer; | |
| 70 | |
| 71 // Decode the input data into the bitmap buffer. | |
| 72 bool success = | |
| 73 WebPDecode(webp_data.bytes, webp_data.size, &config) == VP8_STATUS_OK; | |
| 74 | |
| 75 // Now free the buffer. It is safe to call this even when the buffer is | |
| 76 // external and not allocated by WebPDecode. | |
| 77 WebPFreeDecBuffer(&config.output); | |
| 78 | |
| 79 if (!success) { | |
| 80 LOG(WARNING) << "Failed to decode WebP data."; | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 } // namespace client | |
| 88 } // namespace blimp | |
| OLD | NEW |