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