| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/context_provider_factory_impl_android.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/singleton.h" |
| 11 #include "cc/output/context_provider.h" |
| 12 #include "cc/output/vulkan_in_process_context_provider.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" |
| 16 #include "content/browser/gpu/compositor_util.h" |
| 17 #include "content/browser/gpu/gpu_surface_tracker.h" |
| 18 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 19 #include "content/common/host_shared_bitmap_manager.h" |
| 20 #include "content/public/common/content_switches.h" |
| 21 #include "gpu/command_buffer/client/gles2_interface.h" |
| 22 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
| 23 #include "gpu/ipc/client/gpu_channel_host.h" |
| 24 |
| 25 namespace content { |
| 26 |
| 27 namespace { |
| 28 |
| 29 command_buffer_metrics::ContextType ToCommandBufferContextType( |
| 30 ui::ContextProviderFactory::ContextType context_type) { |
| 31 switch (context_type) { |
| 32 case ui::ContextProviderFactory::ContextType:: |
| 33 BLIMP_RENDER_COMPOSITOR_CONTEXT: |
| 34 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; |
| 35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: |
| 36 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; |
| 37 } |
| 38 NOTREACHED(); |
| 39 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 // static |
| 45 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { |
| 46 return base::Singleton<ContextProviderFactoryImpl>::get(); |
| 47 } |
| 48 |
| 49 ContextProviderFactoryImpl::ContextProviderFactoryImpl() |
| 50 : in_handle_pending_requests_(false), |
| 51 surface_client_id_(0), |
| 52 weak_factory_(this) {} |
| 53 |
| 54 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {} |
| 55 |
| 56 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() |
| 57 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), |
| 58 widget(gfx::kNullAcceleratedWidget), |
| 59 support_locking(false), |
| 60 automatic_flushes(false), |
| 61 shared_context_provider(nullptr) {} |
| 62 |
| 63 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( |
| 64 const ContextProvidersRequest& other) = default; |
| 65 |
| 66 ContextProviderFactoryImpl::ContextProvidersRequest:: |
| 67 ~ContextProvidersRequest() = default; |
| 68 |
| 69 scoped_refptr<cc::VulkanContextProvider> |
| 70 ContextProviderFactoryImpl::GetSharedVulkanContextProvider() { |
| 71 if (!shared_vulkan_context_provider_) |
| 72 shared_vulkan_context_provider_ = |
| 73 cc::VulkanInProcessContextProvider::Create(); |
| 74 |
| 75 return shared_vulkan_context_provider_.get(); |
| 76 } |
| 77 |
| 78 void ContextProviderFactoryImpl::CreateDisplayContextProvider( |
| 79 gfx::AcceleratedWidget widget, |
| 80 gpu::SharedMemoryLimits shared_memory_limits, |
| 81 gpu::gles2::ContextCreationAttribHelper attributes, |
| 82 bool support_locking, |
| 83 bool automatic_flushes, |
| 84 ContextProviderCallback result_callback) { |
| 85 DCHECK(widget != gfx::kNullAcceleratedWidget); |
| 86 CreateContextProviderInternal( |
| 87 command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT, widget, |
| 88 shared_memory_limits, attributes, support_locking, automatic_flushes, |
| 89 nullptr, result_callback); |
| 90 } |
| 91 |
| 92 void ContextProviderFactoryImpl::CreateOffscreenContextProvider( |
| 93 ContextType context_type, |
| 94 gpu::SharedMemoryLimits shared_memory_limits, |
| 95 gpu::gles2::ContextCreationAttribHelper attributes, |
| 96 bool support_locking, |
| 97 bool automatic_flushes, |
| 98 cc::ContextProvider* shared_context_provider, |
| 99 ContextProviderCallback result_callback) { |
| 100 CreateContextProviderInternal( |
| 101 ToCommandBufferContextType(context_type), gfx::kNullAcceleratedWidget, |
| 102 shared_memory_limits, attributes, support_locking, automatic_flushes, |
| 103 shared_context_provider, result_callback); |
| 104 } |
| 105 |
| 106 cc::SurfaceManager* ContextProviderFactoryImpl::GetSurfaceManager() { |
| 107 if (!surface_manager_) |
| 108 surface_manager_ = base::WrapUnique(new cc::SurfaceManager); |
| 109 |
| 110 return surface_manager_.get(); |
| 111 } |
| 112 |
| 113 uint32_t ContextProviderFactoryImpl::AllocateSurfaceClientId() { |
| 114 return ++surface_client_id_; |
| 115 } |
| 116 |
| 117 cc::SharedBitmapManager* ContextProviderFactoryImpl::GetSharedBitmapManager() { |
| 118 return HostSharedBitmapManager::current(); |
| 119 } |
| 120 |
| 121 gpu::GpuMemoryBufferManager* |
| 122 ContextProviderFactoryImpl::GetGpuMemoryBufferManager() { |
| 123 return BrowserGpuMemoryBufferManager::current(); |
| 124 } |
| 125 |
| 126 void ContextProviderFactoryImpl::CreateContextProviderInternal( |
| 127 command_buffer_metrics::ContextType context_type, |
| 128 gfx::AcceleratedWidget widget, |
| 129 gpu::SharedMemoryLimits shared_memory_limits, |
| 130 gpu::gles2::ContextCreationAttribHelper attributes, |
| 131 bool support_locking, |
| 132 bool automatic_flushes, |
| 133 cc::ContextProvider* shared_context_provider, |
| 134 ContextProviderCallback result_callback) { |
| 135 DCHECK(!result_callback.is_null()); |
| 136 |
| 137 ContextProvidersRequest context_request; |
| 138 context_request.context_type = context_type; |
| 139 context_request.widget = widget; |
| 140 context_request.shared_memory_limits = shared_memory_limits; |
| 141 context_request.attributes = attributes; |
| 142 context_request.support_locking = support_locking; |
| 143 context_request.automatic_flushes = automatic_flushes; |
| 144 context_request.shared_context_provider = shared_context_provider; |
| 145 context_request.result_callback = result_callback; |
| 146 |
| 147 context_provider_requests_.push_back(context_request); |
| 148 HandlePendingRequests(); |
| 149 } |
| 150 |
| 151 void ContextProviderFactoryImpl::HandlePendingRequests() { |
| 152 // Failure to initialize the context could result in new requests. Handle |
| 153 // them after going through the current list. |
| 154 if (in_handle_pending_requests_) |
| 155 return; |
| 156 |
| 157 { |
| 158 base::AutoReset<bool> auto_reset_in_handle_requests( |
| 159 &in_handle_pending_requests_, true); |
| 160 |
| 161 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host( |
| 162 EnsureGpuChannelEstablished()); |
| 163 if (!gpu_channel_host) |
| 164 return; |
| 165 |
| 166 if (!context_provider_requests_.empty()) { |
| 167 std::list<ContextProvidersRequest> context_requests = |
| 168 context_provider_requests_; |
| 169 context_provider_requests_.clear(); |
| 170 |
| 171 for (ContextProvidersRequest& context_request : context_requests) { |
| 172 scoped_refptr<cc::ContextProvider> context_provider; |
| 173 |
| 174 const bool create_onscreen_context = |
| 175 context_request.widget != gfx::kNullAcceleratedWidget; |
| 176 gpu::SurfaceHandle surface_handle = |
| 177 create_onscreen_context |
| 178 ? GpuSurfaceTracker::GetInstance()->GetSurfaceForNativeWidget( |
| 179 context_request.widget) |
| 180 : gpu::kNullSurfaceHandle; |
| 181 |
| 182 // Is the request for an onscreen context? Make sure the surface is |
| 183 // still valid in that case. |
| 184 if (create_onscreen_context && |
| 185 surface_handle == gpu::kNullSurfaceHandle) { |
| 186 context_request.result_callback.Run(context_provider); |
| 187 continue; |
| 188 } |
| 189 |
| 190 context_provider = new ContextProviderCommandBuffer( |
| 191 gpu_channel_host, gpu::GPU_STREAM_DEFAULT, |
| 192 gpu::GpuStreamPriority::NORMAL, surface_handle, |
| 193 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") + |
| 194 std::string("CompositorContextProvider")), |
| 195 context_request.automatic_flushes, context_request.support_locking, |
| 196 context_request.shared_memory_limits, context_request.attributes, |
| 197 static_cast<ContextProviderCommandBuffer*>( |
| 198 context_request.shared_context_provider), |
| 199 context_request.context_type); |
| 200 context_request.result_callback.Run(context_provider); |
| 201 } |
| 202 } |
| 203 } |
| 204 |
| 205 if (!context_provider_requests_.empty()) |
| 206 HandlePendingRequests(); |
| 207 } |
| 208 |
| 209 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() { |
| 210 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 211 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) |
| 212 const int64_t kGpuChannelTimeoutInSeconds = 40; |
| 213 #else |
| 214 const int64_t kGpuChannelTimeoutInSeconds = 10; |
| 215 #endif |
| 216 |
| 217 BrowserGpuChannelHostFactory* factory = |
| 218 BrowserGpuChannelHostFactory::instance(); |
| 219 |
| 220 if (factory->GetGpuChannel()) |
| 221 return factory->GetGpuChannel(); |
| 222 |
| 223 factory->EstablishGpuChannel( |
| 224 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, |
| 225 weak_factory_.GetWeakPtr())); |
| 226 establish_gpu_channel_timeout_.Start( |
| 227 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), |
| 228 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); |
| 229 |
| 230 return nullptr; |
| 231 } |
| 232 |
| 233 void ContextProviderFactoryImpl::OnGpuChannelEstablished() { |
| 234 establish_gpu_channel_timeout_.Stop(); |
| 235 HandlePendingRequests(); |
| 236 } |
| 237 |
| 238 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { |
| 239 LOG(FATAL) << "Timed out waiting for GPU channel."; |
| 240 } |
| 241 |
| 242 } // namespace content |
| OLD | NEW |