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/test/test_context_provider.h"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/logging.h"
13 #include "cc/test/test_gles2_interface.h"
14 #include "cc/test/test_web_graphics_context_3d.h"
19 scoped_refptr
<TestContextProvider
> TestContextProvider::Create() {
20 return Create(TestWebGraphicsContext3D::Create().Pass());
24 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
25 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
28 return new TestContextProvider(context
.Pass());
31 TestContextProvider::TestContextProvider(
32 scoped_ptr
<TestWebGraphicsContext3D
> context
)
33 : context3d_(context
.Pass()),
34 context_gl_(new TestGLES2Interface(context3d_
.get())),
37 weak_ptr_factory_(this) {
38 DCHECK(main_thread_checker_
.CalledOnValidThread());
40 context_thread_checker_
.DetachFromThread();
41 context3d_
->set_test_support(&support_
);
44 TestContextProvider::~TestContextProvider() {
45 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
46 context_thread_checker_
.CalledOnValidThread());
49 bool TestContextProvider::BindToCurrentThread() {
50 // This is called on the thread the context will be used.
51 DCHECK(context_thread_checker_
.CalledOnValidThread());
56 if (context3d_
->isContextLost()) {
57 base::AutoLock
lock(destroyed_lock_
);
63 context3d_
->set_context_lost_callback(
64 base::Bind(&TestContextProvider::OnLostContext
,
65 base::Unretained(this)));
70 ContextProvider::Capabilities
TestContextProvider::ContextCapabilities() {
72 DCHECK(context_thread_checker_
.CalledOnValidThread());
74 return context3d_
->test_capabilities();
77 gpu::gles2::GLES2Interface
* TestContextProvider::ContextGL() {
80 DCHECK(context_thread_checker_
.CalledOnValidThread());
82 return context_gl_
.get();
85 gpu::ContextSupport
* TestContextProvider::ContextSupport() {
89 class GrContext
* TestContextProvider::GrContext() {
91 DCHECK(context_thread_checker_
.CalledOnValidThread());
93 // TODO(danakj): Make a test GrContext that works with a test Context3d.
97 bool TestContextProvider::IsContextLost() {
99 DCHECK(context_thread_checker_
.CalledOnValidThread());
101 return context3d_
->isContextLost();
104 void TestContextProvider::VerifyContexts() {
106 DCHECK(context_thread_checker_
.CalledOnValidThread());
108 if (context3d_
->isContextLost()) {
109 base::AutoLock
lock(destroyed_lock_
);
114 void TestContextProvider::DeleteCachedResources() {
117 bool TestContextProvider::DestroyedOnMainThread() {
118 DCHECK(main_thread_checker_
.CalledOnValidThread());
120 base::AutoLock
lock(destroyed_lock_
);
124 void TestContextProvider::OnLostContext() {
125 DCHECK(context_thread_checker_
.CalledOnValidThread());
127 base::AutoLock
lock(destroyed_lock_
);
132 if (!lost_context_callback_
.is_null())
133 base::ResetAndReturn(&lost_context_callback_
).Run();
136 TestWebGraphicsContext3D
* TestContextProvider::TestContext3d() {
138 DCHECK(context_thread_checker_
.CalledOnValidThread());
140 return context3d_
.get();
143 TestWebGraphicsContext3D
* TestContextProvider::UnboundTestContext3d() {
144 return context3d_
.get();
147 void TestContextProvider::SetMemoryAllocation(
148 const ManagedMemoryPolicy
& policy
) {
149 if (memory_policy_changed_callback_
.is_null())
151 memory_policy_changed_callback_
.Run(policy
);
154 void TestContextProvider::SetLostContextCallback(
155 const LostContextCallback
& cb
) {
156 DCHECK(context_thread_checker_
.CalledOnValidThread());
157 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
158 lost_context_callback_
= cb
;
161 void TestContextProvider::SetMemoryPolicyChangedCallback(
162 const MemoryPolicyChangedCallback
& cb
) {
163 DCHECK(context_thread_checker_
.CalledOnValidThread());
164 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
165 memory_policy_changed_callback_
= cb
;
168 void TestContextProvider::SetMaxTransferBufferUsageBytes(
169 size_t max_transfer_buffer_usage_bytes
) {
170 context3d_
->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes
);