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 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
90 WrapContextWithAttributes(
91 scoped_ptr<gpu::GLInProcessContext> context,
92 const blink::WebGraphicsContext3D::Attributes& attributes) {
94 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
96 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
97 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
98 context.Pass(), attributes));
101 class VideoContextProvider
102 : public StreamTextureFactorySynchronousImpl::ContextProvider {
104 VideoContextProvider(
105 scoped_ptr<gpu::GLInProcessContext> gl_in_process_context)
106 : gl_in_process_context_(gl_in_process_context.get()) {
108 context_provider_ = webkit::gpu::ContextProviderInProcess::Create(
109 WrapContext(gl_in_process_context.Pass()),
110 "Video-Offscreen-main-thread");
111 context_provider_->BindToCurrentThread();
114 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
115 uint32 stream_id) OVERRIDE {
116 return gl_in_process_context_->GetSurfaceTexture(stream_id);
119 virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE {
120 return context_provider_->ContextGL();
124 friend class base::RefCountedThreadSafe<VideoContextProvider>;
125 virtual ~VideoContextProvider() {}
127 scoped_refptr<cc::ContextProvider> context_provider_;
128 gpu::GLInProcessContext* gl_in_process_context_;
130 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
135 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
137 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
138 : record_full_layer_(true),
139 num_hardware_compositors_(0) {
140 SynchronousCompositorFactory::SetInstance(this);
143 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
145 scoped_refptr<base::MessageLoopProxy>
146 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
147 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
151 SynchronousCompositorFactoryImpl::RecordFullLayer() {
152 return record_full_layer_;
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>();
162 InputHandlerManagerClient*
163 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
164 return synchronous_input_event_filter();
167 scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl::
168 GetSharedOffscreenContextProviderForMainThread() {
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();
183 offscreen_context_for_main_thread_ = NULL;
185 return offscreen_context_for_main_thread_;
188 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
189 CreateOnscreenContextProviderForCompositorThread() {
192 if (!share_context_.get())
193 share_context_ = CreateContext(service_, NULL);
194 return webkit::gpu::ContextProviderInProcess::Create(
195 WrapContext(CreateContext(service_, share_context_.get())),
199 gpu::GLInProcessContext* SynchronousCompositorFactoryImpl::GetShareContext() {
200 DCHECK(share_context_.get());
201 return share_context_.get();
204 scoped_refptr<StreamTextureFactory>
205 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
206 scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
207 StreamTextureFactorySynchronousImpl::Create(
209 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
210 base::Unretained(this)),
215 blink::WebGraphicsContext3D*
216 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
217 const blink::WebGraphicsContext3D::Attributes& attributes) {
218 return WrapContextWithAttributes(CreateOffscreenContext(attributes),
219 attributes).release();
222 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
223 base::AutoLock lock(num_hardware_compositor_lock_);
224 num_hardware_compositors_++;
227 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
228 base::AutoLock lock(num_hardware_compositor_lock_);
229 DCHECK_GT(num_hardware_compositors_, 0u);
230 num_hardware_compositors_--;
233 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
234 base::AutoLock lock(num_hardware_compositor_lock_);
235 return num_hardware_compositors_ > 0;
238 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
239 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
240 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
242 // This check only guarantees the main thread context is created after
243 // a compositor did successfully initialize hardware draw in the past.
244 // In particular this does not guarantee that the main thread context
245 // will fail creation when all compositors release hardware draw.
246 if (CanCreateMainThreadContext() && !video_context_provider_) {
248 DCHECK(share_context_.get());
250 video_context_provider_ = new VideoContextProvider(
251 CreateContext(service_, share_context_.get()));
253 return video_context_provider_;
256 void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
257 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
262 void SynchronousCompositorFactoryImpl::SetRecordFullDocument(
263 bool record_full_document) {
264 record_full_layer_ = record_full_document;
267 } // namespace content