OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" | 5 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" |
| 6 |
6 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
7 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
8 #include "base/single_thread_task_runner.h" | |
9 #include "base/test/scoped_task_environment.h" | 10 #include "base/test/scoped_task_environment.h" |
10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
11 #include "gpu/ipc/service/gpu_channel.h" | 12 #include "media/base/media_switches.h" |
12 #include "media/gpu/ipc/common/media_messages.h" | 13 #include "mojo/public/cpp/system/platform_handle.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "services/service_manager/public/cpp/bind_source_info.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 using testing::_; | |
17 using testing::SaveArg; | |
18 using testing::InvokeWithoutArgs; | |
19 using testing::Return; | |
20 | |
21 namespace media { | 17 namespace media { |
22 | 18 |
23 static const size_t kOutputFrameSizeInBytes = 1024; | 19 static const int32_t kArbitraryBitstreamBufferId = 123; |
24 static const gfx::Size kDummyFrameCodedSize(10, 10); | |
25 | 20 |
26 class MockFilteredSender : public gpu::FilteredSender { | 21 // Test fixture for the unit that is created via the mojom interface for |
27 public: | |
28 MOCK_METHOD1(AddFilter, void(IPC::MessageFilter*)); | |
29 MOCK_METHOD1(RemoveFilter, void(IPC::MessageFilter*)); | |
30 MOCK_METHOD1(Send, bool(IPC::Message*)); | |
31 }; | |
32 | |
33 class MockJpegDecodeAccelerator : public media::JpegDecodeAccelerator { | |
34 public: | |
35 MOCK_METHOD1(Initialize, bool(Client*)); | |
36 MOCK_METHOD2(Decode, | |
37 void(const BitstreamBuffer&, | |
38 const scoped_refptr<media::VideoFrame>&)); | |
39 MOCK_METHOD0(IsSupported, bool()); | |
40 }; | |
41 | |
42 // Test fixture for the unit that is created via the constructor of | |
43 // class GpuJpegDecodeAccelerator. Uses a FakeJpegDecodeAccelerator to | 22 // class GpuJpegDecodeAccelerator. Uses a FakeJpegDecodeAccelerator to |
44 // simulate the actual decoding without the need for special hardware. | 23 // simulate the actual decoding without the need for special hardware. |
45 class GpuJpegDecodeAcceleratorTest : public ::testing::Test { | 24 class GpuJpegDecodeAcceleratorTest : public ::testing::Test { |
46 public: | 25 public: |
47 GpuJpegDecodeAcceleratorTest() : io_thread_("io") {} | 26 GpuJpegDecodeAcceleratorTest() : io_thread_("io") {} |
48 ~GpuJpegDecodeAcceleratorTest() override{}; | 27 ~GpuJpegDecodeAcceleratorTest() override{}; |
49 | 28 |
50 void SetUp() override { | 29 void SetUp() override { |
51 output_frame_memory_.CreateAndMapAnonymous(kOutputFrameSizeInBytes); | 30 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 31 switches::kUseFakeJpegDecodeAccelerator); |
52 io_thread_.Start(); | 32 io_thread_.Start(); |
53 std::vector<GpuJpegDecodeAcceleratorFactoryProvider::CreateAcceleratorCB> | |
54 accelerator_factory_functions; | |
55 accelerator_factory_functions.push_back( | |
56 base::Bind(&GpuJpegDecodeAcceleratorTest::GetMockJpegDecodeAccelerator, | |
57 base::Unretained(this))); | |
58 system_under_test_ = base::MakeUnique<GpuJpegDecodeAccelerator>( | |
59 &gpu_channel_, io_thread_.task_runner(), accelerator_factory_functions); | |
60 } | 33 } |
61 | 34 |
62 MOCK_METHOD1(GetMockJpegDecodeAccelerator, | 35 void OnDecodeAck(const base::Closure& continuation, |
63 std::unique_ptr<JpegDecodeAccelerator>( | 36 int32_t bitstream_buffer_id, |
64 scoped_refptr<base::SingleThreadTaskRunner>)); | 37 mojom::Error error) { |
65 | 38 EXPECT_EQ(kArbitraryBitstreamBufferId, bitstream_buffer_id); |
66 void SendStubFrame(IPC::MessageFilter* message_filter, int32_t route_id) { | 39 continuation.Run(); |
67 AcceleratedJpegDecoderMsg_Decode_Params decode_params; | |
68 decode_params.output_video_frame_handle = output_frame_memory_.handle(); | |
69 decode_params.output_buffer_size = | |
70 static_cast<uint32_t>(kOutputFrameSizeInBytes); | |
71 decode_params.coded_size = kDummyFrameCodedSize; | |
72 AcceleratedJpegDecoderMsg_Decode message(route_id, decode_params); | |
73 message_filter->OnMessageReceived(message); | |
74 } | 40 } |
75 | 41 |
76 protected: | 42 protected: |
77 base::Thread io_thread_; | 43 base::Thread io_thread_; |
78 MockFilteredSender gpu_channel_; | |
79 std::unique_ptr<GpuJpegDecodeAccelerator> system_under_test_; | |
80 base::SharedMemory output_frame_memory_; | |
81 | 44 |
82 private: | 45 private: |
83 // This is required to allow base::ThreadTaskRunnerHandle::Get() from the | 46 // This is required to allow base::ThreadTaskRunnerHandle::Get() from the |
84 // test execution thread. | 47 // test execution thread. |
85 base::test::ScopedTaskEnvironment scoped_task_environment_; | 48 base::test::ScopedTaskEnvironment scoped_task_environment_; |
86 }; | 49 }; |
87 | 50 |
88 // Tests that the communication for decoding a frame between the caller of | 51 TEST_F(GpuJpegDecodeAcceleratorTest, InitializeAndDecode) { |
89 // IPC:MessageFilter and the media::JpegDecodeAccelerator works as expected. | 52 mojom::GpuJpegDecodeAcceleratorPtr jpeg_decoder_; |
90 TEST_F(GpuJpegDecodeAcceleratorTest, DecodeFrameCallArrivesAtDecoder) { | 53 GpuJpegDecodeAccelerator::Create(io_thread_.task_runner(), |
91 static const int32_t kArbitraryRouteId = 123; | 54 service_manager::BindSourceInfo(), |
92 auto io_task_runner = io_thread_.task_runner(); | 55 mojo::MakeRequest(&jpeg_decoder_)); |
93 auto main_task_runner = base::ThreadTaskRunnerHandle::Get(); | |
94 auto decoder = base::MakeUnique<MockJpegDecodeAccelerator>(); | |
95 auto* decoder_ptr = decoder.get(); | |
96 ON_CALL(*decoder, Initialize(_)).WillByDefault(Return(true)); | |
97 | 56 |
98 IPC::MessageFilter* message_filter = nullptr; | 57 bool succeeded = false; |
99 EXPECT_CALL(*this, GetMockJpegDecodeAccelerator(_)) | 58 jpeg_decoder_->Initialize(&succeeded); |
100 .WillOnce(InvokeWithoutArgs([&decoder]() { return std::move(decoder); })); | 59 EXPECT_TRUE(succeeded); |
101 EXPECT_CALL(gpu_channel_, AddFilter(_)).WillOnce(SaveArg<0>(&message_filter)); | 60 |
| 61 const size_t kInputBufferSizeInBytes = 512; |
| 62 const size_t kOutputFrameSizeInBytes = 1024; |
| 63 const gfx::Size kDummyFrameCodedSize(10, 10); |
| 64 const char kKeyId[] = "key id"; |
| 65 const char kIv[] = "0123456789abcdef"; |
| 66 std::vector<SubsampleEntry> subsamples; |
| 67 subsamples.push_back(SubsampleEntry(10, 5)); |
| 68 subsamples.push_back(SubsampleEntry(15, 7)); |
| 69 |
102 base::RunLoop run_loop; | 70 base::RunLoop run_loop; |
103 auto response_cb = base::Bind( | 71 mojo::ScopedSharedBufferHandle input_buffer_handle = |
104 [](base::RunLoop* run_loop, | 72 mojo::SharedBufferHandle::Create(kInputBufferSizeInBytes); |
105 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 73 mojom::BitstreamBufferPtr buffer = mojom::BitstreamBuffer::New(); |
106 bool succeeded) { | 74 buffer->id = kArbitraryBitstreamBufferId; |
107 EXPECT_TRUE(succeeded); | 75 buffer->memory_handle = std::move(input_buffer_handle); |
108 if (main_task_runner->BelongsToCurrentThread() == false) { | 76 buffer->size = kInputBufferSizeInBytes; |
109 DLOG(INFO) << "Response arrived on thread other than main thread."; | 77 buffer->key_id = kKeyId; |
110 main_task_runner->PostTask(FROM_HERE, run_loop->QuitClosure()); | 78 buffer->iv = kIv; |
111 return; | 79 buffer->subsamples = subsamples; |
112 } | 80 |
113 run_loop->Quit(); | 81 mojo::ScopedSharedBufferHandle output_frame_handle = |
114 }, | 82 mojo::SharedBufferHandle::Create(kOutputFrameSizeInBytes); |
115 &run_loop, main_task_runner); | 83 |
116 system_under_test_->AddClient(kArbitraryRouteId, response_cb); | 84 jpeg_decoder_->Decode( |
| 85 std::move(buffer), kDummyFrameCodedSize, std::move(output_frame_handle), |
| 86 base::checked_cast<uint32_t>(kOutputFrameSizeInBytes), |
| 87 base::Bind(&GpuJpegDecodeAcceleratorTest::OnDecodeAck, |
| 88 base::Unretained(this), run_loop.QuitClosure())); |
117 run_loop.Run(); | 89 run_loop.Run(); |
118 ASSERT_TRUE(message_filter); | |
119 | |
120 // Send a AcceleratedJpegDecoderMsg_Decode to the |message_filter| on the | |
121 // io_thread. | |
122 base::RunLoop run_loop2; | |
123 EXPECT_CALL(*decoder_ptr, Decode(_, _)); | |
124 io_task_runner->PostTaskAndReply( | |
125 FROM_HERE, | |
126 base::Bind(&GpuJpegDecodeAcceleratorTest::SendStubFrame, | |
127 base::Unretained(this), message_filter, kArbitraryRouteId), | |
128 run_loop2.QuitClosure()); | |
129 run_loop2.Run(); | |
130 } | 90 } |
131 | 91 |
132 } // namespace media | 92 } // namespace media |
OLD | NEW |