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 <set> |
| 6 |
| 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_s
urface.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" |
| 15 |
| 16 using ::testing::_; |
| 17 using ::testing::Expectation; |
| 18 using ::testing::Ne; |
| 19 using ::testing::Return; |
| 20 |
| 21 namespace content { |
| 22 |
| 23 class BufferQueueTest : public ::testing::Test { |
| 24 public: |
| 25 virtual void SetUp() OVERRIDE { |
| 26 scoped_refptr<cc::TestContextProvider> context_provider = |
| 27 cc::TestContextProvider::Create(cc::TestWebGraphicsContext3D::Create()); |
| 28 context_provider->BindToCurrentThread(); |
| 29 output_surface_.reset(new BufferQueue(context_provider, GL_RGBA8_OES)); |
| 30 } |
| 31 |
| 32 unsigned current_surface() { return output_surface_->current_surface_.image; } |
| 33 const std::vector<BufferQueue::AllocatedSurface>& available_surfaces() { |
| 34 return output_surface_->available_surfaces_; |
| 35 } |
| 36 const std::queue<BufferQueue::AllocatedSurface>& in_flight_surfaces() { |
| 37 return output_surface_->in_flight_surfaces_; |
| 38 } |
| 39 |
| 40 int CountBuffers() { |
| 41 int n = available_surfaces().size() + in_flight_surfaces().size(); |
| 42 if (current_surface()) |
| 43 n++; |
| 44 return n; |
| 45 } |
| 46 |
| 47 // Check that each buffer is unique if present. |
| 48 void CheckUnique() { |
| 49 std::set<unsigned> buffers; |
| 50 EXPECT_TRUE(InsertUnique(&buffers, current_surface())); |
| 51 for (size_t i = 0; i < available_surfaces().size(); i++) |
| 52 EXPECT_TRUE(InsertUnique(&buffers, available_surfaces()[i].image)); |
| 53 std::queue<BufferQueue::AllocatedSurface> copy = in_flight_surfaces(); |
| 54 while (!copy.empty()) { |
| 55 EXPECT_TRUE(InsertUnique(&buffers, copy.front().image)); |
| 56 copy.pop(); |
| 57 } |
| 58 } |
| 59 |
| 60 protected: |
| 61 bool InsertUnique(std::set<unsigned>* set, unsigned value) { |
| 62 if (!value) |
| 63 return true; |
| 64 if (set->find(value) != set->end()) |
| 65 return false; |
| 66 set->insert(value); |
| 67 return true; |
| 68 } |
| 69 |
| 70 scoped_ptr<BufferQueue> output_surface_; |
| 71 }; |
| 72 |
| 73 namespace { |
| 74 |
| 75 class MockedContext : public cc::TestWebGraphicsContext3D { |
| 76 public: |
| 77 MOCK_METHOD2(bindFramebuffer, void(GLenum, GLuint)); |
| 78 MOCK_METHOD2(bindTexture, void(GLenum, GLuint)); |
| 79 MOCK_METHOD2(bindTexImage2DCHROMIUM, void(GLenum, GLint)); |
| 80 MOCK_METHOD4(createImageCHROMIUM, GLuint(GLsizei, GLsizei, GLenum, GLenum)); |
| 81 MOCK_METHOD1(destroyImageCHROMIUM, void(GLuint)); |
| 82 MOCK_METHOD5(framebufferTexture2D, |
| 83 void(GLenum, GLenum, GLenum, GLuint, GLint)); |
| 84 }; |
| 85 |
| 86 scoped_ptr<BufferQueue> CreateOutputSurfaceWithMock(MockedContext** context) { |
| 87 *context = new MockedContext(); |
| 88 scoped_refptr<cc::TestContextProvider> context_provider = |
| 89 cc::TestContextProvider::Create( |
| 90 scoped_ptr<cc::TestWebGraphicsContext3D>(*context)); |
| 91 context_provider->BindToCurrentThread(); |
| 92 scoped_ptr<BufferQueue> buffer_queue( |
| 93 new BufferQueue(context_provider, GL_RGBA8_OES)); |
| 94 buffer_queue->Initialize(); |
| 95 return buffer_queue.Pass(); |
| 96 } |
| 97 |
| 98 TEST(BufferQueueStandaloneTest, FboInitialization) { |
| 99 MockedContext* context; |
| 100 scoped_ptr<BufferQueue> output_surface = |
| 101 CreateOutputSurfaceWithMock(&context); |
| 102 |
| 103 EXPECT_CALL(*context, bindFramebuffer(GL_FRAMEBUFFER, Ne(0U))); |
| 104 ON_CALL(*context, framebufferTexture2D(_, _, _, _, _)) |
| 105 .WillByDefault(Return()); |
| 106 |
| 107 output_surface->Reshape(gfx::Size(10, 20), 1.0f); |
| 108 } |
| 109 |
| 110 TEST(BufferQueueStandaloneTest, FboBinding) { |
| 111 MockedContext* context; |
| 112 scoped_ptr<BufferQueue> output_surface = |
| 113 CreateOutputSurfaceWithMock(&context); |
| 114 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, Ne(0U))); |
| 115 EXPECT_CALL(*context, destroyImageCHROMIUM(1)); |
| 116 Expectation image = |
| 117 EXPECT_CALL( |
| 118 *context, |
| 119 createImageCHROMIUM(0, 0, GL_RGBA8_OES, GL_IMAGE_SCANOUT_CHROMIUM)) |
| 120 .WillOnce(Return(1)); |
| 121 Expectation fb = |
| 122 EXPECT_CALL(*context, bindFramebuffer(GL_FRAMEBUFFER, Ne(0U))); |
| 123 Expectation tex = EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, Ne(0U))); |
| 124 Expectation bind_tex = |
| 125 EXPECT_CALL(*context, bindTexImage2DCHROMIUM(GL_TEXTURE_2D, 1)) |
| 126 .After(tex, image); |
| 127 EXPECT_CALL( |
| 128 *context, |
| 129 framebufferTexture2D( |
| 130 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Ne(0U), _)) |
| 131 .After(fb, bind_tex); |
| 132 |
| 133 output_surface->BindFramebuffer(); |
| 134 } |
| 135 |
| 136 TEST_F(BufferQueueTest, MultipleBindCalls) { |
| 137 // Check that multiple bind calls do not create or change surfaces. |
| 138 output_surface_->BindFramebuffer(); |
| 139 EXPECT_EQ(1, CountBuffers()); |
| 140 unsigned int fb = current_surface(); |
| 141 output_surface_->BindFramebuffer(); |
| 142 EXPECT_EQ(1, CountBuffers()); |
| 143 EXPECT_EQ(fb, current_surface()); |
| 144 } |
| 145 |
| 146 TEST_F(BufferQueueTest, CheckDoubleBuffering) { |
| 147 // Check buffer flow through double buffering path. |
| 148 EXPECT_EQ(0, CountBuffers()); |
| 149 output_surface_->BindFramebuffer(); |
| 150 EXPECT_EQ(1, CountBuffers()); |
| 151 EXPECT_NE(0U, current_surface()); |
| 152 output_surface_->SwapBuffers(); |
| 153 EXPECT_EQ(1U, in_flight_surfaces().size()); |
| 154 output_surface_->PageFlipComplete(); |
| 155 EXPECT_EQ(1U, in_flight_surfaces().size()); |
| 156 output_surface_->BindFramebuffer(); |
| 157 EXPECT_EQ(2, CountBuffers()); |
| 158 CheckUnique(); |
| 159 EXPECT_NE(0U, current_surface()); |
| 160 EXPECT_EQ(1U, in_flight_surfaces().size()); |
| 161 output_surface_->SwapBuffers(); |
| 162 CheckUnique(); |
| 163 EXPECT_EQ(2U, in_flight_surfaces().size()); |
| 164 output_surface_->PageFlipComplete(); |
| 165 CheckUnique(); |
| 166 EXPECT_EQ(1U, in_flight_surfaces().size()); |
| 167 EXPECT_EQ(1U, available_surfaces().size()); |
| 168 output_surface_->BindFramebuffer(); |
| 169 EXPECT_EQ(2, CountBuffers()); |
| 170 CheckUnique(); |
| 171 EXPECT_TRUE(available_surfaces().empty()); |
| 172 } |
| 173 |
| 174 TEST_F(BufferQueueTest, CheckTripleBuffering) { |
| 175 // Check buffer flow through triple buffering path. |
| 176 |
| 177 // This bit is the same sequence tested in the doublebuffering case. |
| 178 output_surface_->BindFramebuffer(); |
| 179 output_surface_->SwapBuffers(); |
| 180 output_surface_->PageFlipComplete(); |
| 181 output_surface_->BindFramebuffer(); |
| 182 output_surface_->SwapBuffers(); |
| 183 |
| 184 EXPECT_EQ(2, CountBuffers()); |
| 185 CheckUnique(); |
| 186 EXPECT_EQ(2U, in_flight_surfaces().size()); |
| 187 output_surface_->BindFramebuffer(); |
| 188 EXPECT_EQ(3, CountBuffers()); |
| 189 CheckUnique(); |
| 190 EXPECT_NE(0U, current_surface()); |
| 191 EXPECT_EQ(2U, in_flight_surfaces().size()); |
| 192 output_surface_->PageFlipComplete(); |
| 193 EXPECT_EQ(3, CountBuffers()); |
| 194 CheckUnique(); |
| 195 EXPECT_NE(0U, current_surface()); |
| 196 EXPECT_EQ(1U, in_flight_surfaces().size()); |
| 197 EXPECT_EQ(1U, available_surfaces().size()); |
| 198 } |
| 199 |
| 200 } // namespace |
| 201 } // namespace content |
OLD | NEW |