Index: services/image_decoder/image_decoder_impl.cc |
diff --git a/services/image_decoder/image_decoder_impl.cc b/services/image_decoder/image_decoder_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..71e5ec0ef9ef843df6b5c4fa1365b862fd5a7e5b |
--- /dev/null |
+++ b/services/image_decoder/image_decoder_impl.cc |
@@ -0,0 +1,85 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+#include "services/image_decoder/image_decoder_impl.h" |
+ |
+#include <string.h> |
+ |
+#include "base/logging.h" |
+#include "content/public/child/image_decoder_utils.h" |
+#include "ipc/ipc_channel.h" |
+#include "skia/ext/image_operations.h" |
+#include "skia/public/type_converters.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/codec/jpeg_codec.h" |
+#include "ui/gfx/geometry/size.h" |
+ |
+namespace services { |
+ |
+namespace { |
+// TODO(amistry): Replace with more correct value. |
+int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; |
+} |
+ |
+ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) |
+ : max_message_size_(max_message_size), binding_(this) { |
+} |
+ |
+ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) |
+ : max_message_size_(kMaxMessageSize), binding_(this, request.Pass()) { |
+} |
+ |
+ImageDecoderImpl::~ImageDecoderImpl() { |
+} |
+ |
+void ImageDecoderImpl::DecodeImage( |
+ mojo::Array<uint8_t> encoded_data, |
+ ImageCodec codec, |
+ bool shrink_to_fit, |
+ const mojo::Callback<void(bool, skia::BitmapPtr)>& callback) { |
+ SkBitmap decoded_image; |
+ if (codec == IMAGE_CODEC_ROBUST_JPEG) { |
+ // Our robust jpeg decoding is using IJG libjpeg. |
+ if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG && |
+ encoded_data.size()) { |
+ scoped_ptr<SkBitmap> decoded_jpeg( |
+ gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size())); |
+ if (decoded_jpeg.get() && !decoded_jpeg->empty()) |
+ decoded_image = *decoded_jpeg; |
+ } |
+ } else { |
+ decoded_image = content::DecodeImage(&encoded_data[0], gfx::Size(), |
+ encoded_data.size()); |
+ } |
+ |
+ if (!decoded_image.isNull()) { |
+ skia::BitmapPtr dummy_image = skia::Bitmap::New(); |
+ int64_t struct_size = GetSerializedSize_(dummy_image); |
+ int64_t image_size = decoded_image.computeSize64(); |
+ int halves = 0; |
+ while (struct_size + (image_size >> 2 * halves) > max_message_size_) |
+ halves++; |
+ if (halves) { |
+ if (shrink_to_fit) { |
+ // If decoded image is too large for IPC message, shrink it by halves. |
+ // This prevents quality loss, and should never overshrink on displays |
+ // smaller than 3600x2400. |
+ // TODO (Issue 416916): Instead of shrinking, return via shared memory |
+ decoded_image = skia::ImageOperations::Resize( |
+ decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
+ decoded_image.width() >> halves, decoded_image.height() >> halves); |
+ } else { |
+ decoded_image.reset(); |
+ } |
+ } |
+ } |
+ |
+ if (decoded_image.isNull()) { |
+ callback.Run(false, nullptr); |
+ return; |
+ } |
+ |
+ callback.Run(true, skia::Bitmap::From(decoded_image)); |
+} |
+ |
+} // namespace services |