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 "webkit/common/gpu/context_provider_in_process.h"
10 #include "base/callback_helpers.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/stringprintf.h"
13 #include "cc/output/managed_memory_policy.h"
14 #include "gpu/command_buffer/client/gles2_implementation.h"
15 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
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
);
50 scoped_refptr
<ContextProviderInProcess
>
51 ContextProviderInProcess::CreateOffscreen(
52 bool lose_context_when_out_of_memory
) {
53 blink::WebGraphicsContext3D::Attributes attributes
;
54 attributes
.depth
= false;
55 attributes
.stencil
= true;
56 attributes
.antialias
= false;
57 attributes
.shareResources
= true;
58 attributes
.noAutomaticFlushes
= true;
61 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
62 attributes
, lose_context_when_out_of_memory
),
66 ContextProviderInProcess::ContextProviderInProcess(
67 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d
,
68 const std::string
& debug_name
)
69 : context3d_(context3d
.Pass()),
71 debug_name_(debug_name
) {
72 DCHECK(main_thread_checker_
.CalledOnValidThread());
74 context_thread_checker_
.DetachFromThread();
77 ContextProviderInProcess::~ContextProviderInProcess() {
78 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
79 context_thread_checker_
.CalledOnValidThread());
82 blink::WebGraphicsContext3D
* ContextProviderInProcess::WebContext3D() {
83 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
84 DCHECK(context_thread_checker_
.CalledOnValidThread());
86 return context3d_
.get();
89 bool ContextProviderInProcess::BindToCurrentThread() {
92 // This is called on the thread the context will be used.
93 DCHECK(context_thread_checker_
.CalledOnValidThread());
95 if (lost_context_callback_proxy_
)
98 if (!context3d_
->InitializeOnCurrentThread())
101 InitializeCapabilities();
103 std::string unique_context_name
=
104 base::StringPrintf("%s-%p", debug_name_
.c_str(), context3d_
.get());
105 context3d_
->pushGroupMarkerEXT(unique_context_name
.c_str());
107 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
111 void ContextProviderInProcess::InitializeCapabilities() {
112 capabilities_
.gpu
= context3d_
->GetImplementation()->capabilities();
114 size_t mapped_memory_limit
= context3d_
->GetMappedMemoryLimit();
115 capabilities_
.max_transfer_buffer_usage_bytes
=
116 mapped_memory_limit
==
117 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit
118 ? std::numeric_limits
<size_t>::max()
119 : mapped_memory_limit
;
122 cc::ContextProvider::Capabilities
123 ContextProviderInProcess::ContextCapabilities() {
124 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
125 DCHECK(context_thread_checker_
.CalledOnValidThread());
126 return capabilities_
;
129 ::gpu::gles2::GLES2Interface
* ContextProviderInProcess::ContextGL() {
131 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
132 DCHECK(context_thread_checker_
.CalledOnValidThread());
134 return context3d_
->GetGLInterface();
137 ::gpu::ContextSupport
* ContextProviderInProcess::ContextSupport() {
139 if (!lost_context_callback_proxy_
)
140 return NULL
; // Not bound to anything.
142 DCHECK(context_thread_checker_
.CalledOnValidThread());
144 return context3d_
->GetContextSupport();
147 class GrContext
* ContextProviderInProcess::GrContext() {
148 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
149 DCHECK(context_thread_checker_
.CalledOnValidThread());
152 return gr_context_
->get();
155 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_
.get()));
156 return gr_context_
->get();
159 bool ContextProviderInProcess::IsContextLost() {
160 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
161 DCHECK(context_thread_checker_
.CalledOnValidThread());
163 return context3d_
->isContextLost();
166 void ContextProviderInProcess::VerifyContexts() {
167 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
168 DCHECK(context_thread_checker_
.CalledOnValidThread());
170 if (context3d_
->isContextLost())
174 void ContextProviderInProcess::DeleteCachedResources() {
175 DCHECK(context_thread_checker_
.CalledOnValidThread());
178 gr_context_
->FreeGpuResources();
181 void ContextProviderInProcess::OnLostContext() {
182 DCHECK(context_thread_checker_
.CalledOnValidThread());
184 base::AutoLock
lock(destroyed_lock_
);
189 if (!lost_context_callback_
.is_null())
190 base::ResetAndReturn(&lost_context_callback_
).Run();
192 gr_context_
->OnLostContext();
195 bool ContextProviderInProcess::DestroyedOnMainThread() {
196 DCHECK(main_thread_checker_
.CalledOnValidThread());
198 base::AutoLock
lock(destroyed_lock_
);
202 void ContextProviderInProcess::SetLostContextCallback(
203 const LostContextCallback
& lost_context_callback
) {
204 DCHECK(context_thread_checker_
.CalledOnValidThread());
205 DCHECK(lost_context_callback_
.is_null() ||
206 lost_context_callback
.is_null());
207 lost_context_callback_
= lost_context_callback
;
210 void ContextProviderInProcess::SetMemoryPolicyChangedCallback(
211 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
212 // There's no memory manager for the in-process implementation.
216 } // namespace webkit