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