| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/observer_list.h" | |
| 15 #include "base/sys_info.h" | |
| 16 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 17 #include "content/browser/android/in_process/context_provider_in_process.h" | |
| 18 #include "content/browser/android/in_process/synchronous_compositor_impl.h" | 15 #include "content/browser/android/in_process/synchronous_compositor_impl.h" |
| 19 #include "content/browser/android/in_process/synchronous_compositor_registry_in_
proc.h" | 16 #include "content/browser/android/in_process/synchronous_compositor_registry_in_
proc.h" |
| 20 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | |
| 21 #include "content/common/gpu/client/context_provider_command_buffer.h" | |
| 22 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | |
| 23 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 25 #include "content/renderer/android/synchronous_compositor_external_begin_frame_s
ource.h" | 19 #include "content/renderer/android/synchronous_compositor_external_begin_frame_s
ource.h" |
| 26 #include "content/renderer/android/synchronous_compositor_output_surface.h" | 20 #include "content/renderer/android/synchronous_compositor_output_surface.h" |
| 27 #include "content/renderer/gpu/frame_swap_message_queue.h" | 21 #include "content/renderer/gpu/frame_swap_message_queue.h" |
| 28 #include "content/renderer/render_thread_impl.h" | 22 #include "content/renderer/render_thread_impl.h" |
| 29 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" | |
| 30 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
| 31 #include "gpu/command_buffer/client/gles2_interface.h" | 23 #include "gpu/command_buffer/client/gles2_interface.h" |
| 32 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 24 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| 33 #include "gpu/ipc/client/gpu_channel_host.h" | 25 #include "gpu/ipc/client/gpu_channel_host.h" |
| 34 #include "ui/gl/android/surface_texture.h" | |
| 35 #include "ui/gl/gl_surface.h" | |
| 36 #include "ui/gl/gl_surface_stub.h" | |
| 37 | |
| 38 using cc_blink::ContextProviderWebContext; | |
| 39 using gpu_blink::WebGraphicsContext3DImpl; | |
| 40 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl; | |
| 41 | 26 |
| 42 namespace content { | 27 namespace content { |
| 43 | 28 |
| 44 namespace { | |
| 45 | |
| 46 struct ContextHolder { | |
| 47 std::unique_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> | |
| 48 command_buffer; | |
| 49 gpu::GLInProcessContext* gl_in_process_context; | |
| 50 }; | |
| 51 | |
| 52 ContextHolder CreateContextHolder( | |
| 53 const gpu::gles2::ContextCreationAttribHelper& attributes, | |
| 54 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | |
| 55 const gpu::GLInProcessContextSharedMemoryLimits& mem_limits) { | |
| 56 bool is_offscreen = true; | |
| 57 std::unique_ptr<gpu::GLInProcessContext> context( | |
| 58 gpu::GLInProcessContext::Create( | |
| 59 service, nullptr /* surface */, is_offscreen, | |
| 60 gfx::kNullAcceleratedWidget, gfx::Size(1, 1), | |
| 61 nullptr /* share_context */, attributes, gfx::PreferDiscreteGpu, | |
| 62 mem_limits, BrowserGpuMemoryBufferManager::current(), nullptr)); | |
| 63 | |
| 64 gpu::GLInProcessContext* context_ptr = context.get(); | |
| 65 | |
| 66 ContextHolder holder; | |
| 67 holder.command_buffer = | |
| 68 std::unique_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>( | |
| 69 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( | |
| 70 std::move(context), attributes)); | |
| 71 holder.gl_in_process_context = context_ptr; | |
| 72 | |
| 73 return holder; | |
| 74 } | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() { | 29 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl() { |
| 79 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 30 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 80 switches::kSingleProcess)) { | 31 switches::kSingleProcess)) { |
| 81 // TODO(boliu): Figure out how to deal with this more nicely. | 32 // TODO(boliu): Figure out how to deal with this more nicely. |
| 82 SynchronousCompositorFactory::SetInstance(this); | 33 SynchronousCompositorFactory::SetInstance(this); |
| 83 } | 34 } |
| 84 } | 35 } |
| 85 | 36 |
| 86 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} | 37 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {} |
| 87 | 38 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 113 return synchronous_input_event_filter(); | 64 return synchronous_input_event_filter(); |
| 114 } | 65 } |
| 115 | 66 |
| 116 std::unique_ptr<cc::BeginFrameSource> | 67 std::unique_ptr<cc::BeginFrameSource> |
| 117 SynchronousCompositorFactoryImpl::CreateExternalBeginFrameSource( | 68 SynchronousCompositorFactoryImpl::CreateExternalBeginFrameSource( |
| 118 int routing_id) { | 69 int routing_id) { |
| 119 return base::WrapUnique(new SynchronousCompositorExternalBeginFrameSource( | 70 return base::WrapUnique(new SynchronousCompositorExternalBeginFrameSource( |
| 120 routing_id, SynchronousCompositorRegistryInProc::GetInstance())); | 71 routing_id, SynchronousCompositorRegistryInProc::GetInstance())); |
| 121 } | 72 } |
| 122 | 73 |
| 123 // SynchronousCompositorStreamTextureFactoryImpl. | |
| 124 | |
| 125 class SynchronousCompositorStreamTextureFactoryImpl::VideoContextProvider | |
| 126 : public StreamTextureFactorySynchronousImpl::ContextProvider { | |
| 127 public: | |
| 128 VideoContextProvider(scoped_refptr<cc::ContextProvider> context_provider, | |
| 129 gpu::GLInProcessContext* gl_in_process_context) | |
| 130 : context_provider_(context_provider), | |
| 131 gl_in_process_context_(gl_in_process_context) { | |
| 132 context_provider_->BindToCurrentThread(); | |
| 133 } | |
| 134 | |
| 135 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture( | |
| 136 uint32_t stream_id) override { | |
| 137 return gl_in_process_context_->GetSurfaceTexture(stream_id); | |
| 138 } | |
| 139 | |
| 140 uint32_t CreateStreamTexture(uint32_t texture_id) override { | |
| 141 return gl_in_process_context_->CreateStreamTexture(texture_id); | |
| 142 } | |
| 143 | |
| 144 gpu::gles2::GLES2Interface* ContextGL() override { | |
| 145 return context_provider_->ContextGL(); | |
| 146 } | |
| 147 | |
| 148 void AddObserver(StreamTextureFactoryContextObserver* obs) override { | |
| 149 observer_list_.AddObserver(obs); | |
| 150 } | |
| 151 | |
| 152 void RemoveObserver(StreamTextureFactoryContextObserver* obs) override { | |
| 153 observer_list_.RemoveObserver(obs); | |
| 154 } | |
| 155 | |
| 156 void RestoreContext() { | |
| 157 FOR_EACH_OBSERVER(StreamTextureFactoryContextObserver, observer_list_, | |
| 158 ResetStreamTextureProxy()); | |
| 159 } | |
| 160 | |
| 161 private: | |
| 162 friend class base::RefCountedThreadSafe<VideoContextProvider>; | |
| 163 ~VideoContextProvider() override {} | |
| 164 | |
| 165 scoped_refptr<cc::ContextProvider> context_provider_; | |
| 166 gpu::GLInProcessContext* gl_in_process_context_; | |
| 167 base::ObserverList<StreamTextureFactoryContextObserver> observer_list_; | |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider); | |
| 170 }; | |
| 171 | |
| 172 namespace { | |
| 173 base::LazyInstance<SynchronousCompositorStreamTextureFactoryImpl>::Leaky | |
| 174 g_stream_texture_factory = LAZY_INSTANCE_INITIALIZER; | |
| 175 } // namespace | |
| 176 | |
| 177 // static | |
| 178 SynchronousCompositorStreamTextureFactoryImpl* | |
| 179 SynchronousCompositorStreamTextureFactoryImpl::GetInstance() { | |
| 180 return g_stream_texture_factory.Pointer(); | |
| 181 } | |
| 182 | |
| 183 SynchronousCompositorStreamTextureFactoryImpl:: | |
| 184 SynchronousCompositorStreamTextureFactoryImpl() | |
| 185 : num_hardware_compositors_(0) {} | |
| 186 | |
| 187 scoped_refptr<StreamTextureFactory> | |
| 188 SynchronousCompositorStreamTextureFactoryImpl::CreateStreamTextureFactory() { | |
| 189 return StreamTextureFactorySynchronousImpl::Create( | |
| 190 base::Bind(&SynchronousCompositorStreamTextureFactoryImpl:: | |
| 191 TryCreateStreamTextureFactory, | |
| 192 base::Unretained(this))); | |
| 193 } | |
| 194 | |
| 195 void SynchronousCompositorStreamTextureFactoryImpl:: | |
| 196 CompositorInitializedHardwareDraw() { | |
| 197 base::AutoLock lock(num_hardware_compositor_lock_); | |
| 198 num_hardware_compositors_++; | |
| 199 if (num_hardware_compositors_ == 1 && main_thread_task_runner_.get()) { | |
| 200 main_thread_task_runner_->PostTask( | |
| 201 FROM_HERE, base::Bind(&SynchronousCompositorStreamTextureFactoryImpl:: | |
| 202 RestoreContextOnMainThread, | |
| 203 base::Unretained(this))); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void SynchronousCompositorStreamTextureFactoryImpl:: | |
| 208 CompositorReleasedHardwareDraw() { | |
| 209 base::AutoLock lock(num_hardware_compositor_lock_); | |
| 210 DCHECK_GT(num_hardware_compositors_, 0u); | |
| 211 num_hardware_compositors_--; | |
| 212 } | |
| 213 | |
| 214 void SynchronousCompositorStreamTextureFactoryImpl:: | |
| 215 RestoreContextOnMainThread() { | |
| 216 if (CanCreateMainThreadContext() && video_context_provider_.get()) | |
| 217 video_context_provider_->RestoreContext(); | |
| 218 } | |
| 219 | |
| 220 bool SynchronousCompositorStreamTextureFactoryImpl:: | |
| 221 CanCreateMainThreadContext() { | |
| 222 base::AutoLock lock(num_hardware_compositor_lock_); | |
| 223 return num_hardware_compositors_ > 0; | |
| 224 } | |
| 225 | |
| 226 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | |
| 227 SynchronousCompositorStreamTextureFactoryImpl::TryCreateStreamTextureFactory() { | |
| 228 { | |
| 229 base::AutoLock lock(num_hardware_compositor_lock_); | |
| 230 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 231 } | |
| 232 | |
| 233 // Always fail creation even if |video_context_provider_| is not NULL. | |
| 234 // This is to avoid synchronous calls that may deadlock. Setting | |
| 235 // |video_context_provider_| to null is also not safe since it makes | |
| 236 // synchronous destruction uncontrolled and possibly deadlock. | |
| 237 if (!CanCreateMainThreadContext()) { | |
| 238 return scoped_refptr< | |
| 239 StreamTextureFactorySynchronousImpl::ContextProvider>(); | |
| 240 } | |
| 241 | |
| 242 if (!video_context_provider_.get()) { | |
| 243 DCHECK(android_view_service_.get()); | |
| 244 | |
| 245 // This is for an offscreen context, so the default framebuffer doesn't need | |
| 246 // any alpha, depth, stencil, antialiasing. | |
| 247 gpu::gles2::ContextCreationAttribHelper attributes; | |
| 248 attributes.alpha_size = -1; | |
| 249 attributes.depth_size = 0; | |
| 250 attributes.stencil_size = 0; | |
| 251 attributes.samples = 0; | |
| 252 attributes.sample_buffers = 0; | |
| 253 attributes.bind_generates_resource = false; | |
| 254 attributes.lose_context_when_out_of_memory = true; | |
| 255 | |
| 256 // This needs to run in on-screen |android_view_service_| context due to | |
| 257 // SurfaceTexture limitations. | |
| 258 ContextHolder holder = | |
| 259 CreateContextHolder(attributes, android_view_service_, | |
| 260 gpu::GLInProcessContextSharedMemoryLimits()); | |
| 261 video_context_provider_ = new VideoContextProvider( | |
| 262 ContextProviderInProcess::Create(std::move(holder.command_buffer), | |
| 263 "Video-Offscreen-main-thread"), | |
| 264 holder.gl_in_process_context); | |
| 265 } | |
| 266 return video_context_provider_; | |
| 267 } | |
| 268 | |
| 269 void SynchronousCompositorStreamTextureFactoryImpl::SetDeferredGpuService( | |
| 270 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { | |
| 271 DCHECK(!android_view_service_.get()); | |
| 272 android_view_service_ = service; | |
| 273 | |
| 274 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 275 switches::kSingleProcess)) { | |
| 276 RenderThreadImpl::SetStreamTextureFactory(CreateStreamTextureFactory()); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 } // namespace content | 74 } // namespace content |
| OLD | NEW |