OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "components/image_decoder/image_decoder_impl.h" |
| 5 |
| 6 #include <string.h> |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "content/public/child/image_decoder_utils.h" |
| 10 #include "ipc/ipc_channel.h" |
| 11 #include "skia/ext/image_operations.h" |
| 12 #include "skia/public/type_converters.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/geometry/size.h" |
| 15 |
| 16 #if defined(OS_CHROMEOS) |
| 17 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h" |
| 18 #endif |
| 19 |
| 20 namespace image_decoder { |
| 21 |
| 22 namespace { |
| 23 int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; |
| 24 } |
| 25 |
| 26 ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) |
| 27 : max_message_size_(max_message_size), binding_(this) { |
| 28 } |
| 29 |
| 30 ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) |
| 31 : max_message_size_(kMaxMessageSize), binding_(this, request.Pass()) { |
| 32 } |
| 33 |
| 34 ImageDecoderImpl::~ImageDecoderImpl() { |
| 35 } |
| 36 |
| 37 void ImageDecoderImpl::DecodeImage( |
| 38 mojo::Array<uint8_t> encoded_data, |
| 39 ImageCodec codec, |
| 40 bool shrink_to_fit, |
| 41 const mojo::Callback<void(skia::BitmapPtr)>& callback) { |
| 42 if (encoded_data.size() == 0) { |
| 43 callback.Run(nullptr); |
| 44 return; |
| 45 } |
| 46 |
| 47 SkBitmap decoded_image; |
| 48 if (codec == IMAGE_CODEC_ROBUST_JPEG) { |
| 49 #if defined(OS_CHROMEOS) |
| 50 // Our robust jpeg decoding is using IJG libjpeg. |
| 51 if (encoded_data.size()) { |
| 52 scoped_ptr<SkBitmap> decoded_jpeg( |
| 53 gfx::JPEGCodecRobustSlow::Decode(&encoded_data[0], |
| 54 encoded_data.size())); |
| 55 if (decoded_jpeg.get() && !decoded_jpeg->empty()) |
| 56 decoded_image = *decoded_jpeg; |
| 57 } |
| 58 #endif // defined(OS_CHROMEOS) |
| 59 } else { |
| 60 decoded_image = content::DecodeImage(&encoded_data[0], gfx::Size(), |
| 61 encoded_data.size()); |
| 62 } |
| 63 |
| 64 if (!decoded_image.isNull()) { |
| 65 skia::BitmapPtr dummy_image = skia::Bitmap::New(); |
| 66 int64_t struct_size = GetSerializedSize_(dummy_image); |
| 67 int64_t image_size = decoded_image.computeSize64(); |
| 68 int halves = 0; |
| 69 while (struct_size + (image_size >> 2 * halves) > max_message_size_) |
| 70 halves++; |
| 71 if (halves) { |
| 72 if (shrink_to_fit) { |
| 73 // If decoded image is too large for IPC message, shrink it by halves. |
| 74 // This prevents quality loss, and should never overshrink on displays |
| 75 // smaller than 3600x2400. |
| 76 // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| 77 decoded_image = skia::ImageOperations::Resize( |
| 78 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 79 decoded_image.width() >> halves, decoded_image.height() >> halves); |
| 80 } else { |
| 81 decoded_image.reset(); |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 if (decoded_image.isNull()) |
| 87 callback.Run(nullptr); |
| 88 else |
| 89 callback.Run(skia::Bitmap::From(decoded_image)); |
| 90 } |
| 91 |
| 92 } // namespace image_decoder |
OLD | NEW |