| 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/ws/gpu_service_proxy.h" | 5 #include "services/ui/ws/gpu_service_proxy.h" |
| 6 | 6 |
| 7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "gpu/ipc/client/gpu_channel_host.h" | 11 #include "gpu/ipc/client/gpu_channel_host.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 #include "services/service_manager/public/cpp/connection.h" | 13 #include "services/service_manager/public/cpp/connection.h" |
| 13 #include "services/ui/ws/gpu_service_proxy_delegate.h" | 14 #include "services/ui/ws/gpu_service_proxy_delegate.h" |
| 14 #include "services/ui/ws/mus_gpu_memory_buffer_manager.h" | 15 #include "services/ui/ws/mus_gpu_memory_buffer_manager.h" |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 namespace ws { | 18 namespace ws { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const int32_t kInternalGpuChannelClientId = 1; | 22 const int32_t kInternalGpuChannelClientId = 1; |
| 22 const uint64_t kInternalGpuChannelClientTracingId = 1; | 23 const uint64_t kInternalGpuChannelClientTracingId = 1; |
| 23 | 24 |
| 25 class GpuServiceImpl : public mojom::GpuService { |
| 26 public: |
| 27 GpuServiceImpl(int client_id, |
| 28 gpu::GPUInfo* gpu_info, |
| 29 gpu::GpuHostMemoryBufferManager* gpu_memory_buffer_manager, |
| 30 mojom::GpuServiceInternal* gpu_service_internal) |
| 31 : client_id_(client_id), |
| 32 gpu_info_(gpu_info), |
| 33 gpu_memory_buffer_manager_(gpu_memory_buffer_manager), |
| 34 gpu_service_internal_(gpu_service_internal) {} |
| 35 ~GpuServiceImpl() override {} |
| 36 |
| 37 private: |
| 38 void OnGpuChannelEstablished(const EstablishGpuChannelCallback& callback, |
| 39 mojo::ScopedMessagePipeHandle channel_handle) { |
| 40 callback.Run(client_id_, std::move(channel_handle), *gpu_info_); |
| 41 } |
| 42 |
| 43 // mojom::GpuService overrides: |
| 44 void EstablishGpuChannel( |
| 45 const EstablishGpuChannelCallback& callback) override { |
| 46 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful |
| 47 // tracing id. |
| 48 const uint64_t client_tracing_id = 0; |
| 49 constexpr bool is_gpu_host = false; |
| 50 gpu_service_internal_->EstablishGpuChannel( |
| 51 client_id_, client_tracing_id, is_gpu_host, |
| 52 base::Bind(&GpuServiceImpl::OnGpuChannelEstablished, |
| 53 base::Unretained(this), callback)); |
| 54 } |
| 55 |
| 56 void CreateGpuMemoryBuffer( |
| 57 gfx::GpuMemoryBufferId id, |
| 58 const gfx::Size& size, |
| 59 gfx::BufferFormat format, |
| 60 gfx::BufferUsage usage, |
| 61 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) |
| 62 override { |
| 63 #if 0 |
| 64 NOTIMPLEMENTED(); |
| 65 callback.Run(gfx::GpuMemoryBufferHandle()); |
| 66 #else |
| 67 gpu_memory_buffer_manager_->AllocateGpuMemoryBufferForClient( |
| 68 id, size, format, usage, client_id_, callback); |
| 69 #endif |
| 70 } |
| 71 |
| 72 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, |
| 73 const gpu::SyncToken& sync_token) override { |
| 74 NOTIMPLEMENTED(); |
| 75 } |
| 76 |
| 77 const int client_id_; |
| 78 const gpu::GPUInfo* gpu_info_; |
| 79 gpu::GpuHostMemoryBufferManager* gpu_memory_buffer_manager_; |
| 80 mojom::GpuServiceInternal* gpu_service_internal_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(GpuServiceImpl); |
| 83 }; |
| 84 |
| 24 } // namespace | 85 } // namespace |
| 25 | 86 |
| 26 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) | 87 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) |
| 27 : delegate_(delegate), | 88 : delegate_(delegate), |
| 28 next_client_id_(kInternalGpuChannelClientId + 1), | 89 next_client_id_(kInternalGpuChannelClientId + 1), |
| 29 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 90 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 30 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 91 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 31 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 92 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 32 gpu_main_.OnStart(); | 93 gpu_main_.OnStart(); |
| 33 // TODO(sad): Once GPU process is split, this would look like: | 94 // TODO(sad): Once GPU process is split, this would look like: |
| 34 // connector->ConnectToInterface("service:gpu", &gpu_service_); | 95 // connector->ConnectToInterface("service:gpu", &gpu_service_); |
| 35 gpu_main_.Create(GetProxy(&gpu_service_)); | 96 gpu_main_.Create(GetProxy(&gpu_service_)); |
| 36 gpu_service_->Initialize( | 97 gpu_service_->Initialize( |
| 37 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); | 98 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); |
| 99 |
| 100 io_thread_ = base::MakeUnique<base::Thread>("GPUIOThread"); |
| 101 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 102 thread_options.priority = base::ThreadPriority::NORMAL; |
| 103 CHECK(io_thread_->StartWithOptions(thread_options)); |
| 104 |
| 105 gpu_memory_buffer_manager_ = |
| 106 base::MakeUnique<gpu::GpuHostMemoryBufferManager>(); |
| 107 gpu_memory_buffer_manager_->SetIO(io_thread_->task_runner()); |
| 38 } | 108 } |
| 39 | 109 |
| 40 GpuServiceProxy::~GpuServiceProxy() { | 110 GpuServiceProxy::~GpuServiceProxy() { |
| 41 if (gpu_channel_) | 111 if (gpu_channel_) |
| 42 gpu_channel_->DestroyChannel(); | 112 gpu_channel_->DestroyChannel(); |
| 43 } | 113 } |
| 44 | 114 |
| 45 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { | 115 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { |
| 46 bindings_.AddBinding(this, std::move(request)); | 116 mojo::MakeStrongBinding( |
| 117 base::MakeUnique<GpuServiceImpl>(next_client_id_++, &gpu_info_, |
| 118 gpu_memory_buffer_manager_.get(), |
| 119 gpu_service_.get()), |
| 120 std::move(request)); |
| 47 } | 121 } |
| 48 | 122 |
| 49 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { | 123 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { |
| 50 gpu_info_ = gpu_info; | 124 gpu_info_ = gpu_info; |
| 51 | 125 |
| 52 constexpr bool is_gpu_host = true; | 126 constexpr bool is_gpu_host = true; |
| 53 gpu_service_->EstablishGpuChannel( | 127 gpu_service_->EstablishGpuChannel( |
| 54 kInternalGpuChannelClientId, kInternalGpuChannelClientTracingId, | 128 kInternalGpuChannelClientId, kInternalGpuChannelClientTracingId, |
| 55 is_gpu_host, base::Bind(&GpuServiceProxy::OnInternalGpuChannelEstablished, | 129 is_gpu_host, base::Bind(&GpuServiceProxy::OnInternalGpuChannelEstablished, |
| 56 base::Unretained(this))); | 130 base::Unretained(this))); |
| 57 } | 131 } |
| 58 | 132 |
| 59 void GpuServiceProxy::OnInternalGpuChannelEstablished( | 133 void GpuServiceProxy::OnInternalGpuChannelEstablished( |
| 60 mojo::ScopedMessagePipeHandle channel_handle) { | 134 mojo::ScopedMessagePipeHandle channel_handle) { |
| 61 io_thread_ = base::MakeUnique<base::Thread>("GPUIOThread"); | |
| 62 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
| 63 thread_options.priority = base::ThreadPriority::NORMAL; | |
| 64 CHECK(io_thread_->StartWithOptions(thread_options)); | |
| 65 | |
| 66 gpu_memory_buffer_manager_ = base::MakeUnique<MusGpuMemoryBufferManager>( | |
| 67 gpu_service_.get(), kInternalGpuChannelClientId); | |
| 68 gpu_channel_ = gpu::GpuChannelHost::Create( | 135 gpu_channel_ = gpu::GpuChannelHost::Create( |
| 69 this, kInternalGpuChannelClientId, gpu_info_, | 136 this, kInternalGpuChannelClientId, gpu_info_, |
| 70 IPC::ChannelHandle(channel_handle.release()), &shutdown_event_, | 137 IPC::ChannelHandle(channel_handle.release()), &shutdown_event_, |
| 71 gpu_memory_buffer_manager_.get()); | 138 gpu_memory_buffer_manager_.get()); |
| 72 if (delegate_) | 139 if (delegate_) |
| 73 delegate_->OnGpuChannelEstablished(gpu_channel_); | 140 delegate_->OnGpuChannelEstablished(gpu_channel_); |
| 74 } | 141 } |
| 75 | 142 |
| 76 void GpuServiceProxy::OnGpuChannelEstablished( | |
| 77 const EstablishGpuChannelCallback& callback, | |
| 78 int32_t client_id, | |
| 79 mojo::ScopedMessagePipeHandle channel_handle) { | |
| 80 callback.Run(client_id, std::move(channel_handle), gpu_info_); | |
| 81 } | |
| 82 | |
| 83 void GpuServiceProxy::EstablishGpuChannel( | |
| 84 const EstablishGpuChannelCallback& callback) { | |
| 85 const int client_id = next_client_id_++; | |
| 86 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful tracing | |
| 87 // id. | |
| 88 const uint64_t client_tracing_id = 0; | |
| 89 constexpr bool is_gpu_host = false; | |
| 90 gpu_service_->EstablishGpuChannel( | |
| 91 client_id, client_tracing_id, is_gpu_host, | |
| 92 base::Bind(&GpuServiceProxy::OnGpuChannelEstablished, | |
| 93 base::Unretained(this), callback, client_id)); | |
| 94 } | |
| 95 | |
| 96 void GpuServiceProxy::CreateGpuMemoryBuffer( | |
| 97 gfx::GpuMemoryBufferId id, | |
| 98 const gfx::Size& size, | |
| 99 gfx::BufferFormat format, | |
| 100 gfx::BufferUsage usage, | |
| 101 uint64_t surface_id, | |
| 102 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) { | |
| 103 NOTIMPLEMENTED(); | |
| 104 } | |
| 105 | |
| 106 void GpuServiceProxy::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
| 107 const gpu::SyncToken& sync_token) { | |
| 108 NOTIMPLEMENTED(); | |
| 109 } | |
| 110 | |
| 111 bool GpuServiceProxy::IsMainThread() { | 143 bool GpuServiceProxy::IsMainThread() { |
| 112 return main_thread_task_runner_->BelongsToCurrentThread(); | 144 return main_thread_task_runner_->BelongsToCurrentThread(); |
| 113 } | 145 } |
| 114 | 146 |
| 115 scoped_refptr<base::SingleThreadTaskRunner> | 147 scoped_refptr<base::SingleThreadTaskRunner> |
| 116 GpuServiceProxy::GetIOThreadTaskRunner() { | 148 GpuServiceProxy::GetIOThreadTaskRunner() { |
| 117 return io_thread_->task_runner(); | 149 return io_thread_->task_runner(); |
| 118 } | 150 } |
| 119 | 151 |
| 120 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( | 152 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( |
| 121 size_t size) { | 153 size_t size) { |
| 122 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 154 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 123 if (!shm->CreateAnonymous(size)) | 155 if (!shm->CreateAnonymous(size)) |
| 124 shm.reset(); | 156 shm.reset(); |
| 125 return shm; | 157 return shm; |
| 126 } | 158 } |
| 127 | 159 |
| 128 } // namespace ws | 160 } // namespace ws |
| 129 } // namespace ui | 161 } // namespace ui |
| OLD | NEW |