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"
15 #include "third_party/skia/include/gpu/GrContext.h"
16 #include "third_party/skia/include/gpu/gl/SkNullGLContext.h"
21 scoped_refptr
<TestContextProvider
> TestContextProvider::Create() {
22 return Create(TestWebGraphicsContext3D::Create());
26 scoped_refptr
<TestContextProvider
> TestContextProvider::CreateWorker() {
27 scoped_refptr
<TestContextProvider
> worker_context_provider
=
28 Create(TestWebGraphicsContext3D::Create());
29 if (!worker_context_provider
)
31 // Worker contexts are bound to the thread they are created on.
32 if (!worker_context_provider
->BindToCurrentThread())
34 worker_context_provider
->SetupLock();
35 return worker_context_provider
;
39 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
40 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
43 return new TestContextProvider(context
.Pass());
46 TestContextProvider::TestContextProvider(
47 scoped_ptr
<TestWebGraphicsContext3D
> context
)
48 : context3d_(context
.Pass()),
49 context_gl_(new TestGLES2Interface(context3d_
.get())),
52 weak_ptr_factory_(this) {
53 DCHECK(main_thread_checker_
.CalledOnValidThread());
55 context_thread_checker_
.DetachFromThread();
56 context3d_
->set_test_support(&support_
);
59 TestContextProvider::~TestContextProvider() {
60 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
61 context_thread_checker_
.CalledOnValidThread());
64 bool TestContextProvider::BindToCurrentThread() {
65 // This is called on the thread the context will be used.
66 DCHECK(context_thread_checker_
.CalledOnValidThread());
71 if (context_gl_
->GetGraphicsResetStatusKHR() != GL_NO_ERROR
) {
72 base::AutoLock
lock(destroyed_lock_
);
78 context3d_
->set_context_lost_callback(
79 base::Bind(&TestContextProvider::OnLostContext
,
80 base::Unretained(this)));
85 void TestContextProvider::DetachFromThread() {
86 context_thread_checker_
.DetachFromThread();
89 ContextProvider::Capabilities
TestContextProvider::ContextCapabilities() {
91 DCHECK(context_thread_checker_
.CalledOnValidThread());
93 return context3d_
->test_capabilities();
96 gpu::gles2::GLES2Interface
* TestContextProvider::ContextGL() {
99 DCHECK(context_thread_checker_
.CalledOnValidThread());
101 return context_gl_
.get();
104 gpu::ContextSupport
* TestContextProvider::ContextSupport() {
108 class GrContext
* TestContextProvider::GrContext() {
110 DCHECK(context_thread_checker_
.CalledOnValidThread());
113 return gr_context_
.get();
115 skia::RefPtr
<class SkGLContext
> gl_context
=
116 skia::AdoptRef(SkNullGLContext::Create(kNone_GrGLStandard
));
117 gl_context
->makeCurrent();
118 gr_context_
= skia::AdoptRef(GrContext::Create(
119 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(gl_context
->gl())));
121 // If GlContext is already lost, also abandon the new GrContext.
122 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR
)
123 gr_context_
->abandonContext();
125 return gr_context_
.get();
128 void TestContextProvider::InvalidateGrContext(uint32_t state
) {
130 DCHECK(context_thread_checker_
.CalledOnValidThread());
133 gr_context_
.get()->resetContext(state
);
136 void TestContextProvider::SetupLock() {
139 base::Lock
* TestContextProvider::GetLock() {
140 return &context_lock_
;
143 void TestContextProvider::VerifyContexts() {
145 DCHECK(context_thread_checker_
.CalledOnValidThread());
147 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR
) {
148 base::AutoLock
lock(destroyed_lock_
);
153 void TestContextProvider::DeleteCachedResources() {
156 bool TestContextProvider::DestroyedOnMainThread() {
157 DCHECK(main_thread_checker_
.CalledOnValidThread());
159 base::AutoLock
lock(destroyed_lock_
);
163 void TestContextProvider::OnLostContext() {
164 DCHECK(context_thread_checker_
.CalledOnValidThread());
166 base::AutoLock
lock(destroyed_lock_
);
171 if (!lost_context_callback_
.is_null())
172 base::ResetAndReturn(&lost_context_callback_
).Run();
174 gr_context_
->abandonContext();
177 TestWebGraphicsContext3D
* TestContextProvider::TestContext3d() {
179 DCHECK(context_thread_checker_
.CalledOnValidThread());
181 return context3d_
.get();
184 TestWebGraphicsContext3D
* TestContextProvider::UnboundTestContext3d() {
185 return context3d_
.get();
188 void TestContextProvider::SetMemoryAllocation(
189 const ManagedMemoryPolicy
& policy
) {
190 if (memory_policy_changed_callback_
.is_null())
192 memory_policy_changed_callback_
.Run(policy
);
195 void TestContextProvider::SetLostContextCallback(
196 const LostContextCallback
& cb
) {
197 DCHECK(context_thread_checker_
.CalledOnValidThread());
198 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
199 lost_context_callback_
= cb
;
202 void TestContextProvider::SetMemoryPolicyChangedCallback(
203 const MemoryPolicyChangedCallback
& cb
) {
204 DCHECK(context_thread_checker_
.CalledOnValidThread());
205 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
206 memory_policy_changed_callback_
= cb
;
209 void TestContextProvider::SetMaxTransferBufferUsageBytes(
210 size_t max_transfer_buffer_usage_bytes
) {
211 context3d_
->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes
);