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"
22 class TestOutputSurface
: public OutputSurface
{
24 explicit TestOutputSurface(scoped_refptr
<ContextProvider
> context_provider
)
25 : OutputSurface(context_provider
) {}
27 TestOutputSurface(scoped_refptr
<ContextProvider
> context_provider
,
28 scoped_refptr
<ContextProvider
> worker_context_provider
)
29 : OutputSurface(worker_context_provider
) {}
31 explicit TestOutputSurface(scoped_ptr
<SoftwareOutputDevice
> software_device
)
32 : OutputSurface(software_device
.Pass()) {}
34 TestOutputSurface(scoped_refptr
<ContextProvider
> context_provider
,
35 scoped_ptr
<SoftwareOutputDevice
> software_device
)
36 : OutputSurface(context_provider
, software_device
.Pass()) {}
38 void SwapBuffers(CompositorFrame
* frame
) override
{
39 client_
->DidSwapBuffers();
40 client_
->DidSwapBuffersComplete();
43 void CommitVSyncParametersForTesting(base::TimeTicks timebase
,
44 base::TimeDelta interval
) {
45 CommitVSyncParameters(timebase
, interval
);
48 void DidSwapBuffersForTesting() { client_
->DidSwapBuffers(); }
50 void OnSwapBuffersCompleteForTesting() { client_
->DidSwapBuffersComplete(); }
55 class TestSoftwareOutputDevice
: public SoftwareOutputDevice
{
57 TestSoftwareOutputDevice();
58 ~TestSoftwareOutputDevice() override
;
60 // Overriden from cc:SoftwareOutputDevice
61 void DiscardBackbuffer() override
;
62 void EnsureBackbuffer() override
;
64 int discard_backbuffer_count() { return discard_backbuffer_count_
; }
65 int ensure_backbuffer_count() { return ensure_backbuffer_count_
; }
68 int discard_backbuffer_count_
;
69 int ensure_backbuffer_count_
;
72 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
73 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
75 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
77 void TestSoftwareOutputDevice::DiscardBackbuffer() {
78 SoftwareOutputDevice::DiscardBackbuffer();
79 discard_backbuffer_count_
++;
82 void TestSoftwareOutputDevice::EnsureBackbuffer() {
83 SoftwareOutputDevice::EnsureBackbuffer();
84 ensure_backbuffer_count_
++;
87 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientSuccess
) {
88 scoped_refptr
<TestContextProvider
> provider
= TestContextProvider::Create();
89 TestOutputSurface
output_surface(provider
);
90 EXPECT_FALSE(output_surface
.HasClient());
92 FakeOutputSurfaceClient client
;
93 EXPECT_TRUE(output_surface
.BindToClient(&client
));
94 EXPECT_TRUE(output_surface
.HasClient());
96 // Verify DidLoseOutputSurface callback is hooked up correctly.
97 EXPECT_FALSE(client
.did_lose_output_surface_called());
98 output_surface
.context_provider()->ContextGL()->LoseContextCHROMIUM(
99 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
100 output_surface
.context_provider()->ContextGL()->Flush();
101 EXPECT_TRUE(client
.did_lose_output_surface_called());
104 TEST(OutputSurfaceTest
, ClientPointerIndicatesWorkerBindToClientSuccess
) {
105 scoped_refptr
<TestContextProvider
> provider
= TestContextProvider::Create();
106 scoped_refptr
<TestContextProvider
> worker_provider
=
107 TestContextProvider::Create();
108 TestOutputSurface
output_surface(provider
, worker_provider
);
109 EXPECT_FALSE(output_surface
.HasClient());
111 FakeOutputSurfaceClient client
;
112 EXPECT_TRUE(output_surface
.BindToClient(&client
));
113 EXPECT_TRUE(output_surface
.HasClient());
115 // Verify DidLoseOutputSurface callback is hooked up correctly.
116 EXPECT_FALSE(client
.did_lose_output_surface_called());
117 output_surface
.context_provider()->ContextGL()->LoseContextCHROMIUM(
118 GL_GUILTY_CONTEXT_RESET_ARB
, GL_INNOCENT_CONTEXT_RESET_ARB
);
119 output_surface
.context_provider()->ContextGL()->Flush();
120 EXPECT_TRUE(client
.did_lose_output_surface_called());
123 TEST(OutputSurfaceTest
, ClientPointerIndicatesBindToClientFailure
) {
124 scoped_refptr
<TestContextProvider
> context_provider
=
125 TestContextProvider::Create();
127 // Lose the context so BindToClient fails.
128 context_provider
->UnboundTestContext3d()->set_context_lost(true);
130 TestOutputSurface
output_surface(context_provider
);
131 EXPECT_FALSE(output_surface
.HasClient());
133 FakeOutputSurfaceClient client
;
134 EXPECT_FALSE(output_surface
.BindToClient(&client
));
135 EXPECT_FALSE(output_surface
.HasClient());
138 TEST(OutputSurfaceTest
, ClientPointerIndicatesWorkerBindToClientFailure
) {
139 scoped_refptr
<TestContextProvider
> context_provider
=
140 TestContextProvider::Create();
141 scoped_refptr
<TestContextProvider
> worker_context_provider
=
142 TestContextProvider::Create();
144 // Lose the context so BindToClient fails.
145 worker_context_provider
->UnboundTestContext3d()->set_context_lost(true);
147 TestOutputSurface
output_surface(context_provider
, worker_context_provider
);
148 EXPECT_FALSE(output_surface
.HasClient());
150 FakeOutputSurfaceClient client
;
151 EXPECT_FALSE(output_surface
.BindToClient(&client
));
152 EXPECT_FALSE(output_surface
.HasClient());
155 TEST(OutputSurfaceTest
, MemoryAllocation
) {
156 scoped_refptr
<TestContextProvider
> context_provider
=
157 TestContextProvider::Create();
159 TestOutputSurface
output_surface(context_provider
);
161 FakeOutputSurfaceClient client
;
162 EXPECT_TRUE(output_surface
.BindToClient(&client
));
164 ManagedMemoryPolicy
policy(0);
165 policy
.bytes_limit_when_visible
= 1234;
166 policy
.priority_cutoff_when_visible
=
167 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
;
169 context_provider
->SetMemoryAllocation(policy
);
170 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
171 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY
,
172 client
.memory_policy().priority_cutoff_when_visible
);
174 policy
.priority_cutoff_when_visible
=
175 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
;
176 context_provider
->SetMemoryAllocation(policy
);
177 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING
,
178 client
.memory_policy().priority_cutoff_when_visible
);
180 // 0 bytes limit should be ignored.
181 policy
.bytes_limit_when_visible
= 0;
182 context_provider
->SetMemoryAllocation(policy
);
183 EXPECT_EQ(1234u, client
.memory_policy().bytes_limit_when_visible
);
186 TEST(OutputSurfaceTest
, SoftwareOutputDeviceBackbufferManagement
) {
187 TestSoftwareOutputDevice
* software_output_device
=
188 new TestSoftwareOutputDevice();
190 // TestOutputSurface now owns software_output_device and has responsibility to
192 TestOutputSurface
output_surface(make_scoped_ptr(software_output_device
));
194 EXPECT_EQ(0, software_output_device
->ensure_backbuffer_count());
195 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
197 output_surface
.EnsureBackbuffer();
198 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
199 EXPECT_EQ(0, software_output_device
->discard_backbuffer_count());
200 output_surface
.DiscardBackbuffer();
202 EXPECT_EQ(1, software_output_device
->ensure_backbuffer_count());
203 EXPECT_EQ(1, software_output_device
->discard_backbuffer_count());