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 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(CreateExpiredBeginFrameArgsForTesting());
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)),
129 client_(&output_surface_) {}
132 void BindOutputSurface() {
133 EXPECT_TRUE(output_surface_.BindToClient(&client_));
134 EXPECT_TRUE(output_surface_.HasClient());
137 void InitializeNewContextExpectFail() {
138 EXPECT_FALSE(output_surface_.InitializeNewContext3d(context_provider_));
139 EXPECT_TRUE(output_surface_.HasClient());
141 EXPECT_FALSE(output_surface_.context_provider());
142 EXPECT_TRUE(output_surface_.software_device());
145 scoped_refptr<TestContextProvider> context_provider_;
146 TestOutputSurface output_surface_;
147 FakeOutputSurfaceClient client_;
150 TEST_F(OutputSurfaceTestInitializeNewContext3d, Success) {
152 EXPECT_FALSE(client_.deferred_initialize_called());
154 EXPECT_TRUE(output_surface_.InitializeNewContext3d(context_provider_));
155 EXPECT_TRUE(client_.deferred_initialize_called());
156 EXPECT_EQ(context_provider_.get(), output_surface_.context_provider());
158 EXPECT_FALSE(client_.did_lose_output_surface_called());
159 context_provider_->ContextGL()->LoseContextCHROMIUM(
160 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
161 context_provider_->ContextGL()->Flush();
162 EXPECT_TRUE(client_.did_lose_output_surface_called());
164 output_surface_.ReleaseGL();
165 EXPECT_FALSE(output_surface_.context_provider());
168 TEST_F(OutputSurfaceTestInitializeNewContext3d, Context3dMakeCurrentFails) {
171 context_provider_->UnboundTestContext3d()->set_context_lost(true);
172 InitializeNewContextExpectFail();
175 TEST(OutputSurfaceTest, MemoryAllocation) {
176 scoped_refptr<TestContextProvider> context_provider =
177 TestContextProvider::Create();
179 TestOutputSurface output_surface(context_provider);
181 FakeOutputSurfaceClient client;
182 EXPECT_TRUE(output_surface.BindToClient(&client));
184 ManagedMemoryPolicy policy(0);
185 policy.bytes_limit_when_visible = 1234;
186 policy.priority_cutoff_when_visible =
187 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
189 context_provider->SetMemoryAllocation(policy);
190 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
191 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY,
192 client.memory_policy().priority_cutoff_when_visible);
194 policy.priority_cutoff_when_visible =
195 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
196 context_provider->SetMemoryAllocation(policy);
197 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
198 client.memory_policy().priority_cutoff_when_visible);
200 // 0 bytes limit should be ignored.
201 policy.bytes_limit_when_visible = 0;
202 context_provider->SetMemoryAllocation(policy);
203 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
206 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) {
207 TestSoftwareOutputDevice* software_output_device =
208 new TestSoftwareOutputDevice();
210 // TestOutputSurface now owns software_output_device and has responsibility to
212 scoped_ptr<TestSoftwareOutputDevice> p(software_output_device);
213 TestOutputSurface output_surface(p.PassAs<SoftwareOutputDevice>());
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());