OLD | NEW |
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 "chrome/utility/image_decoder_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "ipc/ipc_channel.h" | 10 #include "ipc/ipc_channel.h" |
11 #include "skia/public/type_converters.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/gfx/codec/jpeg_codec.h" | 13 #include "ui/gfx/codec/jpeg_codec.h" |
15 | 14 |
16 namespace mojom { | 15 namespace mojom { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 bool CreateJPEGImage(int width, | 19 bool CreateJPEGImage(int width, |
21 int height, | 20 int height, |
(...skipping 17 matching lines...) Expand all Loading... |
39 class Request { | 38 class Request { |
40 public: | 39 public: |
41 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} | 40 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} |
42 | 41 |
43 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { | 42 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { |
44 decoder_->DecodeImage( | 43 decoder_->DecodeImage( |
45 mojo::Array<uint8_t>::From(image), ImageCodec::DEFAULT, | 44 mojo::Array<uint8_t>::From(image), ImageCodec::DEFAULT, |
46 shrink, base::Bind(&Request::OnRequestDone, base::Unretained(this))); | 45 shrink, base::Bind(&Request::OnRequestDone, base::Unretained(this))); |
47 } | 46 } |
48 | 47 |
49 const skia::mojom::BitmapPtr& bitmap() const { return bitmap_; } | 48 const SkBitmap& bitmap() const { return bitmap_; } |
50 | 49 |
51 private: | 50 private: |
52 void OnRequestDone(skia::mojom::BitmapPtr result_image) { | 51 void OnRequestDone(const SkBitmap& result_image) { bitmap_ = result_image; } |
53 bitmap_ = std::move(result_image); | |
54 } | |
55 | 52 |
56 ImageDecoderImpl* decoder_; | 53 ImageDecoderImpl* decoder_; |
57 skia::mojom::BitmapPtr bitmap_; | 54 SkBitmap bitmap_; |
58 }; | 55 }; |
59 | 56 |
60 } // namespace | 57 } // namespace |
61 | 58 |
62 // Test that DecodeImage() doesn't return image message > (max message size) | 59 // Test that DecodeImage() doesn't return image message > (max message size) |
63 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) { | 60 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) { |
64 // Using actual limit generates 14000 x 9400 images, which causes the test to | 61 // Using actual limit generates 14000 x 9400 images, which causes the test to |
65 // timeout. We test with a smaller limit for efficiency. | 62 // timeout. We test with a smaller limit for efficiency. |
66 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; | 63 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; |
67 | 64 |
68 ImageDecoderImpl decoder(kTestMessageSize); | 65 ImageDecoderImpl decoder(kTestMessageSize); |
69 | 66 |
70 // Approx max height for 3:2 image that will fit in IPC message; | 67 // Approx max height for 3:2 image that will fit in IPC message; |
71 // 1.5 for width/height ratio, 4 for bytes/pixel | 68 // 1.5 for width/height ratio, 4 for bytes/pixel |
72 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); | 69 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); |
73 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); | 70 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); |
74 | 71 |
75 // Sizes which should trigger dimension-halving 0, 1 and 2 times | 72 // Sizes which should trigger dimension-halving 0, 1 and 2 times |
76 int heights[] = {max_height_for_msg - 10, | 73 int heights[] = {max_height_for_msg - 10, |
77 max_height_for_msg + 10, | 74 max_height_for_msg + 10, |
78 2 * max_height_for_msg + 10}; | 75 2 * max_height_for_msg + 10}; |
79 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; | 76 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; |
80 for (size_t i = 0; i < arraysize(heights); i++) { | 77 for (size_t i = 0; i < arraysize(heights); i++) { |
81 std::vector<unsigned char> jpg; | 78 std::vector<unsigned char> jpg; |
82 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); | 79 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); |
83 | 80 |
84 Request request(&decoder); | 81 Request request(&decoder); |
85 request.DecodeImage(jpg, true); | 82 request.DecodeImage(jpg, true); |
86 ASSERT_FALSE(request.bitmap().is_null()); | 83 ASSERT_FALSE(request.bitmap().isNull()); |
87 SkBitmap bitmap = request.bitmap().To<SkBitmap>(); | |
88 | 84 |
89 // Check that image has been shrunk appropriately | 85 // Check that image has been shrunk appropriately |
90 EXPECT_LT(bitmap.computeSize64() + base_msg_size, | 86 EXPECT_LT(request.bitmap().computeSize64() + base_msg_size, |
91 static_cast<int64_t>(kTestMessageSize)); | 87 static_cast<int64_t>(kTestMessageSize)); |
92 // Android does its own image shrinking for memory conservation deeper in | 88 // Android does its own image shrinking for memory conservation deeper in |
93 // the decode, so more specific tests here won't work. | 89 // the decode, so more specific tests here won't work. |
94 #if !defined(OS_ANDROID) | 90 #if !defined(OS_ANDROID) |
95 EXPECT_EQ(widths[i] >> i, bitmap.width()); | 91 EXPECT_EQ(widths[i] >> i, request.bitmap().width()); |
96 EXPECT_EQ(heights[i] >> i, bitmap.height()); | 92 EXPECT_EQ(heights[i] >> i, request.bitmap().height()); |
97 | 93 |
98 // Check that if resize not requested and image exceeds IPC size limit, | 94 // Check that if resize not requested and image exceeds IPC size limit, |
99 // an empty image is returned | 95 // an empty image is returned |
100 if (heights[i] > max_height_for_msg) { | 96 if (heights[i] > max_height_for_msg) { |
101 Request request(&decoder); | 97 Request request(&decoder); |
102 request.DecodeImage(jpg, false); | 98 request.DecodeImage(jpg, false); |
103 EXPECT_TRUE(request.bitmap().is_null()); | 99 EXPECT_TRUE(request.bitmap().isNull()); |
104 } | 100 } |
105 #endif | 101 #endif |
106 } | 102 } |
107 } | 103 } |
108 | 104 |
109 TEST(ImageDecoderImplTest, DecodeImageFailed) { | 105 TEST(ImageDecoderImplTest, DecodeImageFailed) { |
110 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); | 106 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); |
111 | 107 |
112 // The "jpeg" is just some "random" data; | 108 // The "jpeg" is just some "random" data; |
113 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; | 109 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; |
114 std::vector<unsigned char> jpg(kRandomData, | 110 std::vector<unsigned char> jpg(kRandomData, |
115 kRandomData + sizeof(kRandomData)); | 111 kRandomData + sizeof(kRandomData)); |
116 | 112 |
117 Request request(&decoder); | 113 Request request(&decoder); |
118 request.DecodeImage(jpg, false); | 114 request.DecodeImage(jpg, false); |
119 EXPECT_TRUE(request.bitmap().is_null()); | 115 EXPECT_TRUE(request.bitmap().isNull()); |
120 } | 116 } |
121 | 117 |
122 } // namespace mojom | 118 } // namespace mojom |
OLD | NEW |