Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / webkit / common / gpu / context_provider_in_process.cc
blob6f1be9ce25529551336115b95d3f39b10c2da1d3
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 <set>
9 #include "base/bind.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"
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 blink::WebGraphicsContext3D::Attributes attributes;
53 attributes.depth = false;
54 attributes.stencil = true;
55 attributes.antialias = false;
56 attributes.shareResources = true;
57 attributes.noAutomaticFlushes = true;
59 return Create(
60 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
61 attributes), "Offscreen");
64 ContextProviderInProcess::ContextProviderInProcess(
65 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d,
66 const std::string& debug_name)
67 : context3d_(context3d.Pass()),
68 destroyed_(false),
69 debug_name_(debug_name) {
70 DCHECK(main_thread_checker_.CalledOnValidThread());
71 DCHECK(context3d_);
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() {
88 DCHECK(context3d_);
90 // This is called on the thread the context will be used.
91 DCHECK(context_thread_checker_.CalledOnValidThread());
93 if (lost_context_callback_proxy_)
94 return true;
96 if (!context3d_->makeContextCurrent())
97 return false;
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));
106 return true;
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() {
121 DCHECK(context3d_);
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() {
129 DCHECK(context3d_);
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());
142 if (gr_context_)
143 return gr_context_->get();
145 gr_context_.reset(
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())
162 OnLostContext();
165 void ContextProviderInProcess::OnLostContext() {
166 DCHECK(context_thread_checker_.CalledOnValidThread());
168 base::AutoLock lock(destroyed_lock_);
169 if (destroyed_)
170 return;
171 destroyed_ = true;
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_);
181 return destroyed_;
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.
197 } // namespace gpu
198 } // namespace webkit