| 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/public/cpp/gpu/gpu.h" | 5 #include "services/ui/public/cpp/gpu/gpu.h" |
| 6 | 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" | 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" | 9 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
| 10 #include "mojo/public/cpp/system/platform_handle.h" | 10 #include "mojo/public/cpp/system/platform_handle.h" |
| 11 #include "services/service_manager/public/cpp/connector.h" | 11 #include "services/service_manager/public/cpp/connector.h" |
| 12 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h" | 12 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h" |
| 13 #include "services/ui/public/interfaces/constants.mojom.h" | 13 #include "services/ui/public/interfaces/constants.mojom.h" |
| 14 #include "services/ui/public/interfaces/gpu.mojom.h" | 14 #include "services/ui/public/interfaces/gpu.mojom.h" |
| 15 | 15 |
| 16 namespace ui { | 16 namespace ui { |
| 17 | 17 |
| 18 Gpu::Gpu(service_manager::Connector* connector, | 18 Gpu::Gpu(service_manager::Connector* connector, |
| 19 service_manager::InterfaceProvider* provider, |
| 19 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 20 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 20 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 21 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 21 io_task_runner_(std::move(task_runner)), | 22 io_task_runner_(std::move(task_runner)), |
| 22 connector_(connector), | 23 connector_(connector), |
| 24 interface_provider_(provider), |
| 23 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 25 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 24 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 26 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 25 DCHECK(main_task_runner_); | 27 DCHECK(main_task_runner_); |
| 26 DCHECK(connector_); | 28 DCHECK(connector_ || interface_provider_); |
| 27 mojom::GpuPtr gpu_service_ptr; | 29 mojom::GpuPtr gpu_ptr; |
| 28 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_ptr); | 30 if (connector_) |
| 29 gpu_memory_buffer_manager_ = base::MakeUnique<ClientGpuMemoryBufferManager>( | 31 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_ptr); |
| 30 std::move(gpu_service_ptr)); | 32 else |
| 33 interface_provider_->GetInterface(&gpu_ptr); |
| 34 gpu_memory_buffer_manager_ = |
| 35 base::MakeUnique<ClientGpuMemoryBufferManager>(std::move(gpu_ptr)); |
| 31 if (!io_task_runner_) { | 36 if (!io_task_runner_) { |
| 32 io_thread_.reset(new base::Thread("GPUIOThread")); | 37 io_thread_.reset(new base::Thread("GPUIOThread")); |
| 33 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 38 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 34 thread_options.priority = base::ThreadPriority::NORMAL; | 39 thread_options.priority = base::ThreadPriority::NORMAL; |
| 35 CHECK(io_thread_->StartWithOptions(thread_options)); | 40 CHECK(io_thread_->StartWithOptions(thread_options)); |
| 36 io_task_runner_ = io_thread_->task_runner(); | 41 io_task_runner_ = io_thread_->task_runner(); |
| 37 } | 42 } |
| 38 } | 43 } |
| 39 | 44 |
| 40 Gpu::~Gpu() { | 45 Gpu::~Gpu() { |
| 41 DCHECK(IsMainThread()); | 46 DCHECK(IsMainThread()); |
| 42 for (const auto& callback : establish_callbacks_) | 47 for (const auto& callback : establish_callbacks_) |
| 43 callback.Run(nullptr); | 48 callback.Run(nullptr); |
| 44 shutdown_event_.Signal(); | 49 shutdown_event_.Signal(); |
| 45 if (gpu_channel_) | 50 if (gpu_channel_) |
| 46 gpu_channel_->DestroyChannel(); | 51 gpu_channel_->DestroyChannel(); |
| 47 } | 52 } |
| 48 | 53 |
| 49 // static | 54 // static |
| 50 std::unique_ptr<Gpu> Gpu::Create( | 55 std::unique_ptr<Gpu> Gpu::Create( |
| 51 service_manager::Connector* connector, | 56 service_manager::Connector* connector, |
| 52 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 57 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 53 return base::WrapUnique(new Gpu(connector, std::move(task_runner))); | 58 return base::WrapUnique(new Gpu(connector, nullptr, std::move(task_runner))); |
| 59 } |
| 60 |
| 61 std::unique_ptr<Gpu> Gpu::Create( |
| 62 service_manager::InterfaceProvider* provider, |
| 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 64 return base::WrapUnique(new Gpu(nullptr, provider, std::move(task_runner))); |
| 54 } | 65 } |
| 55 | 66 |
| 56 void Gpu::EstablishGpuChannel( | 67 void Gpu::EstablishGpuChannel( |
| 57 const gpu::GpuChannelEstablishedCallback& callback) { | 68 const gpu::GpuChannelEstablishedCallback& callback) { |
| 58 DCHECK(IsMainThread()); | 69 DCHECK(IsMainThread()); |
| 59 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannel(); | 70 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannel(); |
| 60 if (channel) { | 71 if (channel) { |
| 61 main_task_runner_->PostTask(FROM_HERE, | 72 main_task_runner_->PostTask(FROM_HERE, |
| 62 base::Bind(callback, std::move(channel))); | 73 base::Bind(callback, std::move(channel))); |
| 63 return; | 74 return; |
| 64 } | 75 } |
| 65 establish_callbacks_.push_back(callback); | 76 establish_callbacks_.push_back(callback); |
| 66 if (gpu_service_) | 77 if (gpu_) |
| 67 return; | 78 return; |
| 68 | 79 |
| 69 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); | 80 if (connector_) |
| 70 gpu_service_->EstablishGpuChannel( | 81 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_); |
| 82 else |
| 83 interface_provider_->GetInterface(&gpu_); |
| 84 gpu_->EstablishGpuChannel( |
| 71 base::Bind(&Gpu::OnEstablishedGpuChannel, base::Unretained(this))); | 85 base::Bind(&Gpu::OnEstablishedGpuChannel, base::Unretained(this))); |
| 72 } | 86 } |
| 73 | 87 |
| 74 scoped_refptr<gpu::GpuChannelHost> Gpu::EstablishGpuChannelSync() { | 88 scoped_refptr<gpu::GpuChannelHost> Gpu::EstablishGpuChannelSync() { |
| 75 DCHECK(IsMainThread()); | 89 DCHECK(IsMainThread()); |
| 76 if (GetGpuChannel()) | 90 if (GetGpuChannel()) |
| 77 return gpu_channel_; | 91 return gpu_channel_; |
| 78 | 92 |
| 79 int client_id = 0; | 93 int client_id = 0; |
| 80 mojo::ScopedMessagePipeHandle channel_handle; | 94 mojo::ScopedMessagePipeHandle channel_handle; |
| 81 gpu::GPUInfo gpu_info; | 95 gpu::GPUInfo gpu_info; |
| 82 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); | 96 if (connector_) |
| 97 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_); |
| 98 else |
| 99 interface_provider_->GetInterface(&gpu_); |
| 83 | 100 |
| 84 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 101 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 85 if (!gpu_service_->EstablishGpuChannel(&client_id, &channel_handle, | 102 if (!gpu_->EstablishGpuChannel(&client_id, &channel_handle, &gpu_info)) { |
| 86 &gpu_info)) { | |
| 87 DLOG(WARNING) | 103 DLOG(WARNING) |
| 88 << "Channel encountered error while establishing gpu channel."; | 104 << "Channel encountered error while establishing gpu channel."; |
| 89 return nullptr; | 105 return nullptr; |
| 90 } | 106 } |
| 91 OnEstablishedGpuChannel(client_id, std::move(channel_handle), gpu_info); | 107 OnEstablishedGpuChannel(client_id, std::move(channel_handle), gpu_info); |
| 92 return gpu_channel_; | 108 return gpu_channel_; |
| 93 } | 109 } |
| 94 | 110 |
| 95 gpu::GpuMemoryBufferManager* Gpu::GetGpuMemoryBufferManager() { | 111 gpu::GpuMemoryBufferManager* Gpu::GetGpuMemoryBufferManager() { |
| 96 return gpu_memory_buffer_manager_.get(); | 112 return gpu_memory_buffer_manager_.get(); |
| 97 } | 113 } |
| 98 | 114 |
| 99 scoped_refptr<gpu::GpuChannelHost> Gpu::GetGpuChannel() { | 115 scoped_refptr<gpu::GpuChannelHost> Gpu::GetGpuChannel() { |
| 100 DCHECK(IsMainThread()); | 116 DCHECK(IsMainThread()); |
| 101 if (gpu_channel_ && gpu_channel_->IsLost()) { | 117 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 102 gpu_channel_->DestroyChannel(); | 118 gpu_channel_->DestroyChannel(); |
| 103 gpu_channel_ = nullptr; | 119 gpu_channel_ = nullptr; |
| 104 } | 120 } |
| 105 return gpu_channel_; | 121 return gpu_channel_; |
| 106 } | 122 } |
| 107 | 123 |
| 108 void Gpu::OnEstablishedGpuChannel(int client_id, | 124 void Gpu::OnEstablishedGpuChannel(int client_id, |
| 109 mojo::ScopedMessagePipeHandle channel_handle, | 125 mojo::ScopedMessagePipeHandle channel_handle, |
| 110 const gpu::GPUInfo& gpu_info) { | 126 const gpu::GPUInfo& gpu_info) { |
| 111 DCHECK(IsMainThread()); | 127 DCHECK(IsMainThread()); |
| 112 DCHECK(gpu_service_.get()); | 128 DCHECK(gpu_.get()); |
| 113 DCHECK(!gpu_channel_); | 129 DCHECK(!gpu_channel_); |
| 114 | 130 |
| 115 if (client_id && channel_handle.is_valid()) { | 131 if (client_id && channel_handle.is_valid()) { |
| 116 gpu_channel_ = gpu::GpuChannelHost::Create( | 132 gpu_channel_ = gpu::GpuChannelHost::Create( |
| 117 this, client_id, gpu_info, IPC::ChannelHandle(channel_handle.release()), | 133 this, client_id, gpu_info, IPC::ChannelHandle(channel_handle.release()), |
| 118 &shutdown_event_, gpu_memory_buffer_manager_.get()); | 134 &shutdown_event_, gpu_memory_buffer_manager_.get()); |
| 119 } | 135 } |
| 120 | 136 |
| 121 gpu_service_.reset(); | 137 gpu_.reset(); |
| 122 for (const auto& i : establish_callbacks_) | 138 for (const auto& i : establish_callbacks_) |
| 123 i.Run(gpu_channel_); | 139 i.Run(gpu_channel_); |
| 124 establish_callbacks_.clear(); | 140 establish_callbacks_.clear(); |
| 125 } | 141 } |
| 126 | 142 |
| 127 bool Gpu::IsMainThread() { | 143 bool Gpu::IsMainThread() { |
| 128 return main_task_runner_->BelongsToCurrentThread(); | 144 return main_task_runner_->BelongsToCurrentThread(); |
| 129 } | 145 } |
| 130 | 146 |
| 131 scoped_refptr<base::SingleThreadTaskRunner> Gpu::GetIOThreadTaskRunner() { | 147 scoped_refptr<base::SingleThreadTaskRunner> Gpu::GetIOThreadTaskRunner() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 144 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 160 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 145 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 161 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 146 if (result != MOJO_RESULT_OK) | 162 if (result != MOJO_RESULT_OK) |
| 147 return nullptr; | 163 return nullptr; |
| 148 DCHECK_EQ(shared_memory_size, size); | 164 DCHECK_EQ(shared_memory_size, size); |
| 149 | 165 |
| 150 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 166 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 151 } | 167 } |
| 152 | 168 |
| 153 } // namespace ui | 169 } // namespace ui |
| OLD | NEW |