| 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/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "cc/output/managed_memory_policy.h" | 12 #include "cc/output/managed_memory_policy.h" |
| 13 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" | 13 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" | 14 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 15 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h" | 15 #include "ui/compositor/test/grcontext_for_webgraphicscontext3d.h" |
| 16 | 16 |
| 17 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl; | 17 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 18 | 18 |
| 19 namespace webkit { | 19 namespace ui { |
| 20 namespace gpu { | |
| 21 | 20 |
| 22 class ContextProviderInProcess::LostContextCallbackProxy | 21 class InProcessContextProvider::LostContextCallbackProxy |
| 23 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { | 22 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { |
| 24 public: | 23 public: |
| 25 explicit LostContextCallbackProxy(ContextProviderInProcess* provider) | 24 explicit LostContextCallbackProxy(InProcessContextProvider* provider) |
| 26 : provider_(provider) { | 25 : provider_(provider) { |
| 27 provider_->context3d_->setContextLostCallback(this); | 26 provider_->context3d_->setContextLostCallback(this); |
| 28 } | 27 } |
| 29 | 28 |
| 30 virtual ~LostContextCallbackProxy() { | 29 virtual ~LostContextCallbackProxy() { |
| 31 provider_->context3d_->setContextLostCallback(NULL); | 30 provider_->context3d_->setContextLostCallback(NULL); |
| 32 } | 31 } |
| 33 | 32 |
| 34 virtual void onContextLost() { | 33 virtual void onContextLost() { |
| 35 provider_->OnLostContext(); | 34 provider_->OnLostContext(); |
| 36 } | 35 } |
| 37 | 36 |
| 38 private: | 37 private: |
| 39 ContextProviderInProcess* provider_; | 38 InProcessContextProvider* provider_; |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 // static | 41 // static |
| 43 scoped_refptr<ContextProviderInProcess> ContextProviderInProcess::Create( | 42 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create( |
| 44 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, | 43 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, |
| 45 const std::string& debug_name) { | 44 const std::string& debug_name) { |
| 46 if (!context3d) | 45 if (!context3d) |
| 47 return NULL; | 46 return NULL; |
| 48 return new ContextProviderInProcess(context3d.Pass(), debug_name); | 47 return new InProcessContextProvider(context3d.Pass(), debug_name); |
| 49 } | 48 } |
| 50 | 49 |
| 51 // static | 50 // static |
| 52 scoped_refptr<ContextProviderInProcess> | 51 scoped_refptr<InProcessContextProvider> |
| 53 ContextProviderInProcess::CreateOffscreen( | 52 InProcessContextProvider::CreateOffscreen( |
| 54 bool lose_context_when_out_of_memory) { | 53 bool lose_context_when_out_of_memory) { |
| 55 blink::WebGraphicsContext3D::Attributes attributes; | 54 blink::WebGraphicsContext3D::Attributes attributes; |
| 56 attributes.depth = false; | 55 attributes.depth = false; |
| 57 attributes.stencil = true; | 56 attributes.stencil = true; |
| 58 attributes.antialias = false; | 57 attributes.antialias = false; |
| 59 attributes.shareResources = true; | 58 attributes.shareResources = true; |
| 60 attributes.noAutomaticFlushes = true; | 59 attributes.noAutomaticFlushes = true; |
| 61 | 60 |
| 62 return Create( | 61 return Create( |
| 63 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext( | 62 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext( |
| 64 attributes, lose_context_when_out_of_memory), | 63 attributes, lose_context_when_out_of_memory), |
| 65 "Offscreen"); | 64 "Offscreen"); |
| 66 } | 65 } |
| 67 | 66 |
| 68 ContextProviderInProcess::ContextProviderInProcess( | 67 InProcessContextProvider::InProcessContextProvider( |
| 69 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, | 68 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d, |
| 70 const std::string& debug_name) | 69 const std::string& debug_name) |
| 71 : context3d_(context3d.Pass()), | 70 : context3d_(context3d.Pass()), |
| 72 destroyed_(false), | 71 destroyed_(false), |
| 73 debug_name_(debug_name) { | 72 debug_name_(debug_name) { |
| 74 DCHECK(main_thread_checker_.CalledOnValidThread()); | 73 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 75 DCHECK(context3d_); | 74 DCHECK(context3d_); |
| 76 context_thread_checker_.DetachFromThread(); | 75 context_thread_checker_.DetachFromThread(); |
| 77 } | 76 } |
| 78 | 77 |
| 79 ContextProviderInProcess::~ContextProviderInProcess() { | 78 InProcessContextProvider::~InProcessContextProvider() { |
| 80 DCHECK(main_thread_checker_.CalledOnValidThread() || | 79 DCHECK(main_thread_checker_.CalledOnValidThread() || |
| 81 context_thread_checker_.CalledOnValidThread()); | 80 context_thread_checker_.CalledOnValidThread()); |
| 82 } | 81 } |
| 83 | 82 |
| 84 blink::WebGraphicsContext3D* ContextProviderInProcess::WebContext3D() { | 83 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_); | 84 DCHECK(context3d_); |
| 93 | 85 |
| 94 // This is called on the thread the context will be used. | 86 // This is called on the thread the context will be used. |
| 95 DCHECK(context_thread_checker_.CalledOnValidThread()); | 87 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 96 | 88 |
| 97 if (lost_context_callback_proxy_) | 89 if (lost_context_callback_proxy_) |
| 98 return true; | 90 return true; |
| 99 | 91 |
| 100 if (!context3d_->InitializeOnCurrentThread()) | 92 if (!context3d_->InitializeOnCurrentThread()) |
| 101 return false; | 93 return false; |
| 102 | 94 |
| 103 InitializeCapabilities(); | 95 InitializeCapabilities(); |
| 104 | 96 |
| 105 std::string unique_context_name = | 97 std::string unique_context_name = |
| 106 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); | 98 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); |
| 107 context3d_->traceBeginCHROMIUM("gpu_toplevel", | 99 context3d_->traceBeginCHROMIUM("gpu_toplevel", |
| 108 unique_context_name.c_str()); | 100 unique_context_name.c_str()); |
| 109 | 101 |
| 110 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | 102 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
| 111 return true; | 103 return true; |
| 112 } | 104 } |
| 113 | 105 |
| 114 void ContextProviderInProcess::InitializeCapabilities() { | 106 void InProcessContextProvider::InitializeCapabilities() { |
| 115 capabilities_.gpu = context3d_->GetImplementation()->capabilities(); | 107 capabilities_.gpu = context3d_->GetImplementation()->capabilities(); |
| 116 | 108 |
| 117 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); | 109 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); |
| 118 capabilities_.max_transfer_buffer_usage_bytes = | 110 capabilities_.max_transfer_buffer_usage_bytes = |
| 119 mapped_memory_limit == | 111 mapped_memory_limit == |
| 120 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit | 112 WebGraphicsContext3DInProcessCommandBufferImpl::kNoLimit |
| 121 ? std::numeric_limits<size_t>::max() | 113 ? std::numeric_limits<size_t>::max() |
| 122 : mapped_memory_limit; | 114 : mapped_memory_limit; |
| 123 } | 115 } |
| 124 | 116 |
| 125 cc::ContextProvider::Capabilities | 117 cc::ContextProvider::Capabilities |
| 126 ContextProviderInProcess::ContextCapabilities() { | 118 InProcessContextProvider::ContextCapabilities() { |
| 127 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 119 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 128 DCHECK(context_thread_checker_.CalledOnValidThread()); | 120 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 129 return capabilities_; | 121 return capabilities_; |
| 130 } | 122 } |
| 131 | 123 |
| 132 ::gpu::gles2::GLES2Interface* ContextProviderInProcess::ContextGL() { | 124 ::gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { |
| 133 DCHECK(context3d_); | 125 DCHECK(context3d_); |
| 134 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 126 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 135 DCHECK(context_thread_checker_.CalledOnValidThread()); | 127 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 136 | 128 |
| 137 return context3d_->GetGLInterface(); | 129 return context3d_->GetGLInterface(); |
| 138 } | 130 } |
| 139 | 131 |
| 140 ::gpu::ContextSupport* ContextProviderInProcess::ContextSupport() { | 132 ::gpu::ContextSupport* InProcessContextProvider::ContextSupport() { |
| 141 DCHECK(context3d_); | 133 DCHECK(context3d_); |
| 142 if (!lost_context_callback_proxy_) | 134 if (!lost_context_callback_proxy_) |
| 143 return NULL; // Not bound to anything. | 135 return NULL; // Not bound to anything. |
| 144 | 136 |
| 145 DCHECK(context_thread_checker_.CalledOnValidThread()); | 137 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 146 | 138 |
| 147 return context3d_->GetContextSupport(); | 139 return context3d_->GetContextSupport(); |
| 148 } | 140 } |
| 149 | 141 |
| 150 class GrContext* ContextProviderInProcess::GrContext() { | 142 class GrContext* InProcessContextProvider::GrContext() { |
| 151 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 143 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 152 DCHECK(context_thread_checker_.CalledOnValidThread()); | 144 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 153 | 145 |
| 154 if (gr_context_) | 146 if (gr_context_) |
| 155 return gr_context_->get(); | 147 return gr_context_->context(); |
| 156 | 148 |
| 157 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get())); | 149 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get())); |
| 158 return gr_context_->get(); | 150 return gr_context_->context(); |
| 159 } | 151 } |
| 160 | 152 |
| 161 bool ContextProviderInProcess::IsContextLost() { | 153 bool InProcessContextProvider::IsContextLost() { |
| 162 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 154 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 163 DCHECK(context_thread_checker_.CalledOnValidThread()); | 155 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 164 | 156 |
| 165 return context3d_->isContextLost(); | 157 return context3d_->isContextLost(); |
| 166 } | 158 } |
| 167 | 159 |
| 168 void ContextProviderInProcess::VerifyContexts() { | 160 void InProcessContextProvider::VerifyContexts() { |
| 169 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 161 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 170 DCHECK(context_thread_checker_.CalledOnValidThread()); | 162 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 171 | 163 |
| 172 if (context3d_->isContextLost()) | 164 if (context3d_->isContextLost()) |
| 173 OnLostContext(); | 165 OnLostContext(); |
| 174 } | 166 } |
| 175 | 167 |
| 176 void ContextProviderInProcess::DeleteCachedResources() { | 168 void InProcessContextProvider::DeleteCachedResources() { |
| 177 DCHECK(context_thread_checker_.CalledOnValidThread()); | 169 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 178 | 170 |
| 179 if (gr_context_) | 171 if (gr_context_) |
| 180 gr_context_->FreeGpuResources(); | 172 gr_context_->FreeGpuResources(); |
| 181 } | 173 } |
| 182 | 174 |
| 183 void ContextProviderInProcess::OnLostContext() { | 175 void InProcessContextProvider::OnLostContext() { |
| 184 DCHECK(context_thread_checker_.CalledOnValidThread()); | 176 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 185 { | 177 { |
| 186 base::AutoLock lock(destroyed_lock_); | 178 base::AutoLock lock(destroyed_lock_); |
| 187 if (destroyed_) | 179 if (destroyed_) |
| 188 return; | 180 return; |
| 189 destroyed_ = true; | 181 destroyed_ = true; |
| 190 } | 182 } |
| 191 if (!lost_context_callback_.is_null()) | 183 if (!lost_context_callback_.is_null()) |
| 192 base::ResetAndReturn(&lost_context_callback_).Run(); | 184 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 193 if (gr_context_) | 185 if (gr_context_) |
| 194 gr_context_->OnLostContext(); | 186 gr_context_->OnLostContext(); |
| 195 } | 187 } |
| 196 | 188 |
| 197 bool ContextProviderInProcess::DestroyedOnMainThread() { | 189 bool InProcessContextProvider::DestroyedOnMainThread() { |
| 198 DCHECK(main_thread_checker_.CalledOnValidThread()); | 190 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 199 | 191 |
| 200 base::AutoLock lock(destroyed_lock_); | 192 base::AutoLock lock(destroyed_lock_); |
| 201 return destroyed_; | 193 return destroyed_; |
| 202 } | 194 } |
| 203 | 195 |
| 204 void ContextProviderInProcess::SetLostContextCallback( | 196 void InProcessContextProvider::SetLostContextCallback( |
| 205 const LostContextCallback& lost_context_callback) { | 197 const LostContextCallback& lost_context_callback) { |
| 206 DCHECK(context_thread_checker_.CalledOnValidThread()); | 198 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 207 DCHECK(lost_context_callback_.is_null() || | 199 DCHECK(lost_context_callback_.is_null() || |
| 208 lost_context_callback.is_null()); | 200 lost_context_callback.is_null()); |
| 209 lost_context_callback_ = lost_context_callback; | 201 lost_context_callback_ = lost_context_callback; |
| 210 } | 202 } |
| 211 | 203 |
| 212 void ContextProviderInProcess::SetMemoryPolicyChangedCallback( | 204 void InProcessContextProvider::SetMemoryPolicyChangedCallback( |
| 213 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | 205 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
| 214 // There's no memory manager for the in-process implementation. | 206 // There's no memory manager for the in-process implementation. |
| 215 } | 207 } |
| 216 | 208 |
| 217 } // namespace gpu | 209 } // namespace ui |
| 218 } // namespace webkit | |
| OLD | NEW |