| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/android/in_process/synchronous_compositor_factory_impl
.h" | 5 #include "content/browser/android/in_process/synchronous_compositor_factory_impl
.h" |
| 6 | 6 |
| 7 #include "content/browser/android/in_process/synchronous_compositor_output_surfa
ce.h" | 7 #include "content/browser/android/in_process/synchronous_compositor_output_surfa
ce.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "gpu/command_buffer/client/gl_in_process_context.h" | 9 #include "gpu/command_buffer/client/gl_in_process_context.h" |
| 10 #include "ui/gl/android/surface_texture.h" | 10 #include "ui/gl/android/surface_texture.h" |
| 11 #include "ui/gl/gl_surface.h" |
| 12 #include "ui/gl/gl_surface_stub.h" |
| 11 #include "webkit/common/gpu/context_provider_in_process.h" | 13 #include "webkit/common/gpu/context_provider_in_process.h" |
| 12 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | 14 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
| 13 | 15 |
| 14 using webkit::gpu::ContextProviderWebContext; | 16 using webkit::gpu::ContextProviderWebContext; |
| 15 | 17 |
| 16 namespace content { | 18 namespace content { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 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> CreateContext( |
| 36 scoped_refptr<gfx::GLSurface> surface, |
| 37 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, |
| 38 gpu::GLInProcessContext* share_context) { |
| 39 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
| 40 |
| 41 if (!surface) |
| 42 surface = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)); |
| 43 |
| 44 gpu::GLInProcessContextAttribs in_process_attribs; |
| 45 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( |
| 46 GetDefaultAttribs(), &in_process_attribs); |
| 47 scoped_ptr<gpu::GLInProcessContext> context( |
| 48 gpu::GLInProcessContext::CreateWithSurface(surface, |
| 49 service, |
| 50 share_context, |
| 51 in_process_attribs, |
| 52 gpu_preference)); |
| 53 return context.Pass(); |
| 54 } |
| 55 |
| 56 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext( |
| 57 scoped_ptr<gpu::GLInProcessContext> context) { |
| 58 if (!context.get()) |
| 59 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); |
| 60 |
| 61 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( |
| 62 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( |
| 63 context.Pass(), GetDefaultAttribs())); |
| 64 } |
| 65 |
| 20 class VideoContextProvider | 66 class VideoContextProvider |
| 21 : public StreamTextureFactorySynchronousImpl::ContextProvider { | 67 : public StreamTextureFactorySynchronousImpl::ContextProvider { |
| 22 public: | 68 public: |
| 23 VideoContextProvider( | 69 VideoContextProvider( |
| 24 const scoped_refptr<cc::ContextProvider>& context_provider, | 70 scoped_ptr<gpu::GLInProcessContext> gl_in_process_context) |
| 25 gpu::GLInProcessContext* gl_in_process_context) | 71 : gl_in_process_context_(gl_in_process_context.get()) { |
| 26 : context_provider_(context_provider), | 72 |
| 27 gl_in_process_context_(gl_in_process_context) {} | 73 context_provider_ = webkit::gpu::ContextProviderInProcess::Create( |
| 74 WrapContext(gl_in_process_context.Pass()), |
| 75 "Video-Offscreen-main-thread"); |
| 76 context_provider_->BindToCurrentThread(); |
| 77 } |
| 28 | 78 |
| 29 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture( | 79 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture( |
| 30 uint32 stream_id) OVERRIDE { | 80 uint32 stream_id) OVERRIDE { |
| 31 return gl_in_process_context_->GetSurfaceTexture(stream_id); | 81 return gl_in_process_context_->GetSurfaceTexture(stream_id); |
| 32 } | 82 } |
| 33 | 83 |
| 34 virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE { | 84 virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE { |
| 35 return context_provider_->ContextGL(); | 85 return context_provider_->ContextGL(); |
| 36 } | 86 } |
| 37 | 87 |
| 38 private: | 88 private: |
| 39 friend class base::RefCountedThreadSafe<VideoContextProvider>; | 89 friend class base::RefCountedThreadSafe<VideoContextProvider>; |
| 40 virtual ~VideoContextProvider() {} | 90 virtual ~VideoContextProvider() {} |
| 41 | 91 |
| 42 scoped_refptr<cc::ContextProvider> context_provider_; | 92 scoped_refptr<cc::ContextProvider> context_provider_; |
| 43 gpu::GLInProcessContext* gl_in_process_context_; | 93 gpu::GLInProcessContext* gl_in_process_context_; |
| 44 | 94 |
| 45 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); | 95 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); |
| 46 }; | 96 }; |
| 47 | 97 |
| 48 } // namespace | 98 } // namespace |
| 49 | 99 |
| 50 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | 100 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 51 | 101 |
| 52 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() | 102 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() |
| 53 : wrapped_gl_context_for_main_thread_(NULL), | 103 : wrapped_gl_context_for_main_thread_(NULL), |
| 104 wrapped_gl_context_for_compositor_thread_(NULL), |
| 54 num_hardware_compositors_(0) { | 105 num_hardware_compositors_(0) { |
| 55 SynchronousCompositorFactory::SetInstance(this); | 106 SynchronousCompositorFactory::SetInstance(this); |
| 56 } | 107 } |
| 57 | 108 |
| 58 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} | 109 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} |
| 59 | 110 |
| 60 scoped_refptr<base::MessageLoopProxy> | 111 scoped_refptr<base::MessageLoopProxy> |
| 61 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() { | 112 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() { |
| 62 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 113 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 63 } | 114 } |
| 64 | 115 |
| 65 scoped_ptr<cc::OutputSurface> | 116 scoped_ptr<cc::OutputSurface> |
| 66 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) { | 117 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) { |
| 67 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( | 118 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( |
| 68 new SynchronousCompositorOutputSurface(routing_id)); | 119 new SynchronousCompositorOutputSurface(routing_id)); |
| 69 return output_surface.PassAs<cc::OutputSurface>(); | 120 return output_surface.PassAs<cc::OutputSurface>(); |
| 70 } | 121 } |
| 71 | 122 |
| 72 InputHandlerManagerClient* | 123 InputHandlerManagerClient* |
| 73 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() { | 124 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() { |
| 74 return synchronous_input_event_filter(); | 125 return synchronous_input_event_filter(); |
| 75 } | 126 } |
| 76 | 127 |
| 77 scoped_refptr<ContextProviderWebContext> | 128 scoped_refptr<ContextProviderWebContext> |
| 78 SynchronousCompositorFactoryImpl::GetOffscreenContextProviderForMainThread() { | 129 SynchronousCompositorFactoryImpl::GetOffscreenContextProviderForMainThread() { |
| 79 // This check only guarantees the main thread context is created after | |
| 80 // a compositor did successfully initialize hardware draw in the past. | |
| 81 // In particular this does not guarantee that the main thread context | |
| 82 // will fail creation when all compositors release hardware draw. | |
| 83 bool failed = false; | 130 bool failed = false; |
| 84 if ((!offscreen_context_for_main_thread_.get() || | 131 if ((!offscreen_context_for_main_thread_.get() || |
| 85 offscreen_context_for_main_thread_->DestroyedOnMainThread())) { | 132 offscreen_context_for_main_thread_->DestroyedOnMainThread())) { |
| 133 scoped_ptr<gpu::GLInProcessContext> context = |
| 134 CreateContext(NULL, NULL, NULL); |
| 135 wrapped_gl_context_for_main_thread_ = context.get(); |
| 86 offscreen_context_for_main_thread_ = | 136 offscreen_context_for_main_thread_ = |
| 87 webkit::gpu::ContextProviderInProcess::Create( | 137 webkit::gpu::ContextProviderInProcess::Create( |
| 88 CreateOffscreenContext(), | 138 WrapContext(context.Pass()), |
| 89 "Compositor-Offscreen"); | 139 "Compositor-Offscreen-main-thread"); |
| 90 failed = !offscreen_context_for_main_thread_.get() || | 140 failed = !offscreen_context_for_main_thread_.get() || |
| 91 !offscreen_context_for_main_thread_->BindToCurrentThread(); | 141 !offscreen_context_for_main_thread_->BindToCurrentThread(); |
| 92 } | 142 } |
| 93 | 143 |
| 94 if (failed) { | 144 if (failed) { |
| 95 offscreen_context_for_main_thread_ = NULL; | 145 offscreen_context_for_main_thread_ = NULL; |
| 96 wrapped_gl_context_for_main_thread_ = NULL; | 146 wrapped_gl_context_for_main_thread_ = NULL; |
| 97 } | 147 } |
| 98 return offscreen_context_for_main_thread_; | 148 return offscreen_context_for_main_thread_; |
| 99 } | 149 } |
| 100 | 150 |
| 101 // This is called on both renderer main thread (offscreen context creation | 151 // This is called on both renderer main thread (offscreen context creation |
| 102 // path shared between cross-process and in-process platforms) and renderer | 152 // path shared between cross-process and in-process platforms) and renderer |
| 103 // compositor impl thread (InitializeHwDraw) in order to support Android | 153 // compositor impl thread (InitializeHwDraw) in order to support Android |
| 104 // WebView synchronously enable and disable hardware mode multiple times in | 154 // WebView synchronously enable and disable hardware mode multiple times in |
| 105 // the same task. This is ok because in-process WGC3D creation may happen on | 155 // the same task. This is ok because in-process WGC3D creation may happen on |
| 106 // any thread and is lightweight. | 156 // any thread and is lightweight. |
| 107 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: | 157 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: |
| 108 GetOffscreenContextProviderForCompositorThread() { | 158 GetOffscreenContextProviderForCompositorThread() { |
| 109 base::AutoLock lock(offscreen_context_for_compositor_thread_lock_); | 159 base::AutoLock lock(offscreen_context_for_compositor_thread_lock_); |
| 160 DCHECK(service_); |
| 161 bool failed = false; |
| 110 if (!offscreen_context_for_compositor_thread_.get() || | 162 if (!offscreen_context_for_compositor_thread_.get() || |
| 111 offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) { | 163 offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) { |
| 164 scoped_ptr<gpu::GLInProcessContext> context = |
| 165 CreateContext(new gfx::GLSurfaceStub, service_, NULL); |
| 166 wrapped_gl_context_for_compositor_thread_ = context.get(); |
| 112 offscreen_context_for_compositor_thread_ = | 167 offscreen_context_for_compositor_thread_ = |
| 113 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); | 168 webkit::gpu::ContextProviderInProcess::Create( |
| 169 WrapContext(context.Pass()), |
| 170 "Compositor-Offscreen-compositor-thread"); |
| 171 failed = !offscreen_context_for_compositor_thread_.get() || |
| 172 !offscreen_context_for_compositor_thread_->BindToCurrentThread(); |
| 173 } |
| 174 if (failed) { |
| 175 offscreen_context_for_compositor_thread_ = NULL; |
| 176 wrapped_gl_context_for_compositor_thread_ = NULL; |
| 114 } | 177 } |
| 115 return offscreen_context_for_compositor_thread_; | 178 return offscreen_context_for_compositor_thread_; |
| 116 } | 179 } |
| 117 | 180 |
| 181 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: |
| 182 CreateOnscreenContextProviderForCompositorThread( |
| 183 scoped_refptr<gfx::GLSurface> surface) { |
| 184 DCHECK(surface); |
| 185 DCHECK(service_); |
| 186 DCHECK(wrapped_gl_context_for_compositor_thread_); |
| 187 |
| 188 return webkit::gpu::ContextProviderInProcess::Create( |
| 189 WrapContext(CreateContext( |
| 190 surface, service_, wrapped_gl_context_for_compositor_thread_)), |
| 191 "Compositor-Onscreen"); |
| 192 } |
| 193 |
| 118 scoped_ptr<StreamTextureFactory> | 194 scoped_ptr<StreamTextureFactory> |
| 119 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) { | 195 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) { |
| 120 scoped_ptr<StreamTextureFactorySynchronousImpl> factory( | 196 scoped_ptr<StreamTextureFactorySynchronousImpl> factory( |
| 121 new StreamTextureFactorySynchronousImpl( | 197 new StreamTextureFactorySynchronousImpl( |
| 122 base::Bind(&SynchronousCompositorFactoryImpl:: | 198 base::Bind(&SynchronousCompositorFactoryImpl:: |
| 123 TryCreateStreamTextureFactory, | 199 TryCreateStreamTextureFactory, |
| 124 base::Unretained(this)), | 200 base::Unretained(this)), |
| 125 view_id)); | 201 view_id)); |
| 126 return factory.PassAs<StreamTextureFactory>(); | 202 return factory.PassAs<StreamTextureFactory>(); |
| 127 } | 203 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 155 | 231 |
| 156 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() { | 232 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() { |
| 157 base::AutoLock lock(num_hardware_compositor_lock_); | 233 base::AutoLock lock(num_hardware_compositor_lock_); |
| 158 return num_hardware_compositors_ > 0; | 234 return num_hardware_compositors_ > 0; |
| 159 } | 235 } |
| 160 | 236 |
| 161 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 237 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 162 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() { | 238 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() { |
| 163 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 239 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 164 context_provider; | 240 context_provider; |
| 165 if (CanCreateMainThreadContext() && | 241 // This check only guarantees the main thread context is created after |
| 166 GetOffscreenContextProviderForMainThread()) { | 242 // a compositor did successfully initialize hardware draw in the past. |
| 167 DCHECK(offscreen_context_for_main_thread_); | 243 // In particular this does not guarantee that the main thread context |
| 168 DCHECK(wrapped_gl_context_for_main_thread_); | 244 // will fail creation when all compositors release hardware draw. |
| 169 context_provider = | 245 if (CanCreateMainThreadContext() && !video_context_provider_) { |
| 170 new VideoContextProvider(offscreen_context_for_main_thread_, | 246 DCHECK(service_); |
| 171 wrapped_gl_context_for_main_thread_); | 247 DCHECK(wrapped_gl_context_for_compositor_thread_); |
| 248 |
| 249 video_context_provider_ = new VideoContextProvider( |
| 250 CreateContext(new gfx::GLSurfaceStub, |
| 251 service_, |
| 252 wrapped_gl_context_for_compositor_thread_)); |
| 172 } | 253 } |
| 173 return context_provider; | 254 return video_context_provider_; |
| 174 } | 255 } |
| 175 | 256 |
| 176 // TODO(boliu): Deduplicate this with synchronous_compositor_output_surface.cc. | 257 void SynchronousCompositorFactoryImpl::SetDeferredGpuService( |
| 177 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> | 258 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { |
| 178 SynchronousCompositorFactoryImpl::CreateOffscreenContext() { | 259 DCHECK(!service_); |
| 179 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | 260 service_ = service; |
| 180 | |
| 181 blink::WebGraphicsContext3D::Attributes attributes; | |
| 182 attributes.antialias = false; | |
| 183 attributes.shareResources = true; | |
| 184 attributes.noAutomaticFlushes = true; | |
| 185 | |
| 186 gpu::GLInProcessContextAttribs in_process_attribs; | |
| 187 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( | |
| 188 attributes, &in_process_attribs); | |
| 189 scoped_ptr<gpu::GLInProcessContext> context( | |
| 190 gpu::GLInProcessContext::CreateContext(true, | |
| 191 NULL, | |
| 192 gfx::Size(1, 1), | |
| 193 attributes.shareResources, | |
| 194 in_process_attribs, | |
| 195 gpu_preference)); | |
| 196 | |
| 197 wrapped_gl_context_for_main_thread_ = context.get(); | |
| 198 if (!context.get()) | |
| 199 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); | |
| 200 | |
| 201 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( | |
| 202 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( | |
| 203 context.Pass(), attributes)); | |
| 204 } | 261 } |
| 205 | 262 |
| 206 } // namespace content | 263 } // namespace content |
| OLD | NEW |