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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "gpu/command_buffer/client/gles2_interface_stub.h"
8 #include "media/base/video_frame.h"
9 #include "media/renderers/gpu_video_accelerator_factories.h"
10 #include "media/renderers/mock_gpu_video_accelerator_factories.h"
11 #include "media/video/gpu_memory_buffer_video_frame_pool.h"
12 #include "testing/gmock/include/gmock/gmock.h"
17 class TestGLES2Interface
: public gpu::gles2::GLES2InterfaceStub
{
19 unsigned gen_textures
= 0u;
20 void GenTextures(GLsizei n
, GLuint
* textures
) override
{
22 *textures
= ++gen_textures
;
25 GLuint
InsertSyncPointCHROMIUM() override
{ return ++sync_point
; }
27 void GenMailboxCHROMIUM(GLbyte
* mailbox
) override
{
28 *reinterpret_cast<unsigned*>(mailbox
) = ++this->mailbox
;
32 unsigned sync_point
= 0u;
33 unsigned mailbox
= 0u;
36 } // unnamed namespace
38 class GpuMemoryBufferVideoFramePoolTest
: public ::testing::Test
{
40 GpuMemoryBufferVideoFramePoolTest() {}
41 void SetUp() override
{ gles2_
.reset(new TestGLES2Interface
); }
43 void TearDown() override
{ base::RunLoop().RunUntilIdle(); }
45 static scoped_refptr
<media::VideoFrame
> CreateTestYUVVideoFrame(
47 const int kDimension
= 10;
48 static uint8 y_data
[kDimension
* kDimension
] = {0};
49 static uint8 u_data
[kDimension
* kDimension
/ 2] = {0};
50 static uint8 v_data
[kDimension
* kDimension
/ 2] = {0};
52 DCHECK_LE(dimension
, kDimension
);
53 gfx::Size
size(dimension
, dimension
);
55 return media::VideoFrame::WrapExternalYuvData(
56 media::VideoFrame::YV12
, // format
58 gfx::Rect(size
), // visible_rect
60 size
.width(), // y_stride
61 size
.width() / 2, // u_stride
62 size
.width() / 2, // v_stride
66 base::TimeDelta()); // timestamp
70 base::MessageLoop media_message_loop_
;
71 scoped_ptr
<TestGLES2Interface
> gles2_
;
74 TEST_F(GpuMemoryBufferVideoFramePoolTest
, NoGpuFactoryNoHardwareVideoFrame
) {
75 scoped_refptr
<VideoFrame
> frame
= CreateTestYUVVideoFrame(10);
76 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
77 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
78 media_message_loop_
.task_runner(), nullptr));
80 scoped_refptr
<VideoFrame
> frame2
=
81 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(frame
);
82 EXPECT_EQ(frame
.get(), frame2
.get());
85 TEST_F(GpuMemoryBufferVideoFramePoolTest
, NoTextureRGNoHardwareVideoFrame
) {
86 scoped_refptr
<VideoFrame
> frame
= CreateTestYUVVideoFrame(10);
87 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
88 new MockGpuVideoAcceleratorFactories
);
89 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
90 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
91 media_message_loop_
.task_runner(), mock_gpu_factories
));
93 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
94 .WillRepeatedly(testing::Return(false));
95 scoped_refptr
<VideoFrame
> frame2
=
96 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(frame
);
97 EXPECT_EQ(frame
.get(), frame2
.get());
100 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareFrame
) {
101 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
102 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
103 new MockGpuVideoAcceleratorFactories
);
104 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
105 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
106 media_message_loop_
.task_runner(), mock_gpu_factories
));
108 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
109 .WillRepeatedly(testing::Return(gles2_
.get()));
110 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
111 .WillRepeatedly(testing::Return(true));
113 scoped_refptr
<VideoFrame
> frame
=
114 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
115 EXPECT_NE(software_frame
.get(), frame
.get());
116 EXPECT_EQ(3u, gles2_
->gen_textures
);
119 TEST_F(GpuMemoryBufferVideoFramePoolTest
, ReuseFirstResource
) {
120 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
121 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
122 new MockGpuVideoAcceleratorFactories
);
123 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
124 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
125 media_message_loop_
.task_runner(), mock_gpu_factories
));
127 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
128 .WillRepeatedly(testing::Return(gles2_
.get()));
129 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
130 .WillRepeatedly(testing::Return(true));
132 scoped_refptr
<VideoFrame
> frame
=
133 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
134 EXPECT_NE(software_frame
.get(), frame
.get());
135 gpu::Mailbox mailbox
= frame
->mailbox_holder(0).mailbox
;
136 unsigned sync_point
= frame
->mailbox_holder(0).sync_point
;
137 EXPECT_EQ(3u, gles2_
->gen_textures
);
139 scoped_refptr
<VideoFrame
> frame2
=
140 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
141 EXPECT_NE(software_frame
.get(), frame2
.get());
142 EXPECT_NE(mailbox
, frame2
->mailbox_holder(0).mailbox
);
143 EXPECT_EQ(6u, gles2_
->gen_textures
);
147 base::RunLoop().RunUntilIdle(); // Run posted closures.
148 frame
= gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
149 EXPECT_NE(software_frame
.get(), frame
.get());
150 EXPECT_EQ(6u, gles2_
->gen_textures
);
151 EXPECT_EQ(frame
->mailbox_holder(0).mailbox
, mailbox
);
152 EXPECT_NE(frame
->mailbox_holder(0).sync_point
, sync_point
);
155 TEST_F(GpuMemoryBufferVideoFramePoolTest
, DropResourceWhenSizeIsDifferent
) {
156 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
157 new MockGpuVideoAcceleratorFactories
);
158 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
159 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
160 media_message_loop_
.task_runner(), mock_gpu_factories
));
162 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
163 .WillRepeatedly(testing::Return(gles2_
.get()));
164 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
165 .WillRepeatedly(testing::Return(true));
167 scoped_refptr
<VideoFrame
> frame
=
168 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
169 CreateTestYUVVideoFrame(10));
170 EXPECT_EQ(3u, gles2_
->gen_textures
);
173 base::RunLoop().RunUntilIdle(); // Run posted closures.
174 frame
= gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
175 CreateTestYUVVideoFrame(4));
176 EXPECT_EQ(6u, gles2_
->gen_textures
);