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"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "cc/debug/test_web_graphics_context_3d.h"
18 class TestContextProvider::LostContextCallbackProxy
19 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback
{
21 explicit LostContextCallbackProxy(TestContextProvider
* provider
)
22 : provider_(provider
) {
23 provider_
->context3d_
->setContextLostCallback(this);
26 virtual ~LostContextCallbackProxy() {
27 provider_
->context3d_
->setContextLostCallback(NULL
);
30 virtual void onContextLost() {
31 provider_
->OnLostContext();
35 TestContextProvider
* provider_
;
38 class TestContextProvider::SwapBuffersCompleteCallbackProxy
39 : public WebKit::WebGraphicsContext3D::
40 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM
{
42 explicit SwapBuffersCompleteCallbackProxy(TestContextProvider
* provider
)
43 : provider_(provider
) {
44 provider_
->context3d_
->setSwapBuffersCompleteCallbackCHROMIUM(this);
47 virtual ~SwapBuffersCompleteCallbackProxy() {
48 provider_
->context3d_
->setSwapBuffersCompleteCallbackCHROMIUM(NULL
);
51 virtual void onSwapBuffersComplete() {
52 provider_
->OnSwapBuffersComplete();
56 TestContextProvider
* provider_
;
60 scoped_refptr
<TestContextProvider
> TestContextProvider::Create() {
61 return Create(TestWebGraphicsContext3D::Create().Pass());
65 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
66 const CreateCallback
& create_callback
) {
67 scoped_refptr
<TestContextProvider
> provider
= new TestContextProvider
;
68 if (!provider
->InitializeOnMainThread(create_callback
))
73 scoped_ptr
<TestWebGraphicsContext3D
> ReturnScopedContext(
74 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
75 return context
.Pass();
79 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
80 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
81 return Create(base::Bind(&ReturnScopedContext
, base::Passed(&context
)));
84 TestContextProvider::TestContextProvider()
87 DCHECK(main_thread_checker_
.CalledOnValidThread());
88 context_thread_checker_
.DetachFromThread();
91 TestContextProvider::~TestContextProvider() {
92 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
93 context_thread_checker_
.CalledOnValidThread());
96 bool TestContextProvider::InitializeOnMainThread(
97 const CreateCallback
& create_callback
) {
98 DCHECK(main_thread_checker_
.CalledOnValidThread());
101 DCHECK(!create_callback
.is_null());
102 context3d_
= create_callback
.Run();
106 bool TestContextProvider::BindToCurrentThread() {
109 // This is called on the thread the context will be used.
110 DCHECK(context_thread_checker_
.CalledOnValidThread());
116 if (!context3d_
->makeContextCurrent()) {
117 base::AutoLock
lock(destroyed_lock_
);
122 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
123 swap_buffers_complete_callback_proxy_
.reset(
124 new SwapBuffersCompleteCallbackProxy(this));
129 ContextProvider::Capabilities
TestContextProvider::ContextCapabilities() {
132 DCHECK(context_thread_checker_
.CalledOnValidThread());
134 return context3d_
->test_capabilities();
137 WebKit::WebGraphicsContext3D
* TestContextProvider::Context3d() {
140 DCHECK(context_thread_checker_
.CalledOnValidThread());
142 return context3d_
.get();
145 class GrContext
* TestContextProvider::GrContext() {
148 DCHECK(context_thread_checker_
.CalledOnValidThread());
150 // TODO(danakj): Make a test GrContext that works with a test Context3d.
154 void TestContextProvider::VerifyContexts() {
157 DCHECK(context_thread_checker_
.CalledOnValidThread());
159 if (context3d_
->isContextLost()) {
160 base::AutoLock
lock(destroyed_lock_
);
165 bool TestContextProvider::DestroyedOnMainThread() {
166 DCHECK(main_thread_checker_
.CalledOnValidThread());
168 base::AutoLock
lock(destroyed_lock_
);
172 void TestContextProvider::OnLostContext() {
173 DCHECK(context_thread_checker_
.CalledOnValidThread());
175 base::AutoLock
lock(destroyed_lock_
);
180 if (!lost_context_callback_
.is_null())
181 base::ResetAndReturn(&lost_context_callback_
).Run();
184 void TestContextProvider::OnSwapBuffersComplete() {
185 DCHECK(context_thread_checker_
.CalledOnValidThread());
186 if (!swap_buffers_complete_callback_
.is_null())
187 swap_buffers_complete_callback_
.Run();
190 TestWebGraphicsContext3D
* TestContextProvider::TestContext3d() {
193 DCHECK(context_thread_checker_
.CalledOnValidThread());
195 return context3d_
.get();
198 TestWebGraphicsContext3D
* TestContextProvider::UnboundTestContext3d() {
200 DCHECK(context_thread_checker_
.CalledOnValidThread());
202 return context3d_
.get();
205 void TestContextProvider::SetMemoryAllocation(
206 const ManagedMemoryPolicy
& policy
,
207 bool discard_backbuffer_when_not_visible
) {
208 if (memory_policy_changed_callback_
.is_null())
210 memory_policy_changed_callback_
.Run(
211 policy
, discard_backbuffer_when_not_visible
);
214 void TestContextProvider::SetLostContextCallback(
215 const LostContextCallback
& cb
) {
216 DCHECK(context_thread_checker_
.CalledOnValidThread());
217 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
218 lost_context_callback_
= cb
;
221 void TestContextProvider::SetSwapBuffersCompleteCallback(
222 const SwapBuffersCompleteCallback
& cb
) {
223 DCHECK(context_thread_checker_
.CalledOnValidThread());
224 DCHECK(swap_buffers_complete_callback_
.is_null() || cb
.is_null());
225 swap_buffers_complete_callback_
= cb
;
228 void TestContextProvider::SetMemoryPolicyChangedCallback(
229 const MemoryPolicyChangedCallback
& cb
) {
230 DCHECK(context_thread_checker_
.CalledOnValidThread());
231 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
232 memory_policy_changed_callback_
= cb
;