Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: content/browser/renderer_host/context_provider_factory_impl_android.cc

Issue 2299333003: Revert of Revert "Revert of content: Fix Context creation logic in ContextProviderFactoryImpl." (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
14 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" 15 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
15 #include "content/browser/gpu/compositor_util.h" 16 #include "content/browser/gpu/compositor_util.h"
16 #include "content/browser/gpu/gpu_surface_tracker.h" 17 #include "content/browser/gpu/gpu_surface_tracker.h"
17 #include "content/common/gpu/client/context_provider_command_buffer.h" 18 #include "content/common/gpu/client/context_provider_command_buffer.h"
18 #include "content/common/host_shared_bitmap_manager.h" 19 #include "content/common/host_shared_bitmap_manager.h"
19 #include "content/public/common/content_switches.h" 20 #include "content/public/common/content_switches.h"
20 #include "gpu/command_buffer/client/gles2_interface.h" 21 #include "gpu/command_buffer/client/gles2_interface.h"
21 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" 22 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
22 #include "gpu/ipc/client/gpu_channel_host.h" 23 #include "gpu/ipc/client/gpu_channel_host.h"
23 24
24 namespace content { 25 namespace content {
25 26
26 namespace { 27 namespace {
27 28
28 command_buffer_metrics::ContextType ToCommandBufferContextType( 29 command_buffer_metrics::ContextType ToCommandBufferContextType(
29 ui::ContextProviderFactory::ContextType context_type) { 30 ui::ContextProviderFactory::ContextType context_type) {
30 switch (context_type) { 31 switch (context_type) {
31 case ui::ContextProviderFactory::ContextType:: 32 case ui::ContextProviderFactory::ContextType::
32 BLIMP_RENDER_COMPOSITOR_CONTEXT: 33 BLIMP_RENDER_COMPOSITOR_CONTEXT:
33 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT; 34 return command_buffer_metrics::BLIMP_RENDER_COMPOSITOR_CONTEXT;
34 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT: 35 case ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT:
35 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT; 36 return command_buffer_metrics::BLIMP_RENDER_WORKER_CONTEXT;
36 } 37 }
37 NOTREACHED(); 38 NOTREACHED();
38 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN; 39 return command_buffer_metrics::CONTEXT_TYPE_UNKNOWN;
39 } 40 }
40 41
41 ContextProviderFactoryImpl* instance = nullptr;
42
43 } // namespace 42 } // namespace
44 43
45 // static 44 // static
46 void ContextProviderFactoryImpl::Initialize( 45 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() {
47 gpu::GpuChannelEstablishFactory* gpu_channel_factory) { 46 return base::Singleton<ContextProviderFactoryImpl>::get();
48 DCHECK(!instance);
49 instance = new ContextProviderFactoryImpl(gpu_channel_factory);
50 } 47 }
51 48
52 void ContextProviderFactoryImpl::Terminate() { 49 ContextProviderFactoryImpl::ContextProviderFactoryImpl()
53 DCHECK(instance); 50 : in_handle_pending_requests_(false),
54 delete instance; 51 surface_client_id_(0),
55 instance = nullptr; 52 weak_factory_(this) {}
56 }
57 53
58 // static 54 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {}
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),
68 surface_client_id_(0),
69 weak_factory_(this) {
70 DCHECK(gpu_channel_factory_);
71 }
72
73 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {
74 in_shutdown_ = true;
75 if (!context_provider_requests_.empty())
76 HandlePendingRequests(nullptr,
77 ContextCreationResult::FAILURE_FACTORY_SHUTDOWN);
78 }
79 55
80 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() 56 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest()
81 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN), 57 : context_type(command_buffer_metrics::CONTEXT_TYPE_UNKNOWN),
82 surface_handle(gpu::kNullSurfaceHandle), 58 surface_handle(gpu::kNullSurfaceHandle),
83 support_locking(false), 59 support_locking(false),
84 automatic_flushes(false), 60 automatic_flushes(false),
85 shared_context_provider(nullptr) {} 61 shared_context_provider(nullptr) {}
86 62
87 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest( 63 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest(
88 const ContextProvidersRequest& other) = default; 64 const ContextProvidersRequest& other) = default;
(...skipping 10 matching lines...) Expand all
99 return shared_vulkan_context_provider_.get(); 75 return shared_vulkan_context_provider_.get();
100 } 76 }
101 77
102 void ContextProviderFactoryImpl::CreateDisplayContextProvider( 78 void ContextProviderFactoryImpl::CreateDisplayContextProvider(
103 gpu::SurfaceHandle surface_handle, 79 gpu::SurfaceHandle surface_handle,
104 gpu::SharedMemoryLimits shared_memory_limits, 80 gpu::SharedMemoryLimits shared_memory_limits,
105 gpu::gles2::ContextCreationAttribHelper attributes, 81 gpu::gles2::ContextCreationAttribHelper attributes,
106 bool support_locking, 82 bool support_locking,
107 bool automatic_flushes, 83 bool automatic_flushes,
108 ContextProviderCallback result_callback) { 84 ContextProviderCallback result_callback) {
109 DCHECK(!in_shutdown_)
110 << "The factory is shutting down, can't handle new requests";
111 DCHECK(surface_handle != gpu::kNullSurfaceHandle); 85 DCHECK(surface_handle != gpu::kNullSurfaceHandle);
112 CreateContextProviderInternal( 86 CreateContextProviderInternal(
113 command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT, 87 command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT,
114 surface_handle, shared_memory_limits, attributes, support_locking, 88 surface_handle, shared_memory_limits, attributes, support_locking,
115 automatic_flushes, nullptr, result_callback); 89 automatic_flushes, nullptr, result_callback);
116 } 90 }
117 91
118 void ContextProviderFactoryImpl::CreateOffscreenContextProvider( 92 void ContextProviderFactoryImpl::CreateOffscreenContextProvider(
119 ContextType context_type, 93 ContextType context_type,
120 gpu::SharedMemoryLimits shared_memory_limits, 94 gpu::SharedMemoryLimits shared_memory_limits,
121 gpu::gles2::ContextCreationAttribHelper attributes, 95 gpu::gles2::ContextCreationAttribHelper attributes,
122 bool support_locking, 96 bool support_locking,
123 bool automatic_flushes, 97 bool automatic_flushes,
124 cc::ContextProvider* shared_context_provider, 98 cc::ContextProvider* shared_context_provider,
125 ContextProviderCallback result_callback) { 99 ContextProviderCallback result_callback) {
126 DCHECK(!in_shutdown_)
127 << "The factory is shutting down, can't handle new requests";
128 CreateContextProviderInternal(ToCommandBufferContextType(context_type), 100 CreateContextProviderInternal(ToCommandBufferContextType(context_type),
129 gpu::kNullSurfaceHandle, shared_memory_limits, 101 gpu::kNullSurfaceHandle, shared_memory_limits,
130 attributes, support_locking, automatic_flushes, 102 attributes, support_locking, automatic_flushes,
131 shared_context_provider, result_callback); 103 shared_context_provider, result_callback);
132 } 104 }
133 105
134 cc::SurfaceManager* ContextProviderFactoryImpl::GetSurfaceManager() { 106 cc::SurfaceManager* ContextProviderFactoryImpl::GetSurfaceManager() {
135 if (!surface_manager_) 107 if (!surface_manager_)
136 surface_manager_ = base::WrapUnique(new cc::SurfaceManager); 108 surface_manager_ = base::WrapUnique(new cc::SurfaceManager);
137 109
(...skipping 28 matching lines...) Expand all
166 context_request.context_type = context_type; 138 context_request.context_type = context_type;
167 context_request.surface_handle = surface_handle; 139 context_request.surface_handle = surface_handle;
168 context_request.shared_memory_limits = shared_memory_limits; 140 context_request.shared_memory_limits = shared_memory_limits;
169 context_request.attributes = attributes; 141 context_request.attributes = attributes;
170 context_request.support_locking = support_locking; 142 context_request.support_locking = support_locking;
171 context_request.automatic_flushes = automatic_flushes; 143 context_request.automatic_flushes = automatic_flushes;
172 context_request.shared_context_provider = shared_context_provider; 144 context_request.shared_context_provider = shared_context_provider;
173 context_request.result_callback = result_callback; 145 context_request.result_callback = result_callback;
174 146
175 context_provider_requests_.push_back(context_request); 147 context_provider_requests_.push_back(context_request);
176 148 HandlePendingRequests();
177 // If the channel is available, the factory will run the callback
178 // synchronously so we'll handle this request there.
179 EstablishGpuChannel();
180 } 149 }
181 150
182 void ContextProviderFactoryImpl::HandlePendingRequests( 151 void ContextProviderFactoryImpl::HandlePendingRequests() {
183 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host,
184 ContextCreationResult result) {
185 DCHECK(!context_provider_requests_.empty()) 152 DCHECK(!context_provider_requests_.empty())
186 << "We don't have any pending requests?"; 153 << "We don't have any pending requests?";
187 154
188 // Failure to initialize the context could result in new requests. Handle 155 // Failure to initialize the context could result in new requests. Handle
189 // them after going through the current list. 156 // them after going through the current list.
190 if (in_handle_pending_requests_) 157 if (in_handle_pending_requests_)
191 return; 158 return;
192 159
193 { 160 {
194 base::AutoReset<bool> auto_reset_in_handle_requests( 161 base::AutoReset<bool> auto_reset_in_handle_requests(
195 &in_handle_pending_requests_, true); 162 &in_handle_pending_requests_, true);
196 163
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
197 std::list<ContextProvidersRequest> context_requests = 173 std::list<ContextProvidersRequest> context_requests =
198 context_provider_requests_; 174 context_provider_requests_;
199 context_provider_requests_.clear(); 175 context_provider_requests_.clear();
200 176
201 for (ContextProvidersRequest& context_request : context_requests) { 177 for (ContextProvidersRequest& context_request : context_requests) {
202 scoped_refptr<cc::ContextProvider> context_provider; 178 scoped_refptr<cc::ContextProvider> context_provider;
203 ContextCreationResult result_to_report = result;
204 179
205 const bool create_onscreen_context = 180 const bool create_onscreen_context =
206 context_request.surface_handle != gpu::kNullSurfaceHandle; 181 context_request.surface_handle != gpu::kNullSurfaceHandle;
207 182
208 // Is the request for an onscreen context? Make sure the surface is 183 // Is the request for an onscreen context? Make sure the surface is
209 // still valid in that case. 184 // still valid in that case. DO NOT run the callback if we don't have a
185 // valid surface.
210 if (create_onscreen_context && 186 if (create_onscreen_context &&
211 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle( 187 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle(
212 context_request.surface_handle)) { 188 context_request.surface_handle)) {
213 // Choose what to report based on severity, factory shutdown trumps 189 continue;
214 // everything, otherwise report GpuSurfaceHandle loss.
215 result_to_report =
216 result_to_report == ContextCreationResult::FAILURE_FACTORY_SHUTDOWN
217 ? result_to_report
218 : ContextCreationResult::FAILURE_GPU_SURFACE_HANDLE_LOST;
219 } else if (gpu_channel_host) {
220 DCHECK_EQ(ContextCreationResult::SUCCESS, result);
221
222 context_provider = new ContextProviderCommandBuffer(
223 gpu_channel_host, gpu::GPU_STREAM_DEFAULT,
224 gpu::GpuStreamPriority::NORMAL, context_request.surface_handle,
225 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") +
226 std::string("CompositorContextProvider")),
227 context_request.automatic_flushes, context_request.support_locking,
228 context_request.shared_memory_limits, context_request.attributes,
229 static_cast<ContextProviderCommandBuffer*>(
230 context_request.shared_context_provider),
231 context_request.context_type);
232 } 190 }
233 191
234 context_request.result_callback.Run(context_provider, result_to_report); 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);
235 } 203 }
236 } 204 }
237 205
238 if (!context_provider_requests_.empty()) 206 if (!context_provider_requests_.empty())
239 EstablishGpuChannel(); 207 HandlePendingRequests();
240 } 208 }
241 209
242 void ContextProviderFactoryImpl::EstablishGpuChannel() { 210 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() {
243 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ 211 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
244 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION) 212 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION)
245 const int64_t kGpuChannelTimeoutInSeconds = 40; 213 const int64_t kGpuChannelTimeoutInSeconds = 40;
246 #else 214 #else
247 const int64_t kGpuChannelTimeoutInSeconds = 10; 215 const int64_t kGpuChannelTimeoutInSeconds = 10;
248 #endif 216 #endif
249 217
250 // Start the timer first, if the result comes synchronously, we want it to 218 BrowserGpuChannelHostFactory* factory =
251 // stop in the callback. 219 BrowserGpuChannelHostFactory::instance();
220
221 if (factory->GetGpuChannel())
222 return factory->GetGpuChannel();
223
224 factory->EstablishGpuChannel(
225 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished,
226 weak_factory_.GetWeakPtr()));
252 establish_gpu_channel_timeout_.Start( 227 establish_gpu_channel_timeout_.Start(
253 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds), 228 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
254 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout); 229 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout);
255 230
256 gpu_channel_factory_->EstablishGpuChannel( 231 return nullptr;
257 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished,
258 weak_factory_.GetWeakPtr()));
259 } 232 }
260 233
261 void ContextProviderFactoryImpl::OnGpuChannelEstablished( 234 void ContextProviderFactoryImpl::OnGpuChannelEstablished(
262 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { 235 scoped_refptr<gpu::GpuChannelHost> gpu_channel) {
263 establish_gpu_channel_timeout_.Stop(); 236 establish_gpu_channel_timeout_.Stop();
264 237
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
265 // We can queue the Gpu Channel initialization requests multiple times as 243 // We can queue the Gpu Channel initialization requests multiple times as
266 // we get context requests. So we might have already handled any pending 244 // we get context requests. So we might have already handled any pending
267 // requests when this callback runs. 245 // requests when this callback runs.
268 if (context_provider_requests_.empty()) 246 if (!context_provider_requests_.empty())
269 return; 247 HandlePendingRequests();
270
271 if (gpu_channel) {
272 HandlePendingRequests(std::move(gpu_channel),
273 ContextCreationResult::SUCCESS);
274 } else {
275 HandlePendingRequests(
276 nullptr,
277 ContextCreationResult::FAILURE_GPU_PROCESS_INITIALIZATION_FAILED);
278 }
279 } 248 }
280 249
281 void ContextProviderFactoryImpl::OnGpuChannelTimeout() { 250 void ContextProviderFactoryImpl::OnGpuChannelTimeout() {
282 LOG(FATAL) << "Timed out waiting for GPU channel."; 251 LOG(FATAL) << "Timed out waiting for GPU channel.";
283 } 252 }
284 253
285 } // namespace content 254 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698