| 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 23 matching lines...) Expand all Loading... |
| 60 g_gpu_service = new GpuService(connector); | 62 g_gpu_service = new GpuService(connector); |
| 61 return base::WrapUnique(g_gpu_service); | 63 return base::WrapUnique(g_gpu_service); |
| 62 } | 64 } |
| 63 | 65 |
| 64 // static | 66 // static |
| 65 GpuService* GpuService::GetInstance() { | 67 GpuService* GpuService::GetInstance() { |
| 66 DCHECK(g_gpu_service); | 68 DCHECK(g_gpu_service); |
| 67 return g_gpu_service; | 69 return g_gpu_service; |
| 68 } | 70 } |
| 69 | 71 |
| 70 void GpuService::EstablishGpuChannel(const base::Closure& callback) { | 72 void GpuService::EstablishGpuChannel( |
| 73 const gpu::GpuChannelEstablishedCallback& callback) { |
| 71 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
| 72 auto runner = base::ThreadTaskRunnerHandle::Get(); | 75 auto runner = base::ThreadTaskRunnerHandle::Get(); |
| 73 if (GetGpuChannelLocked()) { | 76 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannelLocked(); |
| 74 runner->PostTask(FROM_HERE, callback); | 77 if (channel) { |
| 78 PostTask(runner, FROM_HERE, callback, std::move(channel)); |
| 75 return; | 79 return; |
| 76 } | 80 } |
| 77 establish_callbacks_.push_back( | 81 establish_callbacks_.push_back( |
| 78 base::Bind(PostTask, runner, FROM_HERE, callback)); | 82 base::Bind(PostTask, runner, FROM_HERE, callback)); |
| 79 if (!is_establishing_) { | 83 if (!is_establishing_) { |
| 80 is_establishing_ = true; | 84 is_establishing_ = true; |
| 81 main_task_runner_->PostTask( | 85 main_task_runner_->PostTask( |
| 82 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | 86 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, |
| 83 base::Unretained(this))); | 87 base::Unretained(this))); |
| 84 } | 88 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 102 } | 106 } |
| 103 | 107 |
| 104 // Wait until the pending establishing task is finished. | 108 // Wait until the pending establishing task is finished. |
| 105 do { | 109 do { |
| 106 establishing_condition_.Wait(); | 110 establishing_condition_.Wait(); |
| 107 } while (is_establishing_); | 111 } while (is_establishing_); |
| 108 } | 112 } |
| 109 return gpu_channel_; | 113 return gpu_channel_; |
| 110 } | 114 } |
| 111 | 115 |
| 112 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { | |
| 113 base::AutoLock auto_lock(lock_); | |
| 114 return GetGpuChannelLocked(); | |
| 115 } | |
| 116 | |
| 117 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { | 116 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { |
| 118 if (gpu_channel_ && gpu_channel_->IsLost()) { | 117 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 119 main_task_runner_->PostTask( | 118 main_task_runner_->PostTask( |
| 120 FROM_HERE, | 119 FROM_HERE, |
| 121 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); | 120 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); |
| 122 gpu_channel_ = nullptr; | 121 gpu_channel_ = nullptr; |
| 123 } | 122 } |
| 124 return gpu_channel_; | 123 return gpu_channel_; |
| 125 } | 124 } |
| 126 | 125 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 locked ? nullptr : new base::AutoLock(lock_)); | 195 locked ? nullptr : new base::AutoLock(lock_)); |
| 197 DCHECK(is_establishing_); | 196 DCHECK(is_establishing_); |
| 198 DCHECK(!gpu_channel_); | 197 DCHECK(!gpu_channel_); |
| 199 | 198 |
| 200 is_establishing_ = false; | 199 is_establishing_ = false; |
| 201 gpu_channel_ = gpu_channel; | 200 gpu_channel_ = gpu_channel; |
| 202 establishing_condition_.Broadcast(); | 201 establishing_condition_.Broadcast(); |
| 203 gpu_service_.reset(); | 202 gpu_service_.reset(); |
| 204 | 203 |
| 205 for (const auto& i : establish_callbacks_) | 204 for (const auto& i : establish_callbacks_) |
| 206 i.Run(); | 205 i.Run(gpu_channel_); |
| 207 establish_callbacks_.clear(); | 206 establish_callbacks_.clear(); |
| 208 } | 207 } |
| 209 | 208 |
| 210 bool GpuService::IsMainThread() { | 209 bool GpuService::IsMainThread() { |
| 211 return main_task_runner_->BelongsToCurrentThread(); | 210 return main_task_runner_->BelongsToCurrentThread(); |
| 212 } | 211 } |
| 213 | 212 |
| 214 scoped_refptr<base::SingleThreadTaskRunner> | 213 scoped_refptr<base::SingleThreadTaskRunner> |
| 215 GpuService::GetIOThreadTaskRunner() { | 214 GpuService::GetIOThreadTaskRunner() { |
| 216 return io_thread_.task_runner(); | 215 return io_thread_.task_runner(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 229 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 228 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 230 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 229 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 231 if (result != MOJO_RESULT_OK) | 230 if (result != MOJO_RESULT_OK) |
| 232 return nullptr; | 231 return nullptr; |
| 233 DCHECK_EQ(shared_memory_size, size); | 232 DCHECK_EQ(shared_memory_size, size); |
| 234 | 233 |
| 235 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 234 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 236 } | 235 } |
| 237 | 236 |
| 238 } // namespace ui | 237 } // namespace ui |
| OLD | NEW |