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/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
{ gles2_
.reset(new TestGLES2Interface
); }
42 void TearDown() override
{ base::RunLoop().RunUntilIdle(); }
44 static scoped_refptr
<media::VideoFrame
> CreateTestYUVVideoFrame(
46 const int kDimension
= 10;
47 static uint8 y_data
[kDimension
* kDimension
] = {0};
48 static uint8 u_data
[kDimension
* kDimension
/ 2] = {0};
49 static uint8 v_data
[kDimension
* kDimension
/ 2] = {0};
51 DCHECK_LE(dimension
, kDimension
);
52 gfx::Size
size(dimension
, dimension
);
54 return media::VideoFrame::WrapExternalYuvData(
55 media::PIXEL_FORMAT_YV12
, // format
57 gfx::Rect(size
), // visible_rect
59 size
.width(), // y_stride
60 size
.width() / 2, // u_stride
61 size
.width() / 2, // v_stride
65 base::TimeDelta()); // timestamp
69 base::MessageLoop media_message_loop_
;
70 scoped_ptr
<TestGLES2Interface
> gles2_
;
73 TEST_F(GpuMemoryBufferVideoFramePoolTest
, NoGpuFactoryNoHardwareVideoFrame
) {
74 scoped_refptr
<VideoFrame
> frame
= CreateTestYUVVideoFrame(10);
75 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
76 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
77 media_message_loop_
.task_runner(), nullptr));
79 scoped_refptr
<VideoFrame
> frame2
=
80 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(frame
);
81 EXPECT_EQ(frame
.get(), frame2
.get());
84 TEST_F(GpuMemoryBufferVideoFramePoolTest
, NoTextureRGNoHardwareVideoFrame
) {
85 scoped_refptr
<VideoFrame
> frame
= CreateTestYUVVideoFrame(10);
86 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
87 new MockGpuVideoAcceleratorFactories
);
88 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
89 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
90 media_message_loop_
.task_runner(), mock_gpu_factories
));
92 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
93 .WillRepeatedly(testing::Return(false));
94 scoped_refptr
<VideoFrame
> frame2
=
95 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(frame
);
96 EXPECT_EQ(frame
.get(), frame2
.get());
99 TEST_F(GpuMemoryBufferVideoFramePoolTest
, CreateOneHardwareFrame
) {
100 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
101 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
102 new MockGpuVideoAcceleratorFactories
);
103 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
104 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
105 media_message_loop_
.task_runner(), mock_gpu_factories
));
107 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
108 .WillRepeatedly(testing::Return(gles2_
.get()));
109 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
110 .WillRepeatedly(testing::Return(true));
112 scoped_refptr
<VideoFrame
> frame
=
113 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
114 EXPECT_NE(software_frame
.get(), frame
.get());
115 EXPECT_EQ(3u, gles2_
->gen_textures
);
118 TEST_F(GpuMemoryBufferVideoFramePoolTest
, ReuseFirstResource
) {
119 scoped_refptr
<VideoFrame
> software_frame
= CreateTestYUVVideoFrame(10);
120 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
121 new MockGpuVideoAcceleratorFactories
);
122 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
123 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
124 media_message_loop_
.task_runner(), mock_gpu_factories
));
126 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
127 .WillRepeatedly(testing::Return(gles2_
.get()));
128 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
129 .WillRepeatedly(testing::Return(true));
131 scoped_refptr
<VideoFrame
> frame
=
132 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
133 EXPECT_NE(software_frame
.get(), frame
.get());
134 gpu::Mailbox mailbox
= frame
->mailbox_holder(0).mailbox
;
135 unsigned sync_point
= frame
->mailbox_holder(0).sync_point
;
136 EXPECT_EQ(3u, gles2_
->gen_textures
);
138 scoped_refptr
<VideoFrame
> frame2
=
139 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
140 EXPECT_NE(software_frame
.get(), frame2
.get());
141 EXPECT_NE(mailbox
, frame2
->mailbox_holder(0).mailbox
);
142 EXPECT_EQ(6u, gles2_
->gen_textures
);
146 base::RunLoop().RunUntilIdle(); // Run posted closures.
147 frame
= gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(software_frame
);
148 EXPECT_NE(software_frame
.get(), frame
.get());
149 EXPECT_EQ(6u, gles2_
->gen_textures
);
150 EXPECT_EQ(frame
->mailbox_holder(0).mailbox
, mailbox
);
151 EXPECT_NE(frame
->mailbox_holder(0).sync_point
, sync_point
);
154 TEST_F(GpuMemoryBufferVideoFramePoolTest
, DropResourceWhenSizeIsDifferent
) {
155 scoped_refptr
<MockGpuVideoAcceleratorFactories
> mock_gpu_factories(
156 new MockGpuVideoAcceleratorFactories
);
157 scoped_ptr
<GpuMemoryBufferVideoFramePool
> gpu_memory_buffer_pool_
=
158 make_scoped_ptr(new GpuMemoryBufferVideoFramePool(
159 media_message_loop_
.task_runner(), mock_gpu_factories
));
161 EXPECT_CALL(*mock_gpu_factories
.get(), GetGLES2Interface())
162 .WillRepeatedly(testing::Return(gles2_
.get()));
163 EXPECT_CALL(*mock_gpu_factories
.get(), IsTextureRGSupported())
164 .WillRepeatedly(testing::Return(true));
166 scoped_refptr
<VideoFrame
> frame
=
167 gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
168 CreateTestYUVVideoFrame(10));
169 EXPECT_EQ(3u, gles2_
->gen_textures
);
172 base::RunLoop().RunUntilIdle(); // Run posted closures.
173 frame
= gpu_memory_buffer_pool_
->MaybeCreateHardwareFrame(
174 CreateTestYUVVideoFrame(4));
175 EXPECT_EQ(6u, gles2_
->gen_textures
);