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