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" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 return new ContextProviderCommandBuffer(context3d.Pass(), type); | 46 return new ContextProviderCommandBuffer(context3d.Pass(), type); |
47 } | 47 } |
48 | 48 |
49 ContextProviderCommandBuffer::ContextProviderCommandBuffer( | 49 ContextProviderCommandBuffer::ContextProviderCommandBuffer( |
50 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 50 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
51 CommandBufferContextType type) | 51 CommandBufferContextType type) |
52 : context3d_(context3d.Pass()), | 52 : context3d_(context3d.Pass()), |
53 context_type_(type), | 53 context_type_(type), |
54 debug_name_(CommandBufferContextTypeToString(type)), | 54 debug_name_(CommandBufferContextTypeToString(type)), |
| 55 lost_(false), |
55 destroyed_(false) { | 56 destroyed_(false) { |
56 DCHECK(main_thread_checker_.CalledOnValidThread()); | 57 DCHECK(main_thread_checker_.CalledOnValidThread()); |
57 DCHECK(context3d_); | 58 DCHECK(context3d_); |
58 context_thread_checker_.DetachFromThread(); | 59 context_thread_checker_.DetachFromThread(); |
59 } | 60 } |
60 | 61 |
61 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { | 62 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { |
62 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
63 context_thread_checker_.CalledOnValidThread()); | |
64 | |
65 base::AutoLock lock(main_thread_lock_); | 63 base::AutoLock lock(main_thread_lock_); |
66 | 64 DestroyWithLockAcquired(); |
67 // Destroy references to the context3d_ before leaking it. | |
68 if (context3d_->GetCommandBufferProxy()) { | |
69 context3d_->GetCommandBufferProxy()->SetLock(nullptr); | |
70 context3d_->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback( | |
71 CommandBufferProxyImpl::MemoryAllocationChangedCallback()); | |
72 } | |
73 lost_context_callback_proxy_.reset(); | |
74 } | 65 } |
75 | 66 |
76 | 67 |
77 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() { | 68 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() { |
78 return context3d_->GetCommandBufferProxy(); | 69 return context3d_->GetCommandBufferProxy(); |
79 } | 70 } |
80 | 71 |
| 72 void ContextProviderCommandBuffer::Destroy() { |
| 73 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 74 DCHECK(context3d_); |
| 75 // Verify that SetupLock() has been called. |
| 76 DCHECK(context3d_->GetCommandBufferProxy()->lock()); |
| 77 DestroyWithLockAcquired(); |
| 78 } |
| 79 |
81 WebGraphicsContext3DCommandBufferImpl* | 80 WebGraphicsContext3DCommandBufferImpl* |
82 ContextProviderCommandBuffer::WebContext3D() { | 81 ContextProviderCommandBuffer::WebContext3D() { |
83 DCHECK(context3d_); | 82 DCHECK(context3d_); |
84 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 83 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
85 DCHECK(context_thread_checker_.CalledOnValidThread()); | 84 DCHECK(context_thread_checker_.CalledOnValidThread()); |
86 | 85 |
87 return context3d_.get(); | 86 return context3d_.get(); |
88 } | 87 } |
89 | 88 |
90 bool ContextProviderCommandBuffer::BindToCurrentThread() { | 89 bool ContextProviderCommandBuffer::BindToCurrentThread() { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { | 143 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { |
145 if (gr_context_) { | 144 if (gr_context_) { |
146 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 145 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
147 DCHECK(context_thread_checker_.CalledOnValidThread()); | 146 DCHECK(context_thread_checker_.CalledOnValidThread()); |
148 gr_context_->get()->resetContext(state); | 147 gr_context_->get()->resetContext(state); |
149 } | 148 } |
150 } | 149 } |
151 | 150 |
152 void ContextProviderCommandBuffer::SetupLock() { | 151 void ContextProviderCommandBuffer::SetupLock() { |
153 DCHECK(context3d_); | 152 DCHECK(context3d_); |
| 153 DCHECK(!context3d_->GetCommandBufferProxy()->lock()); |
154 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); | 154 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); |
155 } | 155 } |
156 | 156 |
157 base::Lock* ContextProviderCommandBuffer::GetLock() { | 157 base::Lock* ContextProviderCommandBuffer::GetLock() { |
158 return &context_lock_; | 158 return &context_lock_; |
159 } | 159 } |
160 | 160 |
161 cc::ContextProvider::Capabilities | 161 cc::ContextProvider::Capabilities |
162 ContextProviderCommandBuffer::ContextCapabilities() { | 162 ContextProviderCommandBuffer::ContextCapabilities() { |
163 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 163 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
(...skipping 14 matching lines...) Expand all Loading... |
178 DCHECK(context_thread_checker_.CalledOnValidThread()); | 178 DCHECK(context_thread_checker_.CalledOnValidThread()); |
179 | 179 |
180 if (gr_context_) | 180 if (gr_context_) |
181 gr_context_->FreeGpuResources(); | 181 gr_context_->FreeGpuResources(); |
182 } | 182 } |
183 | 183 |
184 void ContextProviderCommandBuffer::OnLostContext() { | 184 void ContextProviderCommandBuffer::OnLostContext() { |
185 DCHECK(context_thread_checker_.CalledOnValidThread()); | 185 DCHECK(context_thread_checker_.CalledOnValidThread()); |
186 { | 186 { |
187 base::AutoLock lock(main_thread_lock_); | 187 base::AutoLock lock(main_thread_lock_); |
188 if (destroyed_) | 188 if (lost_) |
189 return; | 189 return; |
190 destroyed_ = true; | 190 lost_ = true; |
191 } | 191 } |
192 if (!lost_context_callback_.is_null()) | 192 if (!lost_context_callback_.is_null()) |
193 base::ResetAndReturn(&lost_context_callback_).Run(); | 193 base::ResetAndReturn(&lost_context_callback_).Run(); |
194 if (gr_context_) | 194 if (gr_context_) |
195 gr_context_->OnLostContext(); | 195 gr_context_->OnLostContext(); |
196 } | 196 } |
197 | 197 |
198 void ContextProviderCommandBuffer::OnMemoryAllocationChanged( | 198 void ContextProviderCommandBuffer::OnMemoryAllocationChanged( |
199 const gpu::MemoryAllocation& allocation) { | 199 const gpu::MemoryAllocation& allocation) { |
200 DCHECK(context_thread_checker_.CalledOnValidThread()); | 200 DCHECK(context_thread_checker_.CalledOnValidThread()); |
201 | 201 |
202 if (memory_policy_changed_callback_.is_null()) | 202 if (memory_policy_changed_callback_.is_null()) |
203 return; | 203 return; |
204 | 204 |
205 memory_policy_changed_callback_.Run(cc::ManagedMemoryPolicy(allocation)); | 205 memory_policy_changed_callback_.Run(cc::ManagedMemoryPolicy(allocation)); |
206 } | 206 } |
207 | 207 |
208 void ContextProviderCommandBuffer::InitializeCapabilities() { | 208 void ContextProviderCommandBuffer::InitializeCapabilities() { |
209 Capabilities caps; | 209 Capabilities caps; |
210 caps.gpu = context3d_->GetImplementation()->capabilities(); | 210 caps.gpu = context3d_->GetImplementation()->capabilities(); |
211 | 211 |
212 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); | 212 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); |
213 caps.max_transfer_buffer_usage_bytes = | 213 caps.max_transfer_buffer_usage_bytes = |
214 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit | 214 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit |
215 ? std::numeric_limits<size_t>::max() : mapped_memory_limit; | 215 ? std::numeric_limits<size_t>::max() : mapped_memory_limit; |
216 | 216 |
217 capabilities_ = caps; | 217 capabilities_ = caps; |
218 } | 218 } |
219 | 219 |
220 bool ContextProviderCommandBuffer::DestroyedOnMainThread() { | 220 bool ContextProviderCommandBuffer::HasBeenLostOnMainThread() { |
221 DCHECK(main_thread_checker_.CalledOnValidThread()); | 221 DCHECK(main_thread_checker_.CalledOnValidThread()); |
222 | 222 |
223 base::AutoLock lock(main_thread_lock_); | 223 base::AutoLock lock(main_thread_lock_); |
224 return destroyed_; | 224 return lost_; |
225 } | 225 } |
226 | 226 |
227 void ContextProviderCommandBuffer::SetLostContextCallback( | 227 void ContextProviderCommandBuffer::SetLostContextCallback( |
228 const LostContextCallback& lost_context_callback) { | 228 const LostContextCallback& lost_context_callback) { |
229 DCHECK(context_thread_checker_.CalledOnValidThread()); | 229 DCHECK(context_thread_checker_.CalledOnValidThread()); |
230 DCHECK(lost_context_callback_.is_null() || | 230 DCHECK(lost_context_callback_.is_null() || |
231 lost_context_callback.is_null()); | 231 lost_context_callback.is_null()); |
232 lost_context_callback_ = lost_context_callback; | 232 lost_context_callback_ = lost_context_callback; |
233 } | 233 } |
234 | 234 |
235 void ContextProviderCommandBuffer::SetMemoryPolicyChangedCallback( | 235 void ContextProviderCommandBuffer::SetMemoryPolicyChangedCallback( |
236 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | 236 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
237 DCHECK(context_thread_checker_.CalledOnValidThread()); | 237 DCHECK(context_thread_checker_.CalledOnValidThread()); |
238 DCHECK(memory_policy_changed_callback_.is_null() || | 238 DCHECK(memory_policy_changed_callback_.is_null() || |
239 memory_policy_changed_callback.is_null()); | 239 memory_policy_changed_callback.is_null()); |
240 memory_policy_changed_callback_ = memory_policy_changed_callback; | 240 memory_policy_changed_callback_ = memory_policy_changed_callback; |
241 | 241 |
242 if (!memory_policy_changed_callback_.is_null()) { | 242 if (!memory_policy_changed_callback_.is_null()) { |
243 DCHECK(context3d_->GetCommandBufferProxy()); | 243 DCHECK(context3d_->GetCommandBufferProxy()); |
244 context3d_->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback( | 244 context3d_->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback( |
245 base::Bind(&ContextProviderCommandBuffer::OnMemoryAllocationChanged, | 245 base::Bind(&ContextProviderCommandBuffer::OnMemoryAllocationChanged, |
246 base::Unretained(this))); | 246 base::Unretained(this))); |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
| 250 bool ContextProviderCommandBuffer::HasBeenDestroyed() { |
| 251 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 252 return destroyed_; |
| 253 } |
| 254 |
| 255 void ContextProviderCommandBuffer::DestroyWithLockAcquired() { |
| 256 if (context3d_ && context3d_->GetCommandBufferProxy()) { |
| 257 context3d_->GetCommandBufferProxy()->SetLock(nullptr); |
| 258 context3d_->GetCommandBufferProxy()->SetMemoryAllocationChangedCallback( |
| 259 CommandBufferProxyImpl::MemoryAllocationChangedCallback()); |
| 260 } |
| 261 lost_context_callback_proxy_.reset(); |
| 262 gr_context_.reset(); |
| 263 context3d_.reset(); |
| 264 destroyed_ = true; |
| 265 } |
| 266 |
250 } // namespace content | 267 } // namespace content |
OLD | NEW |