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 blink::WebGraphicsContext3D::Attributes attributes
;
53 attributes
.depth
= false;
54 attributes
.stencil
= true;
55 attributes
.antialias
= false;
56 attributes
.shareResources
= true;
57 attributes
.noAutomaticFlushes
= true;
60 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
61 attributes
), "Offscreen");
64 ContextProviderInProcess::ContextProviderInProcess(
65 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d
,
66 const std::string
& debug_name
)
67 : context3d_(context3d
.Pass()),
69 debug_name_(debug_name
) {
70 DCHECK(main_thread_checker_
.CalledOnValidThread());
72 context_thread_checker_
.DetachFromThread();
75 ContextProviderInProcess::~ContextProviderInProcess() {
76 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
77 context_thread_checker_
.CalledOnValidThread());
80 blink::WebGraphicsContext3D
* ContextProviderInProcess::WebContext3D() {
81 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
82 DCHECK(context_thread_checker_
.CalledOnValidThread());
84 return context3d_
.get();
87 bool ContextProviderInProcess::BindToCurrentThread() {
90 // This is called on the thread the context will be used.
91 DCHECK(context_thread_checker_
.CalledOnValidThread());
93 if (lost_context_callback_proxy_
)
96 if (!context3d_
->makeContextCurrent())
99 InitializeCapabilities();
101 std::string unique_context_name
=
102 base::StringPrintf("%s-%p", debug_name_
.c_str(), context3d_
.get());
103 context3d_
->pushGroupMarkerEXT(unique_context_name
.c_str());
105 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
109 void ContextProviderInProcess::InitializeCapabilities() {
110 capabilities_
.gpu
= context3d_
->GetImplementation()->capabilities();
113 cc::ContextProvider::Capabilities
114 ContextProviderInProcess::ContextCapabilities() {
115 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
116 DCHECK(context_thread_checker_
.CalledOnValidThread());
117 return capabilities_
;
120 ::gpu::gles2::GLES2Interface
* ContextProviderInProcess::ContextGL() {
122 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
123 DCHECK(context_thread_checker_
.CalledOnValidThread());
125 return context3d_
->GetGLInterface();
128 ::gpu::ContextSupport
* ContextProviderInProcess::ContextSupport() {
130 if (!lost_context_callback_proxy_
)
131 return NULL
; // Not bound to anything.
133 DCHECK(context_thread_checker_
.CalledOnValidThread());
135 return context3d_
->GetContextSupport();
138 class GrContext
* ContextProviderInProcess::GrContext() {
139 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
140 DCHECK(context_thread_checker_
.CalledOnValidThread());
143 return gr_context_
->get();
146 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_
.get()));
147 return gr_context_
->get();
150 bool ContextProviderInProcess::IsContextLost() {
151 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
152 DCHECK(context_thread_checker_
.CalledOnValidThread());
154 return context3d_
->isContextLost();
157 void ContextProviderInProcess::VerifyContexts() {
158 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
159 DCHECK(context_thread_checker_
.CalledOnValidThread());
161 if (context3d_
->isContextLost())
165 void ContextProviderInProcess::OnLostContext() {
166 DCHECK(context_thread_checker_
.CalledOnValidThread());
168 base::AutoLock
lock(destroyed_lock_
);
173 if (!lost_context_callback_
.is_null())
174 base::ResetAndReturn(&lost_context_callback_
).Run();
177 bool ContextProviderInProcess::DestroyedOnMainThread() {
178 DCHECK(main_thread_checker_
.CalledOnValidThread());
180 base::AutoLock
lock(destroyed_lock_
);
184 void ContextProviderInProcess::SetLostContextCallback(
185 const LostContextCallback
& lost_context_callback
) {
186 DCHECK(context_thread_checker_
.CalledOnValidThread());
187 DCHECK(lost_context_callback_
.is_null() ||
188 lost_context_callback
.is_null());
189 lost_context_callback_
= lost_context_callback
;
192 void ContextProviderInProcess::SetMemoryPolicyChangedCallback(
193 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
194 // There's no memory manager for the in-process implementation.
198 } // namespace webkit