| 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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 attributes.depth = false; | 25 attributes.depth = false; |
| 26 attributes.stencil = false; | 26 attributes.stencil = false; |
| 27 attributes.shareResources = true; | 27 attributes.shareResources = true; |
| 28 attributes.noAutomaticFlushes = true; | 28 attributes.noAutomaticFlushes = true; |
| 29 | 29 |
| 30 return attributes; | 30 return attributes; |
| 31 } | 31 } |
| 32 | 32 |
| 33 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | 33 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 34 | 34 |
| 35 scoped_ptr<gpu::GLInProcessContext> CreateContextWithAttributes( | 35 scoped_ptr<gpu::GLInProcessContext> CreateOffscreenContext( |
| 36 scoped_refptr<gfx::GLSurface> surface, | |
| 37 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | |
| 38 gpu::GLInProcessContext* share_context, | |
| 39 const blink::WebGraphicsContext3D::Attributes& attributes) { | 36 const blink::WebGraphicsContext3D::Attributes& attributes) { |
| 40 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | 37 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
| 41 | 38 |
| 42 if (!surface) | |
| 43 surface = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)); | |
| 44 | |
| 45 gpu::GLInProcessContextAttribs in_process_attribs; | 39 gpu::GLInProcessContextAttribs in_process_attribs; |
| 46 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( | 40 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( |
| 47 attributes, &in_process_attribs); | 41 attributes, &in_process_attribs); |
| 48 in_process_attribs.lose_context_when_out_of_memory = 1; | 42 in_process_attribs.lose_context_when_out_of_memory = 1; |
| 43 |
| 44 scoped_ptr<gpu::GLInProcessContext> context( |
| 45 gpu::GLInProcessContext::CreateContext(true /* is_offscreen */, |
| 46 gfx::kNullAcceleratedWidget, |
| 47 gfx::Size(1, 1), |
| 48 false /* share_resources */, |
| 49 in_process_attribs, |
| 50 gpu_preference)); |
| 51 return context.Pass(); |
| 52 } |
| 53 |
| 54 scoped_ptr<gpu::GLInProcessContext> CreateContext( |
| 55 gfx::GLSurface* surface, |
| 56 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, |
| 57 gpu::GLInProcessContext* share_context) { |
| 58 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
| 59 gpu::GLInProcessContextAttribs in_process_attribs; |
| 60 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( |
| 61 GetDefaultAttribs(), &in_process_attribs); |
| 62 in_process_attribs.lose_context_when_out_of_memory = 1; |
| 49 scoped_ptr<gpu::GLInProcessContext> context( | 63 scoped_ptr<gpu::GLInProcessContext> context( |
| 50 gpu::GLInProcessContext::CreateWithSurface( | 64 gpu::GLInProcessContext::CreateWithSurface( |
| 51 surface, service, share_context, in_process_attribs, gpu_preference)); | 65 surface, service, share_context, in_process_attribs, gpu_preference)); |
| 52 return context.Pass(); | 66 return context.Pass(); |
| 53 } | 67 } |
| 54 | 68 |
| 55 scoped_ptr<gpu::GLInProcessContext> CreateContext( | |
| 56 scoped_refptr<gfx::GLSurface> surface, | |
| 57 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | |
| 58 gpu::GLInProcessContext* share_context) { | |
| 59 return CreateContextWithAttributes( | |
| 60 surface, service, share_context, GetDefaultAttribs()); | |
| 61 } | |
| 62 | |
| 63 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext( | 69 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext( |
| 64 scoped_ptr<gpu::GLInProcessContext> context) { | 70 scoped_ptr<gpu::GLInProcessContext> context) { |
| 65 if (!context.get()) | 71 if (!context.get()) |
| 66 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); | 72 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); |
| 67 | 73 |
| 68 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( | 74 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( |
| 69 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( | 75 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( |
| 70 context.Pass(), GetDefaultAttribs())); | 76 context.Pass(), GetDefaultAttribs())); |
| 71 } | 77 } |
| 72 | 78 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 100 gpu::GLInProcessContext* gl_in_process_context_; | 106 gpu::GLInProcessContext* gl_in_process_context_; |
| 101 | 107 |
| 102 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); | 108 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); |
| 103 }; | 109 }; |
| 104 | 110 |
| 105 } // namespace | 111 } // namespace |
| 106 | 112 |
| 107 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | 113 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 108 | 114 |
| 109 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() | 115 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() |
| 110 : wrapped_gl_context_for_compositor_thread_(NULL), | 116 : num_hardware_compositors_(0) { |
| 111 num_hardware_compositors_(0) { | |
| 112 SynchronousCompositorFactory::SetInstance(this); | 117 SynchronousCompositorFactory::SetInstance(this); |
| 113 } | 118 } |
| 114 | 119 |
| 115 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} | 120 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} |
| 116 | 121 |
| 117 scoped_refptr<base::MessageLoopProxy> | 122 scoped_refptr<base::MessageLoopProxy> |
| 118 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() { | 123 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() { |
| 119 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 124 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 120 } | 125 } |
| 121 | 126 |
| 122 scoped_ptr<cc::OutputSurface> | 127 scoped_ptr<cc::OutputSurface> |
| 123 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) { | 128 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) { |
| 124 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( | 129 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( |
| 125 new SynchronousCompositorOutputSurface(routing_id)); | 130 new SynchronousCompositorOutputSurface(routing_id)); |
| 126 return output_surface.PassAs<cc::OutputSurface>(); | 131 return output_surface.PassAs<cc::OutputSurface>(); |
| 127 } | 132 } |
| 128 | 133 |
| 129 InputHandlerManagerClient* | 134 InputHandlerManagerClient* |
| 130 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() { | 135 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() { |
| 131 return synchronous_input_event_filter(); | 136 return synchronous_input_event_filter(); |
| 132 } | 137 } |
| 133 | 138 |
| 134 scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl:: | 139 scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl:: |
| 135 GetSharedOffscreenContextProviderForMainThread() { | 140 GetSharedOffscreenContextProviderForMainThread() { |
| 136 bool failed = false; | 141 bool failed = false; |
| 137 if ((!offscreen_context_for_main_thread_.get() || | 142 if ((!offscreen_context_for_main_thread_.get() || |
| 138 offscreen_context_for_main_thread_->DestroyedOnMainThread())) { | 143 offscreen_context_for_main_thread_->DestroyedOnMainThread())) { |
| 139 scoped_ptr<gpu::GLInProcessContext> context = | 144 scoped_ptr<gpu::GLInProcessContext> context = |
| 140 CreateContext(NULL, NULL, NULL); | 145 CreateOffscreenContext(GetDefaultAttribs()); |
| 141 offscreen_context_for_main_thread_ = | 146 offscreen_context_for_main_thread_ = |
| 142 webkit::gpu::ContextProviderInProcess::Create( | 147 webkit::gpu::ContextProviderInProcess::Create( |
| 143 WrapContext(context.Pass()), | 148 WrapContext(context.Pass()), |
| 144 "Compositor-Offscreen-main-thread"); | 149 "Compositor-Offscreen-main-thread"); |
| 145 failed = !offscreen_context_for_main_thread_.get() || | 150 failed = !offscreen_context_for_main_thread_.get() || |
| 146 !offscreen_context_for_main_thread_->BindToCurrentThread(); | 151 !offscreen_context_for_main_thread_->BindToCurrentThread(); |
| 147 } | 152 } |
| 148 | 153 |
| 149 if (failed) { | 154 if (failed) { |
| 150 offscreen_context_for_main_thread_ = NULL; | 155 offscreen_context_for_main_thread_ = NULL; |
| 151 } | 156 } |
| 152 return offscreen_context_for_main_thread_; | 157 return offscreen_context_for_main_thread_; |
| 153 } | 158 } |
| 154 | 159 |
| 155 // This is called on the renderer compositor impl thread (InitializeHwDraw) in | 160 void SynchronousCompositorFactoryImpl::EnsureShareContextCreated() { |
| 156 // order to support Android WebView synchronously enable and disable hardware | 161 if (!share_context_.get()) |
| 157 // mode multiple times in the same task. | 162 share_context_ = CreateContext(new gfx::GLSurfaceStub, service_, NULL); |
| 158 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: | |
| 159 GetOffscreenContextProviderForCompositorThread() { | |
| 160 DCHECK(service_); | |
| 161 bool failed = false; | |
| 162 if (!offscreen_context_for_compositor_thread_.get() || | |
| 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(); | |
| 167 offscreen_context_for_compositor_thread_ = | |
| 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; | |
| 177 } | |
| 178 return offscreen_context_for_compositor_thread_; | |
| 179 } | 163 } |
| 180 | 164 |
| 181 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: | 165 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl:: |
| 182 CreateOnscreenContextProviderForCompositorThread( | 166 CreateOnscreenContextProviderForCompositorThread( |
| 183 scoped_refptr<gfx::GLSurface> surface) { | 167 gfx::GLSurface* surface) { |
| 184 DCHECK(surface); | 168 DCHECK(surface); |
| 185 DCHECK(service_); | 169 DCHECK(service_); |
| 186 DCHECK(wrapped_gl_context_for_compositor_thread_); | |
| 187 | 170 |
| 171 EnsureShareContextCreated(); |
| 188 return webkit::gpu::ContextProviderInProcess::Create( | 172 return webkit::gpu::ContextProviderInProcess::Create( |
| 189 WrapContext(CreateContext( | 173 WrapContext(CreateContext(surface, service_, share_context_.get())), |
| 190 surface, service_, wrapped_gl_context_for_compositor_thread_)), | |
| 191 "Compositor-Onscreen"); | 174 "Compositor-Onscreen"); |
| 192 } | 175 } |
| 193 | 176 |
| 194 scoped_refptr<StreamTextureFactory> | 177 scoped_refptr<StreamTextureFactory> |
| 195 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) { | 178 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) { |
| 196 scoped_refptr<StreamTextureFactorySynchronousImpl> factory( | 179 scoped_refptr<StreamTextureFactorySynchronousImpl> factory( |
| 197 StreamTextureFactorySynchronousImpl::Create( | 180 StreamTextureFactorySynchronousImpl::Create( |
| 198 base::Bind( | 181 base::Bind( |
| 199 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory, | 182 &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory, |
| 200 base::Unretained(this)), | 183 base::Unretained(this)), |
| 201 view_id)); | 184 view_id)); |
| 202 return factory; | 185 return factory; |
| 203 } | 186 } |
| 204 | 187 |
| 205 blink::WebGraphicsContext3D* | 188 blink::WebGraphicsContext3D* |
| 206 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D( | 189 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D( |
| 207 const blink::WebGraphicsContext3D::Attributes& attributes) { | 190 const blink::WebGraphicsContext3D::Attributes& attributes) { |
| 208 return WrapContext(CreateContextWithAttributes(NULL, NULL, NULL, attributes)) | 191 return WrapContext(CreateOffscreenContext(attributes)).release(); |
| 209 .release(); | |
| 210 } | 192 } |
| 211 | 193 |
| 212 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() { | 194 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() { |
| 213 base::AutoLock lock(num_hardware_compositor_lock_); | 195 base::AutoLock lock(num_hardware_compositor_lock_); |
| 214 num_hardware_compositors_++; | 196 num_hardware_compositors_++; |
| 215 } | 197 } |
| 216 | 198 |
| 217 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() { | 199 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() { |
| 218 base::AutoLock lock(num_hardware_compositor_lock_); | 200 base::AutoLock lock(num_hardware_compositor_lock_); |
| 219 DCHECK_GT(num_hardware_compositors_, 0u); | 201 DCHECK_GT(num_hardware_compositors_, 0u); |
| 220 num_hardware_compositors_--; | 202 num_hardware_compositors_--; |
| 221 } | 203 } |
| 222 | 204 |
| 223 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() { | 205 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() { |
| 224 base::AutoLock lock(num_hardware_compositor_lock_); | 206 base::AutoLock lock(num_hardware_compositor_lock_); |
| 225 return num_hardware_compositors_ > 0; | 207 return num_hardware_compositors_ > 0; |
| 226 } | 208 } |
| 227 | 209 |
| 228 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 210 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 229 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() { | 211 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() { |
| 230 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 212 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 231 context_provider; | 213 context_provider; |
| 232 // This check only guarantees the main thread context is created after | 214 // This check only guarantees the main thread context is created after |
| 233 // a compositor did successfully initialize hardware draw in the past. | 215 // a compositor did successfully initialize hardware draw in the past. |
| 234 // In particular this does not guarantee that the main thread context | 216 // In particular this does not guarantee that the main thread context |
| 235 // will fail creation when all compositors release hardware draw. | 217 // will fail creation when all compositors release hardware draw. |
| 236 if (CanCreateMainThreadContext() && !video_context_provider_) { | 218 if (CanCreateMainThreadContext() && !video_context_provider_) { |
| 237 DCHECK(service_); | 219 DCHECK(service_); |
| 238 DCHECK(wrapped_gl_context_for_compositor_thread_); | 220 DCHECK(share_context_.get()); |
| 239 | 221 |
| 240 video_context_provider_ = new VideoContextProvider( | 222 video_context_provider_ = new VideoContextProvider( |
| 241 CreateContext(new gfx::GLSurfaceStub, | 223 CreateContext(new gfx::GLSurfaceStub, |
| 242 service_, | 224 service_, |
| 243 wrapped_gl_context_for_compositor_thread_)); | 225 share_context_.get())); |
| 244 } | 226 } |
| 245 return video_context_provider_; | 227 return video_context_provider_; |
| 246 } | 228 } |
| 247 | 229 |
| 248 void SynchronousCompositorFactoryImpl::SetDeferredGpuService( | 230 void SynchronousCompositorFactoryImpl::SetDeferredGpuService( |
| 249 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { | 231 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { |
| 250 DCHECK(!service_); | 232 DCHECK(!service_); |
| 251 service_ = service; | 233 service_ = service; |
| 252 } | 234 } |
| 253 | 235 |
| 254 } // namespace content | 236 } // namespace content |
| OLD | NEW |