| 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/ptr_util.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/singleton.h" | |
| 13 #include "cc/output/context_provider.h" | |
| 14 #include "cc/output/vulkan_in_process_context_provider.h" | |
| 15 #include "cc/surfaces/surface_manager.h" | |
| 16 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | |
| 17 #include "content/browser/gpu/compositor_util.h" | |
| 18 #include "content/common/host_shared_bitmap_manager.h" | |
| 19 #include "content/public/common/content_switches.h" | |
| 20 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 21 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" | |
| 22 #include "gpu/ipc/client/gpu_channel_host.h" | |
| 23 #include "services/ui/public/cpp/gpu/context_provider_command_buffer.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 ui::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 ui::command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; | |
| 35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: | |
| 36 return ui::command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; | |
| 37 } | |
| 38 NOTREACHED(); | |
| 39 return ui::command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; | |
| 40 } | |
| 41 | |
| 42 ContextProviderFactoryImpl* instance = nullptr; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 // static | |
| 47 void ContextProviderFactoryImpl::Initialize( | |
| 48 gpu::GpuChannelEstablishFactory* gpu_channel_factory) { | |
| 49 DCHECK(!instance); | |
| 50 instance = new ContextProviderFactoryImpl(gpu_channel_factory); | |
| 51 } | |
| 52 | |
| 53 void ContextProviderFactoryImpl::Terminate() { | |
| 54 DCHECK(instance); | |
| 55 delete instance; | |
| 56 instance = nullptr; | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() { | |
| 61 return instance; | |
| 62 } | |
| 63 | |
| 64 ContextProviderFactoryImpl::ContextProviderFactoryImpl( | |
| 65 gpu::GpuChannelEstablishFactory* gpu_channel_factory) | |
| 66 : gpu_channel_factory_(gpu_channel_factory), | |
| 67 in_handle_pending_requests_(false), | |
| 68 in_shutdown_(false), | |
| 69 next_sink_id_(1u), | |
| 70 weak_factory_(this) { | |
| 71 DCHECK(gpu_channel_factory_); | |
| 72 } | |
| 73 | |
| 74 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() { | |
| 75 in_shutdown_ = true; | |
| 76 if (!gpu_channel_requests_.empty()) | |
| 77 HandlePendingRequests(nullptr, | |
| 78 GpuChannelHostResult::FAILURE_FACTORY_SHUTDOWN); | |
| 79 } | |
| 80 | |
| 81 scoped_refptr<cc::VulkanContextProvider> | |
| 82 ContextProviderFactoryImpl::GetSharedVulkanContextProvider() { | |
| 83 if (!shared_vulkan_context_provider_) | |
| 84 shared_vulkan_context_provider_ = | |
| 85 cc::VulkanInProcessContextProvider::Create(); | |
| 86 | |
| 87 return shared_vulkan_context_provider_.get(); | |
| 88 } | |
| 89 | |
| 90 void ContextProviderFactoryImpl::RequestGpuChannelHost( | |
| 91 GpuChannelHostCallback callback) { | |
| 92 DCHECK(!in_shutdown_) | |
| 93 << "The factory is shutting down, can't handle new requests"; | |
| 94 | |
| 95 gpu_channel_requests_.push(callback); | |
| 96 // If the channel is available, the factory will run the callback | |
| 97 // synchronously so we'll handle this request there. | |
| 98 EstablishGpuChannel(); | |
| 99 } | |
| 100 | |
| 101 scoped_refptr<cc::ContextProvider> | |
| 102 ContextProviderFactoryImpl::CreateDisplayContextProvider( | |
| 103 gpu::SurfaceHandle surface_handle, | |
| 104 gpu::SharedMemoryLimits shared_memory_limits, | |
| 105 gpu::gles2::ContextCreationAttribHelper attributes, | |
| 106 bool support_locking, | |
| 107 bool automatic_flushes, | |
| 108 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) { | |
| 109 DCHECK(surface_handle != gpu::kNullSurfaceHandle); | |
| 110 return CreateContextProviderInternal( | |
| 111 ui::command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT, | |
| 112 surface_handle, shared_memory_limits, attributes, support_locking, | |
| 113 automatic_flushes, nullptr, std::move(gpu_channel_host)); | |
| 114 } | |
| 115 | |
| 116 scoped_refptr<cc::ContextProvider> | |
| 117 ContextProviderFactoryImpl::CreateOffscreenContextProvider( | |
| 118 ContextType context_type, | |
| 119 gpu::SharedMemoryLimits shared_memory_limits, | |
| 120 gpu::gles2::ContextCreationAttribHelper attributes, | |
| 121 bool support_locking, | |
| 122 bool automatic_flushes, | |
| 123 cc::ContextProvider* shared_context_provider, | |
| 124 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) { | |
| 125 return CreateContextProviderInternal( | |
| 126 ToCommandBufferContextType(context_type), gpu::kNullSurfaceHandle, | |
| 127 shared_memory_limits, attributes, support_locking, automatic_flushes, | |
| 128 shared_context_provider, std::move(gpu_channel_host)); | |
| 129 } | |
| 130 | |
| 131 cc::SurfaceManager* ContextProviderFactoryImpl::GetSurfaceManager() { | |
| 132 if (!surface_manager_) | |
| 133 surface_manager_ = base::WrapUnique(new cc::SurfaceManager); | |
| 134 | |
| 135 return surface_manager_.get(); | |
| 136 } | |
| 137 | |
| 138 cc::FrameSinkId ContextProviderFactoryImpl::AllocateFrameSinkId() { | |
| 139 // The FrameSinkId generated here must be unique with | |
| 140 // RenderWidgetHostViewAndroid's | |
| 141 // FrameSinkId allocation. | |
| 142 // TODO(crbug.com/685777): Centralize allocation in one place for easier | |
| 143 // maintenance. | |
| 144 return cc::FrameSinkId(0 /* client_id */, next_sink_id_++); | |
| 145 } | |
| 146 | |
| 147 gpu::GpuMemoryBufferManager* | |
| 148 ContextProviderFactoryImpl::GetGpuMemoryBufferManager() { | |
| 149 return BrowserGpuMemoryBufferManager::current(); | |
| 150 } | |
| 151 | |
| 152 scoped_refptr<cc::ContextProvider> | |
| 153 ContextProviderFactoryImpl::CreateContextProviderInternal( | |
| 154 ui::command_buffer_metrics::ContextType context_type, | |
| 155 gpu::SurfaceHandle surface_handle, | |
| 156 gpu::SharedMemoryLimits shared_memory_limits, | |
| 157 gpu::gles2::ContextCreationAttribHelper attributes, | |
| 158 bool support_locking, | |
| 159 bool automatic_flushes, | |
| 160 cc::ContextProvider* shared_context_provider, | |
| 161 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) { | |
| 162 return make_scoped_refptr(new ui::ContextProviderCommandBuffer( | |
| 163 std::move(gpu_channel_host), gpu::GPU_STREAM_DEFAULT, | |
| 164 gpu::GpuStreamPriority::NORMAL, surface_handle, | |
| 165 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") + | |
| 166 std::string("CompositorContextProvider")), | |
| 167 automatic_flushes, support_locking, shared_memory_limits, attributes, | |
| 168 static_cast<ui::ContextProviderCommandBuffer*>(shared_context_provider), | |
| 169 context_type)); | |
| 170 } | |
| 171 | |
| 172 void ContextProviderFactoryImpl::HandlePendingRequests( | |
| 173 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host, | |
| 174 GpuChannelHostResult result) { | |
| 175 DCHECK(!gpu_channel_requests_.empty()) | |
| 176 << "We don't have any pending requests?"; | |
| 177 DCHECK(gpu_channel_host || result != GpuChannelHostResult::SUCCESS); | |
| 178 | |
| 179 // Failure to initialize the channel could result in new requests. Handle | |
| 180 // them after going through the current list. | |
| 181 if (in_handle_pending_requests_) | |
| 182 return; | |
| 183 | |
| 184 { | |
| 185 base::AutoReset<bool> auto_reset_in_handle_requests( | |
| 186 &in_handle_pending_requests_, true); | |
| 187 | |
| 188 std::queue<GpuChannelHostCallback> current_gpu_channel_requests; | |
| 189 current_gpu_channel_requests.swap(gpu_channel_requests_); | |
| 190 | |
| 191 while (!current_gpu_channel_requests.empty()) { | |
| 192 current_gpu_channel_requests.front().Run(gpu_channel_host, result); | |
| 193 current_gpu_channel_requests.pop(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 if (!gpu_channel_requests_.empty()) | |
| 198 EstablishGpuChannel(); | |
| 199 } | |
| 200 | |
| 201 void ContextProviderFactoryImpl::EstablishGpuChannel() { | |
| 202 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ | |
| 203 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) | |
| 204 const int64_t kGpuChannelTimeoutInSeconds = 40; | |
| 205 #else | |
| 206 // The GPU watchdog timeout is 15 seconds (1.5x the kGpuTimeout value due to | |
| 207 // logic in GpuWatchdogThread). Make this slightly longer to give the GPU a | |
| 208 // chance to crash itself before crashing the browser. | |
| 209 const int64_t kGpuChannelTimeoutInSeconds = 20; | |
| 210 #endif | |
| 211 | |
| 212 // Start the timer first, if the result comes synchronously, we want it to | |
| 213 // stop in the callback. | |
| 214 establish_gpu_channel_timeout_.Start( | |
| 215 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), | |
| 216 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); | |
| 217 | |
| 218 gpu_channel_factory_->EstablishGpuChannel( | |
| 219 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished, | |
| 220 weak_factory_.GetWeakPtr())); | |
| 221 } | |
| 222 | |
| 223 void ContextProviderFactoryImpl::OnGpuChannelEstablished( | |
| 224 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { | |
| 225 establish_gpu_channel_timeout_.Stop(); | |
| 226 | |
| 227 // We can queue the Gpu Channel initialization requests multiple times as | |
| 228 // we get context requests. So we might have already handled any pending | |
| 229 // requests when this callback runs. | |
| 230 if (gpu_channel_requests_.empty()) | |
| 231 return; | |
| 232 | |
| 233 if (gpu_channel) { | |
| 234 HandlePendingRequests(std::move(gpu_channel), | |
| 235 GpuChannelHostResult::SUCCESS); | |
| 236 } else { | |
| 237 HandlePendingRequests( | |
| 238 nullptr, | |
| 239 GpuChannelHostResult::FAILURE_GPU_PROCESS_INITIALIZATION_FAILED); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { | |
| 244 LOG(FATAL) << "Timed out waiting for GPU channel."; | |
| 245 } | |
| 246 | |
| 247 } // namespace content | |
| OLD | NEW |