4449ca19fa4df556499d5dc7f75fd10d6e6b8da0
[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 class VideoContextProvider
90     : public StreamTextureFactorySynchronousImpl::ContextProvider {
91  public:
92   VideoContextProvider(
93       scoped_ptr<gpu::GLInProcessContext> gl_in_process_context)
94       : gl_in_process_context_(gl_in_process_context.get()) {
95
96     context_provider_ = webkit::gpu::ContextProviderInProcess::Create(
97         WrapContext(gl_in_process_context.Pass()),
98         "Video-Offscreen-main-thread");
99     context_provider_->BindToCurrentThread();
100   }
101
102   virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
103       uint32 stream_id) OVERRIDE {
104     return gl_in_process_context_->GetSurfaceTexture(stream_id);
105   }
106
107   virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE {
108     return context_provider_->ContextGL();
109   }
110
111  private:
112   friend class base::RefCountedThreadSafe<VideoContextProvider>;
113   virtual ~VideoContextProvider() {}
114
115   scoped_refptr<cc::ContextProvider> context_provider_;
116   gpu::GLInProcessContext* gl_in_process_context_;
117
118   DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
119 };
120
121 }  // namespace
122
123 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
124
125 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
126     : record_full_layer_(true),
127       num_hardware_compositors_(0) {
128   SynchronousCompositorFactory::SetInstance(this);
129 }
130
131 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
132
133 scoped_refptr<base::MessageLoopProxy>
134 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
135   return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
136 }
137
138 bool
139 SynchronousCompositorFactoryImpl::RecordFullLayer() {
140   return record_full_layer_;
141 }
142
143 scoped_ptr<cc::OutputSurface>
144 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) {
145   scoped_ptr<SynchronousCompositorOutputSurface> output_surface(
146       new SynchronousCompositorOutputSurface(routing_id));
147   return output_surface.PassAs<cc::OutputSurface>();
148 }
149
150 InputHandlerManagerClient*
151 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
152   return synchronous_input_event_filter();
153 }
154
155 scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl::
156     GetSharedOffscreenContextProviderForMainThread() {
157   bool failed = false;
158   if ((!offscreen_context_for_main_thread_.get() ||
159        offscreen_context_for_main_thread_->DestroyedOnMainThread())) {
160     scoped_ptr<gpu::GLInProcessContext> context =
161         CreateOffscreenContext(GetDefaultAttribs());
162     offscreen_context_for_main_thread_ =
163         webkit::gpu::ContextProviderInProcess::Create(
164             WrapContext(context.Pass()),
165             "Compositor-Offscreen-main-thread");
166     failed = !offscreen_context_for_main_thread_.get() ||
167              !offscreen_context_for_main_thread_->BindToCurrentThread();
168   }
169
170   if (failed) {
171     offscreen_context_for_main_thread_ = NULL;
172   }
173   return offscreen_context_for_main_thread_;
174 }
175
176 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
177     CreateOnscreenContextProviderForCompositorThread() {
178   DCHECK(service_);
179
180   if (!share_context_.get())
181     share_context_ = CreateContext(service_, NULL);
182   return webkit::gpu::ContextProviderInProcess::Create(
183       WrapContext(CreateContext(service_, share_context_.get())),
184       "Child-Compositor");
185 }
186
187 gpu::GLInProcessContext* SynchronousCompositorFactoryImpl::GetShareContext() {
188   DCHECK(share_context_.get());
189   return share_context_.get();
190 }
191
192 scoped_refptr<StreamTextureFactory>
193 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
194   scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
195       StreamTextureFactorySynchronousImpl::Create(
196           base::Bind(
197               &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
198               base::Unretained(this)),
199           frame_id));
200   return factory;
201 }
202
203 blink::WebGraphicsContext3D*
204 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
205     const blink::WebGraphicsContext3D::Attributes& attributes) {
206   return WrapContext(CreateOffscreenContext(attributes)).release();
207 }
208
209 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
210   base::AutoLock lock(num_hardware_compositor_lock_);
211   num_hardware_compositors_++;
212 }
213
214 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
215   base::AutoLock lock(num_hardware_compositor_lock_);
216   DCHECK_GT(num_hardware_compositors_, 0u);
217   num_hardware_compositors_--;
218 }
219
220 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
221   base::AutoLock lock(num_hardware_compositor_lock_);
222   return num_hardware_compositors_ > 0;
223 }
224
225 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
226 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
227   scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
228       context_provider;
229   // This check only guarantees the main thread context is created after
230   // a compositor did successfully initialize hardware draw in the past.
231   // In particular this does not guarantee that the main thread context
232   // will fail creation when all compositors release hardware draw.
233   if (CanCreateMainThreadContext() && !video_context_provider_) {
234     DCHECK(service_);
235     DCHECK(share_context_.get());
236
237     video_context_provider_ = new VideoContextProvider(
238         CreateContext(service_, share_context_.get()));
239   }
240   return video_context_provider_;
241 }
242
243 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
244     scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
245   DCHECK(!service_);
246   service_ = service;
247 }
248
249 void SynchronousCompositorFactoryImpl::DisableRecordFullLayer() {
250   record_full_layer_ = false;
251 }
252
253 }  // namespace content