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/debug/test_context_provider.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "cc/debug/test_web_graphics_context_3d.h"
14 class TestContextProvider::LostContextCallbackProxy
15 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback
{
17 explicit LostContextCallbackProxy(TestContextProvider
* provider
)
18 : provider_(provider
) {
19 provider_
->context3d_
->setContextLostCallback(this);
22 virtual ~LostContextCallbackProxy() {
23 provider_
->context3d_
->setContextLostCallback(NULL
);
26 virtual void onContextLost() {
27 provider_
->OnLostContext();
31 TestContextProvider
* provider_
;
34 class TestContextProvider::SwapBuffersCompleteCallbackProxy
35 : public WebKit::WebGraphicsContext3D::
36 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM
{
38 explicit SwapBuffersCompleteCallbackProxy(TestContextProvider
* provider
)
39 : provider_(provider
) {
40 provider_
->context3d_
->setSwapBuffersCompleteCallbackCHROMIUM(this);
43 virtual ~SwapBuffersCompleteCallbackProxy() {
44 provider_
->context3d_
->setSwapBuffersCompleteCallbackCHROMIUM(NULL
);
47 virtual void onSwapBuffersComplete() {
48 provider_
->OnSwapBuffersComplete();
52 TestContextProvider
* provider_
;
56 scoped_refptr
<TestContextProvider
> TestContextProvider::Create() {
57 return Create(TestWebGraphicsContext3D::Create().Pass());
61 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
62 const CreateCallback
& create_callback
) {
63 scoped_refptr
<TestContextProvider
> provider
= new TestContextProvider
;
64 if (!provider
->InitializeOnMainThread(create_callback
))
69 scoped_ptr
<TestWebGraphicsContext3D
> ReturnScopedContext(
70 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
71 return context
.Pass();
75 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
76 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
77 return Create(base::Bind(&ReturnScopedContext
, base::Passed(&context
)));
80 TestContextProvider::TestContextProvider()
83 DCHECK(main_thread_checker_
.CalledOnValidThread());
84 context_thread_checker_
.DetachFromThread();
87 TestContextProvider::~TestContextProvider() {
88 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
89 context_thread_checker_
.CalledOnValidThread());
92 bool TestContextProvider::InitializeOnMainThread(
93 const CreateCallback
& create_callback
) {
94 DCHECK(main_thread_checker_
.CalledOnValidThread());
97 DCHECK(!create_callback
.is_null());
98 context3d_
= create_callback
.Run();
102 bool TestContextProvider::BindToCurrentThread() {
105 // This is called on the thread the context will be used.
106 DCHECK(context_thread_checker_
.CalledOnValidThread());
112 if (!context3d_
->makeContextCurrent()) {
113 base::AutoLock
lock(destroyed_lock_
);
118 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
119 swap_buffers_complete_callback_proxy_
.reset(
120 new SwapBuffersCompleteCallbackProxy(this));
125 WebKit::WebGraphicsContext3D
* TestContextProvider::Context3d() {
128 DCHECK(context_thread_checker_
.CalledOnValidThread());
130 return context3d_
.get();
133 class GrContext
* TestContextProvider::GrContext() {
136 DCHECK(context_thread_checker_
.CalledOnValidThread());
138 // TODO(danakj): Make a test GrContext that works with a test Context3d.
142 void TestContextProvider::VerifyContexts() {
145 DCHECK(context_thread_checker_
.CalledOnValidThread());
147 if (context3d_
->isContextLost()) {
148 base::AutoLock
lock(destroyed_lock_
);
153 bool TestContextProvider::DestroyedOnMainThread() {
154 DCHECK(main_thread_checker_
.CalledOnValidThread());
156 base::AutoLock
lock(destroyed_lock_
);
160 void TestContextProvider::OnLostContext() {
161 DCHECK(context_thread_checker_
.CalledOnValidThread());
163 base::AutoLock
lock(destroyed_lock_
);
168 if (!lost_context_callback_
.is_null())
169 base::ResetAndReturn(&lost_context_callback_
).Run();
172 void TestContextProvider::OnSwapBuffersComplete() {
173 DCHECK(context_thread_checker_
.CalledOnValidThread());
174 if (!swap_buffers_complete_callback_
.is_null())
175 swap_buffers_complete_callback_
.Run();
178 void TestContextProvider::SetMemoryAllocation(
179 const ManagedMemoryPolicy
& policy
,
180 bool discard_backbuffer_when_not_visible
) {
181 if (memory_policy_changed_callback_
.is_null())
183 memory_policy_changed_callback_
.Run(
184 policy
, discard_backbuffer_when_not_visible
);
187 void TestContextProvider::SetLostContextCallback(
188 const LostContextCallback
& cb
) {
189 DCHECK(context_thread_checker_
.CalledOnValidThread());
190 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
191 lost_context_callback_
= cb
;
194 void TestContextProvider::SetSwapBuffersCompleteCallback(
195 const SwapBuffersCompleteCallback
& cb
) {
196 DCHECK(context_thread_checker_
.CalledOnValidThread());
197 DCHECK(swap_buffers_complete_callback_
.is_null() || cb
.is_null());
198 swap_buffers_complete_callback_
= cb
;
201 void TestContextProvider::SetMemoryPolicyChangedCallback(
202 const MemoryPolicyChangedCallback
& cb
) {
203 DCHECK(context_thread_checker_
.CalledOnValidThread());
204 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
205 memory_policy_changed_callback_
= cb
;