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.
6 #include "base/test/test_simple_task_runner.h"
7 #include "gpu/command_buffer/client/gles2_interface_stub.h"
8 #include "media/base/video_frame.h"
9 #include "media/renderers/mock_gpu_video_accelerator_factories.h"
10 #include "media/video/gpu_memory_buffer_video_frame_pool.h"
11 #include "testing/gmock/include/gmock/gmock.h"
16 class TestGLES2Interface
: public gpu::gles2::GLES2InterfaceStub
{
18 unsigned gen_textures
= 0u;
19 void GenTextures(GLsizei n
, GLuint
* textures
) override
{
21 *textures
= ++gen_textures
;
24 GLuint
InsertSyncPointCHROMIUM() override
{ return ++sync_point
; }
26 void GenMailboxCHROMIUM(GLbyte
* mailbox
) override
{
27 *reinterpret_cast<unsigned*>(mailbox
) = ++this->mailbox
;
31 unsigned sync_point
= 0u;
32 unsigned mailbox
= 0u;
35 } // unnamed namespace
37 class GpuMemoryBufferVideoFramePoolTest
: public ::testing::Test
{
39 GpuMemoryBufferVideoFramePoolTest() {}
40 void SetUp() override
{
41 gles2_
.reset(new TestGLES2Interface
);
42 media_task_runner_
= make_scoped_refptr(new base::TestSimpleTaskRunner
);
43 copy_task_runner_
= make_scoped_refptr(new base::TestSimpleTaskRunner
);
46 void TearDown() override
{ RunUntilIdle(); }
49 media_task_runner_
->RunUntilIdle();
50 copy_task_runner_
->RunUntilIdle();
51 media_task_runner_
->RunUntilIdle();
54 static scoped_refptr
<media::VideoFrame
> CreateTestYUVVideoFrame(
56 const int kDimension
= 10;
57 static uint8 y_data
[kDimension
* kDimension
] = {0};
58 static uint8 u_data
[kDimension
* kDimension
/ 2] = {0};
59 static uint8 v_data
[kDimension
* kDimension
/ 2] = {0};
61 DCHECK_LE(dimension
, kDimension
);
62 gfx::Size
size(dimension
, dimension
);
64 return media::VideoFrame::WrapExternalYuvData(
65 media::PIXEL_FORMAT_YV12
, // format
67 gfx::Rect(size
), // visible_rect
69 size
.width(), // y_stride
70 size
.width() / 2, // u_stride
71 size
.width() / 2, // v_stride
75 base::TimeDelta()); // timestamp
79 scoped_refptr
<base::TestSimpleTaskRunner
> media_task_runner_
;
80 scoped_refptr
<base::TestSimpleTaskRunner
> copy_task_runner_
;
81 scoped_ptr
<TestGLES2Interface
> gles2_
;
84 void MaybeCreateHardwareFrameCallback(
85 scoped_refptr
<VideoFrame
>* video_frame_output
,
86 const scoped_refptr
<VideoFrame
>& video_frame
) {
87 *video_frame_output
= video_frame
;
90 TEST_F(GpuMemoryBufferVideoFramePoolTest
, NoGpuFactoryNoHardwareVideoFrame
) {
91 scoped_refptr
<VideoFrame
> frame
= CreateTestYUVVideoFrame(10);
92 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
93 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
94 media_task_runner_
, copy_task_runner_
.get(), nullptr));
96 scoped_refptr
<VideoFrame
> frame2
;
97 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
98 frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame2
));
100 EXPECT_EQ(frame
.get(), frame2
.get());
103 TEST_F(GpuMemoryBufferVideoFramePoolTest
, VideoFrameOutputFormatUnknown
) {
104 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
105 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
106 new MockGpuVideoAcceleratorFactories
);
107 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
108 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
109 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
111 mock_gpu_factories
->SetVideoFrameOutputFormat(PIXEL_FORMAT_UNKNOWN
);
112 scoped_refptr
<VideoFrame
> frame
;
113 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
114 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
117 EXPECT_EQ(software_frame
.get(), frame
.get());
120 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareFrame
) {
121 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
122 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
123 new MockGpuVideoAcceleratorFactories
);
124 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
125 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
126 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
128 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
129 .WillRepeatedly(testing::Return(gles2_
.get()));
131 scoped_refptr
<VideoFrame
> frame
;
132 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
133 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
137 EXPECT_NE(software_frame
.get(), frame
.get());
138 EXPECT_EQ(3u, gles2_
->gen_textures
);
141 TEST_F(GpuMemoryBufferVideoFramePoolTest
, ReuseFirstResource
) {
142 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
143 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
144 new MockGpuVideoAcceleratorFactories
);
145 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
146 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
147 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
149 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
150 .WillRepeatedly(testing::Return(gles2_
.get()));
152 scoped_refptr
<VideoFrame
> frame
;
153 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
154 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
157 EXPECT_NE(software_frame
.get(), frame
.get());
158 gpu::Mailbox mailbox
= frame
->mailbox_holder(0).mailbox
;
159 unsigned sync_point
= frame
->mailbox_holder(0).sync_point
;
160 EXPECT_EQ(3u, gles2_
->gen_textures
);
162 scoped_refptr
<VideoFrame
> frame2
;
163 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
164 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame2
));
167 EXPECT_NE(software_frame
.get(), frame2
.get());
168 EXPECT_NE(mailbox
, frame2
->mailbox_holder(0).mailbox
);
169 EXPECT_EQ(6u, gles2_
->gen_textures
);
175 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
176 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
179 EXPECT_NE(software_frame
.get(), frame
.get());
180 EXPECT_EQ(6u, gles2_
->gen_textures
);
181 EXPECT_EQ(frame
->mailbox_holder(0).mailbox
, mailbox
);
182 EXPECT_NE(frame
->mailbox_holder(0).sync_point
, sync_point
);
185 TEST_F(GpuMemoryBufferVideoFramePoolTest
, DropResourceWhenSizeIsDifferent
) {
186 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
187 new MockGpuVideoAcceleratorFactories
);
188 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
189 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
190 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
192 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
193 .WillRepeatedly(testing::Return(gles2_
.get()));
195 scoped_refptr
<VideoFrame
> frame
;
196 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
197 CreateTestYUVVideoFrame(10),
198 base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
201 EXPECT_EQ(3u, gles2_
->gen_textures
);
205 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
206 CreateTestYUVVideoFrame(4),
207 base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
209 EXPECT_EQ(6u, gles2_
->gen_textures
);
212 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareUYUVFrame
) {
213 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
214 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
215 new MockGpuVideoAcceleratorFactories
);
216 mock_gpu_factories
->SetVideoFrameOutputFormat(PIXEL_FORMAT_UYVY
);
217 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
218 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
219 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
221 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
222 .WillRepeatedly(testing::Return(gles2_
.get()));
224 scoped_refptr
<VideoFrame
> frame
;
225 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
226 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
230 EXPECT_NE(software_frame
.get(), frame
.get());
231 EXPECT_EQ(1u, gles2_
->gen_textures
);
234 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareNV12Frame
) {
235 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
236 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
237 new MockGpuVideoAcceleratorFactories
);
238 mock_gpu_factories
->SetVideoFrameOutputFormat(PIXEL_FORMAT_NV12
);
239 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
240 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
241 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
243 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
244 .WillRepeatedly(testing::Return(gles2_
.get()));
246 scoped_refptr
<VideoFrame
> frame
;
247 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
248 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
252 EXPECT_NE(software_frame
.get(), frame
.get());
253 EXPECT_EQ(1u, gles2_
->gen_textures
);
256 // AllocateGpuMemoryBuffer can return null (e.g: when the GPU process is down).
257 // This test checks that in that case we don't crash and still create the
259 TEST_F(GpuMemoryBufferVideoFramePoolTest
, AllocateGpuMemoryBufferBFail
) {
260 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
261 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
262 new MockGpuVideoAcceleratorFactories
);
263 mock_gpu_factories
->SetFailToAllocateGpuMemoryBufferForTesting(true);
264 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
265 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
266 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
268 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
269 .WillRepeatedly(testing::Return(gles2_
.get()));
271 scoped_refptr
<VideoFrame
> frame
;
272 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
273 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
277 EXPECT_NE(software_frame
.get(), frame
.get());
278 EXPECT_EQ(3u, gles2_
->gen_textures
);