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

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: remove display_ DCHECK. 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 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 surface_handle(gpu::kNullSurfaceHandle),
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 gpu::SurfaceHandle surface_handle,
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(surface_handle != gpu::kNullSurfaceHandle);
86 CreateContextProviderInternal(
87 command_buffer_metrics::DISPLAY_COMPOSITOR_ONSCREEN_CONTEXT,
88 surface_handle, shared_memory_limits, attributes, support_locking,
89 automatic_flushes, 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(ToCommandBufferContextType(context_type),
101 gpu::kNullSurfaceHandle, shared_memory_limits,
102 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 gpu::SurfaceHandle surface_handle,
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.surface_handle = surface_handle;
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 DCHECK(!context_provider_requests_.empty())
153 << "We don't have any pending requests?";
154
155 // Failure to initialize the context could result in new requests. Handle
156 // them after going through the current list.
157 if (in_handle_pending_requests_)
158 return;
159
160 {
161 base::AutoReset<bool> auto_reset_in_handle_requests(
162 &in_handle_pending_requests_, true);
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
173 std::list<ContextProvidersRequest> context_requests =
174 context_provider_requests_;
175 context_provider_requests_.clear();
176
177 for (ContextProvidersRequest& context_request : context_requests) {
178 scoped_refptr<cc::ContextProvider> context_provider;
179
180 const bool create_onscreen_context =
181 context_request.surface_handle != gpu::kNullSurfaceHandle;
182
183 // Is the request for an onscreen context? Make sure the surface is
184 // still valid in that case. DO NOT run the callback if we don't have a
185 // valid surface.
186 if (create_onscreen_context &&
187 !GpuSurfaceTracker::GetInstance()->IsValidSurfaceHandle(
188 context_request.surface_handle)) {
189 continue;
190 }
191
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);
203 }
204 }
205
206 if (!context_provider_requests_.empty())
207 HandlePendingRequests();
208 }
209
210 gpu::GpuChannelHost* ContextProviderFactoryImpl::EnsureGpuChannelEstablished() {
211 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
212 defined(SYZYASAN) || defined(CYGPROFILE_INSTRUMENTATION)
213 const int64_t kGpuChannelTimeoutInSeconds = 40;
214 #else
215 const int64_t kGpuChannelTimeoutInSeconds = 10;
216 #endif
217
218 BrowserGpuChannelHostFactory* factory =
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()));
227 establish_gpu_channel_timeout_.Start(
228 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
229 this, &ContextProviderFactoryImpl::OnGpuChannelTimeout);
230
231 return nullptr;
232 }
233
234 void ContextProviderFactoryImpl::OnGpuChannelEstablished() {
235 establish_gpu_channel_timeout_.Stop();
236
237 // We can queue the Gpu Channel initialization requests multiple times as
238 // we get context requests. So we might have already handled any pending
239 // requests when this callback runs.
240 if (!context_provider_requests_.empty())
241 HandlePendingRequests();
242 }
243
244 void ContextProviderFactoryImpl::OnGpuChannelTimeout() {
245 LOG(FATAL) << "Timed out waiting for GPU channel.";
246 }
247
248 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698