Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / webkit / common / gpu / context_provider_in_process.cc
blob87b5df6297239a73083d1b8af17e1092ff1d787b
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"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/strings/stringprintf.h"
10 #include "cc/output/managed_memory_policy.h"
11 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
12 #include "gpu/command_buffer/client/gles2_implementation.h"
13 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
15 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl;
17 namespace webkit {
18 namespace gpu {
20 class ContextProviderInProcess::LostContextCallbackProxy
21 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback {
22 public:
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();
36 private:
37 ContextProviderInProcess* provider_;
40 // static
41 scoped_refptr<ContextProviderInProcess> ContextProviderInProcess::Create(
42 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d,
43 const std::string& debug_name) {
44 if (!context3d)
45 return NULL;
46 return new ContextProviderInProcess(context3d.Pass(), debug_name);
49 // static
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;
60 return Create(
61 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
62 attributes, lose_context_when_out_of_memory),
63 "Offscreen");
66 ContextProviderInProcess::ContextProviderInProcess(
67 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d,
68 const std::string& debug_name)
69 : context3d_(context3d.Pass()),
70 destroyed_(false),
71 debug_name_(debug_name) {
72 DCHECK(main_thread_checker_.CalledOnValidThread());
73 DCHECK(context3d_);
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() {
90 DCHECK(context3d_);
92 // This is called on the thread the context will be used.
93 DCHECK(context_thread_checker_.CalledOnValidThread());
95 if (lost_context_callback_proxy_)
96 return true;
98 if (!context3d_->InitializeOnCurrentThread())
99 return false;
101 InitializeCapabilities();
103 std::string unique_context_name =
104 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get());
105 context3d_->traceBeginCHROMIUM("gpu_toplevel",
106 unique_context_name.c_str());
108 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
109 return true;
112 void ContextProviderInProcess::DetachFromThread() {
113 context_thread_checker_.DetachFromThread();
116 void ContextProviderInProcess::InitializeCapabilities() {
117 capabilities_.gpu = context3d_->GetImplementation()->capabilities();
119 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit();
120 capabilities_.max_transfer_buffer_usage_bytes =
121 mapped_memory_limit ==
122 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit
123 ? std::numeric_limits<size_t>::max()
124 : mapped_memory_limit;
127 cc::ContextProvider::Capabilities
128 ContextProviderInProcess::ContextCapabilities() {
129 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
130 DCHECK(context_thread_checker_.CalledOnValidThread());
131 return capabilities_;
134 ::gpu::gles2::GLES2Interface* ContextProviderInProcess::ContextGL() {
135 DCHECK(context3d_);
136 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
137 DCHECK(context_thread_checker_.CalledOnValidThread());
139 return context3d_->GetGLInterface();
142 ::gpu::ContextSupport* ContextProviderInProcess::ContextSupport() {
143 DCHECK(context3d_);
144 if (!lost_context_callback_proxy_)
145 return NULL; // Not bound to anything.
147 DCHECK(context_thread_checker_.CalledOnValidThread());
149 return context3d_->GetContextSupport();
152 class GrContext* ContextProviderInProcess::GrContext() {
153 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
154 DCHECK(context_thread_checker_.CalledOnValidThread());
156 if (gr_context_)
157 return gr_context_->get();
159 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get()));
160 return gr_context_->get();
163 void ContextProviderInProcess::SetupLock() {
164 context3d_->SetLock(&context_lock_);
167 base::Lock* ContextProviderInProcess::GetLock() {
168 return &context_lock_;
171 bool ContextProviderInProcess::IsContextLost() {
172 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
173 DCHECK(context_thread_checker_.CalledOnValidThread());
175 return context3d_->isContextLost();
178 void ContextProviderInProcess::VerifyContexts() {
179 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
180 DCHECK(context_thread_checker_.CalledOnValidThread());
182 if (context3d_->isContextLost())
183 OnLostContext();
186 void ContextProviderInProcess::DeleteCachedResources() {
187 DCHECK(context_thread_checker_.CalledOnValidThread());
189 if (gr_context_)
190 gr_context_->FreeGpuResources();
193 void ContextProviderInProcess::OnLostContext() {
194 DCHECK(context_thread_checker_.CalledOnValidThread());
196 base::AutoLock lock(destroyed_lock_);
197 if (destroyed_)
198 return;
199 destroyed_ = true;
201 if (!lost_context_callback_.is_null())
202 base::ResetAndReturn(&lost_context_callback_).Run();
203 if (gr_context_)
204 gr_context_->OnLostContext();
207 bool ContextProviderInProcess::DestroyedOnMainThread() {
208 DCHECK(main_thread_checker_.CalledOnValidThread());
210 base::AutoLock lock(destroyed_lock_);
211 return destroyed_;
214 void ContextProviderInProcess::SetLostContextCallback(
215 const LostContextCallback& lost_context_callback) {
216 DCHECK(context_thread_checker_.CalledOnValidThread());
217 DCHECK(lost_context_callback_.is_null() ||
218 lost_context_callback.is_null());
219 lost_context_callback_ = lost_context_callback;
222 void ContextProviderInProcess::SetMemoryPolicyChangedCallback(
223 const MemoryPolicyChangedCallback& memory_policy_changed_callback) {
224 // There's no memory manager for the in-process implementation.
227 } // namespace gpu
228 } // namespace webkit