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

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

Issue 2190033002: content: Add ContextProviderFactory to create a render ContextProvider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: .. Created 4 years, 4 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
(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 // static
28 ContextProviderFactoryImpl* ContextProviderFactoryImpl::GetInstance() {
29 return base::Singleton<ContextProviderFactoryImpl>::get();
30 }
31
32 ContextProviderFactoryImpl::ContextProviderFactoryImpl()
33 : in_handle_pending_requests_(false),
34 surface_client_id_(0),
35 weak_factory_(this) {}
36
37 ContextProviderFactoryImpl::~ContextProviderFactoryImpl() {}
38
39 ContextProviderFactoryImpl::ContextProvidersRequest::ContextProvidersRequest() =
40 default;
41
42 ContextProviderFactoryImpl::ContextProvidersRequest::
43 ~ContextProvidersRequest() = default;
44
45 scoped_refptr<cc::VulkanContextProvider>
46 ContextProviderFactoryImpl::GetSharedVulkanContextProvider() {
47 if (!shared_vulkan_context_provider_)
48 shared_vulkan_context_provider_ =
49 cc::VulkanInProcessContextProvider::Create();
50
51 return shared_vulkan_context_provider_.get();
52 }
53
54 void ContextProviderFactoryImpl::RequestContextProviders(
55 gfx::AcceleratedWidget widget,
56 gpu::SharedMemoryLimits shared_memory_limits,
57 gpu::gles2::ContextCreationAttribHelper attributes,
58 bool support_locking,
59 bool automatic_flushes,
60 ContextProviderCallback result_callback) {
61 DCHECK(!result_callback.is_null());
62
63 ContextProvidersRequest context_request;
64 context_request.widget = widget;
65 context_request.shared_memory_limits = shared_memory_limits;
66 context_request.attributes = attributes;
67 context_request.support_locking = support_locking;
68 context_request.automatic_flushes = automatic_flushes;
69 context_request.result_callback = result_callback;
70
71 context_provider_requests_.push_back(context_request);
72 HandlePendingRequests();
73 }
74
75 cc::SurfaceManager* ContextProviderFactoryImpl::GetSurfaceManager() {
76 if (!surface_manager_)
77 surface_manager_ = base::WrapUnique(new cc::SurfaceManager);
78
79 return surface_manager_.get();
80 }
81
82 uint32_t ContextProviderFactoryImpl::AllocateSurfaceClientId() {
83 return ++surface_client_id_;
84 }
85
86 cc::SharedBitmapManager* ContextProviderFactoryImpl::GetSharedBitmapManager() {
87 return HostSharedBitmapManager::current();
88 }
89
90 gpu::GpuMemoryBufferManager*
91 ContextProviderFactoryImpl::GetGpuMemoryBufferManager() {
92 return BrowserGpuMemoryBufferManager::current();
93 }
94
95 void ContextProviderFactoryImpl::HandlePendingRequests() {
96 // Failure to initialize the context could result in new requests. Handle
97 // them after going through the current list.
98 if (in_handle_pending_requests_)
99 return;
100
101 {
102 base::AutoReset<bool> auto_reset_in_handle_requests(
103 &in_handle_pending_requests_, true);
104
105 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(
106 EnsureGpuChannelEstablished());
107 if (!gpu_channel_host)
108 return;
109
110 if (!context_provider_requests_.empty()) {
111 std::list<ContextProvidersRequest> context_requests =
112 context_provider_requests_;
113 context_provider_requests_.clear();
114
115 for (ContextProvidersRequest& context_request : context_requests) {
116 ContextProviders context_providers;
117 CreateCompositorContexts(context_providers, context_request,
118 gpu_channel_host);
119 context_request.result_callback.Run(context_providers);
120 }
121 }
122 }
123
124 if (!context_provider_requests_.empty())
125 HandlePendingRequests();
126 }
127
128 void ContextProviderFactoryImpl::CreateCompositorContexts(
129 ContextProviders& context_providers,
130 ContextProvidersRequest& context_request,
no sievers 2016/08/02 20:44:34 nit: pass by pointer per style guide for non-const
Khushal 2016/08/02 22:22:16 Done.
131 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) {
no sievers 2016/08/02 20:44:34 nit: const scoped_refptr<>&
Khushal 2016/08/02 22:22:16 Done.
132 DCHECK(gpu_channel_host);
133
134 const bool create_onscreen_context =
135 context_request.widget != gfx::kNullAcceleratedWidget;
136 gpu::SurfaceHandle surface_handle =
137 create_onscreen_context
138 ? GpuSurfaceTracker::GetInstance()->GetSurfaceForNativeWidget(
139 context_request.widget)
140 : gpu::kNullSurfaceHandle;
141
142 // Is the request for an onscreen context? Make sure the surface is
143 // still valid in that case.
144 if (create_onscreen_context && surface_handle == gpu::kNullSurfaceHandle)
145 return;
146
147 scoped_refptr<ContextProviderCommandBuffer> worker_context_provider;
148 // The display compositor does not use a worker context.
149 if (!create_onscreen_context) {
150 worker_context_provider =
151 SharedCompositorWorkerContextProvider(gpu_channel_host);
152
153 // Cause the compositor to wait and try again.
154 if (!worker_context_provider)
155 return;
156 }
157
158 // The compositor context shares resources with the worker context unless
159 // the worker is async.
160 ContextProviderCommandBuffer* share_context = worker_context_provider.get();
161 if (IsAsyncWorkerContextEnabled())
162 share_context = nullptr;
163
164 context_providers.compositor_context_provider =
165 new ContextProviderCommandBuffer(
166 gpu_channel_host, gpu::GPU_STREAM_DEFAULT,
167 gpu::GpuStreamPriority::NORMAL, surface_handle,
168 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") +
169 std::string("CompositorContextProvider")),
170 context_request.automatic_flushes, context_request.support_locking,
171 context_request.shared_memory_limits, context_request.attributes,
172 share_context,
173 create_onscreen_context
174 ? command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT
175 : command_buffer_metrics::RENDER_COMPOSITOR_CONTEXT);
176 context_providers.worker_context_provider = worker_context_provider;
177 }
178
179 scoped_refptr<ContextProviderCommandBuffer>
180 ContextProviderFactoryImpl::SharedCompositorWorkerContextProvider(
181 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) {
182 // Try to reuse existing shared worker context provider.
183 if (shared_worker_context_provider_) {
184 // Note: If context is lost, delete reference after releasing the lock.
185 cc::ContextProvider::ScopedContextLock lock(
186 shared_worker_context_provider_.get());
187 if (shared_worker_context_provider_->ContextGL()
188 ->GetGraphicsResetStatusKHR() == GL_NO_ERROR)
189 return shared_worker_context_provider_;
190 }
191
192 int32_t stream_id = gpu::GPU_STREAM_DEFAULT;
193 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL;
194 if (IsAsyncWorkerContextEnabled()) {
195 stream_id = gpu_channel_host->GenerateStreamID();
196 stream_priority = gpu::GpuStreamPriority::LOW;
197 }
198
199 bool support_locking = true;
200 gpu::gles2::ContextCreationAttribHelper attributes;
201 attributes.alpha_size = -1;
202 attributes.depth_size = 0;
203 attributes.stencil_size = 0;
204 attributes.samples = 0;
205 attributes.sample_buffers = 0;
206 attributes.bind_generates_resource = false;
207 attributes.lose_context_when_out_of_memory = true;
208 const bool automatic_flushes = false;
209
210 shared_worker_context_provider_ = new ContextProviderCommandBuffer(
211 std::move(gpu_channel_host), stream_id, stream_priority,
212 gpu::kNullSurfaceHandle,
213 GURL(std::string("chrome://gpu/ContextProviderFactoryImpl::") +
214 std::string("SharedCompositorWorkerContextProvider")),
215 automatic_flushes, support_locking, gpu::SharedMemoryLimits(), attributes,
216 nullptr, command_buffer_metrics::RENDER_WORKER_CONTEXT);
217
218 if (!shared_worker_context_provider_->BindToCurrentThread())
219 shared_worker_context_provider_ = nullptr;
220 return shared_worker_context_provider_;
221 }
222
223 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() {
224 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
225 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION)
226 const int64_t kGpuChannelTimeoutInSeconds = 40;
227 #else
228 const int64_t kGpuChannelTimeoutInSeconds = 10;
229 #endif
230
231 BrowserGpuChannelHostFactory* factory =
232 BrowserGpuChannelHostFactory::instance();
233
234 if (factory->GetGpuChannel())
235 return factory->GetGpuChannel();
236
237 factory->EstablishGpuChannel(
238 CAUSE_FOR_GPU_LAUNCH_DISPLAY_COMPOSITOR_CONTEXT,
239 base::Bind(&ContextProviderFactoryImpl::OnGpuChannelEstablished,
240 weak_factory_.GetWeakPtr()));
241 establish_gpu_channel_timeout_.Start(
242 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
243 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout);
244
245 return nullptr;
246 }
247
248 void ContextProviderFactoryImpl::OnGpuChannelEstablished() {
249 establish_gpu_channel_timeout_.Stop();
250 HandlePendingRequests();
251 }
252
253 void ContextProviderFactoryImpl::OnGpuChannelTimeout() {
254 LOG(FATAL) << "Timed out waiting for GPU channel.";
255 }
256
257 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698