b1f4149196dc091a35b84370aa8f738da3a410ff
[platform/framework/web/crosswalk.git] / src / cc / output / output_surface_unittest.cc
1 // Copyright 2013 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 "cc/output/output_surface.h"
6
7 #include "base/test/test_simple_task_runner.h"
8 #include "cc/output/managed_memory_policy.h"
9 #include "cc/output/output_surface_client.h"
10 #include "cc/output/software_output_device.h"
11 #include "cc/test/begin_frame_args_test.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_output_surface_client.h"
14 #include "cc/test/test_context_provider.h"
15 #include "cc/test/test_web_graphics_context_3d.h"
16 #include "gpu/GLES2/gl2extchromium.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/frame_time.h"
19
20 namespace cc {
21 namespace {
22
23 class TestOutputSurface : public OutputSurface {
24  public:
25   explicit TestOutputSurface(scoped_refptr<ContextProvider> context_provider)
26       : OutputSurface(context_provider) {}
27
28   explicit TestOutputSurface(scoped_ptr<SoftwareOutputDevice> software_device)
29       : OutputSurface(software_device.Pass()) {}
30
31   TestOutputSurface(scoped_refptr<ContextProvider> context_provider,
32                     scoped_ptr<SoftwareOutputDevice> software_device)
33       : OutputSurface(context_provider, software_device.Pass()) {}
34
35   void SwapBuffers(CompositorFrame* frame) override {
36     client_->DidSwapBuffers();
37     client_->DidSwapBuffersComplete();
38   }
39
40   bool InitializeNewContext3d(
41       scoped_refptr<ContextProvider> new_context_provider) {
42     return InitializeAndSetContext3d(new_context_provider);
43   }
44
45   using OutputSurface::ReleaseGL;
46
47   void CommitVSyncParametersForTesting(base::TimeTicks timebase,
48                                        base::TimeDelta interval) {
49     CommitVSyncParameters(timebase, interval);
50   }
51
52   void BeginFrameForTesting() {
53     client_->BeginFrame(CreateExpiredBeginFrameArgsForTesting());
54   }
55
56   void DidSwapBuffersForTesting() { client_->DidSwapBuffers(); }
57
58   void OnSwapBuffersCompleteForTesting() { client_->DidSwapBuffersComplete(); }
59
60  protected:
61 };
62
63 class TestSoftwareOutputDevice : public SoftwareOutputDevice {
64  public:
65   TestSoftwareOutputDevice();
66   ~TestSoftwareOutputDevice() override;
67
68   // Overriden from cc:SoftwareOutputDevice
69   void DiscardBackbuffer() override;
70   void EnsureBackbuffer() override;
71
72   int discard_backbuffer_count() { return discard_backbuffer_count_; }
73   int ensure_backbuffer_count() { return ensure_backbuffer_count_; }
74
75  private:
76   int discard_backbuffer_count_;
77   int ensure_backbuffer_count_;
78 };
79
80 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
81     : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
82
83 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
84
85 void TestSoftwareOutputDevice::DiscardBackbuffer() {
86   SoftwareOutputDevice::DiscardBackbuffer();
87   discard_backbuffer_count_++;
88 }
89
90 void TestSoftwareOutputDevice::EnsureBackbuffer() {
91   SoftwareOutputDevice::EnsureBackbuffer();
92   ensure_backbuffer_count_++;
93 }
94
95 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) {
96   scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
97   TestOutputSurface output_surface(provider);
98   EXPECT_FALSE(output_surface.HasClient());
99
100   FakeOutputSurfaceClient client;
101   EXPECT_TRUE(output_surface.BindToClient(&client));
102   EXPECT_TRUE(output_surface.HasClient());
103   EXPECT_FALSE(client.deferred_initialize_called());
104
105   // Verify DidLoseOutputSurface callback is hooked up correctly.
106   EXPECT_FALSE(client.did_lose_output_surface_called());
107   output_surface.context_provider()->ContextGL()->LoseContextCHROMIUM(
108       GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
109   output_surface.context_provider()->ContextGL()->Flush();
110   EXPECT_TRUE(client.did_lose_output_surface_called());
111 }
112
113 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) {
114   scoped_refptr<TestContextProvider> context_provider =
115       TestContextProvider::Create();
116
117   // Lose the context so BindToClient fails.
118   context_provider->UnboundTestContext3d()->set_context_lost(true);
119
120   TestOutputSurface output_surface(context_provider);
121   EXPECT_FALSE(output_surface.HasClient());
122
123   FakeOutputSurfaceClient client;
124   EXPECT_FALSE(output_surface.BindToClient(&client));
125   EXPECT_FALSE(output_surface.HasClient());
126 }
127
128 class OutputSurfaceTestInitializeNewContext3d : public ::testing::Test {
129  public:
130   OutputSurfaceTestInitializeNewContext3d()
131       : context_provider_(TestContextProvider::Create()),
132         output_surface_(
133             scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice)),
134         client_(&output_surface_) {}
135
136  protected:
137   void BindOutputSurface() {
138     EXPECT_TRUE(output_surface_.BindToClient(&client_));
139     EXPECT_TRUE(output_surface_.HasClient());
140   }
141
142   void InitializeNewContextExpectFail() {
143     EXPECT_FALSE(output_surface_.InitializeNewContext3d(context_provider_));
144     EXPECT_TRUE(output_surface_.HasClient());
145
146     EXPECT_FALSE(output_surface_.context_provider());
147     EXPECT_TRUE(output_surface_.software_device());
148   }
149
150   scoped_refptr<TestContextProvider> context_provider_;
151   TestOutputSurface output_surface_;
152   FakeOutputSurfaceClient client_;
153 };
154
155 TEST_F(OutputSurfaceTestInitializeNewContext3d, Success) {
156   BindOutputSurface();
157   EXPECT_FALSE(client_.deferred_initialize_called());
158
159   EXPECT_TRUE(output_surface_.InitializeNewContext3d(context_provider_));
160   EXPECT_TRUE(client_.deferred_initialize_called());
161   EXPECT_EQ(context_provider_.get(), output_surface_.context_provider());
162
163   EXPECT_FALSE(client_.did_lose_output_surface_called());
164   context_provider_->ContextGL()->LoseContextCHROMIUM(
165       GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
166   context_provider_->ContextGL()->Flush();
167   EXPECT_TRUE(client_.did_lose_output_surface_called());
168
169   output_surface_.ReleaseGL();
170   EXPECT_FALSE(output_surface_.context_provider());
171 }
172
173 TEST_F(OutputSurfaceTestInitializeNewContext3d, Context3dMakeCurrentFails) {
174   BindOutputSurface();
175
176   context_provider_->UnboundTestContext3d()->set_context_lost(true);
177   InitializeNewContextExpectFail();
178 }
179
180 TEST(OutputSurfaceTest, MemoryAllocation) {
181   scoped_refptr<TestContextProvider> context_provider =
182       TestContextProvider::Create();
183
184   TestOutputSurface output_surface(context_provider);
185
186   FakeOutputSurfaceClient client;
187   EXPECT_TRUE(output_surface.BindToClient(&client));
188
189   ManagedMemoryPolicy policy(0);
190   policy.bytes_limit_when_visible = 1234;
191   policy.priority_cutoff_when_visible =
192       gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
193
194   context_provider->SetMemoryAllocation(policy);
195   EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
196   EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY,
197             client.memory_policy().priority_cutoff_when_visible);
198
199   policy.priority_cutoff_when_visible =
200       gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
201   context_provider->SetMemoryAllocation(policy);
202   EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
203             client.memory_policy().priority_cutoff_when_visible);
204
205   // 0 bytes limit should be ignored.
206   policy.bytes_limit_when_visible = 0;
207   context_provider->SetMemoryAllocation(policy);
208   EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
209 }
210
211 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) {
212   TestSoftwareOutputDevice* software_output_device =
213       new TestSoftwareOutputDevice();
214
215   // TestOutputSurface now owns software_output_device and has responsibility to
216   // free it.
217   TestOutputSurface output_surface(make_scoped_ptr(software_output_device));
218
219   EXPECT_EQ(0, software_output_device->ensure_backbuffer_count());
220   EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
221
222   output_surface.EnsureBackbuffer();
223   EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
224   EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
225   output_surface.DiscardBackbuffer();
226
227   EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
228   EXPECT_EQ(1, software_output_device->discard_backbuffer_count());
229 }
230
231 }  // namespace
232 }  // namespace cc