Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / compositor / buffer_queue_unittest.cc
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_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"
15
16 using ::testing::_;
17 using ::testing::Expectation;
18 using ::testing::Ne;
19 using ::testing::Return;
20
21 namespace content {
22 class MockBufferQueue : public BufferQueue {
23  public:
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&));
29 };
30
31 class BufferQueueTest : public ::testing::Test {
32  public:
33   BufferQueueTest() : doublebuffering_(true), first_frame_(true) {}
34
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();
41   }
42
43   unsigned current_surface() { return output_surface_->current_surface_.image; }
44   const std::vector<BufferQueue::AllocatedSurface>& available_surfaces() {
45     return output_surface_->available_surfaces_;
46   }
47   const std::deque<BufferQueue::AllocatedSurface>& in_flight_surfaces() {
48     return output_surface_->in_flight_surfaces_;
49   }
50
51   const BufferQueue::AllocatedSurface& last_frame() {
52     return output_surface_->in_flight_surfaces_.back();
53   }
54   const BufferQueue::AllocatedSurface& next_frame() {
55     return output_surface_->available_surfaces_.back();
56   }
57   const gfx::Size size() { return output_surface_->size_; }
58
59   int CountBuffers() {
60     int n = available_surfaces().size() + in_flight_surfaces().size();
61     if (current_surface())
62       n++;
63     return n;
64   }
65
66   // Check that each buffer is unique if present.
67   void CheckUnique() {
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();
75          ++it)
76       EXPECT_TRUE(InsertUnique(&buffers, it->image));
77   }
78
79   void SwapBuffers() {
80     output_surface_->SwapBuffers(gfx::Rect(output_surface_->size_));
81   }
82
83   void SendDamagedFrame(const gfx::Rect& damage) {
84     // We don't care about the GL-level implementation here, just how it uses
85     // damage rects.
86     output_surface_->BindFramebuffer();
87     output_surface_->SwapBuffers(damage);
88     if (doublebuffering_ || !first_frame_)
89       output_surface_->PageFlipComplete();
90     first_frame_ = false;
91   }
92
93   void SendFullFrame() { SendDamagedFrame(gfx::Rect(output_surface_->size_)); }
94
95  protected:
96   bool InsertUnique(std::set<unsigned>* set, unsigned value) {
97     if (!value)
98       return true;
99     if (set->find(value) != set->end())
100       return false;
101     set->insert(value);
102     return true;
103   }
104
105   scoped_ptr<MockBufferQueue> output_surface_;
106   bool doublebuffering_;
107   bool first_frame_;
108 };
109
110 namespace {
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));
116
117 class MockedContext : public cc::TestWebGraphicsContext3D {
118  public:
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));
127 };
128
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();
139 }
140
141 TEST(BufferQueueStandaloneTest, FboInitialization) {
142   MockedContext* context;
143   scoped_ptr<BufferQueue> output_surface =
144       CreateOutputSurfaceWithMock(&context);
145
146   EXPECT_CALL(*context, bindFramebuffer(GL_FRAMEBUFFER, Ne(0U)));
147   ON_CALL(*context, framebufferTexture2D(_, _, _, _, _))
148       .WillByDefault(Return());
149
150   output_surface->Reshape(gfx::Size(10, 20), 1.0f);
151 }
152
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));
159   Expectation image =
160       EXPECT_CALL(*context,
161                   createGpuMemoryBufferImageCHROMIUM(
162                       0, 0, GL_RGBA, GL_SCANOUT_CHROMIUM)).WillOnce(Return(1));
163   Expectation fb =
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))
168           .After(tex, image);
169   EXPECT_CALL(
170       *context,
171       framebufferTexture2D(
172           GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Ne(0U), _))
173       .After(fb, bind_tex);
174
175   output_surface->BindFramebuffer();
176 }
177
178 TEST_F(BufferQueueTest, PartialSwapReuse) {
179   // Check that
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);
188   SendFullFrame();
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);
194 }
195
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);
201   SendFullFrame();
202   SendDamagedFrame(small_damage);
203   SendFullFrame();
204   SendFullFrame();
205   EXPECT_EQ(next_frame().damage, screen_rect);
206 }
207
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))
215       .Times(1);
216
217   SendFullFrame();
218   SendDamagedFrame(small_damage);
219   SendDamagedFrame(overlapping_damage);
220   EXPECT_EQ(next_frame().damage, overlapping_damage);
221 }
222
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());
231 }
232
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());
239   SwapBuffers();
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());
245   CheckUnique();
246   EXPECT_NE(0U, current_surface());
247   EXPECT_EQ(1U, in_flight_surfaces().size());
248   SwapBuffers();
249   CheckUnique();
250   EXPECT_EQ(2U, in_flight_surfaces().size());
251   output_surface_->PageFlipComplete();
252   CheckUnique();
253   EXPECT_EQ(1U, in_flight_surfaces().size());
254   EXPECT_EQ(1U, available_surfaces().size());
255   output_surface_->BindFramebuffer();
256   EXPECT_EQ(2, CountBuffers());
257   CheckUnique();
258   EXPECT_TRUE(available_surfaces().empty());
259 }
260
261 TEST_F(BufferQueueTest, CheckTripleBuffering) {
262   // Check buffer flow through triple buffering path.
263
264   // This bit is the same sequence tested in the doublebuffering case.
265   output_surface_->BindFramebuffer();
266   SwapBuffers();
267   output_surface_->PageFlipComplete();
268   output_surface_->BindFramebuffer();
269   SwapBuffers();
270
271   EXPECT_EQ(2, CountBuffers());
272   CheckUnique();
273   EXPECT_EQ(2U, in_flight_surfaces().size());
274   output_surface_->BindFramebuffer();
275   EXPECT_EQ(3, CountBuffers());
276   CheckUnique();
277   EXPECT_NE(0U, current_surface());
278   EXPECT_EQ(2U, in_flight_surfaces().size());
279   output_surface_->PageFlipComplete();
280   EXPECT_EQ(3, CountBuffers());
281   CheckUnique();
282   EXPECT_NE(0U, current_surface());
283   EXPECT_EQ(1U, in_flight_surfaces().size());
284   EXPECT_EQ(1U, available_surfaces().size());
285 }
286
287 }  // namespace
288 }  // namespace content