| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/ui/common/gpu_service.h" | 5 #include "services/ui/common/gpu_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" | 11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
| 12 #include "mojo/public/cpp/system/platform_handle.h" | 12 #include "mojo/public/cpp/system/platform_handle.h" |
| 13 #include "services/shell/public/cpp/connector.h" | 13 #include "services/shell/public/cpp/connector.h" |
| 14 #include "services/ui/common/gpu_type_converters.h" | 14 #include "services/ui/common/gpu_type_converters.h" |
| 15 #include "services/ui/common/mojo_gpu_memory_buffer_manager.h" | 15 #include "services/ui/common/mojo_gpu_memory_buffer_manager.h" |
| 16 #include "services/ui/common/switches.h" | 16 #include "services/ui/common/switches.h" |
| 17 #include "services/ui/public/interfaces/gpu_service.mojom.h" | 17 #include "services/ui/public/interfaces/gpu_service.mojom.h" |
| 18 | 18 |
| 19 namespace ui { | 19 namespace ui { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, | 23 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 24 const tracked_objects::Location& from_here, | 24 const tracked_objects::Location& from_here, |
| 25 const base::Closure& callback) { | 25 const gpu::GpuChannelEstablishedCallback& callback, |
| 26 runner->PostTask(from_here, callback); | 26 scoped_refptr<gpu::GpuChannelHost> established_channel_host) { |
| 27 runner->PostTask(from_here, |
| 28 base::Bind(callback, std::move(established_channel_host))); |
| 27 } | 29 } |
| 28 | 30 |
| 29 GpuService* g_gpu_service = nullptr; | 31 GpuService* g_gpu_service = nullptr; |
| 30 } | 32 } |
| 31 | 33 |
| 32 GpuService::GpuService(shell::Connector* connector) | 34 GpuService::GpuService(shell::Connector* connector) |
| 33 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 35 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 34 connector_(connector), | 36 connector_(connector), |
| 35 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 37 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 36 base::WaitableEvent::InitialState::NOT_SIGNALED), | 38 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 48 GpuService::~GpuService() { | 50 GpuService::~GpuService() { |
| 49 DCHECK(IsMainThread()); | 51 DCHECK(IsMainThread()); |
| 50 if (gpu_channel_) | 52 if (gpu_channel_) |
| 51 gpu_channel_->DestroyChannel(); | 53 gpu_channel_->DestroyChannel(); |
| 52 } | 54 } |
| 53 | 55 |
| 54 // static | 56 // static |
| 55 void GpuService::Initialize(shell::Connector* connector) { | 57 void GpuService::Initialize(shell::Connector* connector) { |
| 56 DCHECK(!g_gpu_service); | 58 DCHECK(!g_gpu_service); |
| 57 g_gpu_service = new GpuService(connector); | 59 g_gpu_service = new GpuService(connector); |
| 60 gpu::GpuChannelEstablishFactory::SetInstance(g_gpu_service); |
| 58 } | 61 } |
| 59 | 62 |
| 60 // static | 63 // static |
| 61 void GpuService::Terminate() { | 64 void GpuService::Terminate() { |
| 62 DCHECK(g_gpu_service); | 65 DCHECK(g_gpu_service); |
| 66 gpu::GpuChannelEstablishFactory::SetInstance(nullptr); |
| 63 delete g_gpu_service; | 67 delete g_gpu_service; |
| 64 g_gpu_service = nullptr; | 68 g_gpu_service = nullptr; |
| 65 } | 69 } |
| 66 | 70 |
| 67 // static | 71 // static |
| 68 GpuService* GpuService::GetInstance() { | 72 GpuService* GpuService::GetInstance() { |
| 69 DCHECK(g_gpu_service); | 73 DCHECK(g_gpu_service); |
| 70 return g_gpu_service; | 74 return g_gpu_service; |
| 71 } | 75 } |
| 72 | 76 |
| 73 void GpuService::EstablishGpuChannel(const base::Closure& callback) { | 77 void GpuService::EstablishGpuChannel( |
| 78 const gpu::GpuChannelEstablishedCallback& callback) { |
| 74 base::AutoLock auto_lock(lock_); | 79 base::AutoLock auto_lock(lock_); |
| 75 auto runner = base::ThreadTaskRunnerHandle::Get(); | 80 auto runner = base::ThreadTaskRunnerHandle::Get(); |
| 76 if (GetGpuChannelLocked()) { | 81 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannelLocked(); |
| 77 runner->PostTask(FROM_HERE, callback); | 82 if (channel) { |
| 83 PostTask(runner, FROM_HERE, callback, std::move(channel)); |
| 78 return; | 84 return; |
| 79 } | 85 } |
| 80 establish_callbacks_.push_back( | 86 establish_callbacks_.push_back( |
| 81 base::Bind(PostTask, runner, FROM_HERE, callback)); | 87 base::Bind(PostTask, runner, FROM_HERE, callback)); |
| 82 if (!is_establishing_) { | 88 if (!is_establishing_) { |
| 83 is_establishing_ = true; | 89 is_establishing_ = true; |
| 84 main_task_runner_->PostTask( | 90 main_task_runner_->PostTask( |
| 85 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | 91 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, |
| 86 base::Unretained(this))); | 92 base::Unretained(this))); |
| 87 } | 93 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 105 } | 111 } |
| 106 | 112 |
| 107 // Wait until the pending establishing task is finished. | 113 // Wait until the pending establishing task is finished. |
| 108 do { | 114 do { |
| 109 establishing_condition_.Wait(); | 115 establishing_condition_.Wait(); |
| 110 } while (is_establishing_); | 116 } while (is_establishing_); |
| 111 } | 117 } |
| 112 return gpu_channel_; | 118 return gpu_channel_; |
| 113 } | 119 } |
| 114 | 120 |
| 115 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { | |
| 116 base::AutoLock auto_lock(lock_); | |
| 117 return GetGpuChannelLocked(); | |
| 118 } | |
| 119 | |
| 120 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { | 121 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { |
| 121 if (gpu_channel_ && gpu_channel_->IsLost()) { | 122 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 122 main_task_runner_->PostTask( | 123 main_task_runner_->PostTask( |
| 123 FROM_HERE, | 124 FROM_HERE, |
| 124 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); | 125 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); |
| 125 gpu_channel_ = nullptr; | 126 gpu_channel_ = nullptr; |
| 126 } | 127 } |
| 127 return gpu_channel_; | 128 return gpu_channel_; |
| 128 } | 129 } |
| 129 | 130 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 locked ? nullptr : new base::AutoLock(lock_)); | 200 locked ? nullptr : new base::AutoLock(lock_)); |
| 200 DCHECK(is_establishing_); | 201 DCHECK(is_establishing_); |
| 201 DCHECK(!gpu_channel_); | 202 DCHECK(!gpu_channel_); |
| 202 | 203 |
| 203 is_establishing_ = false; | 204 is_establishing_ = false; |
| 204 gpu_channel_ = gpu_channel; | 205 gpu_channel_ = gpu_channel; |
| 205 establishing_condition_.Broadcast(); | 206 establishing_condition_.Broadcast(); |
| 206 gpu_service_.reset(); | 207 gpu_service_.reset(); |
| 207 | 208 |
| 208 for (const auto& i : establish_callbacks_) | 209 for (const auto& i : establish_callbacks_) |
| 209 i.Run(); | 210 i.Run(gpu_channel_); |
| 210 establish_callbacks_.clear(); | 211 establish_callbacks_.clear(); |
| 211 } | 212 } |
| 212 | 213 |
| 213 bool GpuService::IsMainThread() { | 214 bool GpuService::IsMainThread() { |
| 214 return main_task_runner_->BelongsToCurrentThread(); | 215 return main_task_runner_->BelongsToCurrentThread(); |
| 215 } | 216 } |
| 216 | 217 |
| 217 scoped_refptr<base::SingleThreadTaskRunner> | 218 scoped_refptr<base::SingleThreadTaskRunner> |
| 218 GpuService::GetIOThreadTaskRunner() { | 219 GpuService::GetIOThreadTaskRunner() { |
| 219 return io_thread_.task_runner(); | 220 return io_thread_.task_runner(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 232 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 233 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 233 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 234 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 234 if (result != MOJO_RESULT_OK) | 235 if (result != MOJO_RESULT_OK) |
| 235 return nullptr; | 236 return nullptr; |
| 236 DCHECK_EQ(shared_memory_size, size); | 237 DCHECK_EQ(shared_memory_size, size); |
| 237 | 238 |
| 238 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 239 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 239 } | 240 } |
| 240 | 241 |
| 241 } // namespace ui | 242 } // namespace ui |
| OLD | NEW |