1 // Copyright 2014 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.
7 #include "cc/test/test_context_provider.h"
8 #include "cc/test/test_web_graphics_context_3d.h"
9 #include "content/browser/compositor/buffer_queue.h"
10 #include "content/browser/compositor/gpu_surfaceless_browser_compositor_output_surface.h"
11 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
12 #include "gpu/GLES2/gl2extchromium.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/khronos/GLES2/gl2ext.h"
18 using ::testing::Expectation
;
20 using ::testing::Return
;
24 class StubGpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
26 StubGpuMemoryBufferImpl() {}
28 // Overridden from gfx::GpuMemoryBuffer:
29 void* Map() override
{ return nullptr; }
30 void Unmap() override
{}
31 bool IsMapped() const override
{ return false; }
32 Format
GetFormat() const override
{ return gfx::GpuMemoryBuffer::RGBX_8888
; }
33 uint32
GetStride() const override
{ return 0; }
34 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
35 return gfx::GpuMemoryBufferHandle();
37 ClientBuffer
AsClientBuffer() override
{
38 return reinterpret_cast<ClientBuffer
>(this);
42 class StubBrowserGpuMemoryBufferManager
: public BrowserGpuMemoryBufferManager
{
44 StubBrowserGpuMemoryBufferManager()
45 : BrowserGpuMemoryBufferManager(nullptr, 1) {}
47 scoped_ptr
<gfx::GpuMemoryBuffer
> AllocateGpuMemoryBufferForScanout(
48 const gfx::Size
& size
,
49 gfx::GpuMemoryBuffer::Format format
,
50 int32 surface_id
) override
{
51 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(new StubGpuMemoryBufferImpl
);
55 class MockBufferQueue
: public BufferQueue
{
57 MockBufferQueue(scoped_refptr
<cc::ContextProvider
> context_provider
,
58 BrowserGpuMemoryBufferManager
* gpu_memory_buffer_manager
,
59 unsigned int internalformat
)
60 : BufferQueue(context_provider
,
63 gpu_memory_buffer_manager
,
65 MOCK_METHOD4(CopyBufferDamage
,
66 void(int, int, const gfx::Rect
&, const gfx::Rect
&));
69 class BufferQueueTest
: public ::testing::Test
{
71 BufferQueueTest() : doublebuffering_(true), first_frame_(true) {}
73 void SetUp() override
{
74 scoped_refptr
<cc::TestContextProvider
> context_provider
=
75 cc::TestContextProvider::Create(cc::TestWebGraphicsContext3D::Create());
76 context_provider
->BindToCurrentThread();
77 gpu_memory_buffer_manager_
.reset(new StubBrowserGpuMemoryBufferManager
);
78 output_surface_
.reset(new MockBufferQueue(
79 context_provider
, gpu_memory_buffer_manager_
.get(), GL_RGBA
));
80 output_surface_
->Initialize();
83 unsigned current_surface() { return output_surface_
->current_surface_
.image
; }
84 const std::vector
<BufferQueue::AllocatedSurface
>& available_surfaces() {
85 return output_surface_
->available_surfaces_
;
87 const std::deque
<BufferQueue::AllocatedSurface
>& in_flight_surfaces() {
88 return output_surface_
->in_flight_surfaces_
;
91 const BufferQueue::AllocatedSurface
& last_frame() {
92 return output_surface_
->in_flight_surfaces_
.back();
94 const BufferQueue::AllocatedSurface
& next_frame() {
95 return output_surface_
->available_surfaces_
.back();
97 const gfx::Size
size() { return output_surface_
->size_
; }
100 int n
= available_surfaces().size() + in_flight_surfaces().size();
101 if (current_surface())
106 // Check that each buffer is unique if present.
108 std::set
<unsigned> buffers
;
109 EXPECT_TRUE(InsertUnique(&buffers
, current_surface()));
110 for (size_t i
= 0; i
< available_surfaces().size(); i
++)
111 EXPECT_TRUE(InsertUnique(&buffers
, available_surfaces()[i
].image
));
112 for (std::deque
<BufferQueue::AllocatedSurface
>::const_iterator it
=
113 in_flight_surfaces().begin();
114 it
!= in_flight_surfaces().end();
116 EXPECT_TRUE(InsertUnique(&buffers
, it
->image
));
120 output_surface_
->SwapBuffers(gfx::Rect(output_surface_
->size_
));
123 void SendDamagedFrame(const gfx::Rect
& damage
) {
124 // We don't care about the GL-level implementation here, just how it uses
126 output_surface_
->BindFramebuffer();
127 output_surface_
->SwapBuffers(damage
);
128 if (doublebuffering_
|| !first_frame_
)
129 output_surface_
->PageFlipComplete();
130 first_frame_
= false;
133 void SendFullFrame() { SendDamagedFrame(gfx::Rect(output_surface_
->size_
)); }
136 bool InsertUnique(std::set
<unsigned>* set
, unsigned value
) {
139 if (set
->find(value
) != set
->end())
145 scoped_ptr
<BrowserGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
146 scoped_ptr
<MockBufferQueue
> output_surface_
;
147 bool doublebuffering_
;
152 const gfx::Size screen_size
= gfx::Size(30, 30);
153 const gfx::Rect screen_rect
= gfx::Rect(screen_size
);
154 const gfx::Rect small_damage
= gfx::Rect(gfx::Size(10, 10));
155 const gfx::Rect large_damage
= gfx::Rect(gfx::Size(20, 20));
156 const gfx::Rect overlapping_damage
= gfx::Rect(gfx::Size(5, 20));
158 class MockedContext
: public cc::TestWebGraphicsContext3D
{
160 MOCK_METHOD2(bindFramebuffer
, void(GLenum
, GLuint
));
161 MOCK_METHOD2(bindTexture
, void(GLenum
, GLuint
));
162 MOCK_METHOD2(bindTexImage2DCHROMIUM
, void(GLenum
, GLint
));
163 MOCK_METHOD4(createImageCHROMIUM
,
164 GLuint(ClientBuffer
, GLsizei
, GLsizei
, GLenum
));
165 MOCK_METHOD1(destroyImageCHROMIUM
, void(GLuint
));
166 MOCK_METHOD5(framebufferTexture2D
,
167 void(GLenum
, GLenum
, GLenum
, GLuint
, GLint
));
170 scoped_ptr
<BufferQueue
> CreateOutputSurfaceWithMock(
171 MockedContext
** context
,
172 BrowserGpuMemoryBufferManager
* gpu_memory_buffer_manager
) {
173 *context
= new MockedContext();
174 scoped_refptr
<cc::TestContextProvider
> context_provider
=
175 cc::TestContextProvider::Create(
176 scoped_ptr
<cc::TestWebGraphicsContext3D
>(*context
));
177 context_provider
->BindToCurrentThread();
178 scoped_ptr
<BufferQueue
> buffer_queue(new BufferQueue(
179 context_provider
, GL_RGBA
, nullptr, gpu_memory_buffer_manager
, 1));
180 buffer_queue
->Initialize();
181 return buffer_queue
.Pass();
184 TEST(BufferQueueStandaloneTest
, FboInitialization
) {
185 MockedContext
* context
;
186 scoped_ptr
<BrowserGpuMemoryBufferManager
> gpu_memory_buffer_manager(
187 new StubBrowserGpuMemoryBufferManager
);
188 scoped_ptr
<BufferQueue
> output_surface
=
189 CreateOutputSurfaceWithMock(&context
, gpu_memory_buffer_manager
.get());
191 EXPECT_CALL(*context
, bindFramebuffer(GL_FRAMEBUFFER
, Ne(0U)));
192 ON_CALL(*context
, framebufferTexture2D(_
, _
, _
, _
, _
))
193 .WillByDefault(Return());
195 output_surface
->Reshape(gfx::Size(10, 20), 1.0f
);
198 TEST(BufferQueueStandaloneTest
, FboBinding
) {
199 MockedContext
* context
;
200 scoped_ptr
<BrowserGpuMemoryBufferManager
> gpu_memory_buffer_manager(
201 new StubBrowserGpuMemoryBufferManager
);
202 scoped_ptr
<BufferQueue
> output_surface
=
203 CreateOutputSurfaceWithMock(&context
, gpu_memory_buffer_manager
.get());
204 EXPECT_CALL(*context
, bindTexture(GL_TEXTURE_2D
, Ne(0U)));
205 EXPECT_CALL(*context
, destroyImageCHROMIUM(1));
207 EXPECT_CALL(*context
, createImageCHROMIUM(_
, 0, 0, GL_RGBA
))
208 .WillOnce(Return(1));
210 EXPECT_CALL(*context
, bindFramebuffer(GL_FRAMEBUFFER
, Ne(0U)));
211 Expectation tex
= EXPECT_CALL(*context
, bindTexture(GL_TEXTURE_2D
, Ne(0U)));
212 Expectation bind_tex
=
213 EXPECT_CALL(*context
, bindTexImage2DCHROMIUM(GL_TEXTURE_2D
, 1))
217 framebufferTexture2D(
218 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, Ne(0U), _
))
219 .After(fb
, bind_tex
);
221 output_surface
->BindFramebuffer();
224 TEST_F(BufferQueueTest
, PartialSwapReuse
) {
226 output_surface_
->Reshape(screen_size
, 1.0f
);
227 ASSERT_TRUE(doublebuffering_
);
228 EXPECT_CALL(*output_surface_
,
229 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
230 EXPECT_CALL(*output_surface_
,
231 CopyBufferDamage(_
, _
, small_damage
, small_damage
)).Times(1);
232 EXPECT_CALL(*output_surface_
,
233 CopyBufferDamage(_
, _
, large_damage
, small_damage
)).Times(1);
235 SendDamagedFrame(small_damage
);
236 SendDamagedFrame(small_damage
);
237 SendDamagedFrame(large_damage
);
238 // Verify that the damage has propagated.
239 EXPECT_EQ(next_frame().damage
, large_damage
);
242 TEST_F(BufferQueueTest
, PartialSwapFullFrame
) {
243 output_surface_
->Reshape(screen_size
, 1.0f
);
244 ASSERT_TRUE(doublebuffering_
);
245 EXPECT_CALL(*output_surface_
,
246 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
248 SendDamagedFrame(small_damage
);
251 EXPECT_EQ(next_frame().damage
, screen_rect
);
254 TEST_F(BufferQueueTest
, PartialSwapOverlapping
) {
255 output_surface_
->Reshape(screen_size
, 1.0f
);
256 ASSERT_TRUE(doublebuffering_
);
257 EXPECT_CALL(*output_surface_
,
258 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
259 EXPECT_CALL(*output_surface_
,
260 CopyBufferDamage(_
, _
, overlapping_damage
, small_damage
))
264 SendDamagedFrame(small_damage
);
265 SendDamagedFrame(overlapping_damage
);
266 EXPECT_EQ(next_frame().damage
, overlapping_damage
);
269 TEST_F(BufferQueueTest
, MultipleBindCalls
) {
270 // Check that multiple bind calls do not create or change surfaces.
271 output_surface_
->BindFramebuffer();
272 EXPECT_EQ(1, CountBuffers());
273 unsigned int fb
= current_surface();
274 output_surface_
->BindFramebuffer();
275 EXPECT_EQ(1, CountBuffers());
276 EXPECT_EQ(fb
, current_surface());
279 TEST_F(BufferQueueTest
, CheckDoubleBuffering
) {
280 // Check buffer flow through double buffering path.
281 EXPECT_EQ(0, CountBuffers());
282 output_surface_
->BindFramebuffer();
283 EXPECT_EQ(1, CountBuffers());
284 EXPECT_NE(0U, current_surface());
286 EXPECT_EQ(1U, in_flight_surfaces().size());
287 output_surface_
->PageFlipComplete();
288 EXPECT_EQ(1U, in_flight_surfaces().size());
289 output_surface_
->BindFramebuffer();
290 EXPECT_EQ(2, CountBuffers());
292 EXPECT_NE(0U, current_surface());
293 EXPECT_EQ(1U, in_flight_surfaces().size());
296 EXPECT_EQ(2U, in_flight_surfaces().size());
297 output_surface_
->PageFlipComplete();
299 EXPECT_EQ(1U, in_flight_surfaces().size());
300 EXPECT_EQ(1U, available_surfaces().size());
301 output_surface_
->BindFramebuffer();
302 EXPECT_EQ(2, CountBuffers());
304 EXPECT_TRUE(available_surfaces().empty());
307 TEST_F(BufferQueueTest
, CheckTripleBuffering
) {
308 // Check buffer flow through triple buffering path.
310 // This bit is the same sequence tested in the doublebuffering case.
311 output_surface_
->BindFramebuffer();
313 output_surface_
->PageFlipComplete();
314 output_surface_
->BindFramebuffer();
317 EXPECT_EQ(2, CountBuffers());
319 EXPECT_EQ(2U, in_flight_surfaces().size());
320 output_surface_
->BindFramebuffer();
321 EXPECT_EQ(3, CountBuffers());
323 EXPECT_NE(0U, current_surface());
324 EXPECT_EQ(2U, in_flight_surfaces().size());
325 output_surface_
->PageFlipComplete();
326 EXPECT_EQ(3, CountBuffers());
328 EXPECT_NE(0U, current_surface());
329 EXPECT_EQ(1U, in_flight_surfaces().size());
330 EXPECT_EQ(1U, available_surfaces().size());
334 } // namespace content