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 "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"
16 using webkit::gpu::ContextProviderWebContext;
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;
33 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
35 scoped_ptr<gpu::GLInProcessContext> CreateOffscreenContext(
36 const blink::WebGraphicsContext3D::Attributes& attributes) {
37 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
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;
44 scoped_ptr<gpu::GLInProcessContext> context(
45 gpu::GLInProcessContext::Create(NULL /* service */,
47 true /* is_offscreen */,
48 gfx::kNullAcceleratedWidget,
50 NULL /* share_context */,
51 false /* share_resources */,
54 return context.Pass();
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;
66 scoped_ptr<gpu::GLInProcessContext> context(
67 gpu::GLInProcessContext::Create(service,
69 false /* is_offscreen */,
70 gfx::kNullAcceleratedWidget,
73 false /* share_resources */,
76 return context.Pass();
79 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext(
80 scoped_ptr<gpu::GLInProcessContext> context) {
82 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
84 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
85 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
86 context.Pass(), GetDefaultAttribs()));
89 class VideoContextProvider
90 : public StreamTextureFactorySynchronousImpl::ContextProvider {
93 scoped_ptr<gpu::GLInProcessContext> gl_in_process_context)
94 : gl_in_process_context_(gl_in_process_context.get()) {
96 context_provider_ = webkit::gpu::ContextProviderInProcess::Create(
97 WrapContext(gl_in_process_context.Pass()),
98 "Video-Offscreen-main-thread");
99 context_provider_->BindToCurrentThread();
102 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
103 uint32 stream_id) OVERRIDE {
104 return gl_in_process_context_->GetSurfaceTexture(stream_id);
107 virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE {
108 return context_provider_->ContextGL();
112 friend class base::RefCountedThreadSafe<VideoContextProvider>;
113 virtual ~VideoContextProvider() {}
115 scoped_refptr<cc::ContextProvider> context_provider_;
116 gpu::GLInProcessContext* gl_in_process_context_;
118 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
123 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
125 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
126 : record_full_layer_(true),
127 num_hardware_compositors_(0) {
128 SynchronousCompositorFactory::SetInstance(this);
131 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
133 scoped_refptr<base::MessageLoopProxy>
134 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
135 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
139 SynchronousCompositorFactoryImpl::RecordFullLayer() {
140 return record_full_layer_;
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>();
150 InputHandlerManagerClient*
151 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
152 return synchronous_input_event_filter();
155 scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl::
156 GetSharedOffscreenContextProviderForMainThread() {
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();
171 offscreen_context_for_main_thread_ = NULL;
173 return offscreen_context_for_main_thread_;
176 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
177 CreateOnscreenContextProviderForCompositorThread() {
180 if (!share_context_.get())
181 share_context_ = CreateContext(service_, NULL);
182 return webkit::gpu::ContextProviderInProcess::Create(
183 WrapContext(CreateContext(service_, share_context_.get())),
187 gpu::GLInProcessContext* SynchronousCompositorFactoryImpl::GetShareContext() {
188 DCHECK(share_context_.get());
189 return share_context_.get();
192 scoped_refptr<StreamTextureFactory>
193 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
194 scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
195 StreamTextureFactorySynchronousImpl::Create(
197 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
198 base::Unretained(this)),
203 blink::WebGraphicsContext3D*
204 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
205 const blink::WebGraphicsContext3D::Attributes& attributes) {
206 return WrapContext(CreateOffscreenContext(attributes)).release();
209 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
210 base::AutoLock lock(num_hardware_compositor_lock_);
211 num_hardware_compositors_++;
214 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
215 base::AutoLock lock(num_hardware_compositor_lock_);
216 DCHECK_GT(num_hardware_compositors_, 0u);
217 num_hardware_compositors_--;
220 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
221 base::AutoLock lock(num_hardware_compositor_lock_);
222 return num_hardware_compositors_ > 0;
225 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
226 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
227 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
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_) {
235 DCHECK(share_context_.get());
237 video_context_provider_ = new VideoContextProvider(
238 CreateContext(service_, share_context_.get()));
240 return video_context_provider_;
243 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
244 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
249 void SynchronousCompositorFactoryImpl::DisableRecordFullLayer() {
250 record_full_layer_ = false;
253 } // namespace content