Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_impl.h" | 5 #include "content/browser/android/in_process/synchronous_compositor_impl.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
| 10 #include "cc/input/input_handler.h" | 10 #include "cc/input/input_handler.h" |
| 11 #include "cc/input/layer_scroll_offset_delegate.h" | 11 #include "cc/input/layer_scroll_offset_delegate.h" |
| 12 #include "content/browser/android/in_process/synchronous_input_event_filter.h" | 12 #include "content/browser/android/in_process/synchronous_input_event_filter.h" |
| 13 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
| 14 #include "content/public/browser/android/synchronous_compositor_client.h" | 14 #include "content/public/browser/android/synchronous_compositor_client.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/renderer/android/synchronous_compositor_factory.h" | 18 #include "content/renderer/android/synchronous_compositor_factory.h" |
| 19 #include "content/renderer/media/android/stream_texture_factory_android_synchron ous_impl.h" | |
| 20 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
| 21 #include "gpu/command_buffer/service/stream_texture_manager_in_process_android.h " | |
| 22 #include "ui/gl/android/surface_texture_bridge.h" | |
| 19 #include "ui/gl/gl_surface.h" | 23 #include "ui/gl/gl_surface.h" |
| 20 #include "webkit/common/gpu/context_provider_in_process.h" | 24 #include "webkit/common/gpu/context_provider_in_process.h" |
| 25 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" | |
| 21 | 26 |
| 22 namespace content { | 27 namespace content { |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 | 30 |
| 31 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | |
| 32 | |
| 26 int GetInProcessRendererId() { | 33 int GetInProcessRendererId() { |
| 27 content::RenderProcessHost::iterator it = | 34 content::RenderProcessHost::iterator it = |
| 28 content::RenderProcessHost::AllHostsIterator(); | 35 content::RenderProcessHost::AllHostsIterator(); |
| 29 if (it.IsAtEnd()) { | 36 if (it.IsAtEnd()) { |
| 30 // There should always be one RPH in single process mode. | 37 // There should always be one RPH in single process mode. |
| 31 NOTREACHED(); | 38 NOTREACHED(); |
| 32 return 0; | 39 return 0; |
| 33 } | 40 } |
| 34 | 41 |
| 35 int id = it.GetCurrentValue()->GetID(); | 42 int id = it.GetCurrentValue()->GetID(); |
| 36 it.Advance(); | 43 it.Advance(); |
| 37 DCHECK(it.IsAtEnd()); // Not multiprocess compatible. | 44 DCHECK(it.IsAtEnd()); // Not multiprocess compatible. |
| 38 return id; | 45 return id; |
| 39 } | 46 } |
| 40 | 47 |
| 48 class VideoContextProvider | |
| 49 : public StreamTextureFactorySynchronousImpl::ContextProvider { | |
| 50 public: | |
| 51 VideoContextProvider( | |
| 52 const scoped_refptr<cc::ContextProvider>& context_provider, | |
| 53 gpu::GLInProcessContext* gl_in_process_context) | |
| 54 : context_provider_(context_provider), | |
| 55 gl_in_process_context_(gl_in_process_context) {} | |
| 56 | |
| 57 virtual scoped_refptr<gfx::SurfaceTextureBridge> GetSurfaceTexture( | |
| 58 uint32 stream_id) OVERRIDE { | |
| 59 return gl_in_process_context_->GetSurfaceTexture(stream_id); | |
| 60 } | |
| 61 | |
| 62 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { | |
| 63 return context_provider_->Context3d(); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 scoped_refptr<cc::ContextProvider> context_provider_; | |
| 68 gpu::GLInProcessContext* gl_in_process_context_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); | |
| 71 }; | |
| 72 | |
| 41 class SynchronousCompositorFactoryImpl : public SynchronousCompositorFactory { | 73 class SynchronousCompositorFactoryImpl : public SynchronousCompositorFactory { |
| 42 public: | 74 public: |
| 43 SynchronousCompositorFactoryImpl() { | 75 SynchronousCompositorFactoryImpl() |
| 76 : offscreen_context_as_gl_in_process_context_(NULL) { | |
| 44 SynchronousCompositorFactory::SetInstance(this); | 77 SynchronousCompositorFactory::SetInstance(this); |
| 45 } | 78 } |
| 46 | 79 |
| 47 // SynchronousCompositorFactory | 80 // SynchronousCompositorFactory |
| 48 virtual scoped_refptr<base::MessageLoopProxy> | 81 virtual scoped_refptr<base::MessageLoopProxy> |
| 49 GetCompositorMessageLoop() OVERRIDE { | 82 GetCompositorMessageLoop() OVERRIDE { |
| 50 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 83 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 51 } | 84 } |
| 52 | 85 |
| 53 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( | 86 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( |
| 54 int routing_id) OVERRIDE { | 87 int routing_id) OVERRIDE { |
| 55 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( | 88 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( |
| 56 new SynchronousCompositorOutputSurface(routing_id)); | 89 new SynchronousCompositorOutputSurface(routing_id)); |
| 57 return output_surface.PassAs<cc::OutputSurface>(); | 90 return output_surface.PassAs<cc::OutputSurface>(); |
| 58 } | 91 } |
| 59 | 92 |
| 60 virtual InputHandlerManagerClient* GetInputHandlerManagerClient() OVERRIDE { | 93 virtual InputHandlerManagerClient* GetInputHandlerManagerClient() OVERRIDE { |
| 61 return synchronous_input_event_filter(); | 94 return synchronous_input_event_filter(); |
| 62 } | 95 } |
| 63 | 96 |
| 64 SynchronousInputEventFilter* synchronous_input_event_filter() { | 97 SynchronousInputEventFilter* synchronous_input_event_filter() { |
| 65 return &synchronous_input_event_filter_; | 98 return &synchronous_input_event_filter_; |
| 66 } | 99 } |
| 67 | 100 |
| 101 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> | |
| 102 CreateOffscreenContext() { | |
| 103 if (!gfx::GLSurface::InitializeOneOff()) | |
| 104 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); | |
| 105 | |
| 106 const char* allowed_extensions = "*"; | |
|
boliu
2013/08/20 21:33:49
Leave a comment about deduping this code.
no sievers
2013/08/20 21:59:58
Well, ideally we wouldn't have one thing (GLInProc
| |
| 107 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | |
| 108 | |
| 109 WebKit::WebGraphicsContext3D::Attributes attributes; | |
| 110 attributes.antialias = false; | |
| 111 attributes.shareResources = true; | |
| 112 attributes.noAutomaticFlushes = true; | |
| 113 | |
| 114 gpu::GLInProcessContextAttribs in_process_attribs; | |
| 115 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( | |
| 116 attributes, &in_process_attribs); | |
| 117 scoped_ptr<gpu::GLInProcessContext> context( | |
| 118 gpu::GLInProcessContext::CreateContext(true, | |
| 119 NULL, | |
| 120 gfx::Size(1, 1), | |
| 121 attributes.shareResources, | |
| 122 allowed_extensions, | |
| 123 in_process_attribs, | |
| 124 gpu_preference)); | |
| 125 | |
| 126 offscreen_context_as_gl_in_process_context_ = context.get(); | |
| 127 if (!context.get()) | |
| 128 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(); | |
| 129 | |
| 130 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( | |
| 131 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( | |
| 132 context.Pass(), attributes)); | |
| 133 } | |
| 134 | |
| 68 virtual scoped_refptr<cc::ContextProvider> | 135 virtual scoped_refptr<cc::ContextProvider> |
| 69 GetOffscreenContextProviderForMainThread() OVERRIDE { | 136 GetOffscreenContextProviderForMainThread() OVERRIDE { |
| 70 if (!offscreen_context_for_main_thread_.get() || | 137 if (!offscreen_context_for_main_thread_.get() || |
| 71 offscreen_context_for_main_thread_->DestroyedOnMainThread()) { | 138 offscreen_context_for_main_thread_->DestroyedOnMainThread()) { |
| 72 offscreen_context_for_main_thread_ = | 139 offscreen_context_for_main_thread_ = |
| 73 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); | 140 webkit::gpu::ContextProviderInProcess::Create(base::Bind( |
| 141 &SynchronousCompositorFactoryImpl::CreateOffscreenContext, | |
| 142 base::Unretained(this))); | |
| 74 if (offscreen_context_for_main_thread_.get() && | 143 if (offscreen_context_for_main_thread_.get() && |
| 75 !offscreen_context_for_main_thread_->BindToCurrentThread()) | 144 !offscreen_context_for_main_thread_->BindToCurrentThread()) { |
| 76 offscreen_context_for_main_thread_ = NULL; | 145 offscreen_context_for_main_thread_ = NULL; |
| 146 offscreen_context_as_gl_in_process_context_ = NULL; | |
| 147 } | |
| 77 } | 148 } |
| 78 return offscreen_context_for_main_thread_; | 149 return offscreen_context_for_main_thread_; |
| 79 } | 150 } |
| 80 | 151 |
| 81 // This is called on both renderer main thread (offscreen context creation | 152 // This is called on both renderer main thread (offscreen context creation |
| 82 // path shared between cross-process and in-process platforms) and renderer | 153 // path shared between cross-process and in-process platforms) and renderer |
| 83 // compositor impl thread (InitializeHwDraw) in order to support Android | 154 // compositor impl thread (InitializeHwDraw) in order to support Android |
| 84 // WebView synchronously enable and disable hardware mode multiple times in | 155 // WebView synchronously enable and disable hardware mode multiple times in |
| 85 // the same task. This is ok because in-process WGC3D creation may happen on | 156 // the same task. This is ok because in-process WGC3D creation may happen on |
| 86 // any thread and is lightweight. | 157 // any thread and is lightweight. |
| 87 virtual scoped_refptr<cc::ContextProvider> | 158 virtual scoped_refptr<cc::ContextProvider> |
| 88 GetOffscreenContextProviderForCompositorThread() OVERRIDE { | 159 GetOffscreenContextProviderForCompositorThread() OVERRIDE { |
| 89 base::AutoLock lock(offscreen_context_for_compositor_thread_creation_lock_); | 160 base::AutoLock lock(offscreen_context_for_compositor_thread_creation_lock_); |
| 90 if (!offscreen_context_for_compositor_thread_.get() || | 161 if (!offscreen_context_for_compositor_thread_.get() || |
| 91 offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) { | 162 offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) { |
| 92 offscreen_context_for_compositor_thread_ = | 163 offscreen_context_for_compositor_thread_ = |
| 93 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); | 164 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); |
| 94 } | 165 } |
| 95 return offscreen_context_for_compositor_thread_; | 166 return offscreen_context_for_compositor_thread_; |
| 96 } | 167 } |
| 97 | 168 |
| 169 virtual scoped_ptr<StreamTextureFactory> CreateStreamTextureFactory( | |
| 170 int view_id) OVERRIDE { | |
| 171 scoped_refptr<VideoContextProvider> context_provider = | |
| 172 new VideoContextProvider(offscreen_context_for_main_thread_, | |
| 173 offscreen_context_as_gl_in_process_context_); | |
| 174 return make_scoped_ptr(new StreamTextureFactorySynchronousImpl( | |
| 175 context_provider.get(), view_id)) | |
| 176 .PassAs<StreamTextureFactory>(); | |
| 177 } | |
| 178 | |
| 98 private: | 179 private: |
| 99 SynchronousInputEventFilter synchronous_input_event_filter_; | 180 SynchronousInputEventFilter synchronous_input_event_filter_; |
| 100 | 181 |
| 101 // Only guards construction of |offscreen_context_for_compositor_thread_|, | 182 // Only guards construction of |offscreen_context_for_compositor_thread_|, |
| 102 // not usage. | 183 // not usage. |
| 103 base::Lock offscreen_context_for_compositor_thread_creation_lock_; | 184 base::Lock offscreen_context_for_compositor_thread_creation_lock_; |
| 104 scoped_refptr<cc::ContextProvider> offscreen_context_for_main_thread_; | 185 scoped_refptr<cc::ContextProvider> offscreen_context_for_main_thread_; |
| 186 gpu::GLInProcessContext* offscreen_context_as_gl_in_process_context_; | |
|
boliu
2013/08/20 21:33:49
Indicate with var name or a comment that this is a
no sievers
2013/08/20 21:59:58
Done.
| |
| 105 scoped_refptr<cc::ContextProvider> offscreen_context_for_compositor_thread_; | 187 scoped_refptr<cc::ContextProvider> offscreen_context_for_compositor_thread_; |
| 106 }; | 188 }; |
| 107 | 189 |
| 108 base::LazyInstance<SynchronousCompositorFactoryImpl>::Leaky g_factory = | 190 base::LazyInstance<SynchronousCompositorFactoryImpl>::Leaky g_factory = |
| 109 LAZY_INSTANCE_INITIALIZER; | 191 LAZY_INSTANCE_INITIALIZER; |
| 110 | 192 |
| 111 } // namespace | 193 } // namespace |
| 112 | 194 |
| 113 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SynchronousCompositorImpl); | 195 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SynchronousCompositorImpl); |
| 114 | 196 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 g_factory.Get(); // Ensure it's initialized. | 369 g_factory.Get(); // Ensure it's initialized. |
| 288 SynchronousCompositorImpl::CreateForWebContents(contents); | 370 SynchronousCompositorImpl::CreateForWebContents(contents); |
| 289 } | 371 } |
| 290 if (SynchronousCompositorImpl* instance = | 372 if (SynchronousCompositorImpl* instance = |
| 291 SynchronousCompositorImpl::FromWebContents(contents)) { | 373 SynchronousCompositorImpl::FromWebContents(contents)) { |
| 292 instance->SetClient(client); | 374 instance->SetClient(client); |
| 293 } | 375 } |
| 294 } | 376 } |
| 295 | 377 |
| 296 } // namespace content | 378 } // namespace content |
| OLD | NEW |