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 BeginFrameForTesting() {
53 client_->BeginFrame(CreateExpiredBeginFrameArgsForTesting());
56 void DidSwapBuffersForTesting() { client_->DidSwapBuffers(); }
58 void OnSwapBuffersCompleteForTesting() { client_->DidSwapBuffersComplete(); }
63 class TestSoftwareOutputDevice : public SoftwareOutputDevice {
65 TestSoftwareOutputDevice();
66 ~TestSoftwareOutputDevice() override;
68 // Overriden from cc:SoftwareOutputDevice
69 void DiscardBackbuffer() override;
70 void EnsureBackbuffer() override;
72 int discard_backbuffer_count() { return discard_backbuffer_count_; }
73 int ensure_backbuffer_count() { return ensure_backbuffer_count_; }
76 int discard_backbuffer_count_;
77 int ensure_backbuffer_count_;
80 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
81 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
83 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
85 void TestSoftwareOutputDevice::DiscardBackbuffer() {
86 SoftwareOutputDevice::DiscardBackbuffer();
87 discard_backbuffer_count_++;
90 void TestSoftwareOutputDevice::EnsureBackbuffer() {
91 SoftwareOutputDevice::EnsureBackbuffer();
92 ensure_backbuffer_count_++;
95 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) {
96 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
97 TestOutputSurface output_surface(provider);
98 EXPECT_FALSE(output_surface.HasClient());
100 FakeOutputSurfaceClient client;
101 EXPECT_TRUE(output_surface.BindToClient(&client));
102 EXPECT_TRUE(output_surface.HasClient());
103 EXPECT_FALSE(client.deferred_initialize_called());
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());
113 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) {
114 scoped_refptr<TestContextProvider> context_provider =
115 TestContextProvider::Create();
117 // Lose the context so BindToClient fails.
118 context_provider->UnboundTestContext3d()->set_context_lost(true);
120 TestOutputSurface output_surface(context_provider);
121 EXPECT_FALSE(output_surface.HasClient());
123 FakeOutputSurfaceClient client;
124 EXPECT_FALSE(output_surface.BindToClient(&client));
125 EXPECT_FALSE(output_surface.HasClient());
128 class OutputSurfaceTestInitializeNewContext3d : public ::testing::Test {
130 OutputSurfaceTestInitializeNewContext3d()
131 : context_provider_(TestContextProvider::Create()),
133 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice)),
134 client_(&output_surface_) {}
137 void BindOutputSurface() {
138 EXPECT_TRUE(output_surface_.BindToClient(&client_));
139 EXPECT_TRUE(output_surface_.HasClient());
142 void InitializeNewContextExpectFail() {
143 EXPECT_FALSE(output_surface_.InitializeNewContext3d(context_provider_));
144 EXPECT_TRUE(output_surface_.HasClient());
146 EXPECT_FALSE(output_surface_.context_provider());
147 EXPECT_TRUE(output_surface_.software_device());
150 scoped_refptr<TestContextProvider> context_provider_;
151 TestOutputSurface output_surface_;
152 FakeOutputSurfaceClient client_;
155 TEST_F(OutputSurfaceTestInitializeNewContext3d, Success) {
157 EXPECT_FALSE(client_.deferred_initialize_called());
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());
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());
169 output_surface_.ReleaseGL();
170 EXPECT_FALSE(output_surface_.context_provider());
173 TEST_F(OutputSurfaceTestInitializeNewContext3d, Context3dMakeCurrentFails) {
176 context_provider_->UnboundTestContext3d()->set_context_lost(true);
177 InitializeNewContextExpectFail();
180 TEST(OutputSurfaceTest, MemoryAllocation) {
181 scoped_refptr<TestContextProvider> context_provider =
182 TestContextProvider::Create();
184 TestOutputSurface output_surface(context_provider);
186 FakeOutputSurfaceClient client;
187 EXPECT_TRUE(output_surface.BindToClient(&client));
189 ManagedMemoryPolicy policy(0);
190 policy.bytes_limit_when_visible = 1234;
191 policy.priority_cutoff_when_visible =
192 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
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);
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);
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);
211 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) {
212 TestSoftwareOutputDevice* software_output_device =
213 new TestSoftwareOutputDevice();
215 // TestOutputSurface now owns software_output_device and has responsibility to
217 TestOutputSurface output_surface(make_scoped_ptr(software_output_device));
219 EXPECT_EQ(0, software_output_device->ensure_backbuffer_count());
220 EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
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();
227 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
228 EXPECT_EQ(1, software_output_device->discard_backbuffer_count());