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/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "cc/output/managed_memory_policy.h" | 17 #include "cc/output/managed_memory_policy.h" |
17 #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" |
| 20 #include "gpu/command_buffer/client/gpu_switches.h" |
18 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" | 21 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" |
19 #include "third_party/skia/include/gpu/GrContext.h" | 22 #include "third_party/skia/include/gpu/GrContext.h" |
20 | 23 |
21 namespace content { | 24 namespace content { |
22 | 25 |
23 class ContextProviderCommandBuffer::LostContextCallbackProxy | 26 class ContextProviderCommandBuffer::LostContextCallbackProxy |
24 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { | 27 : public WebGraphicsContext3DCommandBufferImpl:: |
| 28 WebGraphicsContextLostCallback { |
25 public: | 29 public: |
26 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) | 30 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) |
27 : provider_(provider) { | 31 : provider_(provider) { |
28 provider_->context3d_->setContextLostCallback(this); | 32 provider_->context3d_->SetContextLostCallback(this); |
29 } | 33 } |
30 | 34 |
31 ~LostContextCallbackProxy() override { | 35 ~LostContextCallbackProxy() override { |
32 provider_->context3d_->setContextLostCallback(NULL); | 36 provider_->context3d_->SetContextLostCallback(nullptr); |
33 } | 37 } |
34 | 38 |
35 void onContextLost() override { provider_->OnLostContext(); } | 39 void onContextLost() override { provider_->OnLostContext(); } |
36 | 40 |
37 private: | 41 private: |
38 ContextProviderCommandBuffer* provider_; | 42 ContextProviderCommandBuffer* provider_; |
39 }; | 43 }; |
40 | 44 |
41 ContextProviderCommandBuffer::ContextProviderCommandBuffer( | 45 ContextProviderCommandBuffer::ContextProviderCommandBuffer( |
42 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 46 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
43 const gpu::SharedMemoryLimits& memory_limits, | 47 const gpu::SharedMemoryLimits& memory_limits, |
44 CommandBufferContextType type) | 48 CommandBufferContextType type) |
45 : context3d_(std::move(context3d)), | 49 : context3d_(std::move(context3d)), |
46 memory_limits_(memory_limits), | 50 memory_limits_(memory_limits), |
47 context_type_(type), | 51 context_type_(type), |
48 debug_name_(CommandBufferContextTypeToString(type)) { | 52 debug_name_(CommandBufferContextTypeToString(type)) { |
49 DCHECK(main_thread_checker_.CalledOnValidThread()); | 53 DCHECK(main_thread_checker_.CalledOnValidThread()); |
50 DCHECK(context3d_); | 54 DCHECK(context3d_); |
51 context_thread_checker_.DetachFromThread(); | 55 context_thread_checker_.DetachFromThread(); |
52 } | 56 } |
53 | 57 |
54 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { | 58 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { |
55 DCHECK(main_thread_checker_.CalledOnValidThread() || | 59 DCHECK(main_thread_checker_.CalledOnValidThread() || |
56 context_thread_checker_.CalledOnValidThread()); | 60 context_thread_checker_.CalledOnValidThread()); |
57 | 61 |
58 // Destroy references to the context3d_ before leaking it. | 62 // Destroy references to the context3d_ before leaking it. |
| 63 // TODO(danakj): Delete this. |
59 if (context3d_->GetCommandBufferProxy()) | 64 if (context3d_->GetCommandBufferProxy()) |
60 context3d_->GetCommandBufferProxy()->SetLock(nullptr); | 65 context3d_->GetCommandBufferProxy()->SetLock(nullptr); |
61 lost_context_callback_proxy_.reset(); | 66 |
| 67 if (lost_context_callback_proxy_) { |
| 68 // Disconnect lost callbacks during destruction. |
| 69 lost_context_callback_proxy_.reset(); |
| 70 } |
62 } | 71 } |
63 | 72 |
64 gpu::CommandBufferProxyImpl* | 73 gpu::CommandBufferProxyImpl* |
65 ContextProviderCommandBuffer::GetCommandBufferProxy() { | 74 ContextProviderCommandBuffer::GetCommandBufferProxy() { |
66 return context3d_->GetCommandBufferProxy(); | 75 return context3d_->GetCommandBufferProxy(); |
67 } | 76 } |
68 | 77 |
69 WebGraphicsContext3DCommandBufferImpl* | 78 WebGraphicsContext3DCommandBufferImpl* |
70 ContextProviderCommandBuffer::WebContext3D() { | 79 ContextProviderCommandBuffer::WebContext3D() { |
71 DCHECK(context3d_); | 80 DCHECK(context3d_); |
72 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 81 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
73 DCHECK(context_thread_checker_.CalledOnValidThread()); | 82 DCHECK(context_thread_checker_.CalledOnValidThread()); |
74 | |
75 return context3d_.get(); | 83 return context3d_.get(); |
76 } | 84 } |
77 | 85 |
78 bool ContextProviderCommandBuffer::BindToCurrentThread() { | 86 bool ContextProviderCommandBuffer::BindToCurrentThread() { |
79 // This is called on the thread the context will be used. | 87 // This is called on the thread the context will be used. |
80 DCHECK(context_thread_checker_.CalledOnValidThread()); | 88 DCHECK(context_thread_checker_.CalledOnValidThread()); |
81 | 89 |
82 if (lost_context_callback_proxy_) | 90 if (lost_context_callback_proxy_) |
83 return true; | 91 return true; |
84 | 92 |
85 context3d_->SetContextType(context_type_); | 93 context3d_->SetContextType(context_type_); |
86 if (!context3d_->InitializeOnCurrentThread(memory_limits_)) | 94 if (!context3d_->InitializeOnCurrentThread(memory_limits_)) |
87 return false; | 95 return false; |
| 96 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
88 | 97 |
| 98 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 99 switches::kEnableGpuClientTracing)) { |
| 100 // This wraps the real GLES2Implementation and we should always use this |
| 101 // instead when it's present. |
| 102 trace_impl_.reset(new gpu::gles2::GLES2TraceImplementation( |
| 103 context3d_->GetImplementation())); |
| 104 } |
| 105 |
| 106 // Do this last once the context is set up. |
89 std::string unique_context_name = | 107 std::string unique_context_name = |
90 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); | 108 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); |
91 context3d_->GetGLInterface()->TraceBeginCHROMIUM("gpu_toplevel", | 109 ContextGL()->TraceBeginCHROMIUM("gpu_toplevel", unique_context_name.c_str()); |
92 unique_context_name.c_str()); | |
93 | |
94 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
95 return true; | 110 return true; |
96 } | 111 } |
97 | 112 |
98 void ContextProviderCommandBuffer::DetachFromThread() { | 113 void ContextProviderCommandBuffer::DetachFromThread() { |
99 context_thread_checker_.DetachFromThread(); | 114 context_thread_checker_.DetachFromThread(); |
100 } | 115 } |
101 | 116 |
102 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { | 117 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { |
103 DCHECK(context3d_); | 118 DCHECK(context3d_); |
104 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 119 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
105 DCHECK(context_thread_checker_.CalledOnValidThread()); | 120 DCHECK(context_thread_checker_.CalledOnValidThread()); |
106 | 121 |
| 122 if (trace_impl_) |
| 123 return trace_impl_.get(); |
107 return context3d_->GetImplementation(); | 124 return context3d_->GetImplementation(); |
108 } | 125 } |
109 | 126 |
110 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { | 127 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { |
111 return context3d_->GetContextSupport(); | 128 return context3d_->GetContextSupport(); |
112 } | 129 } |
113 | 130 |
114 class GrContext* ContextProviderCommandBuffer::GrContext() { | 131 class GrContext* ContextProviderCommandBuffer::GrContext() { |
115 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 132 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
116 DCHECK(context_thread_checker_.CalledOnValidThread()); | 133 DCHECK(context_thread_checker_.CalledOnValidThread()); |
117 | 134 |
118 if (gr_context_) | 135 if (gr_context_) |
119 return gr_context_->get(); | 136 return gr_context_->get(); |
120 | 137 |
121 gr_context_.reset(new skia_bindings::GrContextForGLES2Interface( | 138 gr_context_.reset(new skia_bindings::GrContextForGLES2Interface(ContextGL())); |
122 context3d_->GetGLInterface())); | |
123 | 139 |
124 // If GlContext is already lost, also abandon the new GrContext. | 140 // If GlContext is already lost, also abandon the new GrContext. |
125 if (gr_context_->get() && | 141 if (gr_context_->get() && |
126 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) | 142 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
127 gr_context_->get()->abandonContext(); | 143 gr_context_->get()->abandonContext(); |
128 | 144 |
129 return gr_context_->get(); | 145 return gr_context_->get(); |
130 } | 146 } |
131 | 147 |
132 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { | 148 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { |
133 if (gr_context_) { | 149 if (gr_context_) { |
134 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 150 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
135 DCHECK(context_thread_checker_.CalledOnValidThread()); | 151 DCHECK(context_thread_checker_.CalledOnValidThread()); |
136 gr_context_->ResetContext(state); | 152 gr_context_->ResetContext(state); |
137 } | 153 } |
138 } | 154 } |
139 | 155 |
140 void ContextProviderCommandBuffer::SetupLock() { | 156 void ContextProviderCommandBuffer::SetupLock() { |
141 DCHECK(context3d_); | 157 DCHECK(context3d_); |
142 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); | 158 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); |
143 } | 159 } |
144 | 160 |
145 base::Lock* ContextProviderCommandBuffer::GetLock() { | 161 base::Lock* ContextProviderCommandBuffer::GetLock() { |
146 return &context_lock_; | 162 return &context_lock_; |
147 } | 163 } |
148 | 164 |
149 gpu::Capabilities ContextProviderCommandBuffer::ContextCapabilities() { | 165 gpu::Capabilities ContextProviderCommandBuffer::ContextCapabilities() { |
150 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 166 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
151 DCHECK(context_thread_checker_.CalledOnValidThread()); | 167 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 168 // Skips past the trace_impl_ as it doesn't have capabilities. |
152 return context3d_->GetImplementation()->capabilities(); | 169 return context3d_->GetImplementation()->capabilities(); |
153 } | 170 } |
154 | 171 |
155 void ContextProviderCommandBuffer::DeleteCachedResources() { | 172 void ContextProviderCommandBuffer::DeleteCachedResources() { |
156 DCHECK(context_thread_checker_.CalledOnValidThread()); | 173 DCHECK(context_thread_checker_.CalledOnValidThread()); |
157 | 174 |
158 if (gr_context_) | 175 if (gr_context_) |
159 gr_context_->FreeGpuResources(); | 176 gr_context_->FreeGpuResources(); |
160 } | 177 } |
161 | 178 |
162 void ContextProviderCommandBuffer::OnLostContext() { | 179 void ContextProviderCommandBuffer::OnLostContext() { |
163 DCHECK(context_thread_checker_.CalledOnValidThread()); | 180 DCHECK(context_thread_checker_.CalledOnValidThread()); |
164 | 181 |
165 if (!lost_context_callback_.is_null()) | 182 if (!lost_context_callback_.is_null()) |
166 lost_context_callback_.Run(); | 183 lost_context_callback_.Run(); |
167 if (gr_context_) | 184 if (gr_context_) |
168 gr_context_->OnLostContext(); | 185 gr_context_->OnLostContext(); |
169 } | 186 } |
170 | 187 |
171 void ContextProviderCommandBuffer::SetLostContextCallback( | 188 void ContextProviderCommandBuffer::SetLostContextCallback( |
172 const LostContextCallback& lost_context_callback) { | 189 const LostContextCallback& lost_context_callback) { |
173 DCHECK(context_thread_checker_.CalledOnValidThread()); | 190 DCHECK(context_thread_checker_.CalledOnValidThread()); |
174 DCHECK(lost_context_callback_.is_null() || | 191 DCHECK(lost_context_callback_.is_null() || |
175 lost_context_callback.is_null()); | 192 lost_context_callback.is_null()); |
176 lost_context_callback_ = lost_context_callback; | 193 lost_context_callback_ = lost_context_callback; |
177 } | 194 } |
178 | 195 |
179 } // namespace content | 196 } // namespace content |
OLD | NEW |