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().Pass());
26 scoped_refptr
<TestContextProvider
> TestContextProvider::Create(
27 scoped_ptr
<TestWebGraphicsContext3D
> context
) {
30 return new TestContextProvider(context
.Pass());
33 TestContextProvider::TestContextProvider(
34 scoped_ptr
<TestWebGraphicsContext3D
> context
)
35 : context3d_(context
.Pass()),
36 context_gl_(new TestGLES2Interface(context3d_
.get())),
39 weak_ptr_factory_(this) {
40 DCHECK(main_thread_checker_
.CalledOnValidThread());
42 context_thread_checker_
.DetachFromThread();
43 context3d_
->set_test_support(&support_
);
46 TestContextProvider::~TestContextProvider() {
47 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
48 context_thread_checker_
.CalledOnValidThread());
51 bool TestContextProvider::BindToCurrentThread() {
52 // This is called on the thread the context will be used.
53 DCHECK(context_thread_checker_
.CalledOnValidThread());
58 if (context_gl_
->GetGraphicsResetStatusKHR() != GL_NO_ERROR
) {
59 base::AutoLock
lock(destroyed_lock_
);
65 context3d_
->set_context_lost_callback(
66 base::Bind(&TestContextProvider::OnLostContext
,
67 base::Unretained(this)));
72 void TestContextProvider::DetachFromThread() {
73 context_thread_checker_
.DetachFromThread();
76 ContextProvider::Capabilities
TestContextProvider::ContextCapabilities() {
78 DCHECK(context_thread_checker_
.CalledOnValidThread());
80 return context3d_
->test_capabilities();
83 gpu::gles2::GLES2Interface
* TestContextProvider::ContextGL() {
86 DCHECK(context_thread_checker_
.CalledOnValidThread());
88 return context_gl_
.get();
91 gpu::ContextSupport
* TestContextProvider::ContextSupport() {
95 class GrContext
* TestContextProvider::GrContext() {
97 DCHECK(context_thread_checker_
.CalledOnValidThread());
100 return gr_context_
.get();
102 skia::RefPtr
<class SkGLContext
> gl_context
=
103 skia::AdoptRef(SkNullGLContext::Create(kNone_GrGLStandard
));
104 gl_context
->makeCurrent();
105 gr_context_
= skia::AdoptRef(GrContext::Create(
106 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(gl_context
->gl())));
108 // If GlContext is already lost, also abandon the new GrContext.
109 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR
)
110 gr_context_
->abandonContext();
112 return gr_context_
.get();
115 void TestContextProvider::InvalidateGrContext(uint32_t state
) {
117 DCHECK(context_thread_checker_
.CalledOnValidThread());
120 gr_context_
.get()->resetContext(state
);
123 void TestContextProvider::SetupLock() {
126 base::Lock
* TestContextProvider::GetLock() {
127 return &context_lock_
;
130 void TestContextProvider::VerifyContexts() {
132 DCHECK(context_thread_checker_
.CalledOnValidThread());
134 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR
) {
135 base::AutoLock
lock(destroyed_lock_
);
140 void TestContextProvider::DeleteCachedResources() {
143 bool TestContextProvider::DestroyedOnMainThread() {
144 DCHECK(main_thread_checker_
.CalledOnValidThread());
146 base::AutoLock
lock(destroyed_lock_
);
150 void TestContextProvider::OnLostContext() {
151 DCHECK(context_thread_checker_
.CalledOnValidThread());
153 base::AutoLock
lock(destroyed_lock_
);
158 if (!lost_context_callback_
.is_null())
159 base::ResetAndReturn(&lost_context_callback_
).Run();
161 gr_context_
->abandonContext();
164 TestWebGraphicsContext3D
* TestContextProvider::TestContext3d() {
166 DCHECK(context_thread_checker_
.CalledOnValidThread());
168 return context3d_
.get();
171 TestWebGraphicsContext3D
* TestContextProvider::UnboundTestContext3d() {
172 return context3d_
.get();
175 void TestContextProvider::SetMemoryAllocation(
176 const ManagedMemoryPolicy
& policy
) {
177 if (memory_policy_changed_callback_
.is_null())
179 memory_policy_changed_callback_
.Run(policy
);
182 void TestContextProvider::SetLostContextCallback(
183 const LostContextCallback
& cb
) {
184 DCHECK(context_thread_checker_
.CalledOnValidThread());
185 DCHECK(lost_context_callback_
.is_null() || cb
.is_null());
186 lost_context_callback_
= cb
;
189 void TestContextProvider::SetMemoryPolicyChangedCallback(
190 const MemoryPolicyChangedCallback
& cb
) {
191 DCHECK(context_thread_checker_
.CalledOnValidThread());
192 DCHECK(memory_policy_changed_callback_
.is_null() || cb
.is_null());
193 memory_policy_changed_callback_
= cb
;
196 void TestContextProvider::SetMaxTransferBufferUsageBytes(
197 size_t max_transfer_buffer_usage_bytes
) {
198 context3d_
->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes
);