| 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" |
| 11 #include "cc/output/context_provider.h" | 11 #include "cc/output/context_provider.h" |
| 12 #include "cc/output/vulkan_in_process_context_provider.h" | 12 #include "cc/output/vulkan_in_process_context_provider.h" |
| 13 #include "cc/surfaces/surface_manager.h" | 13 #include "cc/surfaces/surface_manager.h" |
| 14 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | |
| 15 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | 14 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" |
| 16 #include "content/browser/gpu/compositor_util.h" | 15 #include "content/browser/gpu/compositor_util.h" |
| 17 #include "content/browser/gpu/gpu_surface_tracker.h" | 16 #include "content/browser/gpu/gpu_surface_tracker.h" |
| 18 #include "content/common/gpu/client/context_provider_command_buffer.h" | 17 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 19 #include "content/common/host_shared_bitmap_manager.h" | 18 #include "content/common/host_shared_bitmap_manager.h" |
| 20 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 21 #include "gpu/command_buffer/client/gles2_interface.h" | 20 #include "gpu/command_buffer/client/gles2_interface.h" |
| 22 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" | 21 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
| 23 #include "gpu/ipc/client/gpu_channel_host.h" | 22 #include "gpu/ipc/client/gpu_channel_host.h" |
| 24 | 23 |
| 25 namespace content { | 24 namespace content { |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 command_buffer_metrics::ContextType ToCommandBufferContextType( | 28 command_buffer_metrics::ContextType ToCommandBufferContextType( |
| 30 ui::ContextProviderFactory::ContextType context_type) { | 29 ui::ContextProviderFactory::ContextType context_type) { |
| 31 switch (context_type) { | 30 switch (context_type) { |
| 32 case ui::ContextProviderFactory::ContextType:: | 31 case ui::ContextProviderFactory::ContextType:: |
| 33 BLIMP_RENDER_COMPOSITOR_CONTEXT: | 32 BLIMP_RENDER_COMPOSITOR_CONTEXT: |
| 34 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; | 33 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; |
| 35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: | 34 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: |
| 36 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; | 35 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; |
| 37 } | 36 } |
| 38 NOTREACHED(); | 37 NOTREACHED(); |
| 39 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; | 38 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; |
| 40 } | 39 } |
| 41 | 40 |
| 41 ContextProviderFactoryImpl* instance = nullptr; |
| 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 // static | 45 // static |
| 45 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { | 46 void ContextProviderFactoryImpl::Initialize( |
| 46 return base::Singleton<ContextProviderFactoryImpl>::get(); | 47 gpu::GpuChannelEstablishFactory* gpu_channel_factory) { |
| 48 DCHECK(!instance); |
| 49 instance = new ContextProviderFactoryImpl(gpu_channel_factory); |
| 47 } | 50 } |
| 48 | 51 |
| 49 ContextProviderFactoryImpl::ContextProviderFactoryImpl() | 52 void ContextProviderFactoryImpl::Terminate() { |
| 50 : in_handle_pending_requests_(false), | 53 DCHECK(instance); |
| 54 delete instance; |
| 55 instance = nullptr; |
| 56 } |
| 57 |
| 58 // static |
| 59 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { |
| 60 return instance; |
| 61 } |
| 62 |
| 63 ContextProviderFactoryImpl::ContextProviderFactoryImpl( |
| 64 gpu::GpuChannelEstablishFactory* gpu_channel_factory) |
| 65 : gpu_channel_factory_(gpu_channel_factory), |
| 66 in_handle_pending_requests_(false), |
| 67 in_shutdown_(false), |
| 51 surface_client_id_(0), | 68 surface_client_id_(0), |
| 52 weak_factory_(this) {} | 69 weak_factory_(this) { |
| 70 DCHECK(gpu_channel_factory_); |
| 71 } |
| 53 | 72 |
| 54 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {} | 73 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() { |
| 74 in_shutdown_ = true; |
| 75 if (!context_provider_requests_.empty()) |
| 76 HandlePendingRequests(nullptr, |
| 77 ContextCreationResult::FAILURE_FACTORY_SHUTDOWN); |
| 78 } |
| 55 | 79 |
| 56 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() | 80 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() |
| 57 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), | 81 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), |
| 58 surface_handle(gpu::kNullSurfaceHandle), | 82 surface_handle(gpu::kNullSurfaceHandle), |
| 59 support_locking(false), | 83 support_locking(false), |
| 60 automatic_flushes(false), | 84 automatic_flushes(false), |
| 61 shared_context_provider(nullptr) {} | 85 shared_context_provider(nullptr) {} |
| 62 | 86 |
| 63 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( | 87 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( |
| 64 const ContextProvidersRequest& other) = default; | 88 const ContextProvidersRequest& other) = default; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 context_request.context_type = context_type; | 162 context_request.context_type = context_type; |
| 139 context_request.surface_handle = surface_handle; | 163 context_request.surface_handle = surface_handle; |
| 140 context_request.shared_memory_limits = shared_memory_limits; | 164 context_request.shared_memory_limits = shared_memory_limits; |
| 141 context_request.attributes = attributes; | 165 context_request.attributes = attributes; |
| 142 context_request.support_locking = support_locking; | 166 context_request.support_locking = support_locking; |
| 143 context_request.automatic_flushes = automatic_flushes; | 167 context_request.automatic_flushes = automatic_flushes; |
| 144 context_request.shared_context_provider = shared_context_provider; | 168 context_request.shared_context_provider = shared_context_provider; |
| 145 context_request.result_callback = result_callback; | 169 context_request.result_callback = result_callback; |
| 146 | 170 |
| 147 context_provider_requests_.push_back(context_request); | 171 context_provider_requests_.push_back(context_request); |
| 148 HandlePendingRequests(); | 172 |
| 173 // If the channel is available, the factory will run the callback |
| 174 // synchronously so we'll handle this request there. |
| 175 EstablishGpuChannel(); |
| 149 } | 176 } |
| 150 | 177 |
| 151 void ContextProviderFactoryImpl::HandlePendingRequests() { | 178 void ContextProviderFactoryImpl::HandlePendingRequests( |
| 179 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host, |
| 180 ContextCreationResult result) { |
| 181 DCHECK(!in_shutdown_) |
| 182 << "The factory is shutting down, can't handle new requests"; |
| 152 DCHECK(!context_provider_requests_.empty()) | 183 DCHECK(!context_provider_requests_.empty()) |
| 153 << "We don't have any pending requests?"; | 184 << "We don't have any pending requests?"; |
| 154 | 185 |
| 155 // Failure to initialize the context could result in new requests. Handle | 186 // Failure to initialize the context could result in new requests. Handle |
| 156 // them after going through the current list. | 187 // them after going through the current list. |
| 157 if (in_handle_pending_requests_) | 188 if (in_handle_pending_requests_) |
| 158 return; | 189 return; |
| 159 | 190 |
| 160 { | 191 { |
| 161 base::AutoReset<bool> auto_reset_in_handle_requests( | 192 base::AutoReset<bool> auto_reset_in_handle_requests( |
| 162 &in_handle_pending_requests_, true); | 193 &in_handle_pending_requests_, true); |
| 163 | 194 |
| 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 = | 195 std::list<ContextProvidersRequest> context_requests = |
| 174 context_provider_requests_; | 196 context_provider_requests_; |
| 175 context_provider_requests_.clear(); | 197 context_provider_requests_.clear(); |
| 176 | 198 |
| 177 for (ContextProvidersRequest& context_request : context_requests) { | 199 for (ContextProvidersRequest& context_request : context_requests) { |
| 178 scoped_refptr<cc::ContextProvider> context_provider; | 200 scoped_refptr<cc::ContextProvider> context_provider; |
| 201 ContextCreationResult result_to_report = result; |
| 179 | 202 |
| 180 const bool create_onscreen_context = | 203 const bool create_onscreen_context = |
| 181 context_request.surface_handle != gpu::kNullSurfaceHandle; | 204 context_request.surface_handle != gpu::kNullSurfaceHandle; |
| 182 | 205 |
| 183 // Is the request for an onscreen context? Make sure the surface is | 206 // 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 | 207 // still valid in that case. |
| 185 // valid surface. | |
| 186 if (create_onscreen_context && | 208 if (create_onscreen_context && |
| 187 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle( | 209 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle( |
| 188 context_request.surface_handle)) { | 210 context_request.surface_handle)) { |
| 189 continue; | 211 // Choose what to report based on severity, factory shutdown trumps |
| 212 // everything, otherwise report GpuSurfaceHandle loss. |
| 213 result_to_report = |
| 214 result_to_report == ContextCreationResult::FAILURE_FACTORY_SHUTDOWN |
| 215 ? result_to_report |
| 216 : ContextCreationResult::FAILURE_GPU_SURFACE_HANDLE_LOST; |
| 217 } else if (gpu_channel_host) { |
| 218 DCHECK_EQ(ContextCreationResult::SUCCESS, result); |
| 219 |
| 220 context_provider = new ContextProviderCommandBuffer( |
| 221 gpu_channel_host, gpu::GPU_STREAM_DEFAULT, |
| 222 gpu::GpuStreamPriority::NORMAL, context_request.surface_handle, |
| 223 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") + |
| 224 std::string("CompositorContextProvider")), |
| 225 context_request.automatic_flushes, context_request.support_locking, |
| 226 context_request.shared_memory_limits, context_request.attributes, |
| 227 static_cast<ContextProviderCommandBuffer*>( |
| 228 context_request.shared_context_provider), |
| 229 context_request.context_type); |
| 190 } | 230 } |
| 191 | 231 |
| 192 context_provider = new ContextProviderCommandBuffer( | 232 context_request.result_callback.Run(context_provider, result_to_report); |
| 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 } | 233 } |
| 204 } | 234 } |
| 205 | 235 |
| 206 if (!context_provider_requests_.empty()) | 236 if (!context_provider_requests_.empty()) |
| 207 HandlePendingRequests(); | 237 EstablishGpuChannel(); |
| 208 } | 238 } |
| 209 | 239 |
| 210 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() { | 240 void ContextProviderFactoryImpl::EstablishGpuChannel() { |
| 211 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ | 241 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 212 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) | 242 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) |
| 213 const int64_t kGpuChannelTimeoutInSeconds = 40; | 243 const int64_t kGpuChannelTimeoutInSeconds = 40; |
| 214 #else | 244 #else |
| 215 const int64_t kGpuChannelTimeoutInSeconds = 10; | 245 const int64_t kGpuChannelTimeoutInSeconds = 10; |
| 216 #endif | 246 #endif |
| 217 | 247 |
| 218 BrowserGpuChannelHostFactory* factory = | 248 // Start the timer first, if the result comes synchronously, we want it to |
| 219 BrowserGpuChannelHostFactory::instance(); | 249 // stop in the callback. |
| 220 | |
| 221 if (factory->GetGpuChannel()) | |
| 222 return factory->GetGpuChannel(); | |
| 223 | |
| 224 factory->EstablishGpuChannel( | |
| 225 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, | |
| 226 weak_factory_.GetWeakPtr())); | |
| 227 establish_gpu_channel_timeout_.Start( | 250 establish_gpu_channel_timeout_.Start( |
| 228 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), | 251 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), |
| 229 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); | 252 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); |
| 230 | 253 |
| 231 return nullptr; | 254 gpu_channel_factory_->EstablishGpuChannel( |
| 255 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, |
| 256 weak_factory_.GetWeakPtr())); |
| 232 } | 257 } |
| 233 | 258 |
| 234 void ContextProviderFactoryImpl::OnGpuChannelEstablished( | 259 void ContextProviderFactoryImpl::OnGpuChannelEstablished( |
| 235 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { | 260 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { |
| 236 establish_gpu_channel_timeout_.Stop(); | 261 establish_gpu_channel_timeout_.Stop(); |
| 237 | 262 |
| 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 | 263 // We can queue the Gpu Channel initialization requests multiple times as |
| 244 // we get context requests. So we might have already handled any pending | 264 // we get context requests. So we might have already handled any pending |
| 245 // requests when this callback runs. | 265 // requests when this callback runs. |
| 246 if (!context_provider_requests_.empty()) | 266 if (context_provider_requests_.empty()) |
| 247 HandlePendingRequests(); | 267 return; |
| 268 |
| 269 if (gpu_channel) { |
| 270 HandlePendingRequests(std::move(gpu_channel), |
| 271 ContextCreationResult::SUCCESS); |
| 272 } else { |
| 273 HandlePendingRequests( |
| 274 nullptr, |
| 275 ContextCreationResult::FAILURE_GPU_PROCESS_INITIALIZATION_FAILED); |
| 276 } |
| 248 } | 277 } |
| 249 | 278 |
| 250 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { | 279 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { |
| 251 LOG(FATAL) << "Timed out waiting for GPU channel."; | 280 LOG(FATAL) << "Timed out waiting for GPU channel."; |
| 252 } | 281 } |
| 253 | 282 |
| 254 } // namespace content | 283 } // namespace content |
| OLD | NEW |