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