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