1 // Copyright (c) 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 "content/browser/android/in_process/context_provider_in_process.h"
8 #include "base/callback_helpers.h"
9 #include "base/strings/stringprintf.h"
10 #include "cc/output/managed_memory_policy.h"
11 #include "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h"
12 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
13 #include "gpu/command_buffer/client/gles2_implementation.h"
14 #include "third_party/skia/include/gpu/GrContext.h"
16 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl
;
20 class ContextProviderInProcess::LostContextCallbackProxy
21 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback
{
23 explicit LostContextCallbackProxy(ContextProviderInProcess
* provider
)
24 : provider_(provider
) {
25 provider_
->context3d_
->setContextLostCallback(this);
28 virtual ~LostContextCallbackProxy() {
29 provider_
->context3d_
->setContextLostCallback(NULL
);
32 virtual void onContextLost() {
33 provider_
->OnLostContext();
37 ContextProviderInProcess
* provider_
;
41 scoped_refptr
<ContextProviderInProcess
> ContextProviderInProcess::Create(
42 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d
,
43 const std::string
& debug_name
) {
46 return new ContextProviderInProcess(context3d
.Pass(), debug_name
);
49 ContextProviderInProcess::ContextProviderInProcess(
50 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d
,
51 const std::string
& debug_name
)
52 : context3d_(context3d
.Pass()),
54 debug_name_(debug_name
) {
55 DCHECK(main_thread_checker_
.CalledOnValidThread());
57 context_thread_checker_
.DetachFromThread();
60 ContextProviderInProcess::~ContextProviderInProcess() {
61 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
62 context_thread_checker_
.CalledOnValidThread());
65 blink::WebGraphicsContext3D
* ContextProviderInProcess::WebContext3D() {
66 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
67 DCHECK(context_thread_checker_
.CalledOnValidThread());
69 return context3d_
.get();
72 bool ContextProviderInProcess::BindToCurrentThread() {
75 // This is called on the thread the context will be used.
76 DCHECK(context_thread_checker_
.CalledOnValidThread());
78 if (lost_context_callback_proxy_
)
81 if (!context3d_
->InitializeOnCurrentThread())
84 InitializeCapabilities();
86 const std::string unique_context_name
=
87 base::StringPrintf("%s-%p", debug_name_
.c_str(), context3d_
.get());
88 context3d_
->traceBeginCHROMIUM("gpu_toplevel",
89 unique_context_name
.c_str());
91 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
95 void ContextProviderInProcess::DetachFromThread() {
96 context_thread_checker_
.DetachFromThread();
99 void ContextProviderInProcess::InitializeCapabilities() {
100 capabilities_
.gpu
= context3d_
->GetImplementation()->capabilities();
102 size_t mapped_memory_limit
= context3d_
->GetMappedMemoryLimit();
103 capabilities_
.max_transfer_buffer_usage_bytes
=
104 mapped_memory_limit
==
105 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit
106 ? std::numeric_limits
<size_t>::max()
107 : mapped_memory_limit
;
110 cc::ContextProvider::Capabilities
111 ContextProviderInProcess::ContextCapabilities() {
112 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
113 DCHECK(context_thread_checker_
.CalledOnValidThread());
114 return capabilities_
;
117 ::gpu::gles2::GLES2Interface
* ContextProviderInProcess::ContextGL() {
119 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
120 DCHECK(context_thread_checker_
.CalledOnValidThread());
122 return context3d_
->GetGLInterface();
125 ::gpu::ContextSupport
* ContextProviderInProcess::ContextSupport() {
127 if (!lost_context_callback_proxy_
)
128 return NULL
; // Not bound to anything.
130 DCHECK(context_thread_checker_
.CalledOnValidThread());
132 return context3d_
->GetContextSupport();
135 class GrContext
* ContextProviderInProcess::GrContext() {
136 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
137 DCHECK(context_thread_checker_
.CalledOnValidThread());
140 return gr_context_
->get();
142 gr_context_
.reset(new GrContextForWebGraphicsContext3D(context3d_
.get()));
143 return gr_context_
->get();
146 void ContextProviderInProcess::InvalidateGrContext(uint32_t state
) {
147 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
148 DCHECK(context_thread_checker_
.CalledOnValidThread());
151 return gr_context_
->get()->resetContext(state
);
154 void ContextProviderInProcess::SetupLock() {
155 context3d_
->SetLock(&context_lock_
);
158 base::Lock
* ContextProviderInProcess::GetLock() {
159 return &context_lock_
;
162 void ContextProviderInProcess::VerifyContexts() {
163 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
164 DCHECK(context_thread_checker_
.CalledOnValidThread());
166 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR
)
170 void ContextProviderInProcess::DeleteCachedResources() {
171 DCHECK(context_thread_checker_
.CalledOnValidThread());
174 gr_context_
->FreeGpuResources();
177 void ContextProviderInProcess::OnLostContext() {
178 DCHECK(context_thread_checker_
.CalledOnValidThread());
180 base::AutoLock
lock(destroyed_lock_
);
185 if (!lost_context_callback_
.is_null())
186 base::ResetAndReturn(&lost_context_callback_
).Run();
188 gr_context_
->OnLostContext();
191 bool ContextProviderInProcess::DestroyedOnMainThread() {
192 DCHECK(main_thread_checker_
.CalledOnValidThread());
194 base::AutoLock
lock(destroyed_lock_
);
198 void ContextProviderInProcess::SetLostContextCallback(
199 const LostContextCallback
& lost_context_callback
) {
200 DCHECK(context_thread_checker_
.CalledOnValidThread());
201 DCHECK(lost_context_callback_
.is_null() ||
202 lost_context_callback
.is_null());
203 lost_context_callback_
= lost_context_callback
;
206 void ContextProviderInProcess::SetMemoryPolicyChangedCallback(
207 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
208 // There's no memory manager for the in-process implementation.
211 } // namespace content