Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / android / in_process / synchronous_compositor_factory_impl.cc
blob4aa5ddbd66d81407a330b2cd730e7aabbef12340
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;
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 const blink::WebGraphicsContext3D::Attributes& attributes,
85 const content::WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits&
86 mem_limits) {
87 DCHECK(RenderThreadImpl::current());
88 CauseForGpuLaunch cause =
89 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
90 scoped_refptr<GpuChannelHost> gpu_channel_host(
91 RenderThreadImpl::current()->EstablishGpuChannelSync(cause));
92 CHECK(gpu_channel_host.get());
94 int surface_id = 0;
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 : 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);
162 bool
163 SynchronousCompositorFactoryImpl::RecordFullLayer() {
164 return record_full_layer_;
167 scoped_ptr<cc::OutputSurface>
168 SynchronousCompositorFactoryImpl::CreateOutputSurface(
169 int routing_id,
170 scoped_refptr<content::FrameSwapMessageQueue> frame_swap_message_queue) {
171 scoped_refptr<cc::ContextProvider> onscreen_context =
172 CreateContextProviderForCompositor(RENDER_COMPOSITOR_CONTEXT);
173 scoped_refptr<cc::ContextProvider> worker_context =
174 CreateContextProviderForCompositor(RENDER_WORKER_CONTEXT);
176 return make_scoped_ptr(new SynchronousCompositorOutputSurface(
177 onscreen_context, worker_context, routing_id, frame_swap_message_queue));
180 InputHandlerManagerClient*
181 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
182 return synchronous_input_event_filter();
185 scoped_ptr<cc::BeginFrameSource>
186 SynchronousCompositorFactoryImpl::CreateExternalBeginFrameSource(
187 int routing_id) {
188 return make_scoped_ptr(
189 new SynchronousCompositorExternalBeginFrameSource(routing_id));
192 bool SynchronousCompositorFactoryImpl::OverrideWithFactory() {
193 return !use_ipc_command_buffer_;
196 scoped_refptr<ContextProviderWebContext>
197 SynchronousCompositorFactoryImpl::CreateOffscreenContextProvider(
198 const blink::WebGraphicsContext3D::Attributes& attributes,
199 const std::string& debug_name) {
200 DCHECK(!use_ipc_command_buffer_);
201 ContextHolder holder =
202 CreateContextHolder(attributes, GpuThreadService(),
203 gpu::GLInProcessContextSharedMemoryLimits(), true);
204 return ContextProviderInProcess::Create(holder.command_buffer.Pass(),
205 debug_name);
208 scoped_refptr<cc::ContextProvider>
209 SynchronousCompositorFactoryImpl::CreateContextProviderForCompositor(
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(GetDefaultAttribs(), mem_limits);
223 return ContextProviderCommandBuffer::Create(context.Pass(), type);
226 gpu::GLInProcessContextSharedMemoryLimits mem_limits;
227 mem_limits.mapped_memory_reclaim_limit = mapped_memory_reclaim_limit;
228 ContextHolder holder =
229 CreateContextHolder(attributes, GpuThreadService(), mem_limits, true);
230 return ContextProviderInProcess::Create(holder.command_buffer.Pass(),
231 "Child-Compositor");
234 scoped_refptr<StreamTextureFactory>
235 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
236 scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
237 StreamTextureFactorySynchronousImpl::Create(
238 base::Bind(
239 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
240 base::Unretained(this)),
241 frame_id));
242 return factory;
245 WebGraphicsContext3DInProcessCommandBufferImpl*
246 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
247 const blink::WebGraphicsContext3D::Attributes& attributes) {
248 DCHECK(!use_ipc_command_buffer_);
249 ContextHolder holder =
250 CreateContextHolder(attributes, GpuThreadService(),
251 gpu::GLInProcessContextSharedMemoryLimits(), true);
252 return holder.command_buffer.release();
255 gpu::GPUInfo SynchronousCompositorFactoryImpl::GetGPUInfo() const {
256 DCHECK(!use_ipc_command_buffer_);
257 return content::GpuDataManager::GetInstance()->GetGPUInfo();
260 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
261 base::AutoLock lock(num_hardware_compositor_lock_);
262 num_hardware_compositors_++;
263 if (num_hardware_compositors_ == 1 && main_thread_task_runner_.get()) {
264 main_thread_task_runner_->PostTask(
265 FROM_HERE,
266 base::Bind(
267 &SynchronousCompositorFactoryImpl::RestoreContextOnMainThread,
268 base::Unretained(this)));
272 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
273 base::AutoLock lock(num_hardware_compositor_lock_);
274 DCHECK_GT(num_hardware_compositors_, 0u);
275 num_hardware_compositors_--;
278 void SynchronousCompositorFactoryImpl::RestoreContextOnMainThread() {
279 if (CanCreateMainThreadContext() && video_context_provider_.get())
280 video_context_provider_->RestoreContext();
283 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
284 base::AutoLock lock(num_hardware_compositor_lock_);
285 return num_hardware_compositors_ > 0;
288 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
289 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
291 base::AutoLock lock(num_hardware_compositor_lock_);
292 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get();
295 // Always fail creation even if |video_context_provider_| is not NULL.
296 // This is to avoid synchronous calls that may deadlock. Setting
297 // |video_context_provider_| to null is also not safe since it makes
298 // synchronous destruction uncontrolled and possibly deadlock.
299 if (!CanCreateMainThreadContext()) {
300 return
301 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>();
304 if (!video_context_provider_.get()) {
305 DCHECK(android_view_service_.get());
307 blink::WebGraphicsContext3D::Attributes attributes = GetDefaultAttribs();
308 attributes.shareResources = false;
309 // This needs to run in on-screen |android_view_service_| context due to
310 // SurfaceTexture limitations.
311 ContextHolder holder =
312 CreateContextHolder(attributes, android_view_service_,
313 gpu::GLInProcessContextSharedMemoryLimits(), false);
314 video_context_provider_ = new VideoContextProvider(
315 ContextProviderInProcess::Create(holder.command_buffer.Pass(),
316 "Video-Offscreen-main-thread"),
317 holder.gl_in_process_context);
319 return video_context_provider_;
322 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
323 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
324 DCHECK(!android_view_service_.get());
325 android_view_service_ = service;
328 base::Thread* SynchronousCompositorFactoryImpl::CreateInProcessGpuThread(
329 const InProcessChildThreadParams& params) {
330 DCHECK(android_view_service_.get());
331 return new InProcessGpuThread(params,
332 android_view_service_->sync_point_manager());
335 scoped_refptr<gpu::InProcessCommandBuffer::Service>
336 SynchronousCompositorFactoryImpl::GpuThreadService() {
337 DCHECK(android_view_service_.get());
338 // Create thread lazily on first use.
339 if (!gpu_thread_service_.get()) {
340 gpu_thread_service_ = new gpu::GpuInProcessThread(
341 android_view_service_->sync_point_manager());
343 return gpu_thread_service_;
346 void SynchronousCompositorFactoryImpl::SetRecordFullDocument(
347 bool record_full_document) {
348 record_full_layer_ = record_full_document;
351 void SynchronousCompositorFactoryImpl::SetUseIpcCommandBuffer() {
352 use_ipc_command_buffer_ = true;
355 } // namespace content