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 bool TestContextProvider::DestroyedOnMainThread() {
115 DCHECK(main_thread_checker_
.CalledOnValidThread());
117 base::AutoLock
lock(destroyed_lock_
);
121 void TestContextProvider::OnLostContext() {
122 DCHECK(context_thread_checker_
.CalledOnValidThread());
124 base::AutoLock
lock(destroyed_lock_
);
129 if (!lost_context_callback_
.is_null())
130 base::ResetAndReturn(&lost_context_callback_
).Run();
133 TestWebGraphicsContext3D
* TestContextProvider::TestContext3d() {
135 DCHECK(context_thread_checker_
.CalledOnValidThread());
137 return context3d_
.get();
140 TestWebGraphicsContext3D
* TestContextProvider::UnboundTestContext3d() {
141 return context3d_
.get();
144 void TestContextProvider::SetMemoryAllocation(
145 const ManagedMemoryPolicy
& policy
) {
146 if (memory_policy_changed_callback_
.is_null())
148 memory_policy_changed_callback_
.Run(policy
);
151 void TestContextProvider::SetLostContextCallback(
152 const LostContextCallback
& cb
) {
153 DCHECK(context_thread_checker_
.CalledOnValidThread());
154 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
155 lost_context_callback_
= cb
;
158 void TestContextProvider::SetMemoryPolicyChangedCallback(
159 const MemoryPolicyChangedCallback
& cb
) {
160 DCHECK(context_thread_checker_
.CalledOnValidThread());
161 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
162 memory_policy_changed_callback_
= cb
;
165 void TestContextProvider::SetMaxTransferBufferUsageBytes(
166 size_t max_transfer_buffer_usage_bytes
) {
167 context3d_
->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes
);