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 "skia/ext/image_operations.h" |
| 15 #include "skia/public/type_converters.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(int64_t max_message_size) |
| 30 : max_message_size_(max_message_size), binding_(this) { |
| 31 } |
| 32 |
| 33 ImageDecoderImpl::ImageDecoderImpl( |
| 34 mojo::InterfaceRequest<mojom::ImageDecoder> request) |
| 35 : max_message_size_(kMaxMessageSize), binding_(this, std::move(request)) { |
| 36 } |
| 37 |
| 38 ImageDecoderImpl::~ImageDecoderImpl() { |
| 39 } |
| 40 |
| 41 void ImageDecoderImpl::DecodeImage( |
| 42 mojo::Array<uint8_t> encoded_data, |
| 43 mojom::ImageCodec codec, |
| 44 bool shrink_to_fit, |
| 45 const mojo::Callback<void(skia::mojom::BitmapPtr)>& callback) { |
| 46 if (encoded_data.size() == 0) { |
| 47 callback.Run(nullptr); |
| 48 return; |
| 49 } |
| 50 |
| 51 SkBitmap decoded_image; |
| 52 #if defined(OS_CHROMEOS) |
| 53 if (codec == mojom::ImageCodec::ROBUST_JPEG) { |
| 54 // Our robust jpeg decoding is using IJG libjpeg. |
| 55 if (encoded_data.size()) { |
| 56 scoped_ptr<SkBitmap> decoded_jpeg( |
| 57 gfx::JPEGCodecRobustSlow::Decode(encoded_data.storage().data(), |
| 58 encoded_data.size())); |
| 59 if (decoded_jpeg.get() && !decoded_jpeg->empty()) |
| 60 decoded_image = *decoded_jpeg; |
| 61 } |
| 62 } else if (codec == mojom::ImageCodec::ROBUST_PNG) { |
| 63 // Our robust PNG decoding is using libpng. |
| 64 if (encoded_data.size()) { |
| 65 SkBitmap decoded_png; |
| 66 if (gfx::PNGCodec::Decode(encoded_data.storage().data(), |
| 67 encoded_data.size(), &decoded_png)) { |
| 68 decoded_image = decoded_png; |
| 69 } |
| 70 } |
| 71 } |
| 72 #endif // defined(OS_CHROMEOS) |
| 73 if (codec == mojom::ImageCodec::DEFAULT) { |
| 74 decoded_image = content::DecodeImage(encoded_data.storage().data(), |
| 75 gfx::Size(), encoded_data.size()); |
| 76 } |
| 77 |
| 78 if (!decoded_image.isNull()) { |
| 79 skia::mojom::BitmapPtr dummy_image = skia::mojom::Bitmap::New(); |
| 80 int64_t struct_size = |
| 81 skia::mojom::GetSerializedSize_(dummy_image, nullptr) + kPadding; |
| 82 int64_t image_size = decoded_image.computeSize64(); |
| 83 int halves = 0; |
| 84 while (struct_size + (image_size >> 2 * halves) > max_message_size_) |
| 85 halves++; |
| 86 if (halves) { |
| 87 if (shrink_to_fit) { |
| 88 // If decoded image is too large for IPC message, shrink it by halves. |
| 89 // This prevents quality loss, and should never overshrink on displays |
| 90 // smaller than 3600x2400. |
| 91 // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| 92 decoded_image = skia::ImageOperations::Resize( |
| 93 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 94 decoded_image.width() >> halves, decoded_image.height() >> halves); |
| 95 } else { |
| 96 decoded_image.reset(); |
| 97 } |
| 98 } |
| 99 } |
| 100 |
| 101 if (decoded_image.isNull()) |
| 102 callback.Run(nullptr); |
| 103 else |
| 104 callback.Run(skia::mojom::Bitmap::From(decoded_image)); |
| 105 } |
OLD | NEW |