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.
5 #include "cc/output/output_surface.h"
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"
23 class TestOutputSurface
: public OutputSurface
{
25 explicit TestOutputSurface(scoped_refptr
<ContextProvider
> context_provider
)
26 : OutputSurface(context_provider
) {}
28 explicit TestOutputSurface(scoped_ptr
<SoftwareOutputDevice
> software_device
)
29 : OutputSurface(software_device
.Pass()) {}
31 TestOutputSurface(scoped_refptr
<ContextProvider
> context_provider
,
32 scoped_ptr
<SoftwareOutputDevice
> software_device
)
33 : OutputSurface(context_provider
, software_device
.Pass()) {}
35 void SwapBuffers(CompositorFrame
* frame
) override
{
36 client_
->DidSwapBuffers();
37 client_
->DidSwapBuffersComplete();
40 bool InitializeNewContext3d(
41 scoped_refptr
<ContextProvider
> new_context_provider
) {
42 return InitializeAndSetContext3d(new_context_provider
);
45 using OutputSurface::ReleaseGL
;
47 void CommitVSyncParametersForTesting(base::TimeTicks timebase
,
48 base::TimeDelta interval
) {
49 CommitVSyncParameters(timebase
, interval
);
52 void DidSwapBuffersForTesting() { client_
->DidSwapBuffers(); }
54 void OnSwapBuffersCompleteForTesting() { client_
->DidSwapBuffersComplete(); }
59 class TestSoftwareOutputDevice
: public SoftwareOutputDevice
{
61 TestSoftwareOutputDevice();
62 ~TestSoftwareOutputDevice() override
;
64 // Overriden from cc:SoftwareOutputDevice
65 void DiscardBackbuffer() override
;
66 void EnsureBackbuffer() override
;
68 int discard_backbuffer_count() { return discard_backbuffer_count_
; }
69 int ensure_backbuffer_count() { return ensure_backbuffer_count_
; }
72 int discard_backbuffer_count_
;
73 int ensure_backbuffer_count_
;
76 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
77 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
79 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
81 void TestSoftwareOutputDevice::DiscardBackbuffer() {
82 SoftwareOutputDevice::DiscardBackbuffer();
83 discard_backbuffer_count_
++;
86 void TestSoftwareOutputDevice::EnsureBackbuffer() {
87 SoftwareOutputDevice::EnsureBackbuffer();
88 ensure_backbuffer_count_
++;
91 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientSuccess
) {
92 scoped_refptr
<TestContextProvider
> provider
= TestContextProvider::Create();
93 TestOutputSurface
output_surface(provider
);
94 EXPECT_FALSE(output_surface
.HasClient());
96 FakeOutputSurfaceClient client
;
97 EXPECT_TRUE(output_surface
.BindToClient(&client
));
98 EXPECT_TRUE(output_surface
.HasClient());
99 EXPECT_FALSE(client
.deferred_initialize_called());
101 // Verify DidLoseOutputSurface callback is hooked up correctly.
102 EXPECT_FALSE(client
.did_lose_output_surface_called());
103 output_surface
.context_provider()->ContextGL()->LoseContextCHROMIUM(
104 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
105 output_surface
.context_provider()->ContextGL()->Flush();
106 EXPECT_TRUE(client
.did_lose_output_surface_called());
109 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientFailure
) {
110 scoped_refptr
<TestContextProvider
> context_provider
=
111 TestContextProvider::Create();
113 // Lose the context so BindToClient fails.
114 context_provider
->UnboundTestContext3d()->set_context_lost(true);
116 TestOutputSurface
output_surface(context_provider
);
117 EXPECT_FALSE(output_surface
.HasClient());
119 FakeOutputSurfaceClient client
;
120 EXPECT_FALSE(output_surface
.BindToClient(&client
));
121 EXPECT_FALSE(output_surface
.HasClient());
124 class OutputSurfaceTestInitializeNewContext3d
: public ::testing::Test
{
126 OutputSurfaceTestInitializeNewContext3d()
127 : context_provider_(TestContextProvider::Create()),
129 scoped_ptr
<SoftwareOutputDevice
>(new SoftwareOutputDevice
)),
130 client_(&output_surface_
) {}
133 void BindOutputSurface() {
134 EXPECT_TRUE(output_surface_
.BindToClient(&client_
));
135 EXPECT_TRUE(output_surface_
.HasClient());
138 void InitializeNewContextExpectFail() {
139 EXPECT_FALSE(output_surface_
.InitializeNewContext3d(context_provider_
));
140 EXPECT_TRUE(output_surface_
.HasClient());
142 EXPECT_FALSE(output_surface_
.context_provider());
143 EXPECT_TRUE(output_surface_
.software_device());
146 scoped_refptr
<TestContextProvider
> context_provider_
;
147 TestOutputSurface output_surface_
;
148 FakeOutputSurfaceClient client_
;
151 TEST_F(OutputSurfaceTestInitializeNewContext3d
, Success
) {
153 EXPECT_FALSE(client_
.deferred_initialize_called());
155 EXPECT_TRUE(output_surface_
.InitializeNewContext3d(context_provider_
));
156 EXPECT_TRUE(client_
.deferred_initialize_called());
157 EXPECT_EQ(context_provider_
.get(), output_surface_
.context_provider());
159 EXPECT_FALSE(client_
.did_lose_output_surface_called());
160 context_provider_
->ContextGL()->LoseContextCHROMIUM(
161 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
162 context_provider_
->ContextGL()->Flush();
163 EXPECT_TRUE(client_
.did_lose_output_surface_called());
165 output_surface_
.ReleaseGL();
166 EXPECT_FALSE(output_surface_
.context_provider());
169 TEST_F(OutputSurfaceTestInitializeNewContext3d
, Context3dMakeCurrentFails
) {
172 context_provider_
->UnboundTestContext3d()->set_context_lost(true);
173 InitializeNewContextExpectFail();
176 TEST(OutputSurfaceTest
, MemoryAllocation
) {
177 scoped_refptr
<TestContextProvider
> context_provider
=
178 TestContextProvider::Create();
180 TestOutputSurface
output_surface(context_provider
);
182 FakeOutputSurfaceClient client
;
183 EXPECT_TRUE(output_surface
.BindToClient(&client
));
185 ManagedMemoryPolicy
policy(0);
186 policy
.bytes_limit_when_visible
= 1234;
187 policy
.priority_cutoff_when_visible
=
188 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
;
190 context_provider
->SetMemoryAllocation(policy
);
191 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
192 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
,
193 client
.memory_policy().priority_cutoff_when_visible
);
195 policy
.priority_cutoff_when_visible
=
196 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
;
197 context_provider
->SetMemoryAllocation(policy
);
198 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
,
199 client
.memory_policy().priority_cutoff_when_visible
);
201 // 0 bytes limit should be ignored.
202 policy
.bytes_limit_when_visible
= 0;
203 context_provider
->SetMemoryAllocation(policy
);
204 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
207 TEST(OutputSurfaceTest
, SoftwareOutputDeviceBackbufferManagement
) {
208 TestSoftwareOutputDevice
* software_output_device
=
209 new TestSoftwareOutputDevice();
211 // TestOutputSurface now owns software_output_device and has responsibility to
213 TestOutputSurface
output_surface(make_scoped_ptr(software_output_device
));
215 EXPECT_EQ(0, software_output_device
->ensure_backbuffer_count());
216 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
218 output_surface
.EnsureBackbuffer();
219 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
220 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
221 output_surface
.DiscardBackbuffer();
223 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
224 EXPECT_EQ(1, software_output_device
->discard_backbuffer_count());