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_external_begin_frame_source.h"
13 #include "content/browser/android/in_process/synchronous_compositor_impl.h"
14 #include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
15 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
16 #include "content/common/gpu/client/context_provider_command_buffer.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/renderer/gpu/frame_swap_message_queue.h"
23 #include "content/renderer/render_thread_impl.h"
24 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
25 #include "gpu/command_buffer/client/gl_in_process_context.h"
26 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
27 #include "gpu/command_buffer/service/gpu_switches.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
;
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;
56 ContextHolder
CreateContextHolder(
57 const blink::WebGraphicsContext3D::Attributes
& attributes
,
58 scoped_refptr
<gpu::InProcessCommandBuffer::Service
> service
,
59 const gpu::GLInProcessContextSharedMemoryLimits
& mem_limits
,
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();
74 holder
.command_buffer
=
75 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
>(
76 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
77 context
.Pass(), attributes
));
78 holder
.gl_in_process_context
= context_ptr
;
83 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> CreateContext3D(
85 const blink::WebGraphicsContext3D::Attributes
& attributes
,
86 const content::WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits
&
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
));
103 class SynchronousCompositorFactoryImpl::VideoContextProvider
104 : public StreamTextureFactorySynchronousImpl::ContextProvider
{
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
,
134 ResetStreamTextureProxy());
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 : record_full_layer_(true),
150 use_ipc_command_buffer_(false),
151 num_hardware_compositors_(0) {
152 SynchronousCompositorFactory::SetInstance(this);
155 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
157 scoped_refptr
<base::SingleThreadTaskRunner
>
158 SynchronousCompositorFactoryImpl::GetCompositorTaskRunner() {
159 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
);
163 SynchronousCompositorFactoryImpl::RecordFullLayer() {
164 return record_full_layer_
;
167 scoped_ptr
<cc::OutputSurface
>
168 SynchronousCompositorFactoryImpl::CreateOutputSurface(
171 scoped_refptr
<content::FrameSwapMessageQueue
> frame_swap_message_queue
) {
172 scoped_refptr
<cc::ContextProvider
> onscreen_context
=
173 CreateContextProviderForCompositor(surface_id
, RENDER_COMPOSITOR_CONTEXT
);
174 scoped_refptr
<cc::ContextProvider
> worker_context
=
175 CreateContextProviderForCompositor(0, RENDER_WORKER_CONTEXT
);
177 return make_scoped_ptr(new SynchronousCompositorOutputSurface(
178 onscreen_context
, worker_context
, routing_id
, frame_swap_message_queue
));
181 InputHandlerManagerClient
*
182 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
183 return synchronous_input_event_filter();
186 scoped_ptr
<cc::BeginFrameSource
>
187 SynchronousCompositorFactoryImpl::CreateExternalBeginFrameSource(
189 return make_scoped_ptr(
190 new SynchronousCompositorExternalBeginFrameSource(routing_id
));
193 bool SynchronousCompositorFactoryImpl::OverrideWithFactory() {
194 return !use_ipc_command_buffer_
;
197 scoped_refptr
<ContextProviderWebContext
>
198 SynchronousCompositorFactoryImpl::CreateOffscreenContextProvider(
199 const blink::WebGraphicsContext3D::Attributes
& attributes
,
200 const std::string
& debug_name
) {
201 DCHECK(!use_ipc_command_buffer_
);
202 ContextHolder holder
=
203 CreateContextHolder(attributes
, GpuThreadService(),
204 gpu::GLInProcessContextSharedMemoryLimits(), true);
205 return ContextProviderInProcess::Create(holder
.command_buffer
.Pass(),
209 scoped_refptr
<cc::ContextProvider
>
210 SynchronousCompositorFactoryImpl::CreateContextProviderForCompositor(
212 CommandBufferContextType type
) {
213 // This is half of what RenderWidget uses because synchronous compositor
214 // pipeline is only one frame deep. But twice of half for low end here
215 // because 16bit texture is not supported.
216 unsigned int mapped_memory_reclaim_limit
=
217 (base::SysInfo::IsLowEndDevice() ? 2 : 6) * 1024 * 1024;
218 blink::WebGraphicsContext3D::Attributes attributes
= GetDefaultAttribs();
220 if (use_ipc_command_buffer_
) {
221 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits mem_limits
;
222 mem_limits
.mapped_memory_reclaim_limit
= mapped_memory_reclaim_limit
;
223 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context
=
224 CreateContext3D(surface_id
, GetDefaultAttribs(), mem_limits
);
225 return ContextProviderCommandBuffer::Create(context
.Pass(), type
);
228 gpu::GLInProcessContextSharedMemoryLimits mem_limits
;
229 mem_limits
.mapped_memory_reclaim_limit
= mapped_memory_reclaim_limit
;
230 ContextHolder holder
=
231 CreateContextHolder(attributes
, GpuThreadService(), mem_limits
, true);
232 return ContextProviderInProcess::Create(holder
.command_buffer
.Pass(),
236 scoped_refptr
<StreamTextureFactory
>
237 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id
) {
238 scoped_refptr
<StreamTextureFactorySynchronousImpl
> factory(
239 StreamTextureFactorySynchronousImpl::Create(
241 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory
,
242 base::Unretained(this)),
247 WebGraphicsContext3DInProcessCommandBufferImpl
*
248 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
249 const blink::WebGraphicsContext3D::Attributes
& attributes
) {
250 DCHECK(!use_ipc_command_buffer_
);
251 ContextHolder holder
=
252 CreateContextHolder(attributes
, GpuThreadService(),
253 gpu::GLInProcessContextSharedMemoryLimits(), true);
254 return holder
.command_buffer
.release();
257 gpu::GPUInfo
SynchronousCompositorFactoryImpl::GetGPUInfo() const {
258 DCHECK(!use_ipc_command_buffer_
);
259 return content::GpuDataManager::GetInstance()->GetGPUInfo();
262 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
263 base::AutoLock
lock(num_hardware_compositor_lock_
);
264 num_hardware_compositors_
++;
265 if (num_hardware_compositors_
== 1 && main_thread_task_runner_
.get()) {
266 main_thread_task_runner_
->PostTask(
269 &SynchronousCompositorFactoryImpl::RestoreContextOnMainThread
,
270 base::Unretained(this)));
274 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
275 base::AutoLock
lock(num_hardware_compositor_lock_
);
276 DCHECK_GT(num_hardware_compositors_
, 0u);
277 num_hardware_compositors_
--;
280 void SynchronousCompositorFactoryImpl::RestoreContextOnMainThread() {
281 if (CanCreateMainThreadContext() && video_context_provider_
.get())
282 video_context_provider_
->RestoreContext();
285 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
286 base::AutoLock
lock(num_hardware_compositor_lock_
);
287 return num_hardware_compositors_
> 0;
290 scoped_refptr
<StreamTextureFactorySynchronousImpl::ContextProvider
>
291 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
293 base::AutoLock
lock(num_hardware_compositor_lock_
);
294 main_thread_task_runner_
= base::ThreadTaskRunnerHandle::Get();
297 // Always fail creation even if |video_context_provider_| is not NULL.
298 // This is to avoid synchronous calls that may deadlock. Setting
299 // |video_context_provider_| to null is also not safe since it makes
300 // synchronous destruction uncontrolled and possibly deadlock.
301 if (!CanCreateMainThreadContext()) {
303 scoped_refptr
<StreamTextureFactorySynchronousImpl::ContextProvider
>();
306 if (!video_context_provider_
.get()) {
307 DCHECK(android_view_service_
.get());
309 blink::WebGraphicsContext3D::Attributes attributes
= GetDefaultAttribs();
310 attributes
.shareResources
= false;
311 // This needs to run in on-screen |android_view_service_| context due to
312 // SurfaceTexture limitations.
313 ContextHolder holder
=
314 CreateContextHolder(attributes
, android_view_service_
,
315 gpu::GLInProcessContextSharedMemoryLimits(), false);
316 video_context_provider_
= new VideoContextProvider(
317 ContextProviderInProcess::Create(holder
.command_buffer
.Pass(),
318 "Video-Offscreen-main-thread"),
319 holder
.gl_in_process_context
);
321 return video_context_provider_
;
324 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
325 scoped_refptr
<gpu::InProcessCommandBuffer::Service
> service
) {
326 DCHECK(!android_view_service_
.get());
327 android_view_service_
= service
;
330 base::Thread
* SynchronousCompositorFactoryImpl::CreateInProcessGpuThread(
331 const InProcessChildThreadParams
& params
) {
332 DCHECK(android_view_service_
.get());
333 return new InProcessGpuThread(params
,
334 android_view_service_
->sync_point_manager());
337 scoped_refptr
<gpu::InProcessCommandBuffer::Service
>
338 SynchronousCompositorFactoryImpl::GpuThreadService() {
339 DCHECK(android_view_service_
.get());
340 // Create thread lazily on first use.
341 if (!gpu_thread_service_
.get()) {
342 gpu_thread_service_
= new gpu::GpuInProcessThread(
343 android_view_service_
->sync_point_manager());
345 return gpu_thread_service_
;
348 void SynchronousCompositorFactoryImpl::SetRecordFullDocument(
349 bool record_full_document
) {
350 record_full_layer_
= record_full_document
;
353 void SynchronousCompositorFactoryImpl::SetUseIpcCommandBuffer() {
354 use_ipc_command_buffer_
= true;
357 } // namespace content