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
, NoTextureRGNoHardwareVideoFrame
) {
104 scoped_refptr
<VideoFrame
> 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 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
112 .WillRepeatedly(testing::Return(false));
113 scoped_refptr
<VideoFrame
> frame2
;
114 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
115 frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame2
));
118 EXPECT_EQ(frame
.get(), frame2
.get());
121 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareFrame
) {
122 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
123 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
124 new MockGpuVideoAcceleratorFactories
);
125 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
126 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
127 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
129 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
130 .WillRepeatedly(testing::Return(gles2_
.get()));
131 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
132 .WillRepeatedly(testing::Return(true));
134 scoped_refptr
<VideoFrame
> frame
;
135 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
136 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
140 EXPECT_NE(software_frame
.get(), frame
.get());
141 EXPECT_EQ(3u, gles2_
->gen_textures
);
144 TEST_F(GpuMemoryBufferVideoFramePoolTest
, ReuseFirstResource
) {
145 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
146 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
147 new MockGpuVideoAcceleratorFactories
);
148 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
149 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
150 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
152 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
153 .WillRepeatedly(testing::Return(gles2_
.get()));
154 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
155 .WillRepeatedly(testing::Return(true));
157 scoped_refptr
<VideoFrame
> frame
;
158 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
159 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
162 EXPECT_NE(software_frame
.get(), frame
.get());
163 gpu::Mailbox mailbox
= frame
->mailbox_holder(0).mailbox
;
164 unsigned sync_point
= frame
->mailbox_holder(0).sync_point
;
165 EXPECT_EQ(3u, gles2_
->gen_textures
);
167 scoped_refptr
<VideoFrame
> frame2
;
168 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
169 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame2
));
172 EXPECT_NE(software_frame
.get(), frame2
.get());
173 EXPECT_NE(mailbox
, frame2
->mailbox_holder(0).mailbox
);
174 EXPECT_EQ(6u, gles2_
->gen_textures
);
180 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
181 software_frame
, base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
184 EXPECT_NE(software_frame
.get(), frame
.get());
185 EXPECT_EQ(6u, gles2_
->gen_textures
);
186 EXPECT_EQ(frame
->mailbox_holder(0).mailbox
, mailbox
);
187 EXPECT_NE(frame
->mailbox_holder(0).sync_point
, sync_point
);
190 TEST_F(GpuMemoryBufferVideoFramePoolTest
, DropResourceWhenSizeIsDifferent
) {
191 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
192 new MockGpuVideoAcceleratorFactories
);
193 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
194 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
195 media_task_runner_
, copy_task_runner_
.get(), mock_gpu_factories
));
197 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
198 .WillRepeatedly(testing::Return(gles2_
.get()));
199 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
200 .WillRepeatedly(testing::Return(true));
202 scoped_refptr
<VideoFrame
> frame
;
203 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
204 CreateTestYUVVideoFrame(10),
205 base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
208 EXPECT_EQ(3u, gles2_
->gen_textures
);
212 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
213 CreateTestYUVVideoFrame(4),
214 base::Bind(MaybeCreateHardwareFrameCallback
, &frame
));
216 EXPECT_EQ(6u, gles2_
->gen_textures
);