OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser/renderer_host/context_provider_factory_impl_android.h" | 5 #include "content/browser/renderer_host/context_provider_factory_impl_android.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 case ui::ContextProviderFactory::ContextType:: | 32 case ui::ContextProviderFactory::ContextType:: |
33 BLIMP_RENDER_COMPOSITOR_CONTEXT: | 33 BLIMP_RENDER_COMPOSITOR_CONTEXT: |
34 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; | 34 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; |
35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: | 35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: |
36 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; | 36 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; |
37 } | 37 } |
38 NOTREACHED(); | 38 NOTREACHED(); |
39 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; | 39 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; |
40 } | 40 } |
41 | 41 |
| 42 bool IsGpuChannelHostFactoryAvailable() { |
| 43 return BrowserGpuChannelHostFactory::instance() != nullptr; |
| 44 } |
| 45 |
| 46 gpu::GpuChannelHost* GetGpuChannel() { |
| 47 BrowserGpuChannelHostFactory* factory = |
| 48 BrowserGpuChannelHostFactory::instance(); |
| 49 DCHECK(factory); |
| 50 |
| 51 return factory->GetGpuChannel(); |
| 52 } |
| 53 |
42 } // namespace | 54 } // namespace |
43 | 55 |
44 // static | 56 // static |
45 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { | 57 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { |
46 return base::Singleton<ContextProviderFactoryImpl>::get(); | 58 return base::Singleton<ContextProviderFactoryImpl>::get(); |
47 } | 59 } |
48 | 60 |
49 ContextProviderFactoryImpl::ContextProviderFactoryImpl() | 61 ContextProviderFactoryImpl::ContextProviderFactoryImpl() |
50 : in_handle_pending_requests_(false), | 62 : in_handle_pending_requests_(false), |
51 surface_client_id_(0), | 63 surface_client_id_(0), |
52 weak_factory_(this) {} | 64 weak_factory_(this) {} |
53 | 65 |
54 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {} | 66 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() { |
| 67 if (!context_provider_requests_.empty()) |
| 68 HandlePendingRequests(nullptr, |
| 69 ContextCreationResult::FAILURE_BROWSER_SHUTDOWN); |
| 70 } |
55 | 71 |
56 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() | 72 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() |
57 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), | 73 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), |
58 surface_handle(gpu::kNullSurfaceHandle), | 74 surface_handle(gpu::kNullSurfaceHandle), |
59 support_locking(false), | 75 support_locking(false), |
60 automatic_flushes(false), | 76 automatic_flushes(false), |
61 shared_context_provider(nullptr) {} | 77 shared_context_provider(nullptr) {} |
62 | 78 |
63 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( | 79 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( |
64 const ContextProvidersRequest& other) = default; | 80 const ContextProvidersRequest& other) = default; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 context_request.context_type = context_type; | 154 context_request.context_type = context_type; |
139 context_request.surface_handle = surface_handle; | 155 context_request.surface_handle = surface_handle; |
140 context_request.shared_memory_limits = shared_memory_limits; | 156 context_request.shared_memory_limits = shared_memory_limits; |
141 context_request.attributes = attributes; | 157 context_request.attributes = attributes; |
142 context_request.support_locking = support_locking; | 158 context_request.support_locking = support_locking; |
143 context_request.automatic_flushes = automatic_flushes; | 159 context_request.automatic_flushes = automatic_flushes; |
144 context_request.shared_context_provider = shared_context_provider; | 160 context_request.shared_context_provider = shared_context_provider; |
145 context_request.result_callback = result_callback; | 161 context_request.result_callback = result_callback; |
146 | 162 |
147 context_provider_requests_.push_back(context_request); | 163 context_provider_requests_.push_back(context_request); |
148 HandlePendingRequests(); | 164 CheckCanHandlePendingRequests(); |
149 } | 165 } |
150 | 166 |
151 void ContextProviderFactoryImpl::HandlePendingRequests() { | 167 void ContextProviderFactoryImpl::CheckCanHandlePendingRequests() { |
| 168 DCHECK(!context_provider_requests_.empty()) |
| 169 << "We don't have any pending requests?"; |
| 170 if (!IsGpuChannelHostFactoryAvailable()) { |
| 171 HandlePendingRequests(nullptr, |
| 172 ContextCreationResult::FAILURE_BROWSER_SHUTDOWN); |
| 173 return; |
| 174 } |
| 175 |
| 176 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(GetGpuChannel()); |
| 177 if (gpu_channel_host) { |
| 178 HandlePendingRequests(gpu_channel_host, ContextCreationResult::SUCCESS); |
| 179 } else { |
| 180 EstablishGpuChannel(); |
| 181 } |
| 182 } |
| 183 |
| 184 void ContextProviderFactoryImpl::HandlePendingRequests( |
| 185 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host, |
| 186 ContextCreationResult result) { |
152 DCHECK(!context_provider_requests_.empty()) | 187 DCHECK(!context_provider_requests_.empty()) |
153 << "We don't have any pending requests?"; | 188 << "We don't have any pending requests?"; |
154 | 189 |
155 // Failure to initialize the context could result in new requests. Handle | 190 // Failure to initialize the context could result in new requests. Handle |
156 // them after going through the current list. | 191 // them after going through the current list. |
157 if (in_handle_pending_requests_) | 192 if (in_handle_pending_requests_) |
158 return; | 193 return; |
159 | 194 |
160 { | 195 { |
161 base::AutoReset<bool> auto_reset_in_handle_requests( | 196 base::AutoReset<bool> auto_reset_in_handle_requests( |
162 &in_handle_pending_requests_, true); | 197 &in_handle_pending_requests_, true); |
163 | 198 |
164 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host( | |
165 EnsureGpuChannelEstablished()); | |
166 | |
167 // If we don't have a Gpu Channel Host, we will come back here when the Gpu | |
168 // channel is established, since OnGpuChannelEstablished triggers handling | |
169 // of the requests we couldn't process right now. | |
170 if (!gpu_channel_host) | |
171 return; | |
172 | |
173 std::list<ContextProvidersRequest> context_requests = | 199 std::list<ContextProvidersRequest> context_requests = |
174 context_provider_requests_; | 200 context_provider_requests_; |
175 context_provider_requests_.clear(); | 201 context_provider_requests_.clear(); |
176 | 202 |
177 for (ContextProvidersRequest& context_request : context_requests) { | 203 for (ContextProvidersRequest& context_request : context_requests) { |
178 scoped_refptr<cc::ContextProvider> context_provider; | 204 scoped_refptr<cc::ContextProvider> context_provider; |
179 | 205 |
180 const bool create_onscreen_context = | 206 const bool create_onscreen_context = |
181 context_request.surface_handle != gpu::kNullSurfaceHandle; | 207 context_request.surface_handle != gpu::kNullSurfaceHandle; |
182 | 208 |
183 // Is the request for an onscreen context? Make sure the surface is | 209 // Is the request for an onscreen context? Make sure the surface is |
184 // still valid in that case. DO NOT run the callback if we don't have a | 210 // still valid in that case. |
185 // valid surface. | |
186 if (create_onscreen_context && | 211 if (create_onscreen_context && |
187 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle( | 212 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle( |
188 context_request.surface_handle)) { | 213 context_request.surface_handle)) { |
189 continue; | 214 // GPU Surface handle loss trumps other context creation failure |
| 215 // reasons. |
| 216 context_request.result_callback.Run( |
| 217 nullptr, ContextCreationResult::FAILURE_GPU_SURFACE_HANDLE_LOST); |
| 218 } else if (!gpu_channel_host) { |
| 219 context_request.result_callback.Run(nullptr, result); |
| 220 } else { |
| 221 DCHECK_EQ(ContextCreationResult::SUCCESS, result); |
| 222 |
| 223 context_provider = new ContextProviderCommandBuffer( |
| 224 gpu_channel_host, gpu::GPU_STREAM_DEFAULT, |
| 225 gpu::GpuStreamPriority::NORMAL, context_request.surface_handle, |
| 226 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") + |
| 227 std::string("CompositorContextProvider")), |
| 228 context_request.automatic_flushes, context_request.support_locking, |
| 229 context_request.shared_memory_limits, context_request.attributes, |
| 230 static_cast<ContextProviderCommandBuffer*>( |
| 231 context_request.shared_context_provider), |
| 232 context_request.context_type); |
| 233 context_request.result_callback.Run(context_provider, result); |
190 } | 234 } |
191 | |
192 context_provider = new ContextProviderCommandBuffer( | |
193 gpu_channel_host, gpu::GPU_STREAM_DEFAULT, | |
194 gpu::GpuStreamPriority::NORMAL, context_request.surface_handle, | |
195 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") + | |
196 std::string("CompositorContextProvider")), | |
197 context_request.automatic_flushes, context_request.support_locking, | |
198 context_request.shared_memory_limits, context_request.attributes, | |
199 static_cast<ContextProviderCommandBuffer*>( | |
200 context_request.shared_context_provider), | |
201 context_request.context_type); | |
202 context_request.result_callback.Run(context_provider); | |
203 } | 235 } |
204 } | 236 } |
205 | 237 |
206 if (!context_provider_requests_.empty()) | 238 if (!context_provider_requests_.empty()) |
207 HandlePendingRequests(); | 239 CheckCanHandlePendingRequests(); |
208 } | 240 } |
209 | 241 |
210 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() { | 242 void ContextProviderFactoryImpl::EstablishGpuChannel() { |
211 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ | 243 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ |
212 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) | 244 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) |
213 const int64_t kGpuChannelTimeoutInSeconds = 40; | 245 const int64_t kGpuChannelTimeoutInSeconds = 40; |
214 #else | 246 #else |
215 const int64_t kGpuChannelTimeoutInSeconds = 10; | 247 const int64_t kGpuChannelTimeoutInSeconds = 10; |
216 #endif | 248 #endif |
217 | 249 |
218 BrowserGpuChannelHostFactory* factory = | 250 BrowserGpuChannelHostFactory* factory = |
219 BrowserGpuChannelHostFactory::instance(); | 251 BrowserGpuChannelHostFactory::instance(); |
220 | 252 DCHECK(factory); |
221 if (factory->GetGpuChannel()) | |
222 return factory->GetGpuChannel(); | |
223 | 253 |
224 factory->EstablishGpuChannel( | 254 factory->EstablishGpuChannel( |
225 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, | 255 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, |
226 weak_factory_.GetWeakPtr())); | 256 weak_factory_.GetWeakPtr())); |
227 establish_gpu_channel_timeout_.Start( | 257 establish_gpu_channel_timeout_.Start( |
228 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), | 258 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), |
229 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); | 259 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); |
230 | |
231 return nullptr; | |
232 } | 260 } |
233 | 261 |
234 void ContextProviderFactoryImpl::OnGpuChannelEstablished( | 262 void ContextProviderFactoryImpl::OnGpuChannelEstablished( |
235 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { | 263 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { |
236 establish_gpu_channel_timeout_.Stop(); | 264 establish_gpu_channel_timeout_.Stop(); |
237 | 265 |
238 // This should happen only during shutdown. So early out instead of queuing | |
239 // more requests with the factory. | |
240 if (!gpu_channel) | |
241 return; | |
242 | |
243 // We can queue the Gpu Channel initialization requests multiple times as | 266 // We can queue the Gpu Channel initialization requests multiple times as |
244 // we get context requests. So we might have already handled any pending | 267 // we get context requests. So we might have already handled any pending |
245 // requests when this callback runs. | 268 // requests when this callback runs. |
246 if (!context_provider_requests_.empty()) | 269 if (context_provider_requests_.empty()) |
247 HandlePendingRequests(); | 270 return; |
| 271 |
| 272 if (gpu_channel) { |
| 273 HandlePendingRequests(std::move(gpu_channel), |
| 274 ContextCreationResult::SUCCESS); |
| 275 } else if (IsGpuChannelHostFactoryAvailable()) { |
| 276 HandlePendingRequests( |
| 277 nullptr, |
| 278 ContextCreationResult::FAILURE_GPU_PROCESS_INITIALIZATION_FAILED); |
| 279 } else { |
| 280 HandlePendingRequests(nullptr, |
| 281 ContextCreationResult::FAILURE_BROWSER_SHUTDOWN); |
| 282 } |
248 } | 283 } |
249 | 284 |
250 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { | 285 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { |
251 LOG(FATAL) << "Timed out waiting for GPU channel."; | 286 LOG(FATAL) << "Timed out waiting for GPU channel."; |
252 } | 287 } |
253 | 288 |
254 } // namespace content | 289 } // namespace content |
OLD | NEW |