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/common/gpu/client/context_provider_command_buffer.h"
10 #include "base/callback_helpers.h"
11 #include "base/strings/stringprintf.h"
12 #include "cc/output/managed_memory_policy.h"
13 #include "gpu/command_buffer/client/gles2_implementation.h"
14 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
18 class ContextProviderCommandBuffer::LostContextCallbackProxy
19 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback
{
21 explicit LostContextCallbackProxy(ContextProviderCommandBuffer
* provider
)
22 : provider_(provider
) {
23 provider_
->context3d_
->setContextLostCallback(this);
26 virtual ~LostContextCallbackProxy() {
27 provider_
->context3d_
->setContextLostCallback(NULL
);
30 virtual void onContextLost() {
31 provider_
->OnLostContext();
35 ContextProviderCommandBuffer
* provider_
;
38 scoped_refptr
<ContextProviderCommandBuffer
>
39 ContextProviderCommandBuffer::Create(
40 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context3d
,
41 const std::string
& debug_name
) {
45 return new ContextProviderCommandBuffer(context3d
.Pass(), debug_name
);
48 ContextProviderCommandBuffer::ContextProviderCommandBuffer(
49 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context3d
,
50 const std::string
& debug_name
)
51 : context3d_(context3d
.Pass()),
52 debug_name_(debug_name
),
54 DCHECK(main_thread_checker_
.CalledOnValidThread());
56 context_thread_checker_
.DetachFromThread();
59 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() {
60 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
61 context_thread_checker_
.CalledOnValidThread());
63 base::AutoLock
lock(main_thread_lock_
);
65 // Destroy references to the context3d_ before leaking it.
66 if (context3d_
->GetCommandBufferProxy()) {
67 context3d_
->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback(
68 CommandBufferProxyImpl::MemoryAllocationChangedCallback());
70 lost_context_callback_proxy_
.reset();
74 CommandBufferProxyImpl
* ContextProviderCommandBuffer::GetCommandBufferProxy() {
75 return context3d_
->GetCommandBufferProxy();
78 WebGraphicsContext3DCommandBufferImpl
*
79 ContextProviderCommandBuffer::WebContext3D() {
81 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
82 DCHECK(context_thread_checker_
.CalledOnValidThread());
84 return context3d_
.get();
87 bool ContextProviderCommandBuffer::BindToCurrentThread() {
88 // This is called on the thread the context will be used.
89 DCHECK(context_thread_checker_
.CalledOnValidThread());
91 if (lost_context_callback_proxy_
)
94 if (!context3d_
->makeContextCurrent())
97 InitializeCapabilities();
99 std::string unique_context_name
=
100 base::StringPrintf("%s-%p", debug_name_
.c_str(), context3d_
.get());
101 context3d_
->pushGroupMarkerEXT(unique_context_name
.c_str());
103 lost_context_callback_proxy_
.reset(new LostContextCallbackProxy(this));
104 context3d_
->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback(
105 base::Bind(&ContextProviderCommandBuffer::OnMemoryAllocationChanged
,
106 base::Unretained(this)));
110 gpu::gles2::GLES2Interface
* ContextProviderCommandBuffer::ContextGL() {
112 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
113 DCHECK(context_thread_checker_
.CalledOnValidThread());
115 return context3d_
->GetImplementation();
118 gpu::ContextSupport
* ContextProviderCommandBuffer::ContextSupport() {
119 return context3d_
->GetContextSupport();
122 class GrContext
* ContextProviderCommandBuffer::GrContext() {
123 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
124 DCHECK(context_thread_checker_
.CalledOnValidThread());
127 return gr_context_
->get();
130 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_
.get()));
131 return gr_context_
->get();
134 cc::ContextProvider::Capabilities
135 ContextProviderCommandBuffer::ContextCapabilities() {
136 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
137 DCHECK(context_thread_checker_
.CalledOnValidThread());
139 return capabilities_
;
142 bool ContextProviderCommandBuffer::IsContextLost() {
143 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
144 DCHECK(context_thread_checker_
.CalledOnValidThread());
146 return context3d_
->isContextLost();
149 void ContextProviderCommandBuffer::VerifyContexts() {
150 DCHECK(lost_context_callback_proxy_
); // Is bound to thread.
151 DCHECK(context_thread_checker_
.CalledOnValidThread());
153 if (context3d_
->isContextLost())
157 void ContextProviderCommandBuffer::DeleteCachedResources() {
158 DCHECK(context_thread_checker_
.CalledOnValidThread());
161 gr_context_
->FreeGpuResources();
164 void ContextProviderCommandBuffer::OnLostContext() {
165 DCHECK(context_thread_checker_
.CalledOnValidThread());
167 base::AutoLock
lock(main_thread_lock_
);
172 if (!lost_context_callback_
.is_null())
173 base::ResetAndReturn(&lost_context_callback_
).Run();
175 gr_context_
->OnLostContext();
178 void ContextProviderCommandBuffer::OnMemoryAllocationChanged(
179 const gpu::MemoryAllocation
& allocation
) {
180 DCHECK(context_thread_checker_
.CalledOnValidThread());
182 if (memory_policy_changed_callback_
.is_null())
185 memory_policy_changed_callback_
.Run(cc::ManagedMemoryPolicy(allocation
));
188 void ContextProviderCommandBuffer::InitializeCapabilities() {
190 caps
.gpu
= context3d_
->GetImplementation()->capabilities();
192 size_t mapped_memory_limit
= context3d_
->GetMappedMemoryLimit();
193 caps
.max_transfer_buffer_usage_bytes
=
194 mapped_memory_limit
== WebGraphicsContext3DCommandBufferImpl::kNoLimit
195 ? std::numeric_limits
<size_t>::max() : mapped_memory_limit
;
197 capabilities_
= caps
;
200 bool ContextProviderCommandBuffer::DestroyedOnMainThread() {
201 DCHECK(main_thread_checker_
.CalledOnValidThread());
203 base::AutoLock
lock(main_thread_lock_
);
207 void ContextProviderCommandBuffer::SetLostContextCallback(
208 const LostContextCallback
& lost_context_callback
) {
209 DCHECK(context_thread_checker_
.CalledOnValidThread());
210 DCHECK(lost_context_callback_
.is_null() ||
211 lost_context_callback
.is_null());
212 lost_context_callback_
= lost_context_callback
;
215 void ContextProviderCommandBuffer::SetMemoryPolicyChangedCallback(
216 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
217 DCHECK(context_thread_checker_
.CalledOnValidThread());
218 DCHECK(memory_policy_changed_callback_
.is_null() ||
219 memory_policy_changed_callback
.is_null());
220 memory_policy_changed_callback_
= memory_policy_changed_callback
;
223 } // namespace content