| 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 "chrome/utility/image_decoder_impl.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "content/public/child/image_decoder_utils.h" | |
| 13 #include "ipc/ipc_channel.h" | |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 15 #include "skia/ext/image_operations.h" | |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | |
| 17 #include "ui/gfx/geometry/size.h" | |
| 18 | |
| 19 #if defined(OS_CHROMEOS) | |
| 20 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h" | |
| 21 #include "ui/gfx/codec/png_codec.h" | |
| 22 #endif | |
| 23 | |
| 24 namespace { | |
| 25 int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; | |
| 26 int64_t kPadding = 64; | |
| 27 } | |
| 28 | |
| 29 ImageDecoderImpl::ImageDecoderImpl() : ImageDecoderImpl(kMaxMessageSize) {} | |
| 30 | |
| 31 ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) | |
| 32 : max_message_size_(max_message_size) {} | |
| 33 | |
| 34 ImageDecoderImpl::~ImageDecoderImpl() { | |
| 35 } | |
| 36 | |
| 37 void ImageDecoderImpl::DecodeImage(const std::vector<uint8_t>& encoded_data, | |
| 38 mojom::ImageCodec codec, | |
| 39 bool shrink_to_fit, | |
| 40 const DecodeImageCallback& callback) { | |
| 41 if (encoded_data.empty()) { | |
| 42 callback.Run(SkBitmap()); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 SkBitmap decoded_image; | |
| 47 #if defined(OS_CHROMEOS) | |
| 48 if (codec == mojom::ImageCodec::ROBUST_JPEG) { | |
| 49 // Our robust jpeg decoding is using IJG libjpeg. | |
| 50 std::unique_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodecRobustSlow::Decode( | |
| 51 encoded_data.data(), encoded_data.size())); | |
| 52 if (decoded_jpeg.get() && !decoded_jpeg->empty()) | |
| 53 decoded_image = *decoded_jpeg; | |
| 54 } else if (codec == mojom::ImageCodec::ROBUST_PNG) { | |
| 55 // Our robust PNG decoding is using libpng. | |
| 56 SkBitmap decoded_png; | |
| 57 if (gfx::PNGCodec::Decode(encoded_data.data(), encoded_data.size(), | |
| 58 &decoded_png)) { | |
| 59 decoded_image = decoded_png; | |
| 60 } | |
| 61 } | |
| 62 #endif // defined(OS_CHROMEOS) | |
| 63 if (codec == mojom::ImageCodec::DEFAULT) { | |
| 64 decoded_image = content::DecodeImage(encoded_data.data(), gfx::Size(), | |
| 65 encoded_data.size()); | |
| 66 } | |
| 67 | |
| 68 if (!decoded_image.isNull()) { | |
| 69 // When serialized, the space taken up by a skia::mojom::Bitmap excluding | |
| 70 // the pixel data payload should be: | |
| 71 // sizeof(skia::mojom::Bitmap::Data_) + pixel data array header (8 bytes) | |
| 72 // Use a bigger number in case we need padding at the end. | |
| 73 int64_t struct_size = sizeof(skia::mojom::Bitmap::Data_) + kPadding; | |
| 74 int64_t image_size = decoded_image.computeSize64(); | |
| 75 int halves = 0; | |
| 76 while (struct_size + (image_size >> 2 * halves) > max_message_size_) | |
| 77 halves++; | |
| 78 if (halves) { | |
| 79 if (shrink_to_fit) { | |
| 80 // If decoded image is too large for IPC message, shrink it by halves. | |
| 81 // This prevents quality loss, and should never overshrink on displays | |
| 82 // smaller than 3600x2400. | |
| 83 // TODO (Issue 416916): Instead of shrinking, return via shared memory | |
| 84 decoded_image = skia::ImageOperations::Resize( | |
| 85 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 86 decoded_image.width() >> halves, decoded_image.height() >> halves); | |
| 87 } else { | |
| 88 decoded_image.reset(); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 callback.Run(decoded_image); | |
| 94 } | |
| OLD | NEW |