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