Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / content / browser / android / in_process / synchronous_compositor_factory_impl.cc
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.
4
5 #include "content/browser/android/in_process/synchronous_compositor_factory_impl.h"
6
7 #include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/renderer/gpu/frame_swap_message_queue.h"
10 #include "gpu/command_buffer/client/gl_in_process_context.h"
11 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
12 #include "ui/gl/android/surface_texture.h"
13 #include "ui/gl/gl_surface.h"
14 #include "ui/gl/gl_surface_stub.h"
15 #include "webkit/common/gpu/context_provider_in_process.h"
16 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
17
18 using webkit::gpu::ContextProviderWebContext;
19
20 namespace content {
21
22 namespace {
23
24 blink::WebGraphicsContext3D::Attributes GetDefaultAttribs() {
25   blink::WebGraphicsContext3D::Attributes attributes;
26   attributes.antialias = false;
27   attributes.depth = false;
28   attributes.stencil = false;
29   attributes.shareResources = true;
30   attributes.noAutomaticFlushes = true;
31
32   return attributes;
33 }
34
35 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
36 using webkit::gpu::WebGraphicsContext3DImpl;
37
38 scoped_ptr<gpu::GLInProcessContext> CreateOffscreenContext(
39     const blink::WebGraphicsContext3D::Attributes& attributes) {
40   const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
41
42   gpu::gles2::ContextCreationAttribHelper in_process_attribs;
43   WebGraphicsContext3DImpl::ConvertAttributes(
44       attributes, &in_process_attribs);
45   in_process_attribs.lose_context_when_out_of_memory = true;
46
47   scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create(
48       NULL /* service */,
49       NULL /* surface */,
50       true /* is_offscreen */,
51       gfx::kNullAcceleratedWidget,
52       gfx::Size(1, 1),
53       NULL /* share_context */,
54       false /* share_resources */,
55       in_process_attribs,
56       gpu_preference,
57       gpu::GLInProcessContextSharedMemoryLimits()));
58   return context.Pass();
59 }
60
61 scoped_ptr<gpu::GLInProcessContext> CreateContext(
62     scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
63     gpu::GLInProcessContext* share_context,
64     const gpu::GLInProcessContextSharedMemoryLimits& mem_limits) {
65   const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
66   gpu::gles2::ContextCreationAttribHelper in_process_attribs;
67   WebGraphicsContext3DImpl::ConvertAttributes(
68       GetDefaultAttribs(), &in_process_attribs);
69   in_process_attribs.lose_context_when_out_of_memory = true;
70
71   scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create(
72       service,
73       NULL /* surface */,
74       false /* is_offscreen */,
75       gfx::kNullAcceleratedWidget,
76       gfx::Size(1, 1),
77       share_context,
78       false /* share_resources */,
79       in_process_attribs,
80       gpu_preference,
81       mem_limits));
82   return context.Pass();
83 }
84
85 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext(
86     scoped_ptr<gpu::GLInProcessContext> context) {
87   if (!context.get())
88     return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
89
90   return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
91       WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
92           context.Pass(), GetDefaultAttribs()));
93 }
94
95 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
96 WrapContextWithAttributes(
97     scoped_ptr<gpu::GLInProcessContext> context,
98     const blink::WebGraphicsContext3D::Attributes& attributes) {
99   if (!context.get())
100     return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
101
102   return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
103       WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
104           context.Pass(), attributes));
105 }
106
107 class VideoContextProvider
108     : public StreamTextureFactorySynchronousImpl::ContextProvider {
109  public:
110   VideoContextProvider(
111       scoped_ptr<gpu::GLInProcessContext> gl_in_process_context)
112       : gl_in_process_context_(gl_in_process_context.get()) {
113
114     context_provider_ = webkit::gpu::ContextProviderInProcess::Create(
115         WrapContext(gl_in_process_context.Pass()),
116         "Video-Offscreen-main-thread");
117     context_provider_->BindToCurrentThread();
118   }
119
120   virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
121       uint32 stream_id) OVERRIDE {
122     return gl_in_process_context_->GetSurfaceTexture(stream_id);
123   }
124
125   virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE {
126     return context_provider_->ContextGL();
127   }
128
129  private:
130   friend class base::RefCountedThreadSafe<VideoContextProvider>;
131   virtual ~VideoContextProvider() {}
132
133   scoped_refptr<cc::ContextProvider> context_provider_;
134   gpu::GLInProcessContext* gl_in_process_context_;
135
136   DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
137 };
138
139 }  // namespace
140
141 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
142
143 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
144     : record_full_layer_(true),
145       num_hardware_compositors_(0) {
146   SynchronousCompositorFactory::SetInstance(this);
147 }
148
149 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
150
151 scoped_refptr<base::MessageLoopProxy>
152 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
153   return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
154 }
155
156 bool
157 SynchronousCompositorFactoryImpl::RecordFullLayer() {
158   return record_full_layer_;
159 }
160
161 scoped_ptr<cc::OutputSurface>
162 SynchronousCompositorFactoryImpl::CreateOutputSurface(
163     int routing_id,
164     scoped_refptr<content::FrameSwapMessageQueue> frame_swap_message_queue) {
165   scoped_ptr<SynchronousCompositorOutputSurface> output_surface(
166       new SynchronousCompositorOutputSurface(routing_id,
167                                              frame_swap_message_queue));
168   return output_surface.PassAs<cc::OutputSurface>();
169 }
170
171 InputHandlerManagerClient*
172 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
173   return synchronous_input_event_filter();
174 }
175
176 scoped_refptr<ContextProviderWebContext>
177 SynchronousCompositorFactoryImpl::CreateOffscreenContextProvider(
178     const blink::WebGraphicsContext3D::Attributes& attributes,
179     const std::string& debug_name) {
180   scoped_ptr<gpu::GLInProcessContext> context =
181       CreateOffscreenContext(attributes);
182   return webkit::gpu::ContextProviderInProcess::Create(
183       WrapContext(context.Pass()), debug_name);
184 }
185
186 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
187     CreateOnscreenContextProviderForCompositorThread() {
188   DCHECK(service_);
189
190   if (!share_context_.get()) {
191     share_context_ = CreateContext(
192         service_, NULL, gpu::GLInProcessContextSharedMemoryLimits());
193   }
194   gpu::GLInProcessContextSharedMemoryLimits mem_limits;
195   // This is half of what RenderWidget uses because synchronous compositor
196   // pipeline is only one frame deep.
197   mem_limits.mapped_memory_reclaim_limit = 6 * 1024 * 1024;
198   return webkit::gpu::ContextProviderInProcess::Create(
199       WrapContext(CreateContext(service_, share_context_.get(), mem_limits)),
200       "Child-Compositor");
201 }
202
203 gpu::GLInProcessContext* SynchronousCompositorFactoryImpl::GetShareContext() {
204   DCHECK(share_context_.get());
205   return share_context_.get();
206 }
207
208 scoped_refptr<StreamTextureFactory>
209 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
210   scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
211       StreamTextureFactorySynchronousImpl::Create(
212           base::Bind(
213               &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
214               base::Unretained(this)),
215           frame_id));
216   return factory;
217 }
218
219 blink::WebGraphicsContext3D*
220 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
221     const blink::WebGraphicsContext3D::Attributes& attributes) {
222   return WrapContextWithAttributes(CreateOffscreenContext(attributes),
223                                    attributes).release();
224 }
225
226 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
227   base::AutoLock lock(num_hardware_compositor_lock_);
228   num_hardware_compositors_++;
229 }
230
231 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
232   base::AutoLock lock(num_hardware_compositor_lock_);
233   DCHECK_GT(num_hardware_compositors_, 0u);
234   num_hardware_compositors_--;
235 }
236
237 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
238   base::AutoLock lock(num_hardware_compositor_lock_);
239   return num_hardware_compositors_ > 0;
240 }
241
242 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
243 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
244   // Always fail creation even if |video_context_provider_| is not NULL.
245   // This is to avoid synchronous calls that may deadlock. Setting
246   // |video_context_provider_| to null is also not safe since it makes
247   // synchronous destruction uncontrolled and possibly deadlock.
248   if (!CanCreateMainThreadContext()) {
249     return
250         scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>();
251   }
252
253   if (!video_context_provider_) {
254     DCHECK(service_);
255     DCHECK(share_context_.get());
256
257     video_context_provider_ = new VideoContextProvider(
258         CreateContext(service_,
259                       share_context_.get(),
260                       gpu::GLInProcessContextSharedMemoryLimits()));
261   }
262   return video_context_provider_;
263 }
264
265 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
266     scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
267   DCHECK(!service_);
268   service_ = service;
269 }
270
271 void SynchronousCompositorFactoryImpl::SetRecordFullDocument(
272     bool record_full_document) {
273   record_full_layer_ = record_full_document;
274 }
275
276 }  // namespace content