Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/common/gpu/context_provider_in_process.h" | 5 #include "ui/compositor/test/in_process_context_provider.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/debug/trace_event.h" | |
| 12 #include "base/lazy_instance.h" | |
| 11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 12 #include "cc/output/managed_memory_policy.h" | 14 #include "cc/output/managed_memory_policy.h" |
| 13 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" | 15 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" | 16 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 15 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h" | 17 #include "gpu/command_buffer/client/gles2_lib.h" |
| 18 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" | |
| 19 #include "third_party/skia/include/gpu/GrContext.h" | |
| 20 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | |
| 16 | 21 |
| 17 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl; | 22 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 18 | 23 |
| 19 namespace webkit { | 24 namespace ui { |
| 20 namespace gpu { | |
| 21 | 25 |
| 22 class ContextProviderInProcess::LostContextCallbackProxy | 26 namespace { |
| 27 | |
| 28 // Singleton used to initialize and terminate the gles2 library. | |
| 29 class GLES2Initializer { | |
| 30 public: | |
| 31 GLES2Initializer() { gles2::Initialize(); } | |
| 32 | |
| 33 ~GLES2Initializer() { gles2::Terminate(); } | |
| 34 | |
| 35 private: | |
| 36 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
| 37 }; | |
| 38 | |
| 39 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
| 40 LAZY_INSTANCE_INITIALIZER; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 class InProcessContextProvider::LostContextCallbackProxy | |
| 23 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { | 45 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { |
| 24 public: | 46 public: |
| 25 explicit LostContextCallbackProxy(ContextProviderInProcess* provider) | 47 explicit LostContextCallbackProxy(InProcessContextProvider* provider) |
| 26 : provider_(provider) { | 48 : provider_(provider) { |
| 27 provider_->context3d_->setContextLostCallback(this); | 49 provider_->context3d_->setContextLostCallback(this); |
| 28 } | 50 } |
| 29 | 51 |
| 30 virtual ~LostContextCallbackProxy() { | 52 virtual ~LostContextCallbackProxy() { |
| 31 provider_->context3d_->setContextLostCallback(NULL); | 53 provider_->context3d_->setContextLostCallback(NULL); |
| 32 } | 54 } |
| 33 | 55 |
| 34 virtual void onContextLost() { | 56 virtual void onContextLost() { |
| 35 provider_->OnLostContext(); | 57 provider_->OnLostContext(); |
| 36 } | 58 } |
| 37 | 59 |
| 38 private: | 60 private: |
| 39 ContextProviderInProcess* provider_; | 61 InProcessContextProvider* provider_; |
| 40 }; | 62 }; |
| 41 | 63 |
| 42 // static | 64 // static |
| 43 scoped_refptr<ContextProviderInProcess> ContextProviderInProcess::Create( | 65 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create( |
| 44 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, | 66 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, |
| 45 const std::string& debug_name) { | 67 const std::string& debug_name) { |
| 46 if (!context3d) | 68 if (!context3d) |
| 47 return NULL; | 69 return NULL; |
| 48 return new ContextProviderInProcess(context3d.Pass(), debug_name); | 70 return new InProcessContextProvider(context3d.Pass(), debug_name); |
| 49 } | 71 } |
| 50 | 72 |
| 51 // static | 73 // static |
| 52 scoped_refptr<ContextProviderInProcess> | 74 scoped_refptr<InProcessContextProvider> |
| 53 ContextProviderInProcess::CreateOffscreen( | 75 InProcessContextProvider::CreateOffscreen( |
| 54 bool lose_context_when_out_of_memory) { | 76 bool lose_context_when_out_of_memory) { |
| 55 blink::WebGraphicsContext3D::Attributes attributes; | 77 blink::WebGraphicsContext3D::Attributes attributes; |
| 56 attributes.depth = false; | 78 attributes.depth = false; |
| 57 attributes.stencil = true; | 79 attributes.stencil = true; |
| 58 attributes.antialias = false; | 80 attributes.antialias = false; |
| 59 attributes.shareResources = true; | 81 attributes.shareResources = true; |
| 60 attributes.noAutomaticFlushes = true; | 82 attributes.noAutomaticFlushes = true; |
| 61 | 83 |
| 62 return Create( | 84 return Create( |
| 63 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext( | 85 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext( |
| 64 attributes, lose_context_when_out_of_memory), | 86 attributes, lose_context_when_out_of_memory), |
| 65 "Offscreen"); | 87 "Offscreen"); |
| 66 } | 88 } |
| 67 | 89 |
| 68 ContextProviderInProcess::ContextProviderInProcess( | 90 InProcessContextProvider::InProcessContextProvider( |
| 69 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, | 91 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, |
| 70 const std::string& debug_name) | 92 const std::string& debug_name) |
| 71 : context3d_(context3d.Pass()), | 93 : context3d_(context3d.Pass()), |
| 72 destroyed_(false), | 94 destroyed_(false), |
| 73 debug_name_(debug_name) { | 95 debug_name_(debug_name) { |
| 74 DCHECK(main_thread_checker_.CalledOnValidThread()); | 96 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 75 DCHECK(context3d_); | 97 DCHECK(context3d_); |
| 76 context_thread_checker_.DetachFromThread(); | 98 context_thread_checker_.DetachFromThread(); |
| 77 } | 99 } |
| 78 | 100 |
| 79 ContextProviderInProcess::~ContextProviderInProcess() { | 101 InProcessContextProvider::~InProcessContextProvider() { |
| 80 DCHECK(main_thread_checker_.CalledOnValidThread() || | 102 DCHECK(main_thread_checker_.CalledOnValidThread() || |
| 81 context_thread_checker_.CalledOnValidThread()); | 103 context_thread_checker_.CalledOnValidThread()); |
| 82 } | 104 } |
| 83 | 105 |
| 84 blink::WebGraphicsContext3D* ContextProviderInProcess::WebContext3D() { | 106 bool InProcessContextProvider::BindToCurrentThread() { |
| 85 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | |
| 86 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 87 | |
| 88 return context3d_.get(); | |
| 89 } | |
| 90 | |
| 91 bool ContextProviderInProcess::BindToCurrentThread() { | |
| 92 DCHECK(context3d_); | 107 DCHECK(context3d_); |
| 93 | 108 |
| 94 // This is called on the thread the context will be used. | 109 // This is called on the thread the context will be used. |
| 95 DCHECK(context_thread_checker_.CalledOnValidThread()); | 110 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 96 | 111 |
| 97 if (lost_context_callback_proxy_) | 112 if (lost_context_callback_proxy_) |
| 98 return true; | 113 return true; |
| 99 | 114 |
| 100 if (!context3d_->InitializeOnCurrentThread()) | 115 if (!context3d_->InitializeOnCurrentThread()) |
| 101 return false; | 116 return false; |
| 102 | 117 |
| 103 InitializeCapabilities(); | 118 InitializeCapabilities(); |
| 104 | 119 |
| 105 std::string unique_context_name = | 120 std::string unique_context_name = |
| 106 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); | 121 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); |
| 107 context3d_->traceBeginCHROMIUM("gpu_toplevel", | 122 context3d_->traceBeginCHROMIUM("gpu_toplevel", |
| 108 unique_context_name.c_str()); | 123 unique_context_name.c_str()); |
| 109 | 124 |
| 110 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | 125 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
| 111 return true; | 126 return true; |
| 112 } | 127 } |
| 113 | 128 |
| 114 void ContextProviderInProcess::InitializeCapabilities() { | 129 void InProcessContextProvider::InitializeCapabilities() { |
| 115 capabilities_.gpu = context3d_->GetImplementation()->capabilities(); | 130 capabilities_.gpu = context3d_->GetImplementation()->capabilities(); |
| 116 | 131 |
| 117 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); | 132 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); |
| 118 capabilities_.max_transfer_buffer_usage_bytes = | 133 capabilities_.max_transfer_buffer_usage_bytes = |
| 119 mapped_memory_limit == | 134 mapped_memory_limit == |
| 120 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit | 135 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit |
| 121 ? std::numeric_limits<size_t>::max() | 136 ? std::numeric_limits<size_t>::max() |
| 122 : mapped_memory_limit; | 137 : mapped_memory_limit; |
| 123 } | 138 } |
| 124 | 139 |
| 125 cc::ContextProvider::Capabilities | 140 cc::ContextProvider::Capabilities |
| 126 ContextProviderInProcess::ContextCapabilities() { | 141 InProcessContextProvider::ContextCapabilities() { |
| 127 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 142 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 128 DCHECK(context_thread_checker_.CalledOnValidThread()); | 143 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 129 return capabilities_; | 144 return capabilities_; |
| 130 } | 145 } |
| 131 | 146 |
| 132 ::gpu::gles2::GLES2Interface* ContextProviderInProcess::ContextGL() { | 147 ::gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { |
| 133 DCHECK(context3d_); | 148 DCHECK(context3d_); |
| 134 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 149 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 135 DCHECK(context_thread_checker_.CalledOnValidThread()); | 150 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 136 | 151 |
| 137 return context3d_->GetGLInterface(); | 152 return context3d_->GetGLInterface(); |
| 138 } | 153 } |
| 139 | 154 |
| 140 ::gpu::ContextSupport* ContextProviderInProcess::ContextSupport() { | 155 ::gpu::ContextSupport* InProcessContextProvider::ContextSupport() { |
| 141 DCHECK(context3d_); | 156 DCHECK(context3d_); |
| 142 if (!lost_context_callback_proxy_) | 157 if (!lost_context_callback_proxy_) |
| 143 return NULL; // Not bound to anything. | 158 return NULL; // Not bound to anything. |
| 144 | 159 |
| 145 DCHECK(context_thread_checker_.CalledOnValidThread()); | 160 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 146 | 161 |
| 147 return context3d_->GetContextSupport(); | 162 return context3d_->GetContextSupport(); |
| 148 } | 163 } |
| 149 | 164 |
| 150 class GrContext* ContextProviderInProcess::GrContext() { | 165 static void BindGrContextCallback(const GrGLInterface* interface) { |
| 166 cc::ContextProvider* context_provider = | |
| 167 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | |
| 168 | |
| 169 gles2::SetGLContext(context_provider->ContextGL()); | |
| 170 } | |
| 171 | |
| 172 class GrContext* InProcessContextProvider::GrContext() { | |
| 151 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 173 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 152 DCHECK(context_thread_checker_.CalledOnValidThread()); | 174 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 153 | 175 |
| 154 if (gr_context_) | 176 if (gr_context_) |
| 155 return gr_context_->get(); | 177 return gr_context_.get(); |
| 156 | 178 |
| 157 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get())); | 179 // The GrGLInterface factory will make GL calls using the C GLES2 interface. |
| 158 return gr_context_->get(); | 180 // Make sure the gles2 library is initialized first on exactly one thread. |
| 181 g_gles2_initializer.Get(); | |
| 182 gles2::SetGLContext(ContextGL()); | |
| 183 | |
| 184 skia::RefPtr<GrGLInterface> interface = | |
| 185 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | |
| 186 interface->fCallback = BindGrContextCallback; | |
| 187 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
| 188 | |
| 189 gr_context_ = skia::AdoptRef(GrContext::Create( | |
| 190 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | |
| 191 | |
| 192 return gr_context_.get(); | |
| 159 } | 193 } |
| 160 | 194 |
| 161 bool ContextProviderInProcess::IsContextLost() { | 195 bool InProcessContextProvider::IsContextLost() { |
| 162 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 196 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 163 DCHECK(context_thread_checker_.CalledOnValidThread()); | 197 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 164 | 198 |
| 165 return context3d_->isContextLost(); | 199 return context3d_->isContextLost(); |
| 166 } | 200 } |
| 167 | 201 |
| 168 void ContextProviderInProcess::VerifyContexts() { | 202 void InProcessContextProvider::VerifyContexts() { |
| 169 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 203 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 170 DCHECK(context_thread_checker_.CalledOnValidThread()); | 204 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 171 | 205 |
| 172 if (context3d_->isContextLost()) | 206 if (context3d_->isContextLost()) |
| 173 OnLostContext(); | 207 OnLostContext(); |
| 174 } | 208 } |
| 175 | 209 |
| 176 void ContextProviderInProcess::DeleteCachedResources() { | 210 void InProcessContextProvider::DeleteCachedResources() { |
| 177 DCHECK(context_thread_checker_.CalledOnValidThread()); | 211 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 178 | 212 |
| 179 if (gr_context_) | 213 if (gr_context_) { |
| 180 gr_context_->FreeGpuResources(); | 214 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \ |
|
piman
2015/01/21 02:07:36
nit: no need for \
| |
| 215 TRACE_EVENT_SCOPE_THREAD); | |
| 216 gr_context_->freeGpuResources(); | |
| 217 } | |
| 181 } | 218 } |
| 182 | 219 |
| 183 void ContextProviderInProcess::OnLostContext() { | 220 void InProcessContextProvider::OnLostContext() { |
| 184 DCHECK(context_thread_checker_.CalledOnValidThread()); | 221 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 185 { | 222 { |
| 186 base::AutoLock lock(destroyed_lock_); | 223 base::AutoLock lock(destroyed_lock_); |
| 187 if (destroyed_) | 224 if (destroyed_) |
| 188 return; | 225 return; |
| 189 destroyed_ = true; | 226 destroyed_ = true; |
| 190 } | 227 } |
| 191 if (!lost_context_callback_.is_null()) | 228 if (!lost_context_callback_.is_null()) |
| 192 base::ResetAndReturn(&lost_context_callback_).Run(); | 229 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 193 if (gr_context_) | 230 if (gr_context_) |
| 194 gr_context_->OnLostContext(); | 231 gr_context_->abandonContext(); |
| 195 } | 232 } |
| 196 | 233 |
| 197 bool ContextProviderInProcess::DestroyedOnMainThread() { | 234 bool InProcessContextProvider::DestroyedOnMainThread() { |
| 198 DCHECK(main_thread_checker_.CalledOnValidThread()); | 235 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 199 | 236 |
| 200 base::AutoLock lock(destroyed_lock_); | 237 base::AutoLock lock(destroyed_lock_); |
| 201 return destroyed_; | 238 return destroyed_; |
| 202 } | 239 } |
| 203 | 240 |
| 204 void ContextProviderInProcess::SetLostContextCallback( | 241 void InProcessContextProvider::SetLostContextCallback( |
| 205 const LostContextCallback& lost_context_callback) { | 242 const LostContextCallback& lost_context_callback) { |
| 206 DCHECK(context_thread_checker_.CalledOnValidThread()); | 243 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 207 DCHECK(lost_context_callback_.is_null() || | 244 DCHECK(lost_context_callback_.is_null() || |
| 208 lost_context_callback.is_null()); | 245 lost_context_callback.is_null()); |
| 209 lost_context_callback_ = lost_context_callback; | 246 lost_context_callback_ = lost_context_callback; |
| 210 } | 247 } |
| 211 | 248 |
| 212 void ContextProviderInProcess::SetMemoryPolicyChangedCallback( | 249 void InProcessContextProvider::SetMemoryPolicyChangedCallback( |
| 213 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | 250 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
| 214 // There's no memory manager for the in-process implementation. | 251 // There's no memory manager for the in-process implementation. |
| 215 } | 252 } |
| 216 | 253 |
| 217 } // namespace gpu | 254 } // namespace ui |
| 218 } // namespace webkit | |
| OLD | NEW |