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/fake_output_surface.h"
12 #include "cc/test/fake_output_surface_client.h"
13 #include "cc/test/scheduler_test_common.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 bool InitializeNewContext3d(
36 scoped_refptr
<ContextProvider
> new_context_provider
) {
37 return InitializeAndSetContext3d(new_context_provider
);
40 using OutputSurface::ReleaseGL
;
42 void CommitVSyncParametersForTesting(base::TimeTicks timebase
,
43 base::TimeDelta interval
) {
44 CommitVSyncParameters(timebase
, interval
);
47 void BeginFrameForTesting() {
48 client_
->BeginFrame(BeginFrameArgs::CreateExpiredForTesting());
51 void DidSwapBuffersForTesting() { client_
->DidSwapBuffers(); }
53 void OnSwapBuffersCompleteForTesting() { client_
->DidSwapBuffersComplete(); }
58 class TestSoftwareOutputDevice
: public SoftwareOutputDevice
{
60 TestSoftwareOutputDevice();
61 virtual ~TestSoftwareOutputDevice();
63 // Overriden from cc:SoftwareOutputDevice
64 virtual void DiscardBackbuffer() OVERRIDE
;
65 virtual void EnsureBackbuffer() OVERRIDE
;
67 int discard_backbuffer_count() { return discard_backbuffer_count_
; }
68 int ensure_backbuffer_count() { return ensure_backbuffer_count_
; }
71 int discard_backbuffer_count_
;
72 int ensure_backbuffer_count_
;
75 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
76 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
78 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
80 void TestSoftwareOutputDevice::DiscardBackbuffer() {
81 SoftwareOutputDevice::DiscardBackbuffer();
82 discard_backbuffer_count_
++;
85 void TestSoftwareOutputDevice::EnsureBackbuffer() {
86 SoftwareOutputDevice::EnsureBackbuffer();
87 ensure_backbuffer_count_
++;
90 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientSuccess
) {
91 scoped_refptr
<TestContextProvider
> provider
= TestContextProvider::Create();
92 TestOutputSurface
output_surface(provider
);
93 EXPECT_FALSE(output_surface
.HasClient());
95 FakeOutputSurfaceClient client
;
96 EXPECT_TRUE(output_surface
.BindToClient(&client
));
97 EXPECT_TRUE(output_surface
.HasClient());
98 EXPECT_FALSE(client
.deferred_initialize_called());
100 // Verify DidLoseOutputSurface callback is hooked up correctly.
101 EXPECT_FALSE(client
.did_lose_output_surface_called());
102 output_surface
.context_provider()->ContextGL()->LoseContextCHROMIUM(
103 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
104 output_surface
.context_provider()->ContextGL()->Flush();
105 EXPECT_TRUE(client
.did_lose_output_surface_called());
108 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientFailure
) {
109 scoped_refptr
<TestContextProvider
> context_provider
=
110 TestContextProvider::Create();
112 // Lose the context so BindToClient fails.
113 context_provider
->UnboundTestContext3d()->set_context_lost(true);
115 TestOutputSurface
output_surface(context_provider
);
116 EXPECT_FALSE(output_surface
.HasClient());
118 FakeOutputSurfaceClient client
;
119 EXPECT_FALSE(output_surface
.BindToClient(&client
));
120 EXPECT_FALSE(output_surface
.HasClient());
123 class OutputSurfaceTestInitializeNewContext3d
: public ::testing::Test
{
125 OutputSurfaceTestInitializeNewContext3d()
126 : context_provider_(TestContextProvider::Create()),
128 scoped_ptr
<SoftwareOutputDevice
>(new SoftwareOutputDevice
)) {}
131 void BindOutputSurface() {
132 EXPECT_TRUE(output_surface_
.BindToClient(&client_
));
133 EXPECT_TRUE(output_surface_
.HasClient());
136 void InitializeNewContextExpectFail() {
137 EXPECT_FALSE(output_surface_
.InitializeNewContext3d(context_provider_
));
138 EXPECT_TRUE(output_surface_
.HasClient());
140 EXPECT_FALSE(output_surface_
.context_provider());
141 EXPECT_TRUE(output_surface_
.software_device());
144 scoped_refptr
<TestContextProvider
> context_provider_
;
145 TestOutputSurface output_surface_
;
146 FakeOutputSurfaceClient client_
;
149 TEST_F(OutputSurfaceTestInitializeNewContext3d
, Success
) {
151 EXPECT_FALSE(client_
.deferred_initialize_called());
153 EXPECT_TRUE(output_surface_
.InitializeNewContext3d(context_provider_
));
154 EXPECT_TRUE(client_
.deferred_initialize_called());
155 EXPECT_EQ(context_provider_
, output_surface_
.context_provider());
157 EXPECT_FALSE(client_
.did_lose_output_surface_called());
158 context_provider_
->ContextGL()->LoseContextCHROMIUM(
159 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
160 context_provider_
->ContextGL()->Flush();
161 EXPECT_TRUE(client_
.did_lose_output_surface_called());
163 output_surface_
.ReleaseGL();
164 EXPECT_FALSE(output_surface_
.context_provider());
167 TEST_F(OutputSurfaceTestInitializeNewContext3d
, Context3dMakeCurrentFails
) {
170 context_provider_
->UnboundTestContext3d()->set_context_lost(true);
171 InitializeNewContextExpectFail();
174 TEST(OutputSurfaceTest
, MemoryAllocation
) {
175 scoped_refptr
<TestContextProvider
> context_provider
=
176 TestContextProvider::Create();
178 TestOutputSurface
output_surface(context_provider
);
180 FakeOutputSurfaceClient client
;
181 EXPECT_TRUE(output_surface
.BindToClient(&client
));
183 ManagedMemoryPolicy
policy(0);
184 policy
.bytes_limit_when_visible
= 1234;
185 policy
.priority_cutoff_when_visible
=
186 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
;
188 context_provider
->SetMemoryAllocation(policy
);
189 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
190 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
,
191 client
.memory_policy().priority_cutoff_when_visible
);
193 policy
.priority_cutoff_when_visible
=
194 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
;
195 context_provider
->SetMemoryAllocation(policy
);
196 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
,
197 client
.memory_policy().priority_cutoff_when_visible
);
199 // 0 bytes limit should be ignored.
200 policy
.bytes_limit_when_visible
= 0;
201 context_provider
->SetMemoryAllocation(policy
);
202 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
205 TEST(OutputSurfaceTest
, SoftwareOutputDeviceBackbufferManagement
) {
206 TestSoftwareOutputDevice
* software_output_device
=
207 new TestSoftwareOutputDevice();
209 // TestOutputSurface now owns software_output_device and has responsibility to
211 scoped_ptr
<TestSoftwareOutputDevice
> p(software_output_device
);
212 TestOutputSurface
output_surface(p
.PassAs
<SoftwareOutputDevice
>());
214 EXPECT_EQ(0, software_output_device
->ensure_backbuffer_count());
215 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
217 output_surface
.EnsureBackbuffer();
218 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
219 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
220 output_surface
.DiscardBackbuffer();
222 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
223 EXPECT_EQ(1, software_output_device
->discard_backbuffer_count());