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 "gpu/GLES2/gl2extchromium.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/khronos/GLES2/gl2ext.h"
17 using ::testing::Expectation
;
19 using ::testing::Return
;
22 class MockBufferQueue
: public BufferQueue
{
24 MockBufferQueue(scoped_refptr
<cc::ContextProvider
> context_provider
,
25 unsigned int internalformat
)
26 : BufferQueue(context_provider
, internalformat
, nullptr) {}
27 MOCK_METHOD4(CopyBufferDamage
,
28 void(int, int, const gfx::Rect
&, const gfx::Rect
&));
31 class BufferQueueTest
: public ::testing::Test
{
33 BufferQueueTest() : doublebuffering_(true), first_frame_(true) {}
35 void SetUp() override
{
36 scoped_refptr
<cc::TestContextProvider
> context_provider
=
37 cc::TestContextProvider::Create(cc::TestWebGraphicsContext3D::Create());
38 context_provider
->BindToCurrentThread();
39 output_surface_
.reset(new MockBufferQueue(context_provider
, GL_RGBA
));
40 output_surface_
->Initialize();
43 unsigned current_surface() { return output_surface_
->current_surface_
.image
; }
44 const std::vector
<BufferQueue::AllocatedSurface
>& available_surfaces() {
45 return output_surface_
->available_surfaces_
;
47 const std::deque
<BufferQueue::AllocatedSurface
>& in_flight_surfaces() {
48 return output_surface_
->in_flight_surfaces_
;
51 const BufferQueue::AllocatedSurface
& last_frame() {
52 return output_surface_
->in_flight_surfaces_
.back();
54 const BufferQueue::AllocatedSurface
& next_frame() {
55 return output_surface_
->available_surfaces_
.back();
57 const gfx::Size
size() { return output_surface_
->size_
; }
60 int n
= available_surfaces().size() + in_flight_surfaces().size();
61 if (current_surface())
66 // Check that each buffer is unique if present.
68 std::set
<unsigned> buffers
;
69 EXPECT_TRUE(InsertUnique(&buffers
, current_surface()));
70 for (size_t i
= 0; i
< available_surfaces().size(); i
++)
71 EXPECT_TRUE(InsertUnique(&buffers
, available_surfaces()[i
].image
));
72 for (std::deque
<BufferQueue::AllocatedSurface
>::const_iterator it
=
73 in_flight_surfaces().begin();
74 it
!= in_flight_surfaces().end();
76 EXPECT_TRUE(InsertUnique(&buffers
, it
->image
));
80 output_surface_
->SwapBuffers(gfx::Rect(output_surface_
->size_
));
83 void SendDamagedFrame(const gfx::Rect
& damage
) {
84 // We don't care about the GL-level implementation here, just how it uses
86 output_surface_
->BindFramebuffer();
87 output_surface_
->SwapBuffers(damage
);
88 if (doublebuffering_
|| !first_frame_
)
89 output_surface_
->PageFlipComplete();
93 void SendFullFrame() { SendDamagedFrame(gfx::Rect(output_surface_
->size_
)); }
96 bool InsertUnique(std::set
<unsigned>* set
, unsigned value
) {
99 if (set
->find(value
) != set
->end())
105 scoped_ptr
<MockBufferQueue
> output_surface_
;
106 bool doublebuffering_
;
111 const gfx::Size screen_size
= gfx::Size(30, 30);
112 const gfx::Rect screen_rect
= gfx::Rect(screen_size
);
113 const gfx::Rect small_damage
= gfx::Rect(gfx::Size(10, 10));
114 const gfx::Rect large_damage
= gfx::Rect(gfx::Size(20, 20));
115 const gfx::Rect overlapping_damage
= gfx::Rect(gfx::Size(5, 20));
117 class MockedContext
: public cc::TestWebGraphicsContext3D
{
119 MOCK_METHOD2(bindFramebuffer
, void(GLenum
, GLuint
));
120 MOCK_METHOD2(bindTexture
, void(GLenum
, GLuint
));
121 MOCK_METHOD2(bindTexImage2DCHROMIUM
, void(GLenum
, GLint
));
122 MOCK_METHOD4(createGpuMemoryBufferImageCHROMIUM
,
123 GLuint(GLsizei
, GLsizei
, GLenum
, GLenum
));
124 MOCK_METHOD1(destroyImageCHROMIUM
, void(GLuint
));
125 MOCK_METHOD5(framebufferTexture2D
,
126 void(GLenum
, GLenum
, GLenum
, GLuint
, GLint
));
129 scoped_ptr
<BufferQueue
> CreateOutputSurfaceWithMock(MockedContext
** context
) {
130 *context
= new MockedContext();
131 scoped_refptr
<cc::TestContextProvider
> context_provider
=
132 cc::TestContextProvider::Create(
133 scoped_ptr
<cc::TestWebGraphicsContext3D
>(*context
));
134 context_provider
->BindToCurrentThread();
135 scoped_ptr
<BufferQueue
> buffer_queue(
136 new BufferQueue(context_provider
, GL_RGBA
, nullptr));
137 buffer_queue
->Initialize();
138 return buffer_queue
.Pass();
141 TEST(BufferQueueStandaloneTest
, FboInitialization
) {
142 MockedContext
* context
;
143 scoped_ptr
<BufferQueue
> output_surface
=
144 CreateOutputSurfaceWithMock(&context
);
146 EXPECT_CALL(*context
, bindFramebuffer(GL_FRAMEBUFFER
, Ne(0U)));
147 ON_CALL(*context
, framebufferTexture2D(_
, _
, _
, _
, _
))
148 .WillByDefault(Return());
150 output_surface
->Reshape(gfx::Size(10, 20), 1.0f
);
153 TEST(BufferQueueStandaloneTest
, FboBinding
) {
154 MockedContext
* context
;
155 scoped_ptr
<BufferQueue
> output_surface
=
156 CreateOutputSurfaceWithMock(&context
);
157 EXPECT_CALL(*context
, bindTexture(GL_TEXTURE_2D
, Ne(0U)));
158 EXPECT_CALL(*context
, destroyImageCHROMIUM(1));
160 EXPECT_CALL(*context
,
161 createGpuMemoryBufferImageCHROMIUM(
162 0, 0, GL_RGBA
, GL_SCANOUT_CHROMIUM
)).WillOnce(Return(1));
164 EXPECT_CALL(*context
, bindFramebuffer(GL_FRAMEBUFFER
, Ne(0U)));
165 Expectation tex
= EXPECT_CALL(*context
, bindTexture(GL_TEXTURE_2D
, Ne(0U)));
166 Expectation bind_tex
=
167 EXPECT_CALL(*context
, bindTexImage2DCHROMIUM(GL_TEXTURE_2D
, 1))
171 framebufferTexture2D(
172 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, Ne(0U), _
))
173 .After(fb
, bind_tex
);
175 output_surface
->BindFramebuffer();
178 TEST_F(BufferQueueTest
, PartialSwapReuse
) {
180 output_surface_
->Reshape(screen_size
, 1.0f
);
181 ASSERT_TRUE(doublebuffering_
);
182 EXPECT_CALL(*output_surface_
,
183 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
184 EXPECT_CALL(*output_surface_
,
185 CopyBufferDamage(_
, _
, small_damage
, small_damage
)).Times(1);
186 EXPECT_CALL(*output_surface_
,
187 CopyBufferDamage(_
, _
, large_damage
, small_damage
)).Times(1);
189 SendDamagedFrame(small_damage
);
190 SendDamagedFrame(small_damage
);
191 SendDamagedFrame(large_damage
);
192 // Verify that the damage has propagated.
193 EXPECT_EQ(next_frame().damage
, large_damage
);
196 TEST_F(BufferQueueTest
, PartialSwapFullFrame
) {
197 output_surface_
->Reshape(screen_size
, 1.0f
);
198 ASSERT_TRUE(doublebuffering_
);
199 EXPECT_CALL(*output_surface_
,
200 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
202 SendDamagedFrame(small_damage
);
205 EXPECT_EQ(next_frame().damage
, screen_rect
);
208 TEST_F(BufferQueueTest
, PartialSwapOverlapping
) {
209 output_surface_
->Reshape(screen_size
, 1.0f
);
210 ASSERT_TRUE(doublebuffering_
);
211 EXPECT_CALL(*output_surface_
,
212 CopyBufferDamage(_
, _
, small_damage
, screen_rect
)).Times(1);
213 EXPECT_CALL(*output_surface_
,
214 CopyBufferDamage(_
, _
, overlapping_damage
, small_damage
))
218 SendDamagedFrame(small_damage
);
219 SendDamagedFrame(overlapping_damage
);
220 EXPECT_EQ(next_frame().damage
, overlapping_damage
);
223 TEST_F(BufferQueueTest
, MultipleBindCalls
) {
224 // Check that multiple bind calls do not create or change surfaces.
225 output_surface_
->BindFramebuffer();
226 EXPECT_EQ(1, CountBuffers());
227 unsigned int fb
= current_surface();
228 output_surface_
->BindFramebuffer();
229 EXPECT_EQ(1, CountBuffers());
230 EXPECT_EQ(fb
, current_surface());
233 TEST_F(BufferQueueTest
, CheckDoubleBuffering
) {
234 // Check buffer flow through double buffering path.
235 EXPECT_EQ(0, CountBuffers());
236 output_surface_
->BindFramebuffer();
237 EXPECT_EQ(1, CountBuffers());
238 EXPECT_NE(0U, current_surface());
240 EXPECT_EQ(1U, in_flight_surfaces().size());
241 output_surface_
->PageFlipComplete();
242 EXPECT_EQ(1U, in_flight_surfaces().size());
243 output_surface_
->BindFramebuffer();
244 EXPECT_EQ(2, CountBuffers());
246 EXPECT_NE(0U, current_surface());
247 EXPECT_EQ(1U, in_flight_surfaces().size());
250 EXPECT_EQ(2U, in_flight_surfaces().size());
251 output_surface_
->PageFlipComplete();
253 EXPECT_EQ(1U, in_flight_surfaces().size());
254 EXPECT_EQ(1U, available_surfaces().size());
255 output_surface_
->BindFramebuffer();
256 EXPECT_EQ(2, CountBuffers());
258 EXPECT_TRUE(available_surfaces().empty());
261 TEST_F(BufferQueueTest
, CheckTripleBuffering
) {
262 // Check buffer flow through triple buffering path.
264 // This bit is the same sequence tested in the doublebuffering case.
265 output_surface_
->BindFramebuffer();
267 output_surface_
->PageFlipComplete();
268 output_surface_
->BindFramebuffer();
271 EXPECT_EQ(2, CountBuffers());
273 EXPECT_EQ(2U, in_flight_surfaces().size());
274 output_surface_
->BindFramebuffer();
275 EXPECT_EQ(3, CountBuffers());
277 EXPECT_NE(0U, current_surface());
278 EXPECT_EQ(2U, in_flight_surfaces().size());
279 output_surface_
->PageFlipComplete();
280 EXPECT_EQ(3, CountBuffers());
282 EXPECT_NE(0U, current_surface());
283 EXPECT_EQ(1U, in_flight_surfaces().size());
284 EXPECT_EQ(1U, available_surfaces().size());
288 } // namespace content