[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / android / in_process / synchronous_compositor_factory_impl.cc
blobb90edd69a2f25dd9b17141dcebeb9b44df9be868
1 // Copyright 2014 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.
5 #include "content/browser/android/in_process/synchronous_compositor_factory_impl.h"
7 #include "base/command_line.h"
8 #include "base/observer_list.h"
9 #include "base/sys_info.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/browser/android/in_process/context_provider_in_process.h"
12 #include "content/browser/android/in_process/synchronous_compositor_context_provider.h"
13 #include "content/browser/android/in_process/synchronous_compositor_external_begin_frame_source.h"
14 #include "content/browser/android/in_process/synchronous_compositor_impl.h"
15 #include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
16 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
17 #include "content/common/gpu/client/gpu_channel_host.h"
18 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
19 #include "content/gpu/in_process_gpu_thread.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/gpu_data_manager.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/renderer/gpu/frame_swap_message_queue.h"
24 #include "content/renderer/render_thread_impl.h"
25 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
26 #include "gpu/command_buffer/client/gl_in_process_context.h"
27 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
28 #include "ui/gl/android/surface_texture.h"
29 #include "ui/gl/gl_surface.h"
30 #include "ui/gl/gl_surface_stub.h"
32 using cc_blink::ContextProviderWebContext;
33 using gpu_blink::WebGraphicsContext3DImpl;
34 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl;
36 namespace content {
38 namespace {
40 struct ContextHolder {
41 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> command_buffer;
42 gpu::GLInProcessContext* gl_in_process_context;
45 blink::WebGraphicsContext3D::Attributes GetDefaultAttribs() {
46 blink::WebGraphicsContext3D::Attributes attributes;
47 attributes.antialias = false;
48 attributes.depth = false;
49 attributes.stencil = false;
50 attributes.shareResources = true;
51 attributes.noAutomaticFlushes = true;
53 return attributes;
56 ContextHolder CreateContextHolder(
57 const blink::WebGraphicsContext3D::Attributes& attributes,
58 scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
59 const gpu::GLInProcessContextSharedMemoryLimits& mem_limits,
60 bool is_offscreen) {
61 gpu::gles2::ContextCreationAttribHelper in_process_attribs;
62 WebGraphicsContext3DImpl::ConvertAttributes(attributes, &in_process_attribs);
63 in_process_attribs.lose_context_when_out_of_memory = true;
65 scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create(
66 service, NULL /* surface */, is_offscreen, gfx::kNullAcceleratedWidget,
67 gfx::Size(1, 1), NULL /* share_context */, attributes.shareResources,
68 in_process_attribs, gfx::PreferDiscreteGpu, mem_limits,
69 BrowserGpuMemoryBufferManager::current(), nullptr));
71 gpu::GLInProcessContext* context_ptr = context.get();
73 ContextHolder holder;
74 holder.command_buffer =
75 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
76 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
77 context.Pass(), attributes));
78 holder.gl_in_process_context = context_ptr;
80 return holder;
83 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateContext3D(
84 int surface_id,
85 const blink::WebGraphicsContext3D::Attributes& attributes,
86 const content::WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits&
87 mem_limits) {
88 DCHECK(RenderThreadImpl::current());
89 CauseForGpuLaunch cause =
90 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
91 scoped_refptr<GpuChannelHost> gpu_channel_host(
92 RenderThreadImpl::current()->EstablishGpuChannelSync(cause));
93 CHECK(gpu_channel_host.get());
95 bool lose_context_when_out_of_memory = true;
96 return make_scoped_ptr(new WebGraphicsContext3DCommandBufferImpl(
97 surface_id, GURL(), gpu_channel_host.get(), attributes,
98 lose_context_when_out_of_memory, mem_limits, NULL));
101 } // namespace
103 class SynchronousCompositorFactoryImpl::VideoContextProvider
104 : public StreamTextureFactorySynchronousImpl::ContextProvider {
105 public:
106 VideoContextProvider(
107 scoped_refptr<cc::ContextProvider> context_provider,
108 gpu::GLInProcessContext* gl_in_process_context)
109 : context_provider_(context_provider),
110 gl_in_process_context_(gl_in_process_context) {
111 context_provider_->BindToCurrentThread();
114 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
115 uint32 stream_id) override {
116 return gl_in_process_context_->GetSurfaceTexture(stream_id);
119 gpu::gles2::GLES2Interface* ContextGL() override {
120 return context_provider_->ContextGL();
123 void AddObserver(StreamTextureFactoryContextObserver* obs) override {
124 observer_list_.AddObserver(obs);
127 void RemoveObserver(StreamTextureFactoryContextObserver* obs) override {
128 observer_list_.RemoveObserver(obs);
131 void RestoreContext() {
132 FOR_EACH_OBSERVER(StreamTextureFactoryContextObserver,
133 observer_list_,
134 ResetStreamTextureProxy());
137 private:
138 friend class base::RefCountedThreadSafe<VideoContextProvider>;
139 ~VideoContextProvider() override {}
141 scoped_refptr<cc::ContextProvider> context_provider_;
142 gpu::GLInProcessContext* gl_in_process_context_;
143 base::ObserverList<StreamTextureFactoryContextObserver> observer_list_;
145 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
148 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
149 : use_ipc_command_buffer_(false),
150 num_hardware_compositors_(0) {
151 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
152 switches::kSingleProcess)) {
153 // TODO(boliu): Figure out how to deal with this more nicely.
154 SynchronousCompositorFactory::SetInstance(this);
158 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
160 scoped_refptr<base::SingleThreadTaskRunner>
161 SynchronousCompositorFactoryImpl::GetCompositorTaskRunner() {
162 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
165 scoped_ptr<cc::OutputSurface>
166 SynchronousCompositorFactoryImpl::CreateOutputSurface(
167 int routing_id,
168 int surface_id,
169 scoped_refptr<content::FrameSwapMessageQueue> frame_swap_message_queue) {
170 scoped_refptr<cc::ContextProvider> onscreen_context =
171 CreateContextProviderForCompositor(surface_id, RENDER_COMPOSITOR_CONTEXT);
172 scoped_refptr<cc::ContextProvider> worker_context =
173 CreateContextProviderForCompositor(0, RENDER_WORKER_CONTEXT);
175 return make_scoped_ptr(new SynchronousCompositorOutputSurface(
176 onscreen_context, worker_context, routing_id, frame_swap_message_queue));
179 InputHandlerManagerClient*
180 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
181 return synchronous_input_event_filter();
184 scoped_ptr<cc::BeginFrameSource>
185 SynchronousCompositorFactoryImpl::CreateExternalBeginFrameSource(
186 int routing_id) {
187 return make_scoped_ptr(
188 new SynchronousCompositorExternalBeginFrameSource(routing_id));
191 bool SynchronousCompositorFactoryImpl::OverrideWithFactory() {
192 return !use_ipc_command_buffer_;
195 scoped_refptr<ContextProviderWebContext>
196 SynchronousCompositorFactoryImpl::CreateOffscreenContextProvider(
197 const blink::WebGraphicsContext3D::Attributes& attributes,
198 const std::string& debug_name) {
199 DCHECK(!use_ipc_command_buffer_);
200 ContextHolder holder =
201 CreateContextHolder(attributes, GpuThreadService(),
202 gpu::GLInProcessContextSharedMemoryLimits(), true);
203 return ContextProviderInProcess::Create(holder.command_buffer.Pass(),
204 debug_name);
207 scoped_refptr<cc::ContextProvider>
208 SynchronousCompositorFactoryImpl::CreateContextProviderForCompositor(
209 int surface_id,
210 CommandBufferContextType type) {
211 // This is half of what RenderWidget uses because synchronous compositor
212 // pipeline is only one frame deep. But twice of half for low end here
213 // because 16bit texture is not supported.
214 unsigned int mapped_memory_reclaim_limit =
215 (base::SysInfo::IsLowEndDevice() ? 2 : 6) * 1024 * 1024;
216 blink::WebGraphicsContext3D::Attributes attributes = GetDefaultAttribs();
218 if (use_ipc_command_buffer_) {
219 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits mem_limits;
220 mem_limits.mapped_memory_reclaim_limit = mapped_memory_reclaim_limit;
221 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context =
222 CreateContext3D(surface_id, GetDefaultAttribs(), mem_limits);
223 return make_scoped_refptr(
224 new SynchronousCompositorContextProvider(context.Pass(), type));
227 gpu::GLInProcessContextSharedMemoryLimits mem_limits;
228 mem_limits.mapped_memory_reclaim_limit = mapped_memory_reclaim_limit;
229 ContextHolder holder =
230 CreateContextHolder(attributes, GpuThreadService(), mem_limits, true);
231 return ContextProviderInProcess::Create(holder.command_buffer.Pass(),
232 "Child-Compositor");
235 scoped_refptr<StreamTextureFactory>
236 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
237 scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
238 StreamTextureFactorySynchronousImpl::Create(
239 base::Bind(
240 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
241 base::Unretained(this)),
242 frame_id));
243 return factory;
246 WebGraphicsContext3DInProcessCommandBufferImpl*
247 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
248 const blink::WebGraphicsContext3D::Attributes& attributes) {
249 DCHECK(!use_ipc_command_buffer_);
250 ContextHolder holder =
251 CreateContextHolder(attributes, GpuThreadService(),
252 gpu::GLInProcessContextSharedMemoryLimits(), true);
253 return holder.command_buffer.release();
256 gpu::GPUInfo SynchronousCompositorFactoryImpl::GetGPUInfo() const {
257 DCHECK(!use_ipc_command_buffer_);
258 return content::GpuDataManager::GetInstance()->GetGPUInfo();
261 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
262 base::AutoLock lock(num_hardware_compositor_lock_);
263 num_hardware_compositors_++;
264 if (num_hardware_compositors_ == 1 && main_thread_task_runner_.get()) {
265 main_thread_task_runner_->PostTask(
266 FROM_HERE,
267 base::Bind(
268 &SynchronousCompositorFactoryImpl::RestoreContextOnMainThread,
269 base::Unretained(this)));
273 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
274 base::AutoLock lock(num_hardware_compositor_lock_);
275 DCHECK_GT(num_hardware_compositors_, 0u);
276 num_hardware_compositors_--;
279 void SynchronousCompositorFactoryImpl::RestoreContextOnMainThread() {
280 if (CanCreateMainThreadContext() && video_context_provider_.get())
281 video_context_provider_->RestoreContext();
284 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
285 base::AutoLock lock(num_hardware_compositor_lock_);
286 return num_hardware_compositors_ > 0;
289 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
290 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
292 base::AutoLock lock(num_hardware_compositor_lock_);
293 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get();
296 // Always fail creation even if |video_context_provider_| is not NULL.
297 // This is to avoid synchronous calls that may deadlock. Setting
298 // |video_context_provider_| to null is also not safe since it makes
299 // synchronous destruction uncontrolled and possibly deadlock.
300 if (!CanCreateMainThreadContext()) {
301 return
302 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>();
305 if (!video_context_provider_.get()) {
306 DCHECK(android_view_service_.get());
308 blink::WebGraphicsContext3D::Attributes attributes = GetDefaultAttribs();
309 attributes.shareResources = false;
310 // This needs to run in on-screen |android_view_service_| context due to
311 // SurfaceTexture limitations.
312 ContextHolder holder =
313 CreateContextHolder(attributes, android_view_service_,
314 gpu::GLInProcessContextSharedMemoryLimits(), false);
315 video_context_provider_ = new VideoContextProvider(
316 ContextProviderInProcess::Create(holder.command_buffer.Pass(),
317 "Video-Offscreen-main-thread"),
318 holder.gl_in_process_context);
320 return video_context_provider_;
323 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
324 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
325 DCHECK(!android_view_service_.get());
326 android_view_service_ = service;
329 base::Thread* SynchronousCompositorFactoryImpl::CreateInProcessGpuThread(
330 const InProcessChildThreadParams& params) {
331 DCHECK(android_view_service_.get());
332 return new InProcessGpuThread(params,
333 android_view_service_->sync_point_manager());
336 scoped_refptr<gpu::InProcessCommandBuffer::Service>
337 SynchronousCompositorFactoryImpl::GpuThreadService() {
338 DCHECK(android_view_service_.get());
339 // Create thread lazily on first use.
340 if (!gpu_thread_service_.get()) {
341 gpu_thread_service_ = new gpu::GpuInProcessThread(
342 android_view_service_->sync_point_manager());
344 return gpu_thread_service_;
347 void SynchronousCompositorFactoryImpl::SetUseIpcCommandBuffer() {
348 use_ipc_command_buffer_ = true;
351 } // namespace content