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 "services/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/codec/jpeg_codec.h" |
| 15 #include "ui/gfx/geometry/size.h" |
| 16 |
| 17 namespace services { |
| 18 |
| 19 namespace { |
| 20 // TODO(amistry): Replace with more correct value. |
| 21 int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; |
| 22 } |
| 23 |
| 24 ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) |
| 25 : max_message_size_(max_message_size), binding_(this) { |
| 26 } |
| 27 |
| 28 ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) |
| 29 : max_message_size_(kMaxMessageSize), binding_(this, request.Pass()) { |
| 30 } |
| 31 |
| 32 ImageDecoderImpl::~ImageDecoderImpl() { |
| 33 } |
| 34 |
| 35 void ImageDecoderImpl::DecodeImage( |
| 36 mojo::Array<uint8_t> encoded_data, |
| 37 ImageCodec codec, |
| 38 bool shrink_to_fit, |
| 39 const mojo::Callback<void(bool, skia::BitmapPtr)>& callback) { |
| 40 SkBitmap decoded_image; |
| 41 if (codec == IMAGE_CODEC_ROBUST_JPEG) { |
| 42 // Our robust jpeg decoding is using IJG libjpeg. |
| 43 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG && |
| 44 encoded_data.size()) { |
| 45 scoped_ptr<SkBitmap> decoded_jpeg( |
| 46 gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size())); |
| 47 if (decoded_jpeg.get() && !decoded_jpeg->empty()) |
| 48 decoded_image = *decoded_jpeg; |
| 49 } |
| 50 } else { |
| 51 decoded_image = content::DecodeImage(&encoded_data[0], gfx::Size(), |
| 52 encoded_data.size()); |
| 53 } |
| 54 |
| 55 if (!decoded_image.isNull()) { |
| 56 skia::BitmapPtr dummy_image = skia::Bitmap::New(); |
| 57 int64_t struct_size = GetSerializedSize_(dummy_image); |
| 58 int64_t image_size = decoded_image.computeSize64(); |
| 59 int halves = 0; |
| 60 while (struct_size + (image_size >> 2 * halves) > max_message_size_) |
| 61 halves++; |
| 62 if (halves) { |
| 63 if (shrink_to_fit) { |
| 64 // If decoded image is too large for IPC message, shrink it by halves. |
| 65 // This prevents quality loss, and should never overshrink on displays |
| 66 // smaller than 3600x2400. |
| 67 // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| 68 decoded_image = skia::ImageOperations::Resize( |
| 69 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 70 decoded_image.width() >> halves, decoded_image.height() >> halves); |
| 71 } else { |
| 72 decoded_image.reset(); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 if (decoded_image.isNull()) { |
| 78 callback.Run(false, nullptr); |
| 79 return; |
| 80 } |
| 81 |
| 82 callback.Run(true, skia::Bitmap::From(decoded_image)); |
| 83 } |
| 84 |
| 85 } // namespace services |
OLD | NEW |