| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "cc/output/output_surface.h" | |
| 6 | |
| 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" | |
| 19 | |
| 20 namespace cc { | |
| 21 namespace { | |
| 22 | |
| 23 class TestOutputSurface : public OutputSurface { | |
| 24 public: | |
| 25 explicit TestOutputSurface(scoped_refptr<ContextProvider> context_provider) | |
| 26 : OutputSurface(context_provider) {} | |
| 27 | |
| 28 TestOutputSurface(scoped_refptr<ContextProvider> context_provider, | |
| 29 scoped_refptr<ContextProvider> worker_context_provider) | |
| 30 : OutputSurface(worker_context_provider) {} | |
| 31 | |
| 32 explicit TestOutputSurface(scoped_ptr<SoftwareOutputDevice> software_device) | |
| 33 : OutputSurface(software_device.Pass()) {} | |
| 34 | |
| 35 TestOutputSurface(scoped_refptr<ContextProvider> context_provider, | |
| 36 scoped_ptr<SoftwareOutputDevice> software_device) | |
| 37 : OutputSurface(context_provider, software_device.Pass()) {} | |
| 38 | |
| 39 void SwapBuffers(CompositorFrame* frame) override { | |
| 40 client_->DidSwapBuffers(); | |
| 41 client_->DidSwapBuffersComplete(); | |
| 42 } | |
| 43 | |
| 44 bool InitializeNewContext3d(scoped_refptr<ContextProvider> context_provider) { | |
| 45 return InitializeAndSetContext3d(context_provider, nullptr); | |
| 46 } | |
| 47 | |
| 48 using OutputSurface::ReleaseGL; | |
| 49 | |
| 50 void CommitVSyncParametersForTesting(base::TimeTicks timebase, | |
| 51 base::TimeDelta interval) { | |
| 52 CommitVSyncParameters(timebase, interval); | |
| 53 } | |
| 54 | |
| 55 void DidSwapBuffersForTesting() { client_->DidSwapBuffers(); } | |
| 56 | |
| 57 void OnSwapBuffersCompleteForTesting() { client_->DidSwapBuffersComplete(); } | |
| 58 | |
| 59 protected: | |
| 60 }; | |
| 61 | |
| 62 class TestSoftwareOutputDevice : public SoftwareOutputDevice { | |
| 63 public: | |
| 64 TestSoftwareOutputDevice(); | |
| 65 ~TestSoftwareOutputDevice() override; | |
| 66 | |
| 67 // Overriden from cc:SoftwareOutputDevice | |
| 68 void DiscardBackbuffer() override; | |
| 69 void EnsureBackbuffer() override; | |
| 70 | |
| 71 int discard_backbuffer_count() { return discard_backbuffer_count_; } | |
| 72 int ensure_backbuffer_count() { return ensure_backbuffer_count_; } | |
| 73 | |
| 74 private: | |
| 75 int discard_backbuffer_count_; | |
| 76 int ensure_backbuffer_count_; | |
| 77 }; | |
| 78 | |
| 79 TestSoftwareOutputDevice::TestSoftwareOutputDevice() | |
| 80 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {} | |
| 81 | |
| 82 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {} | |
| 83 | |
| 84 void TestSoftwareOutputDevice::DiscardBackbuffer() { | |
| 85 SoftwareOutputDevice::DiscardBackbuffer(); | |
| 86 discard_backbuffer_count_++; | |
| 87 } | |
| 88 | |
| 89 void TestSoftwareOutputDevice::EnsureBackbuffer() { | |
| 90 SoftwareOutputDevice::EnsureBackbuffer(); | |
| 91 ensure_backbuffer_count_++; | |
| 92 } | |
| 93 | |
| 94 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) { | |
| 95 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create(); | |
| 96 TestOutputSurface output_surface(provider); | |
| 97 EXPECT_FALSE(output_surface.HasClient()); | |
| 98 | |
| 99 FakeOutputSurfaceClient client; | |
| 100 EXPECT_TRUE(output_surface.BindToClient(&client)); | |
| 101 EXPECT_TRUE(output_surface.HasClient()); | |
| 102 EXPECT_FALSE(client.deferred_initialize_called()); | |
| 103 | |
| 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()); | |
| 110 } | |
| 111 | |
| 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()); | |
| 118 | |
| 119 FakeOutputSurfaceClient client; | |
| 120 EXPECT_TRUE(output_surface.BindToClient(&client)); | |
| 121 EXPECT_TRUE(output_surface.HasClient()); | |
| 122 EXPECT_FALSE(client.deferred_initialize_called()); | |
| 123 | |
| 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()); | |
| 130 } | |
| 131 | |
| 132 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) { | |
| 133 scoped_refptr<TestContextProvider> context_provider = | |
| 134 TestContextProvider::Create(); | |
| 135 | |
| 136 // Lose the context so BindToClient fails. | |
| 137 context_provider->UnboundTestContext3d()->set_context_lost(true); | |
| 138 | |
| 139 TestOutputSurface output_surface(context_provider); | |
| 140 EXPECT_FALSE(output_surface.HasClient()); | |
| 141 | |
| 142 FakeOutputSurfaceClient client; | |
| 143 EXPECT_FALSE(output_surface.BindToClient(&client)); | |
| 144 EXPECT_FALSE(output_surface.HasClient()); | |
| 145 } | |
| 146 | |
| 147 TEST(OutputSurfaceTest, ClientPointerIndicatesWorkerBindToClientFailure) { | |
| 148 scoped_refptr<TestContextProvider> context_provider = | |
| 149 TestContextProvider::Create(); | |
| 150 scoped_refptr<TestContextProvider> worker_context_provider = | |
| 151 TestContextProvider::Create(); | |
| 152 | |
| 153 // Lose the context so BindToClient fails. | |
| 154 worker_context_provider->UnboundTestContext3d()->set_context_lost(true); | |
| 155 | |
| 156 TestOutputSurface output_surface(context_provider, worker_context_provider); | |
| 157 EXPECT_FALSE(output_surface.HasClient()); | |
| 158 | |
| 159 FakeOutputSurfaceClient client; | |
| 160 EXPECT_FALSE(output_surface.BindToClient(&client)); | |
| 161 EXPECT_FALSE(output_surface.HasClient()); | |
| 162 } | |
| 163 | |
| 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_) {} | |
| 171 | |
| 172 protected: | |
| 173 void BindOutputSurface() { | |
| 174 EXPECT_TRUE(output_surface_.BindToClient(&client_)); | |
| 175 EXPECT_TRUE(output_surface_.HasClient()); | |
| 176 } | |
| 177 | |
| 178 void InitializeNewContextExpectFail() { | |
| 179 EXPECT_FALSE(output_surface_.InitializeNewContext3d(context_provider_)); | |
| 180 EXPECT_TRUE(output_surface_.HasClient()); | |
| 181 | |
| 182 EXPECT_FALSE(output_surface_.context_provider()); | |
| 183 EXPECT_TRUE(output_surface_.software_device()); | |
| 184 } | |
| 185 | |
| 186 scoped_refptr<TestContextProvider> context_provider_; | |
| 187 TestOutputSurface output_surface_; | |
| 188 FakeOutputSurfaceClient client_; | |
| 189 }; | |
| 190 | |
| 191 TEST_F(OutputSurfaceTestInitializeNewContext3d, Success) { | |
| 192 BindOutputSurface(); | |
| 193 EXPECT_FALSE(client_.deferred_initialize_called()); | |
| 194 | |
| 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()); | |
| 198 | |
| 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()); | |
| 204 | |
| 205 output_surface_.ReleaseGL(); | |
| 206 EXPECT_FALSE(output_surface_.context_provider()); | |
| 207 } | |
| 208 | |
| 209 TEST_F(OutputSurfaceTestInitializeNewContext3d, Context3dMakeCurrentFails) { | |
| 210 BindOutputSurface(); | |
| 211 | |
| 212 context_provider_->UnboundTestContext3d()->set_context_lost(true); | |
| 213 InitializeNewContextExpectFail(); | |
| 214 } | |
| 215 | |
| 216 TEST(OutputSurfaceTest, MemoryAllocation) { | |
| 217 scoped_refptr<TestContextProvider> context_provider = | |
| 218 TestContextProvider::Create(); | |
| 219 | |
| 220 TestOutputSurface output_surface(context_provider); | |
| 221 | |
| 222 FakeOutputSurfaceClient client; | |
| 223 EXPECT_TRUE(output_surface.BindToClient(&client)); | |
| 224 | |
| 225 ManagedMemoryPolicy policy(0); | |
| 226 policy.bytes_limit_when_visible = 1234; | |
| 227 policy.priority_cutoff_when_visible = | |
| 228 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY; | |
| 229 | |
| 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); | |
| 234 | |
| 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); | |
| 240 | |
| 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); | |
| 245 } | |
| 246 | |
| 247 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) { | |
| 248 TestSoftwareOutputDevice* software_output_device = | |
| 249 new TestSoftwareOutputDevice(); | |
| 250 | |
| 251 // TestOutputSurface now owns software_output_device and has responsibility to | |
| 252 // free it. | |
| 253 TestOutputSurface output_surface(make_scoped_ptr(software_output_device)); | |
| 254 | |
| 255 EXPECT_EQ(0, software_output_device->ensure_backbuffer_count()); | |
| 256 EXPECT_EQ(0, software_output_device->discard_backbuffer_count()); | |
| 257 | |
| 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(); | |
| 262 | |
| 263 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count()); | |
| 264 EXPECT_EQ(1, software_output_device->discard_backbuffer_count()); | |
| 265 } | |
| 266 | |
| 267 } // namespace | |
| 268 } // namespace cc | |
| OLD | NEW |