OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/compositor/buffer_queue.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <set> | |
11 #include <utility> | |
12 | |
13 #include "base/memory/ptr_util.h" | |
14 #include "cc/test/test_context_provider.h" | |
15 #include "cc/test/test_gpu_memory_buffer_manager.h" | |
16 #include "cc/test/test_web_graphics_context_3d.h" | |
17 #include "content/browser/compositor/gl_helper.h" | |
18 #include "gpu/GLES2/gl2extchromium.h" | |
19 #include "testing/gmock/include/gmock/gmock.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "third_party/khronos/GLES2/gl2ext.h" | |
22 | |
23 using ::testing::_; | |
24 using ::testing::Expectation; | |
25 using ::testing::Ne; | |
26 using ::testing::Return; | |
27 | |
28 namespace content { | |
29 | |
30 class StubGpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { | |
31 public: | |
32 StubGpuMemoryBufferImpl() {} | |
33 | |
34 // Overridden from gfx::GpuMemoryBuffer: | |
35 bool Map() override { return false; } | |
36 void* memory(size_t plane) override { return nullptr; } | |
37 void Unmap() override {} | |
38 gfx::Size GetSize() const override { return gfx::Size(); } | |
39 gfx::BufferFormat GetFormat() const override { | |
40 return gfx::BufferFormat::BGRX_8888; | |
41 } | |
42 int stride(size_t plane) const override { return 0; } | |
43 gfx::GpuMemoryBufferId GetId() const override { | |
44 return gfx::GpuMemoryBufferId(0); | |
45 } | |
46 gfx::GpuMemoryBufferHandle GetHandle() const override { | |
47 return gfx::GpuMemoryBufferHandle(); | |
48 } | |
49 ClientBuffer AsClientBuffer() override { | |
50 return reinterpret_cast<ClientBuffer>(this); | |
51 } | |
52 }; | |
53 | |
54 class StubGpuMemoryBufferManager : public cc::TestGpuMemoryBufferManager { | |
55 public: | |
56 StubGpuMemoryBufferManager() : allocate_succeeds_(true) {} | |
57 | |
58 void set_allocate_succeeds(bool value) { allocate_succeeds_ = value; } | |
59 | |
60 std::unique_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer( | |
61 const gfx::Size& size, | |
62 gfx::BufferFormat format, | |
63 gfx::BufferUsage usage, | |
64 int32_t surface_id) override { | |
65 if (!surface_id) { | |
66 return TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer( | |
67 size, format, usage, surface_id); | |
68 } | |
69 if (allocate_succeeds_) | |
70 return base::WrapUnique<gfx::GpuMemoryBuffer>( | |
71 new StubGpuMemoryBufferImpl); | |
72 return nullptr; | |
73 } | |
74 | |
75 private: | |
76 bool allocate_succeeds_; | |
77 }; | |
78 | |
79 class MockBufferQueue : public BufferQueue { | |
80 public: | |
81 MockBufferQueue(scoped_refptr<cc::ContextProvider> context_provider, | |
82 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
83 unsigned int target, | |
84 unsigned int internalformat) | |
85 : BufferQueue(context_provider, | |
86 target, | |
87 internalformat, | |
88 nullptr, | |
89 gpu_memory_buffer_manager, | |
90 1) {} | |
91 MOCK_METHOD4(CopyBufferDamage, | |
92 void(int, int, const gfx::Rect&, const gfx::Rect&)); | |
93 }; | |
94 | |
95 class BufferQueueTest : public ::testing::Test { | |
96 public: | |
97 BufferQueueTest() : doublebuffering_(true), first_frame_(true) {} | |
98 | |
99 void SetUp() override { | |
100 InitWithContext(cc::TestWebGraphicsContext3D::Create()); | |
101 } | |
102 | |
103 void InitWithContext(std::unique_ptr<cc::TestWebGraphicsContext3D> context) { | |
104 scoped_refptr<cc::TestContextProvider> context_provider = | |
105 cc::TestContextProvider::Create(std::move(context)); | |
106 context_provider->BindToCurrentThread(); | |
107 gpu_memory_buffer_manager_.reset(new StubGpuMemoryBufferManager); | |
108 mock_output_surface_ = | |
109 new MockBufferQueue(context_provider, gpu_memory_buffer_manager_.get(), | |
110 GL_TEXTURE_2D, GL_RGBA); | |
111 output_surface_.reset(mock_output_surface_); | |
112 output_surface_->Initialize(); | |
113 } | |
114 | |
115 unsigned current_surface() { | |
116 return output_surface_->current_surface_ | |
117 ? output_surface_->current_surface_->image | |
118 : 0; | |
119 } | |
120 const std::vector<std::unique_ptr<BufferQueue::AllocatedSurface>>& | |
121 available_surfaces() { | |
122 return output_surface_->available_surfaces_; | |
123 } | |
124 std::deque<std::unique_ptr<BufferQueue::AllocatedSurface>>& | |
125 in_flight_surfaces() { | |
126 return output_surface_->in_flight_surfaces_; | |
127 } | |
128 | |
129 const BufferQueue::AllocatedSurface* displayed_frame() { | |
130 return output_surface_->displayed_surface_.get(); | |
131 } | |
132 const BufferQueue::AllocatedSurface* current_frame() { | |
133 return output_surface_->current_surface_.get(); | |
134 } | |
135 const BufferQueue::AllocatedSurface* next_frame() { | |
136 return output_surface_->available_surfaces_.back().get(); | |
137 } | |
138 const gfx::Size size() { return output_surface_->size_; } | |
139 | |
140 int CountBuffers() { | |
141 int n = available_surfaces().size() + in_flight_surfaces().size() + | |
142 (displayed_frame() ? 1 : 0); | |
143 if (current_surface()) | |
144 n++; | |
145 return n; | |
146 } | |
147 | |
148 // Check that each buffer is unique if present. | |
149 void CheckUnique() { | |
150 std::set<unsigned> buffers; | |
151 EXPECT_TRUE(InsertUnique(&buffers, current_surface())); | |
152 if (displayed_frame()) | |
153 EXPECT_TRUE(InsertUnique(&buffers, displayed_frame()->image)); | |
154 for (auto& surface : available_surfaces()) | |
155 EXPECT_TRUE(InsertUnique(&buffers, surface->image)); | |
156 for (auto& surface : in_flight_surfaces()) { | |
157 if (surface) | |
158 EXPECT_TRUE(InsertUnique(&buffers, surface->image)); | |
159 } | |
160 } | |
161 | |
162 void SwapBuffers() { | |
163 output_surface_->SwapBuffers(gfx::Rect(output_surface_->size_)); | |
164 } | |
165 | |
166 void SendDamagedFrame(const gfx::Rect& damage) { | |
167 // We don't care about the GL-level implementation here, just how it uses | |
168 // damage rects. | |
169 output_surface_->BindFramebuffer(); | |
170 output_surface_->SwapBuffers(damage); | |
171 if (doublebuffering_ || !first_frame_) | |
172 output_surface_->PageFlipComplete(); | |
173 first_frame_ = false; | |
174 } | |
175 | |
176 void SendFullFrame() { SendDamagedFrame(gfx::Rect(output_surface_->size_)); } | |
177 | |
178 protected: | |
179 bool InsertUnique(std::set<unsigned>* set, unsigned value) { | |
180 if (!value) | |
181 return true; | |
182 if (set->find(value) != set->end()) | |
183 return false; | |
184 set->insert(value); | |
185 return true; | |
186 } | |
187 | |
188 std::unique_ptr<StubGpuMemoryBufferManager> gpu_memory_buffer_manager_; | |
189 std::unique_ptr<BufferQueue> output_surface_; | |
190 MockBufferQueue* mock_output_surface_; | |
191 bool doublebuffering_; | |
192 bool first_frame_; | |
193 }; | |
194 | |
195 namespace { | |
196 const gfx::Size screen_size = gfx::Size(30, 30); | |
197 const gfx::Rect screen_rect = gfx::Rect(screen_size); | |
198 const gfx::Rect small_damage = gfx::Rect(gfx::Size(10, 10)); | |
199 const gfx::Rect large_damage = gfx::Rect(gfx::Size(20, 20)); | |
200 const gfx::Rect overlapping_damage = gfx::Rect(gfx::Size(5, 20)); | |
201 | |
202 GLuint CreateImageDefault() { | |
203 static GLuint id = 0; | |
204 return ++id; | |
205 } | |
206 | |
207 class MockedContext : public cc::TestWebGraphicsContext3D { | |
208 public: | |
209 MockedContext() { | |
210 ON_CALL(*this, createImageCHROMIUM(_, _, _, _)) | |
211 .WillByDefault(testing::InvokeWithoutArgs(&CreateImageDefault)); | |
212 } | |
213 MOCK_METHOD2(bindFramebuffer, void(GLenum, GLuint)); | |
214 MOCK_METHOD2(bindTexture, void(GLenum, GLuint)); | |
215 MOCK_METHOD2(bindTexImage2DCHROMIUM, void(GLenum, GLint)); | |
216 MOCK_METHOD4(createImageCHROMIUM, | |
217 GLuint(ClientBuffer, GLsizei, GLsizei, GLenum)); | |
218 MOCK_METHOD1(destroyImageCHROMIUM, void(GLuint)); | |
219 MOCK_METHOD5(framebufferTexture2D, | |
220 void(GLenum, GLenum, GLenum, GLuint, GLint)); | |
221 }; | |
222 | |
223 class BufferQueueMockedContextTest : public BufferQueueTest { | |
224 public: | |
225 void SetUp() override { | |
226 context_ = new MockedContext(); | |
227 InitWithContext(std::unique_ptr<cc::TestWebGraphicsContext3D>(context_)); | |
228 } | |
229 | |
230 protected: | |
231 MockedContext* context_; | |
232 }; | |
233 | |
234 std::unique_ptr<BufferQueue> CreateOutputSurfaceWithMock( | |
235 unsigned int target, | |
236 MockedContext** context, | |
237 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager) { | |
238 *context = new MockedContext(); | |
239 scoped_refptr<cc::TestContextProvider> context_provider = | |
240 cc::TestContextProvider::Create( | |
241 std::unique_ptr<cc::TestWebGraphicsContext3D>(*context)); | |
242 context_provider->BindToCurrentThread(); | |
243 std::unique_ptr<BufferQueue> buffer_queue( | |
244 new BufferQueue(context_provider, target, GL_RGBA, nullptr, | |
245 gpu_memory_buffer_manager, 1)); | |
246 buffer_queue->Initialize(); | |
247 return buffer_queue; | |
248 } | |
249 | |
250 TEST(BufferQueueStandaloneTest, FboInitialization) { | |
251 MockedContext* context; | |
252 std::unique_ptr<StubGpuMemoryBufferManager> gpu_memory_buffer_manager( | |
253 new StubGpuMemoryBufferManager); | |
254 std::unique_ptr<BufferQueue> output_surface = CreateOutputSurfaceWithMock( | |
255 GL_TEXTURE_2D, &context, gpu_memory_buffer_manager.get()); | |
256 | |
257 EXPECT_CALL(*context, bindFramebuffer(GL_FRAMEBUFFER, Ne(0U))); | |
258 ON_CALL(*context, framebufferTexture2D(_, _, _, _, _)) | |
259 .WillByDefault(Return()); | |
260 | |
261 output_surface->Reshape(gfx::Size(10, 20), 1.0f); | |
262 } | |
263 | |
264 TEST(BufferQueueStandaloneTest, FboBinding) { | |
265 GLenum targets[] = { GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB }; | |
266 for (size_t i = 0; i < 2; ++i) { | |
267 GLenum target = targets[i]; | |
268 MockedContext* context; | |
269 std::unique_ptr<StubGpuMemoryBufferManager> gpu_memory_buffer_manager( | |
270 new StubGpuMemoryBufferManager); | |
271 std::unique_ptr<BufferQueue> output_surface = CreateOutputSurfaceWithMock( | |
272 target, &context, gpu_memory_buffer_manager.get()); | |
273 EXPECT_CALL(*context, bindTexture(target, Ne(0U))); | |
274 EXPECT_CALL(*context, destroyImageCHROMIUM(1)); | |
275 Expectation image = | |
276 EXPECT_CALL(*context, createImageCHROMIUM(_, 0, 0, GL_RGBA)) | |
277 .WillOnce(Return(1)); | |
278 Expectation fb = | |
279 EXPECT_CALL(*context, bindFramebuffer(GL_FRAMEBUFFER, Ne(0U))); | |
280 Expectation tex = EXPECT_CALL(*context, bindTexture(target, Ne(0U))); | |
281 Expectation bind_tex = | |
282 EXPECT_CALL(*context, bindTexImage2DCHROMIUM(target, 1)) | |
283 .After(tex, image); | |
284 EXPECT_CALL( | |
285 *context, | |
286 framebufferTexture2D( | |
287 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, Ne(0U), _)) | |
288 .After(fb, bind_tex); | |
289 | |
290 output_surface->BindFramebuffer(); | |
291 } | |
292 } | |
293 | |
294 TEST(BufferQueueStandaloneTest, CheckBoundFramebuffer) { | |
295 std::unique_ptr<StubGpuMemoryBufferManager> gpu_memory_buffer_manager; | |
296 std::unique_ptr<BufferQueue> output_surface; | |
297 scoped_refptr<cc::TestContextProvider> context_provider = | |
298 cc::TestContextProvider::Create(cc::TestWebGraphicsContext3D::Create()); | |
299 context_provider->BindToCurrentThread(); | |
300 gpu_memory_buffer_manager.reset(new StubGpuMemoryBufferManager); | |
301 | |
302 std::unique_ptr<GLHelper> gl_helper; | |
303 gl_helper.reset(new GLHelper(context_provider->ContextGL(), | |
304 context_provider->ContextSupport())); | |
305 | |
306 output_surface.reset(new BufferQueue(context_provider, GL_TEXTURE_2D, GL_RGBA, | |
307 gl_helper.get(), | |
308 gpu_memory_buffer_manager.get(), 1)); | |
309 output_surface->Initialize(); | |
310 output_surface->Reshape(screen_size, 1.0f); | |
311 // Trigger a sub-buffer copy to exercise all paths. | |
312 output_surface->BindFramebuffer(); | |
313 output_surface->SwapBuffers(screen_rect); | |
314 output_surface->PageFlipComplete(); | |
315 output_surface->BindFramebuffer(); | |
316 output_surface->SwapBuffers(small_damage); | |
317 | |
318 int current_fbo = 0; | |
319 context_provider->ContextGL()->GetIntegerv(GL_FRAMEBUFFER_BINDING, | |
320 ¤t_fbo); | |
321 EXPECT_EQ(static_cast<int>(output_surface->fbo()), current_fbo); | |
322 } | |
323 | |
324 TEST_F(BufferQueueTest, PartialSwapReuse) { | |
325 output_surface_->Reshape(screen_size, 1.0f); | |
326 ASSERT_TRUE(doublebuffering_); | |
327 EXPECT_CALL(*mock_output_surface_, | |
328 CopyBufferDamage(_, _, small_damage, screen_rect)).Times(1); | |
329 EXPECT_CALL(*mock_output_surface_, | |
330 CopyBufferDamage(_, _, small_damage, small_damage)).Times(1); | |
331 EXPECT_CALL(*mock_output_surface_, | |
332 CopyBufferDamage(_, _, large_damage, small_damage)).Times(1); | |
333 SendFullFrame(); | |
334 SendDamagedFrame(small_damage); | |
335 SendDamagedFrame(small_damage); | |
336 SendDamagedFrame(large_damage); | |
337 // Verify that the damage has propagated. | |
338 EXPECT_EQ(next_frame()->damage, large_damage); | |
339 } | |
340 | |
341 TEST_F(BufferQueueTest, PartialSwapFullFrame) { | |
342 output_surface_->Reshape(screen_size, 1.0f); | |
343 ASSERT_TRUE(doublebuffering_); | |
344 EXPECT_CALL(*mock_output_surface_, | |
345 CopyBufferDamage(_, _, small_damage, screen_rect)).Times(1); | |
346 SendFullFrame(); | |
347 SendDamagedFrame(small_damage); | |
348 SendFullFrame(); | |
349 SendFullFrame(); | |
350 EXPECT_EQ(next_frame()->damage, screen_rect); | |
351 } | |
352 | |
353 TEST_F(BufferQueueTest, PartialSwapOverlapping) { | |
354 output_surface_->Reshape(screen_size, 1.0f); | |
355 ASSERT_TRUE(doublebuffering_); | |
356 EXPECT_CALL(*mock_output_surface_, | |
357 CopyBufferDamage(_, _, small_damage, screen_rect)).Times(1); | |
358 EXPECT_CALL(*mock_output_surface_, CopyBufferDamage(_, _, overlapping_damage, | |
359 small_damage)).Times(1); | |
360 | |
361 SendFullFrame(); | |
362 SendDamagedFrame(small_damage); | |
363 SendDamagedFrame(overlapping_damage); | |
364 EXPECT_EQ(next_frame()->damage, overlapping_damage); | |
365 } | |
366 | |
367 TEST_F(BufferQueueTest, MultipleBindCalls) { | |
368 // Check that multiple bind calls do not create or change surfaces. | |
369 output_surface_->BindFramebuffer(); | |
370 EXPECT_EQ(1, CountBuffers()); | |
371 unsigned int fb = current_surface(); | |
372 output_surface_->BindFramebuffer(); | |
373 EXPECT_EQ(1, CountBuffers()); | |
374 EXPECT_EQ(fb, current_surface()); | |
375 } | |
376 | |
377 TEST_F(BufferQueueTest, CheckDoubleBuffering) { | |
378 // Check buffer flow through double buffering path. | |
379 EXPECT_EQ(0, CountBuffers()); | |
380 output_surface_->BindFramebuffer(); | |
381 EXPECT_EQ(1, CountBuffers()); | |
382 EXPECT_NE(0U, current_surface()); | |
383 EXPECT_FALSE(displayed_frame()); | |
384 SwapBuffers(); | |
385 EXPECT_EQ(1U, in_flight_surfaces().size()); | |
386 output_surface_->PageFlipComplete(); | |
387 EXPECT_EQ(0U, in_flight_surfaces().size()); | |
388 EXPECT_TRUE(displayed_frame()->texture); | |
389 output_surface_->BindFramebuffer(); | |
390 EXPECT_EQ(2, CountBuffers()); | |
391 CheckUnique(); | |
392 EXPECT_NE(0U, current_surface()); | |
393 EXPECT_EQ(0U, in_flight_surfaces().size()); | |
394 EXPECT_TRUE(displayed_frame()->texture); | |
395 SwapBuffers(); | |
396 CheckUnique(); | |
397 EXPECT_EQ(1U, in_flight_surfaces().size()); | |
398 EXPECT_TRUE(displayed_frame()->texture); | |
399 output_surface_->PageFlipComplete(); | |
400 CheckUnique(); | |
401 EXPECT_EQ(0U, in_flight_surfaces().size()); | |
402 EXPECT_EQ(1U, available_surfaces().size()); | |
403 EXPECT_TRUE(displayed_frame()->texture); | |
404 output_surface_->BindFramebuffer(); | |
405 EXPECT_EQ(2, CountBuffers()); | |
406 CheckUnique(); | |
407 EXPECT_TRUE(available_surfaces().empty()); | |
408 } | |
409 | |
410 TEST_F(BufferQueueTest, CheckTripleBuffering) { | |
411 // Check buffer flow through triple buffering path. | |
412 | |
413 // This bit is the same sequence tested in the doublebuffering case. | |
414 output_surface_->BindFramebuffer(); | |
415 EXPECT_FALSE(displayed_frame()); | |
416 SwapBuffers(); | |
417 output_surface_->PageFlipComplete(); | |
418 output_surface_->BindFramebuffer(); | |
419 SwapBuffers(); | |
420 | |
421 EXPECT_EQ(2, CountBuffers()); | |
422 CheckUnique(); | |
423 EXPECT_EQ(1U, in_flight_surfaces().size()); | |
424 EXPECT_TRUE(displayed_frame()->texture); | |
425 output_surface_->BindFramebuffer(); | |
426 EXPECT_EQ(3, CountBuffers()); | |
427 CheckUnique(); | |
428 EXPECT_NE(0U, current_surface()); | |
429 EXPECT_EQ(1U, in_flight_surfaces().size()); | |
430 EXPECT_TRUE(displayed_frame()->texture); | |
431 output_surface_->PageFlipComplete(); | |
432 EXPECT_EQ(3, CountBuffers()); | |
433 CheckUnique(); | |
434 EXPECT_NE(0U, current_surface()); | |
435 EXPECT_EQ(0U, in_flight_surfaces().size()); | |
436 EXPECT_TRUE(displayed_frame()->texture); | |
437 EXPECT_EQ(1U, available_surfaces().size()); | |
438 } | |
439 | |
440 TEST_F(BufferQueueTest, CheckCorrectBufferOrdering) { | |
441 const size_t kSwapCount = 3; | |
442 for (size_t i = 0; i < kSwapCount; ++i) { | |
443 output_surface_->BindFramebuffer(); | |
444 SwapBuffers(); | |
445 } | |
446 | |
447 EXPECT_EQ(kSwapCount, in_flight_surfaces().size()); | |
448 for (size_t i = 0; i < kSwapCount; ++i) { | |
449 unsigned int next_texture_id = in_flight_surfaces().front()->texture; | |
450 output_surface_->PageFlipComplete(); | |
451 EXPECT_EQ(displayed_frame()->texture, next_texture_id); | |
452 } | |
453 } | |
454 | |
455 TEST_F(BufferQueueTest, ReshapeWithInFlightSurfaces) { | |
456 const size_t kSwapCount = 3; | |
457 for (size_t i = 0; i < kSwapCount; ++i) { | |
458 output_surface_->BindFramebuffer(); | |
459 SwapBuffers(); | |
460 } | |
461 | |
462 output_surface_->Reshape(gfx::Size(10, 20), 1.0f); | |
463 EXPECT_EQ(3u, in_flight_surfaces().size()); | |
464 | |
465 for (size_t i = 0; i < kSwapCount; ++i) { | |
466 output_surface_->PageFlipComplete(); | |
467 EXPECT_FALSE(displayed_frame()); | |
468 } | |
469 | |
470 // The dummy surfacess left should be discarded. | |
471 EXPECT_EQ(0u, available_surfaces().size()); | |
472 } | |
473 | |
474 TEST_F(BufferQueueTest, SwapAfterReshape) { | |
475 const size_t kSwapCount = 3; | |
476 for (size_t i = 0; i < kSwapCount; ++i) { | |
477 output_surface_->BindFramebuffer(); | |
478 SwapBuffers(); | |
479 } | |
480 | |
481 output_surface_->Reshape(gfx::Size(10, 20), 1.0f); | |
482 | |
483 for (size_t i = 0; i < kSwapCount; ++i) { | |
484 output_surface_->BindFramebuffer(); | |
485 SwapBuffers(); | |
486 } | |
487 | |
488 EXPECT_EQ(2 * kSwapCount, in_flight_surfaces().size()); | |
489 | |
490 for (size_t i = 0; i < kSwapCount; ++i) { | |
491 output_surface_->PageFlipComplete(); | |
492 EXPECT_FALSE(displayed_frame()); | |
493 } | |
494 | |
495 CheckUnique(); | |
496 | |
497 for (size_t i = 0; i < kSwapCount; ++i) { | |
498 unsigned int next_texture_id = in_flight_surfaces().front()->texture; | |
499 output_surface_->PageFlipComplete(); | |
500 EXPECT_EQ(displayed_frame()->texture, next_texture_id); | |
501 EXPECT_TRUE(displayed_frame()); | |
502 } | |
503 } | |
504 | |
505 TEST_F(BufferQueueMockedContextTest, RecreateBuffers) { | |
506 // This setup is to easily get one frame in each of: | |
507 // - currently bound for drawing. | |
508 // - in flight to GPU. | |
509 // - currently displayed. | |
510 // - free frame. | |
511 // This tests buffers in all states. | |
512 // Bind/swap pushes frames into the in flight list, then the PageFlipComplete | |
513 // calls pull one frame into displayed and another into the free list. | |
514 output_surface_->BindFramebuffer(); | |
515 SwapBuffers(); | |
516 output_surface_->BindFramebuffer(); | |
517 SwapBuffers(); | |
518 output_surface_->BindFramebuffer(); | |
519 SwapBuffers(); | |
520 output_surface_->BindFramebuffer(); | |
521 output_surface_->PageFlipComplete(); | |
522 output_surface_->PageFlipComplete(); | |
523 // We should have one buffer in each possible state right now, including one | |
524 // being drawn to. | |
525 ASSERT_EQ(1U, in_flight_surfaces().size()); | |
526 ASSERT_EQ(1U, available_surfaces().size()); | |
527 EXPECT_TRUE(displayed_frame()); | |
528 EXPECT_TRUE(current_frame()); | |
529 | |
530 auto* current = current_frame(); | |
531 auto* displayed = displayed_frame(); | |
532 auto* in_flight = in_flight_surfaces().front().get(); | |
533 auto* available = available_surfaces().front().get(); | |
534 | |
535 // Expect all 4 images to be destroyed, 3 of the existing textures to be | |
536 // copied from and 3 new images to be created. | |
537 EXPECT_CALL(*context_, createImageCHROMIUM(_, 0, 0, GL_RGBA)).Times(3); | |
538 Expectation copy1 = EXPECT_CALL(*mock_output_surface_, | |
539 CopyBufferDamage(_, displayed->texture, _, _)) | |
540 .Times(1); | |
541 Expectation copy2 = EXPECT_CALL(*mock_output_surface_, | |
542 CopyBufferDamage(_, current->texture, _, _)) | |
543 .Times(1); | |
544 Expectation copy3 = EXPECT_CALL(*mock_output_surface_, | |
545 CopyBufferDamage(_, in_flight->texture, _, _)) | |
546 .Times(1); | |
547 | |
548 EXPECT_CALL(*context_, destroyImageCHROMIUM(displayed->image)) | |
549 .Times(1) | |
550 .After(copy1); | |
551 EXPECT_CALL(*context_, destroyImageCHROMIUM(current->image)) | |
552 .Times(1) | |
553 .After(copy2); | |
554 EXPECT_CALL(*context_, destroyImageCHROMIUM(in_flight->image)) | |
555 .Times(1) | |
556 .After(copy3); | |
557 EXPECT_CALL(*context_, destroyImageCHROMIUM(available->image)).Times(1); | |
558 // After copying, we expect the framebuffer binding to be updated. | |
559 EXPECT_CALL(*context_, bindFramebuffer(_, _)) | |
560 .After(copy1) | |
561 .After(copy2) | |
562 .After(copy3); | |
563 EXPECT_CALL(*context_, framebufferTexture2D(_, _, _, _, _)) | |
564 .After(copy1) | |
565 .After(copy2) | |
566 .After(copy3); | |
567 | |
568 output_surface_->RecreateBuffers(); | |
569 testing::Mock::VerifyAndClearExpectations(context_); | |
570 testing::Mock::VerifyAndClearExpectations(mock_output_surface_); | |
571 | |
572 // All free buffers should be destroyed, the remaining buffers should all | |
573 // be replaced but still valid. | |
574 EXPECT_EQ(1U, in_flight_surfaces().size()); | |
575 EXPECT_EQ(0U, available_surfaces().size()); | |
576 EXPECT_TRUE(displayed_frame()); | |
577 EXPECT_TRUE(current_frame()); | |
578 } | |
579 | |
580 TEST_F(BufferQueueTest, AllocateFails) { | |
581 output_surface_->Reshape(screen_size, 1.0f); | |
582 | |
583 // Succeed in the two swaps. | |
584 output_surface_->BindFramebuffer(); | |
585 EXPECT_TRUE(current_frame()); | |
586 output_surface_->SwapBuffers(screen_rect); | |
587 | |
588 // Fail the next surface allocation. | |
589 gpu_memory_buffer_manager_->set_allocate_succeeds(false); | |
590 output_surface_->BindFramebuffer(); | |
591 EXPECT_FALSE(current_frame()); | |
592 output_surface_->SwapBuffers(screen_rect); | |
593 EXPECT_FALSE(current_frame()); | |
594 | |
595 // Try another swap. It should copy the buffer damage from the back | |
596 // surface. | |
597 gpu_memory_buffer_manager_->set_allocate_succeeds(true); | |
598 output_surface_->BindFramebuffer(); | |
599 unsigned int source_texture = in_flight_surfaces().front()->texture; | |
600 unsigned int target_texture = current_frame()->texture; | |
601 testing::Mock::VerifyAndClearExpectations(mock_output_surface_); | |
602 EXPECT_CALL(*mock_output_surface_, | |
603 CopyBufferDamage(target_texture, source_texture, small_damage, _)) | |
604 .Times(1); | |
605 output_surface_->SwapBuffers(small_damage); | |
606 testing::Mock::VerifyAndClearExpectations(mock_output_surface_); | |
607 | |
608 // Destroy the just-created buffer, and try another swap. The copy should | |
609 // come from the displayed surface (because both in-flight surfaces are | |
610 // gone now). | |
611 output_surface_->PageFlipComplete(); | |
612 in_flight_surfaces().back().reset(); | |
613 EXPECT_EQ(2u, in_flight_surfaces().size()); | |
614 for (auto& surface : in_flight_surfaces()) | |
615 EXPECT_FALSE(surface); | |
616 output_surface_->BindFramebuffer(); | |
617 source_texture = displayed_frame()->texture; | |
618 EXPECT_TRUE(current_frame()); | |
619 EXPECT_TRUE(displayed_frame()); | |
620 target_texture = current_frame()->texture; | |
621 testing::Mock::VerifyAndClearExpectations(mock_output_surface_); | |
622 EXPECT_CALL(*mock_output_surface_, | |
623 CopyBufferDamage(target_texture, source_texture, small_damage, _)) | |
624 .Times(1); | |
625 output_surface_->SwapBuffers(small_damage); | |
626 testing::Mock::VerifyAndClearExpectations(mock_output_surface_); | |
627 } | |
628 | |
629 } // namespace | |
630 } // namespace content | |
OLD | NEW |