Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: services/image_decoder/image_decoder_impl.cc

Issue 2475543003: Introduce the image_decoder service (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/utility/image_decoder_impl.h" 5 #include "services/image_decoder/image_decoder_impl.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "content/public/child/image_decoder_utils.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" 13 #include "mojo/public/cpp/bindings/strong_binding.h"
15 #include "skia/ext/image_operations.h" 14 #include "skia/ext/image_operations.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/geometry/size.h" 16 #include "ui/gfx/geometry/size.h"
18 17
19 #if defined(OS_CHROMEOS) 18 #if defined(OS_CHROMEOS)
20 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h" 19 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h"
21 #include "ui/gfx/codec/png_codec.h" 20 #include "ui/gfx/codec/png_codec.h"
22 #endif 21 #endif
23 22
23 namespace image_decoder {
24
24 namespace { 25 namespace {
25 int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; 26
26 int64_t kPadding = 64; 27 int64_t kPadding = 64;
27 }
28 28
29 ImageDecoderImpl::ImageDecoderImpl() : ImageDecoderImpl(kMaxMessageSize) {} 29 } // namespace
30 30
31 ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) 31 ImageDecoderImpl::ImageDecoderImpl(
32 : max_message_size_(max_message_size) {} 32 std::unique_ptr<service_manager::ServiceContextRef> service_ref)
33 : service_ref_(std::move(service_ref)) {}
33 34
34 ImageDecoderImpl::~ImageDecoderImpl() { 35 ImageDecoderImpl::~ImageDecoderImpl() = default;
35 }
36 36
37 void ImageDecoderImpl::DecodeImage(const std::vector<uint8_t>& encoded_data, 37 void ImageDecoderImpl::DecodeImage(const std::vector<uint8_t>& encoded_data,
38 mojom::ImageCodec codec, 38 mojom::ImageCodec codec,
39 bool shrink_to_fit, 39 bool shrink_to_fit,
40 int64_t max_size_in_bytes,
40 const DecodeImageCallback& callback) { 41 const DecodeImageCallback& callback) {
41 if (encoded_data.empty()) { 42 if (encoded_data.size() == 0) {
42 callback.Run(SkBitmap()); 43 callback.Run(SkBitmap());
43 return; 44 return;
44 } 45 }
45 46
46 SkBitmap decoded_image; 47 SkBitmap decoded_image;
47 #if defined(OS_CHROMEOS) 48 #if defined(OS_CHROMEOS)
48 if (codec == mojom::ImageCodec::ROBUST_JPEG) { 49 if (codec == mojom::ImageCodec::ROBUST_JPEG) {
49 // Our robust jpeg decoding is using IJG libjpeg. 50 // Our robust jpeg decoding is using IJG libjpeg.
50 std::unique_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodecRobustSlow::Decode( 51 if (encoded_data.size()) {
51 encoded_data.data(), encoded_data.size())); 52 std::unique_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodecRobustSlow::Decode(
52 if (decoded_jpeg.get() && !decoded_jpeg->empty()) 53 encoded_data.data(), encoded_data.size()));
53 decoded_image = *decoded_jpeg; 54 if (decoded_jpeg.get() && !decoded_jpeg->empty())
55 decoded_image = *decoded_jpeg;
56 }
54 } else if (codec == mojom::ImageCodec::ROBUST_PNG) { 57 } else if (codec == mojom::ImageCodec::ROBUST_PNG) {
55 // Our robust PNG decoding is using libpng. 58 // Our robust PNG decoding is using libpng.
56 SkBitmap decoded_png; 59 if (encoded_data.size()) {
57 if (gfx::PNGCodec::Decode(encoded_data.data(), encoded_data.size(), 60 SkBitmap decoded_png;
58 &decoded_png)) { 61 if (gfx::PNGCodec::Decode(
59 decoded_image = decoded_png; 62 encoded_data.data(), encoded_data.size(), &decoded_png)) {
63 decoded_image = decoded_png;
64 }
60 } 65 }
61 } 66 }
62 #endif // defined(OS_CHROMEOS) 67 #endif // defined(OS_CHROMEOS)
63 if (codec == mojom::ImageCodec::DEFAULT) { 68 if (codec == mojom::ImageCodec::DEFAULT) {
64 decoded_image = content::DecodeImage(encoded_data.data(), gfx::Size(), 69 decoded_image = content::DecodeImage(
65 encoded_data.size()); 70 encoded_data.data(), gfx::Size(), encoded_data.size());
66 } 71 }
67 72
68 if (!decoded_image.isNull()) { 73 if (!decoded_image.isNull()) {
69 // When serialized, the space taken up by a skia::mojom::Bitmap excluding 74 // When serialized, the space taken up by a skia::mojom::Bitmap excluding
70 // the pixel data payload should be: 75 // the pixel data payload should be:
71 // sizeof(skia::mojom::Bitmap::Data_) + pixel data array header (8 bytes) 76 // sizeof(skia::mojom::Bitmap::Data_) + pixel data array header (8 bytes)
72 // Use a bigger number in case we need padding at the end. 77 // Use a bigger number in case we need padding at the end.
73 int64_t struct_size = sizeof(skia::mojom::Bitmap::Data_) + kPadding; 78 int64_t struct_size = sizeof(skia::mojom::Bitmap::Data_) + kPadding;
74 int64_t image_size = decoded_image.computeSize64(); 79 int64_t image_size = decoded_image.computeSize64();
75 int halves = 0; 80 int halves = 0;
76 while (struct_size + (image_size >> 2 * halves) > max_message_size_) 81 while (struct_size + (image_size >> 2 * halves) > max_size_in_bytes)
77 halves++; 82 halves++;
78 if (halves) { 83 if (halves) {
84 // If the decoded image is too large, either discard it or shrink it.
85 //
86 // TODO(rockot): Also support exposing the bytes via shared memory for
87 // larger images. https://crbug.com/416916.
79 if (shrink_to_fit) { 88 if (shrink_to_fit) {
80 // If decoded image is too large for IPC message, shrink it by halves. 89 // Shrinking by halves prevents quality loss and should never overshrink
81 // This prevents quality loss, and should never overshrink on displays 90 // on displays smaller than 3600x2400.
82 // smaller than 3600x2400.
83 // TODO (Issue 416916): Instead of shrinking, return via shared memory
84 decoded_image = skia::ImageOperations::Resize( 91 decoded_image = skia::ImageOperations::Resize(
85 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, 92 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3,
86 decoded_image.width() >> halves, decoded_image.height() >> halves); 93 decoded_image.width() >> halves, decoded_image.height() >> halves);
87 } else { 94 } else {
88 decoded_image.reset(); 95 decoded_image.reset();
89 } 96 }
90 } 97 }
91 } 98 }
92 99
93 callback.Run(decoded_image); 100 callback.Run(decoded_image);
94 } 101 }
102
103 } // namespace image_decoder
OLDNEW
« no previous file with comments | « services/image_decoder/image_decoder_impl.h ('k') | services/image_decoder/image_decoder_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698