Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / cc / output / output_surface_unittest.cc
blobf32a96f21839917e8fe7d664b7ed353891865a1d
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"
20 namespace cc {
21 namespace {
23 class TestOutputSurface : public OutputSurface {
24 public:
25 explicit TestOutputSurface(scoped_refptr<ContextProvider> context_provider)
26 : OutputSurface(context_provider) {}
28 TestOutputSurface(scoped_refptr<ContextProvider> context_provider,
29 scoped_refptr<ContextProvider> worker_context_provider)
30 : OutputSurface(worker_context_provider) {}
32 explicit TestOutputSurface(scoped_ptr<SoftwareOutputDevice> software_device)
33 : OutputSurface(software_device.Pass()) {}
35 TestOutputSurface(scoped_refptr<ContextProvider> context_provider,
36 scoped_ptr<SoftwareOutputDevice> software_device)
37 : OutputSurface(context_provider, software_device.Pass()) {}
39 void SwapBuffers(CompositorFrame* frame) override {
40 client_->DidSwapBuffers();
41 client_->DidSwapBuffersComplete();
44 bool InitializeNewContext3d(scoped_refptr<ContextProvider> context_provider) {
45 return InitializeAndSetContext3d(context_provider, nullptr);
48 using OutputSurface::ReleaseGL;
50 void CommitVSyncParametersForTesting(base::TimeTicks timebase,
51 base::TimeDelta interval) {
52 CommitVSyncParameters(timebase, interval);
55 void DidSwapBuffersForTesting() { client_->DidSwapBuffers(); }
57 void OnSwapBuffersCompleteForTesting() { client_->DidSwapBuffersComplete(); }
59 protected:
62 class TestSoftwareOutputDevice : public SoftwareOutputDevice {
63 public:
64 TestSoftwareOutputDevice();
65 ~TestSoftwareOutputDevice() override;
67 // Overriden from cc:SoftwareOutputDevice
68 void DiscardBackbuffer() override;
69 void EnsureBackbuffer() override;
71 int discard_backbuffer_count() { return discard_backbuffer_count_; }
72 int ensure_backbuffer_count() { return ensure_backbuffer_count_; }
74 private:
75 int discard_backbuffer_count_;
76 int ensure_backbuffer_count_;
79 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
80 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
82 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
84 void TestSoftwareOutputDevice::DiscardBackbuffer() {
85 SoftwareOutputDevice::DiscardBackbuffer();
86 discard_backbuffer_count_++;
89 void TestSoftwareOutputDevice::EnsureBackbuffer() {
90 SoftwareOutputDevice::EnsureBackbuffer();
91 ensure_backbuffer_count_++;
94 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) {
95 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
96 TestOutputSurface output_surface(provider);
97 EXPECT_FALSE(output_surface.HasClient());
99 FakeOutputSurfaceClient client;
100 EXPECT_TRUE(output_surface.BindToClient(&client));
101 EXPECT_TRUE(output_surface.HasClient());
102 EXPECT_FALSE(client.deferred_initialize_called());
104 // Verify DidLoseOutputSurface callback is hooked up correctly.
105 EXPECT_FALSE(client.did_lose_output_surface_called());
106 output_surface.context_provider()->ContextGL()->LoseContextCHROMIUM(
107 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
108 output_surface.context_provider()->ContextGL()->Flush();
109 EXPECT_TRUE(client.did_lose_output_surface_called());
112 TEST(OutputSurfaceTest, ClientPointerIndicatesWorkerBindToClientSuccess) {
113 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
114 scoped_refptr<TestContextProvider> worker_provider =
115 TestContextProvider::Create();
116 TestOutputSurface output_surface(provider, worker_provider);
117 EXPECT_FALSE(output_surface.HasClient());
119 FakeOutputSurfaceClient client;
120 EXPECT_TRUE(output_surface.BindToClient(&client));
121 EXPECT_TRUE(output_surface.HasClient());
122 EXPECT_FALSE(client.deferred_initialize_called());
124 // Verify DidLoseOutputSurface callback is hooked up correctly.
125 EXPECT_FALSE(client.did_lose_output_surface_called());
126 output_surface.context_provider()->ContextGL()->LoseContextCHROMIUM(
127 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
128 output_surface.context_provider()->ContextGL()->Flush();
129 EXPECT_TRUE(client.did_lose_output_surface_called());
132 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) {
133 scoped_refptr<TestContextProvider> context_provider =
134 TestContextProvider::Create();
136 // Lose the context so BindToClient fails.
137 context_provider->UnboundTestContext3d()->set_context_lost(true);
139 TestOutputSurface output_surface(context_provider);
140 EXPECT_FALSE(output_surface.HasClient());
142 FakeOutputSurfaceClient client;
143 EXPECT_FALSE(output_surface.BindToClient(&client));
144 EXPECT_FALSE(output_surface.HasClient());
147 TEST(OutputSurfaceTest, ClientPointerIndicatesWorkerBindToClientFailure) {
148 scoped_refptr<TestContextProvider> context_provider =
149 TestContextProvider::Create();
150 scoped_refptr<TestContextProvider> worker_context_provider =
151 TestContextProvider::Create();
153 // Lose the context so BindToClient fails.
154 worker_context_provider->UnboundTestContext3d()->set_context_lost(true);
156 TestOutputSurface output_surface(context_provider, worker_context_provider);
157 EXPECT_FALSE(output_surface.HasClient());
159 FakeOutputSurfaceClient client;
160 EXPECT_FALSE(output_surface.BindToClient(&client));
161 EXPECT_FALSE(output_surface.HasClient());
164 class OutputSurfaceTestInitializeNewContext3d : public ::testing::Test {
165 public:
166 OutputSurfaceTestInitializeNewContext3d()
167 : context_provider_(TestContextProvider::Create()),
168 output_surface_(
169 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice)),
170 client_(&output_surface_) {}
172 protected:
173 void BindOutputSurface() {
174 EXPECT_TRUE(output_surface_.BindToClient(&client_));
175 EXPECT_TRUE(output_surface_.HasClient());
178 void InitializeNewContextExpectFail() {
179 EXPECT_FALSE(output_surface_.InitializeNewContext3d(context_provider_));
180 EXPECT_TRUE(output_surface_.HasClient());
182 EXPECT_FALSE(output_surface_.context_provider());
183 EXPECT_TRUE(output_surface_.software_device());
186 scoped_refptr<TestContextProvider> context_provider_;
187 TestOutputSurface output_surface_;
188 FakeOutputSurfaceClient client_;
191 TEST_F(OutputSurfaceTestInitializeNewContext3d, Success) {
192 BindOutputSurface();
193 EXPECT_FALSE(client_.deferred_initialize_called());
195 EXPECT_TRUE(output_surface_.InitializeNewContext3d(context_provider_));
196 EXPECT_TRUE(client_.deferred_initialize_called());
197 EXPECT_EQ(context_provider_.get(), output_surface_.context_provider());
199 EXPECT_FALSE(client_.did_lose_output_surface_called());
200 context_provider_->ContextGL()->LoseContextCHROMIUM(
201 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
202 context_provider_->ContextGL()->Flush();
203 EXPECT_TRUE(client_.did_lose_output_surface_called());
205 output_surface_.ReleaseGL();
206 EXPECT_FALSE(output_surface_.context_provider());
209 TEST_F(OutputSurfaceTestInitializeNewContext3d, Context3dMakeCurrentFails) {
210 BindOutputSurface();
212 context_provider_->UnboundTestContext3d()->set_context_lost(true);
213 InitializeNewContextExpectFail();
216 TEST(OutputSurfaceTest, MemoryAllocation) {
217 scoped_refptr<TestContextProvider> context_provider =
218 TestContextProvider::Create();
220 TestOutputSurface output_surface(context_provider);
222 FakeOutputSurfaceClient client;
223 EXPECT_TRUE(output_surface.BindToClient(&client));
225 ManagedMemoryPolicy policy(0);
226 policy.bytes_limit_when_visible = 1234;
227 policy.priority_cutoff_when_visible =
228 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
230 context_provider->SetMemoryAllocation(policy);
231 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
232 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY,
233 client.memory_policy().priority_cutoff_when_visible);
235 policy.priority_cutoff_when_visible =
236 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
237 context_provider->SetMemoryAllocation(policy);
238 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
239 client.memory_policy().priority_cutoff_when_visible);
241 // 0 bytes limit should be ignored.
242 policy.bytes_limit_when_visible = 0;
243 context_provider->SetMemoryAllocation(policy);
244 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
247 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) {
248 TestSoftwareOutputDevice* software_output_device =
249 new TestSoftwareOutputDevice();
251 // TestOutputSurface now owns software_output_device and has responsibility to
252 // free it.
253 TestOutputSurface output_surface(make_scoped_ptr(software_output_device));
255 EXPECT_EQ(0, software_output_device->ensure_backbuffer_count());
256 EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
258 output_surface.EnsureBackbuffer();
259 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
260 EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
261 output_surface.DiscardBackbuffer();
263 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
264 EXPECT_EQ(1, software_output_device->discard_backbuffer_count());
267 } // namespace
268 } // namespace cc