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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "cc/output/managed_memory_policy.h" | 17 #include "cc/output/managed_memory_policy.h" |
18 #include "gpu/command_buffer/client/gles2_implementation.h" | 18 #include "gpu/command_buffer/client/gles2_implementation.h" |
19 #include "gpu/command_buffer/client/gles2_trace_implementation.h" | 19 #include "gpu/command_buffer/client/gles2_trace_implementation.h" |
20 #include "gpu/command_buffer/client/gpu_switches.h" | 20 #include "gpu/command_buffer/client/gpu_switches.h" |
21 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" | 21 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" |
22 #include "third_party/skia/include/gpu/GrContext.h" | 22 #include "third_party/skia/include/gpu/GrContext.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
| 26 ContextProviderCommandBuffer::SharedProviders::SharedProviders() = default; |
| 27 ContextProviderCommandBuffer::SharedProviders::~SharedProviders() = default; |
| 28 |
26 class ContextProviderCommandBuffer::LostContextCallbackProxy | 29 class ContextProviderCommandBuffer::LostContextCallbackProxy |
27 : public WebGraphicsContext3DCommandBufferImpl:: | 30 : public WebGraphicsContext3DCommandBufferImpl:: |
28 WebGraphicsContextLostCallback { | 31 WebGraphicsContextLostCallback { |
29 public: | 32 public: |
30 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) | 33 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) |
31 : provider_(provider) { | 34 : provider_(provider) { |
32 provider_->context3d_->SetContextLostCallback(this); | 35 provider_->context3d_->SetContextLostCallback(this); |
33 } | 36 } |
34 | 37 |
35 ~LostContextCallbackProxy() override { | 38 ~LostContextCallbackProxy() override { |
36 provider_->context3d_->SetContextLostCallback(nullptr); | 39 provider_->context3d_->SetContextLostCallback(nullptr); |
37 } | 40 } |
38 | 41 |
39 void onContextLost() override { provider_->OnLostContext(); } | 42 void onContextLost() override { provider_->OnLostContext(); } |
40 | 43 |
41 private: | 44 private: |
42 ContextProviderCommandBuffer* provider_; | 45 ContextProviderCommandBuffer* provider_; |
43 }; | 46 }; |
44 | 47 |
45 ContextProviderCommandBuffer::ContextProviderCommandBuffer( | 48 ContextProviderCommandBuffer::ContextProviderCommandBuffer( |
46 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 49 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
47 const gpu::SharedMemoryLimits& memory_limits, | 50 const gpu::SharedMemoryLimits& memory_limits, |
| 51 ContextProviderCommandBuffer* shared_context_provider, |
48 CommandBufferContextType type) | 52 CommandBufferContextType type) |
49 : context3d_(std::move(context3d)), | 53 : shared_providers_(shared_context_provider |
| 54 ? shared_context_provider->shared_providers_ |
| 55 : new SharedProviders), |
| 56 context3d_(std::move(context3d)), |
50 memory_limits_(memory_limits), | 57 memory_limits_(memory_limits), |
51 context_type_(type), | 58 context_type_(type), |
52 debug_name_(CommandBufferContextTypeToString(type)) { | 59 debug_name_(CommandBufferContextTypeToString(type)) { |
53 DCHECK(main_thread_checker_.CalledOnValidThread()); | 60 DCHECK(main_thread_checker_.CalledOnValidThread()); |
54 DCHECK(context3d_); | 61 DCHECK(context3d_); |
55 context_thread_checker_.DetachFromThread(); | 62 context_thread_checker_.DetachFromThread(); |
56 } | 63 } |
57 | 64 |
58 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { | 65 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { |
59 DCHECK(main_thread_checker_.CalledOnValidThread() || | 66 DCHECK(main_thread_checker_.CalledOnValidThread() || |
60 context_thread_checker_.CalledOnValidThread()); | 67 context_thread_checker_.CalledOnValidThread()); |
61 | 68 |
| 69 { |
| 70 base::AutoLock hold(shared_providers_->lock); |
| 71 auto it = std::find(shared_providers_->list.begin(), |
| 72 shared_providers_->list.end(), this); |
| 73 if (it != shared_providers_->list.end()) |
| 74 shared_providers_->list.erase(it); |
| 75 } |
| 76 |
62 // Destroy references to the context3d_ before leaking it. | 77 // Destroy references to the context3d_ before leaking it. |
63 // TODO(danakj): Delete this. | 78 // TODO(danakj): Delete this. |
64 if (context3d_->GetCommandBufferProxy()) | 79 if (context3d_->GetCommandBufferProxy()) |
65 context3d_->GetCommandBufferProxy()->SetLock(nullptr); | 80 context3d_->GetCommandBufferProxy()->SetLock(nullptr); |
66 | 81 |
67 if (lost_context_callback_proxy_) { | 82 if (lost_context_callback_proxy_) { |
68 // Disconnect lost callbacks during destruction. | 83 // Disconnect lost callbacks during destruction. |
69 lost_context_callback_proxy_.reset(); | 84 lost_context_callback_proxy_.reset(); |
70 } | 85 } |
71 } | 86 } |
(...skipping 12 matching lines...) Expand all Loading... |
84 } | 99 } |
85 | 100 |
86 bool ContextProviderCommandBuffer::BindToCurrentThread() { | 101 bool ContextProviderCommandBuffer::BindToCurrentThread() { |
87 // This is called on the thread the context will be used. | 102 // This is called on the thread the context will be used. |
88 DCHECK(context_thread_checker_.CalledOnValidThread()); | 103 DCHECK(context_thread_checker_.CalledOnValidThread()); |
89 | 104 |
90 if (lost_context_callback_proxy_) | 105 if (lost_context_callback_proxy_) |
91 return true; | 106 return true; |
92 | 107 |
93 context3d_->SetContextType(context_type_); | 108 context3d_->SetContextType(context_type_); |
94 if (!context3d_->InitializeOnCurrentThread(memory_limits_)) | 109 |
95 return false; | 110 // It's possible to be running BindToCurrentThread on two contexts |
| 111 // on different threads at the same time, but which will be in the same share |
| 112 // group. To ensure they end up in the same group, hold the lock on the |
| 113 // shared_providers_ (which they will share) after querying the group, until |
| 114 // this context has been added to the list. |
| 115 { |
| 116 ContextProviderCommandBuffer* shared_context_provider = nullptr; |
| 117 gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr; |
| 118 scoped_refptr<gpu::gles2::ShareGroup> share_group; |
| 119 |
| 120 base::AutoLock hold(shared_providers_->lock); |
| 121 |
| 122 if (!shared_providers_->list.empty()) { |
| 123 shared_context_provider = shared_providers_->list.front(); |
| 124 shared_command_buffer = |
| 125 shared_context_provider->context3d_->GetCommandBufferProxy(); |
| 126 share_group = shared_context_provider->context3d_->GetImplementation() |
| 127 ->share_group(); |
| 128 } |
| 129 |
| 130 if (!context3d_->InitializeOnCurrentThread( |
| 131 memory_limits_, shared_command_buffer, std::move(share_group))) |
| 132 return false; |
| 133 |
| 134 // If any context in the share group has been lost, then abort and don't |
| 135 // continue since we need to go back to the caller of the constructor to |
| 136 // find the correct share group. |
| 137 // This may happen in between the share group being chosen at the |
| 138 // constructor, and getting to run this BindToCurrentThread method which |
| 139 // can be on some other thread. |
| 140 // We intentionally call this *after* creating the command buffer via the |
| 141 // GpuChannelHost. Once that has happened, the service knows we are in the |
| 142 // share group and if a shared context is lost, our context will be informed |
| 143 // also, and the lost context callback will occur for the owner of the |
| 144 // context provider. If we check sooner, the shared context may be lost in |
| 145 // between these two states and our context here would be left in an orphan |
| 146 // share group. |
| 147 if (share_group && share_group->IsLost()) |
| 148 return false; |
| 149 |
| 150 shared_providers_->list.push_back(this); |
| 151 } |
| 152 |
96 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | 153 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
97 | 154 |
98 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 155 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
99 switches::kEnableGpuClientTracing)) { | 156 switches::kEnableGpuClientTracing)) { |
100 // This wraps the real GLES2Implementation and we should always use this | 157 // This wraps the real GLES2Implementation and we should always use this |
101 // instead when it's present. | 158 // instead when it's present. |
102 trace_impl_.reset(new gpu::gles2::GLES2TraceImplementation( | 159 trace_impl_.reset(new gpu::gles2::GLES2TraceImplementation( |
103 context3d_->GetImplementation())); | 160 context3d_->GetImplementation())); |
104 } | 161 } |
105 | 162 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 244 |
188 void ContextProviderCommandBuffer::SetLostContextCallback( | 245 void ContextProviderCommandBuffer::SetLostContextCallback( |
189 const LostContextCallback& lost_context_callback) { | 246 const LostContextCallback& lost_context_callback) { |
190 DCHECK(context_thread_checker_.CalledOnValidThread()); | 247 DCHECK(context_thread_checker_.CalledOnValidThread()); |
191 DCHECK(lost_context_callback_.is_null() || | 248 DCHECK(lost_context_callback_.is_null() || |
192 lost_context_callback.is_null()); | 249 lost_context_callback.is_null()); |
193 lost_context_callback_ = lost_context_callback; | 250 lost_context_callback_ = lost_context_callback; |
194 } | 251 } |
195 | 252 |
196 } // namespace content | 253 } // namespace content |
OLD | NEW |