| 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 "content/common/gpu/client/context_provider_command_buffer.h" | 5 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 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 "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h" | 13 #include "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h" |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" | 14 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 15 #include "third_party/skia/include/gpu/GrContext.h" | 15 #include "third_party/skia/include/gpu/GrContext.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 class ContextProviderCommandBuffer::LostContextCallbackProxy | 19 class ContextProviderCommandBuffer::LostContextCallbackProxy |
| 20 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { | 20 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { |
| 21 public: | 21 public: |
| 22 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) | 22 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) |
| 23 : provider_(provider) { | 23 : provider_(provider) { |
| 24 provider_->WebContext3DNoChecks()->setContextLostCallback(this); | 24 provider_->context3d_->setContextLostCallback(this); |
| 25 } | 25 } |
| 26 | 26 |
| 27 ~LostContextCallbackProxy() override { | 27 ~LostContextCallbackProxy() override { |
| 28 provider_->WebContext3DNoChecks()->setContextLostCallback(NULL); | 28 provider_->context3d_->setContextLostCallback(NULL); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void onContextLost() override { provider_->OnLostContext(); } | 31 void onContextLost() override { provider_->OnLostContext(); } |
| 32 | 32 |
| 33 private: | 33 private: |
| 34 ContextProviderCommandBuffer* provider_; | 34 ContextProviderCommandBuffer* provider_; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 scoped_refptr<ContextProviderCommandBuffer> | 37 scoped_refptr<ContextProviderCommandBuffer> |
| 38 ContextProviderCommandBuffer::Create( | 38 ContextProviderCommandBuffer::Create( |
| 39 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 39 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
| 40 CommandBufferContextType type) { | 40 CommandBufferContextType type) { |
| 41 if (!context3d) | 41 if (!context3d) |
| 42 return NULL; | 42 return NULL; |
| 43 | 43 |
| 44 return new ContextProviderCommandBuffer(context3d.Pass(), type); | 44 return new ContextProviderCommandBuffer(context3d.Pass(), type); |
| 45 } | 45 } |
| 46 | 46 |
| 47 ContextProviderCommandBuffer::ContextProviderCommandBuffer( | 47 ContextProviderCommandBuffer::ContextProviderCommandBuffer( |
| 48 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 48 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
| 49 CommandBufferContextType type) | 49 CommandBufferContextType type) |
| 50 : context_type_(type), | 50 : context3d_(context3d.Pass()), |
| 51 context_type_(type), |
| 51 debug_name_(CommandBufferContextTypeToString(type)) { | 52 debug_name_(CommandBufferContextTypeToString(type)) { |
| 52 gr_interface_ = skia::AdoptRef(new GrGLInterfaceForWebGraphicsContext3D( | |
| 53 context3d.Pass())); | |
| 54 DCHECK(main_thread_checker_.CalledOnValidThread()); | 53 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 55 DCHECK(gr_interface_->WebContext3D()); | 54 DCHECK(context3d_); |
| 56 context_thread_checker_.DetachFromThread(); | 55 context_thread_checker_.DetachFromThread(); |
| 57 } | 56 } |
| 58 | 57 |
| 59 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { | 58 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { |
| 60 DCHECK(main_thread_checker_.CalledOnValidThread() || | 59 DCHECK(main_thread_checker_.CalledOnValidThread() || |
| 61 context_thread_checker_.CalledOnValidThread()); | 60 context_thread_checker_.CalledOnValidThread()); |
| 62 | 61 |
| 63 // Destroy references to the context3d_ before leaking it. | 62 // Destroy references to the context3d_ before leaking it. |
| 64 if (WebContext3DNoChecks()->GetCommandBufferProxy()) | 63 if (context3d_->GetCommandBufferProxy()) |
| 65 WebContext3DNoChecks()->GetCommandBufferProxy()->SetLock(nullptr); | 64 context3d_->GetCommandBufferProxy()->SetLock(nullptr); |
| 66 lost_context_callback_proxy_.reset(); | 65 lost_context_callback_proxy_.reset(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 | 68 |
| 70 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() { | 69 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() { |
| 71 return WebContext3D()->GetCommandBufferProxy(); | 70 return context3d_->GetCommandBufferProxy(); |
| 72 } | 71 } |
| 73 | 72 |
| 74 WebGraphicsContext3DCommandBufferImpl* | 73 WebGraphicsContext3DCommandBufferImpl* |
| 75 ContextProviderCommandBuffer::WebContext3D() { | 74 ContextProviderCommandBuffer::WebContext3D() { |
| 76 DCHECK(gr_interface_); | 75 DCHECK(context3d_); |
| 77 DCHECK(gr_interface_->WebContext3D()); | |
| 78 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 76 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 79 DCHECK(context_thread_checker_.CalledOnValidThread()); | 77 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 80 | 78 |
| 81 return WebContext3DNoChecks(); | 79 return context3d_.get(); |
| 82 } | |
| 83 | |
| 84 WebGraphicsContext3DCommandBufferImpl* | |
| 85 ContextProviderCommandBuffer::WebContext3DNoChecks() { | |
| 86 DCHECK(gr_interface_); | |
| 87 return static_cast<WebGraphicsContext3DCommandBufferImpl*>( | |
| 88 gr_interface_->WebContext3D()); | |
| 89 } | 80 } |
| 90 | 81 |
| 91 bool ContextProviderCommandBuffer::BindToCurrentThread() { | 82 bool ContextProviderCommandBuffer::BindToCurrentThread() { |
| 92 // This is called on the thread the context will be used. | 83 // This is called on the thread the context will be used. |
| 93 DCHECK(context_thread_checker_.CalledOnValidThread()); | 84 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 94 DCHECK(gr_interface_ && gr_interface_->WebContext3D()); | |
| 95 | 85 |
| 96 if (lost_context_callback_proxy_) | 86 if (lost_context_callback_proxy_) |
| 97 return true; | 87 return true; |
| 98 | 88 |
| 99 WebContext3DNoChecks()->SetContextType(context_type_); | 89 context3d_->SetContextType(context_type_); |
| 100 if (!WebContext3DNoChecks()->InitializeOnCurrentThread()) | 90 if (!context3d_->InitializeOnCurrentThread()) |
| 101 return false; | 91 return false; |
| 102 | 92 |
| 103 gr_interface_->BindToCurrentThread(); | |
| 104 InitializeCapabilities(); | 93 InitializeCapabilities(); |
| 105 | 94 |
| 106 std::string unique_context_name = | 95 std::string unique_context_name = |
| 107 base::StringPrintf("%s-%p", debug_name_.c_str(), WebContext3DNoChecks()); | 96 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); |
| 108 WebContext3DNoChecks()->traceBeginCHROMIUM("gpu_toplevel", | 97 context3d_->traceBeginCHROMIUM("gpu_toplevel", unique_context_name.c_str()); |
| 109 unique_context_name.c_str()); | |
| 110 | 98 |
| 111 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | 99 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
| 112 return true; | 100 return true; |
| 113 } | 101 } |
| 114 | 102 |
| 115 void ContextProviderCommandBuffer::DetachFromThread() { | 103 void ContextProviderCommandBuffer::DetachFromThread() { |
| 116 context_thread_checker_.DetachFromThread(); | 104 context_thread_checker_.DetachFromThread(); |
| 117 } | 105 } |
| 118 | 106 |
| 119 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { | 107 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { |
| 108 DCHECK(context3d_); |
| 120 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 109 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 110 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 121 | 111 |
| 122 return WebContext3D()->GetImplementation(); | 112 return context3d_->GetImplementation(); |
| 123 } | 113 } |
| 124 | 114 |
| 125 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { | 115 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { |
| 126 return WebContext3D()->GetContextSupport(); | 116 return context3d_->GetContextSupport(); |
| 127 } | 117 } |
| 128 | 118 |
| 129 class GrContext* ContextProviderCommandBuffer::GrContext() { | 119 class GrContext* ContextProviderCommandBuffer::GrContext() { |
| 130 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 120 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 131 DCHECK(context_thread_checker_.CalledOnValidThread()); | 121 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 132 | 122 |
| 133 if (gr_context_) | 123 if (gr_context_) |
| 134 return gr_context_->get(); | 124 return gr_context_->get(); |
| 135 | 125 |
| 136 gr_context_.reset(new GrContextForWebGraphicsContext3D(gr_interface_)); | 126 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get())); |
| 137 | 127 |
| 138 // If GlContext is already lost, also abandon the new GrContext. | 128 // If GlContext is already lost, also abandon the new GrContext. |
| 139 if (gr_context_->get() && | 129 if (gr_context_->get() && |
| 140 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) | 130 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
| 141 gr_context_->get()->abandonContext(); | 131 gr_context_->get()->abandonContext(); |
| 142 | 132 |
| 143 return gr_context_->get(); | 133 return gr_context_->get(); |
| 144 } | 134 } |
| 145 | 135 |
| 146 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { | 136 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { |
| 147 if (gr_context_) { | 137 if (gr_context_) { |
| 148 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 138 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 149 DCHECK(context_thread_checker_.CalledOnValidThread()); | 139 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 150 gr_context_->get()->resetContext(state); | 140 gr_context_->get()->resetContext(state); |
| 151 } | 141 } |
| 152 } | 142 } |
| 153 | 143 |
| 154 void ContextProviderCommandBuffer::SetupLock() { | 144 void ContextProviderCommandBuffer::SetupLock() { |
| 155 WebContext3D()->GetCommandBufferProxy()->SetLock(&context_lock_); | 145 DCHECK(context3d_); |
| 146 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); |
| 156 } | 147 } |
| 157 | 148 |
| 158 base::Lock* ContextProviderCommandBuffer::GetLock() { | 149 base::Lock* ContextProviderCommandBuffer::GetLock() { |
| 159 return &context_lock_; | 150 return &context_lock_; |
| 160 } | 151 } |
| 161 | 152 |
| 162 cc::ContextProvider::Capabilities | 153 cc::ContextProvider::Capabilities |
| 163 ContextProviderCommandBuffer::ContextCapabilities() { | 154 ContextProviderCommandBuffer::ContextCapabilities() { |
| 164 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 155 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
| 165 DCHECK(context_thread_checker_.CalledOnValidThread()); | 156 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 178 DCHECK(context_thread_checker_.CalledOnValidThread()); | 169 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 179 | 170 |
| 180 if (!lost_context_callback_.is_null()) | 171 if (!lost_context_callback_.is_null()) |
| 181 base::ResetAndReturn(&lost_context_callback_).Run(); | 172 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 182 if (gr_context_) | 173 if (gr_context_) |
| 183 gr_context_->OnLostContext(); | 174 gr_context_->OnLostContext(); |
| 184 } | 175 } |
| 185 | 176 |
| 186 void ContextProviderCommandBuffer::InitializeCapabilities() { | 177 void ContextProviderCommandBuffer::InitializeCapabilities() { |
| 187 Capabilities caps; | 178 Capabilities caps; |
| 188 caps.gpu = WebContext3DNoChecks()->GetImplementation()->capabilities(); | 179 caps.gpu = context3d_->GetImplementation()->capabilities(); |
| 189 | 180 |
| 190 size_t mapped_memory_limit = WebContext3DNoChecks()->GetMappedMemoryLimit(); | 181 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); |
| 191 caps.max_transfer_buffer_usage_bytes = | 182 caps.max_transfer_buffer_usage_bytes = |
| 192 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit | 183 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit |
| 193 ? std::numeric_limits<size_t>::max() : mapped_memory_limit; | 184 ? std::numeric_limits<size_t>::max() : mapped_memory_limit; |
| 194 | 185 |
| 195 capabilities_ = caps; | 186 capabilities_ = caps; |
| 196 } | 187 } |
| 197 | 188 |
| 198 void ContextProviderCommandBuffer::SetLostContextCallback( | 189 void ContextProviderCommandBuffer::SetLostContextCallback( |
| 199 const LostContextCallback& lost_context_callback) { | 190 const LostContextCallback& lost_context_callback) { |
| 200 DCHECK(context_thread_checker_.CalledOnValidThread()); | 191 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 201 DCHECK(lost_context_callback_.is_null() || | 192 DCHECK(lost_context_callback_.is_null() || |
| 202 lost_context_callback.is_null()); | 193 lost_context_callback.is_null()); |
| 203 lost_context_callback_ = lost_context_callback; | 194 lost_context_callback_ = lost_context_callback; |
| 204 } | 195 } |
| 205 | 196 |
| 206 } // namespace content | 197 } // namespace content |
| OLD | NEW |