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 "gpu/ipc/client/gpu_memory_buffer_impl_shared_memory.h" | 12 #include "gpu/ipc/client/gpu_memory_buffer_impl_shared_memory.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
13 #include "mojo/public/cpp/system/buffer.h" | 14 #include "mojo/public/cpp/system/buffer.h" |
14 #include "mojo/public/cpp/system/platform_handle.h" | 15 #include "mojo/public/cpp/system/platform_handle.h" |
15 #include "services/service_manager/public/cpp/connection.h" | 16 #include "services/service_manager/public/cpp/connection.h" |
16 #include "services/ui/common/mus_gpu_memory_buffer_manager.h" | 17 #include "services/ui/common/mus_gpu_memory_buffer_manager.h" |
17 #include "services/ui/ws/gpu_service_proxy_delegate.h" | 18 #include "services/ui/ws/gpu_service_proxy_delegate.h" |
18 #include "ui/gfx/buffer_format_util.h" | 19 #include "ui/gfx/buffer_format_util.h" |
19 | 20 |
20 namespace ui { | 21 namespace ui { |
21 namespace ws { | 22 namespace ws { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // The client Id 1 is reserved for the display compositor. | 26 // The client Id 1 is reserved for the display compositor. |
26 const int32_t kInternalGpuChannelClientId = 2; | 27 const int32_t kInternalGpuChannelClientId = 2; |
27 | 28 |
| 29 // The implementation that relays requests from clients to the real |
| 30 // service implementation in the GPU process over mojom.GpuServiceInternal. |
| 31 class GpuServiceImpl : public mojom::GpuService { |
| 32 public: |
| 33 GpuServiceImpl(int client_id, |
| 34 gpu::GPUInfo* gpu_info, |
| 35 MusGpuMemoryBufferManager* gpu_memory_buffer_manager, |
| 36 mojom::GpuServiceInternal* gpu_service_internal) |
| 37 : client_id_(client_id), |
| 38 gpu_info_(gpu_info), |
| 39 gpu_memory_buffer_manager_(gpu_memory_buffer_manager), |
| 40 gpu_service_internal_(gpu_service_internal) { |
| 41 DCHECK(gpu_memory_buffer_manager_); |
| 42 DCHECK(gpu_service_internal_); |
| 43 } |
| 44 ~GpuServiceImpl() override { |
| 45 gpu_memory_buffer_manager_->DestroyAllGpuMemoryBufferForClient(client_id_); |
| 46 } |
| 47 |
| 48 private: |
| 49 void OnGpuChannelEstablished(const EstablishGpuChannelCallback& callback, |
| 50 mojo::ScopedMessagePipeHandle channel_handle) { |
| 51 callback.Run(client_id_, std::move(channel_handle), *gpu_info_); |
| 52 } |
| 53 |
| 54 // mojom::GpuService overrides: |
| 55 void EstablishGpuChannel( |
| 56 const EstablishGpuChannelCallback& callback) override { |
| 57 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful |
| 58 // tracing id. |
| 59 const uint64_t client_tracing_id = 0; |
| 60 constexpr bool is_gpu_host = false; |
| 61 gpu_service_internal_->EstablishGpuChannel( |
| 62 client_id_, client_tracing_id, is_gpu_host, |
| 63 base::Bind(&GpuServiceImpl::OnGpuChannelEstablished, |
| 64 base::Unretained(this), callback)); |
| 65 } |
| 66 |
| 67 void CreateGpuMemoryBuffer( |
| 68 gfx::GpuMemoryBufferId id, |
| 69 const gfx::Size& size, |
| 70 gfx::BufferFormat format, |
| 71 gfx::BufferUsage usage, |
| 72 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) |
| 73 override { |
| 74 auto handle = gpu_memory_buffer_manager_->CreateGpuMemoryBufferHandle( |
| 75 id, client_id_, size, format, usage, gpu::kNullSurfaceHandle); |
| 76 callback.Run(handle); |
| 77 } |
| 78 |
| 79 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, |
| 80 const gpu::SyncToken& sync_token) override { |
| 81 gpu_memory_buffer_manager_->DestroyGpuMemoryBuffer(id, client_id_, |
| 82 sync_token); |
| 83 } |
| 84 |
| 85 const int client_id_; |
| 86 |
| 87 // The objects these pointers refer to are owned by the GpuServiceProxy |
| 88 // object. |
| 89 const gpu::GPUInfo* gpu_info_; |
| 90 MusGpuMemoryBufferManager* gpu_memory_buffer_manager_; |
| 91 mojom::GpuServiceInternal* gpu_service_internal_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(GpuServiceImpl); |
| 94 }; |
| 95 |
28 } // namespace | 96 } // namespace |
29 | 97 |
30 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) | 98 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) |
31 : delegate_(delegate), | 99 : delegate_(delegate), |
32 next_client_id_(kInternalGpuChannelClientId + 1), | 100 next_client_id_(kInternalGpuChannelClientId + 1), |
33 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 101 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
34 gpu_main_impl_ = base::MakeUnique<GpuMain>(GetProxy(&gpu_main_)); | 102 gpu_main_impl_ = base::MakeUnique<GpuMain>(GetProxy(&gpu_main_)); |
35 gpu_main_impl_->OnStart(); | 103 gpu_main_impl_->OnStart(); |
36 // TODO(sad): Once GPU process is split, this would look like: | 104 // TODO(sad): Once GPU process is split, this would look like: |
37 // connector->ConnectToInterface("gpu", &gpu_main_); | 105 // connector->ConnectToInterface("gpu", &gpu_main_); |
38 gpu_main_->CreateGpuService( | 106 gpu_main_->CreateGpuService( |
39 GetProxy(&gpu_service_), | 107 GetProxy(&gpu_service_), |
40 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); | 108 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); |
41 gpu_memory_buffer_manager_ = base::MakeUnique<MusGpuMemoryBufferManager>( | 109 gpu_memory_buffer_manager_ = base::MakeUnique<MusGpuMemoryBufferManager>( |
42 gpu_service_.get(), next_client_id_++); | 110 gpu_service_.get(), next_client_id_++); |
43 } | 111 } |
44 | 112 |
45 GpuServiceProxy::~GpuServiceProxy() { | 113 GpuServiceProxy::~GpuServiceProxy() { |
46 } | 114 } |
47 | 115 |
48 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { | 116 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { |
49 bindings_.AddBinding(this, std::move(request)); | 117 mojo::MakeStrongBinding( |
| 118 base::MakeUnique<GpuServiceImpl>(next_client_id_++, &gpu_info_, |
| 119 gpu_memory_buffer_manager_.get(), |
| 120 gpu_service_.get()), |
| 121 std::move(request)); |
50 } | 122 } |
51 | 123 |
52 void GpuServiceProxy::CreateDisplayCompositor( | 124 void GpuServiceProxy::CreateDisplayCompositor( |
53 cc::mojom::DisplayCompositorRequest request, | 125 cc::mojom::DisplayCompositorRequest request, |
54 cc::mojom::DisplayCompositorClientPtr client) { | 126 cc::mojom::DisplayCompositorClientPtr client) { |
55 gpu_main_->CreateDisplayCompositor(std::move(request), std::move(client)); | 127 gpu_main_->CreateDisplayCompositor(std::move(request), std::move(client)); |
56 } | 128 } |
57 | 129 |
58 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { | 130 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { |
59 gpu_info_ = gpu_info; | 131 gpu_info_ = gpu_info; |
60 | |
61 delegate_->OnGpuServiceInitialized(); | 132 delegate_->OnGpuServiceInitialized(); |
62 } | 133 } |
63 | 134 |
64 void GpuServiceProxy::OnGpuChannelEstablished( | |
65 const EstablishGpuChannelCallback& callback, | |
66 int32_t client_id, | |
67 mojo::ScopedMessagePipeHandle channel_handle) { | |
68 callback.Run(client_id, std::move(channel_handle), gpu_info_); | |
69 } | |
70 | |
71 void GpuServiceProxy::EstablishGpuChannel( | |
72 const EstablishGpuChannelCallback& callback) { | |
73 const int client_id = next_client_id_++; | |
74 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful tracing | |
75 // id. | |
76 const uint64_t client_tracing_id = 0; | |
77 constexpr bool is_gpu_host = false; | |
78 gpu_service_->EstablishGpuChannel( | |
79 client_id, client_tracing_id, is_gpu_host, | |
80 base::Bind(&GpuServiceProxy::OnGpuChannelEstablished, | |
81 base::Unretained(this), callback, client_id)); | |
82 } | |
83 | |
84 void GpuServiceProxy::CreateGpuMemoryBuffer( | |
85 gfx::GpuMemoryBufferId id, | |
86 const gfx::Size& size, | |
87 gfx::BufferFormat format, | |
88 gfx::BufferUsage usage, | |
89 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) { | |
90 // TODO(sad): Check to see if native gpu memory buffer can be used first. | |
91 if (!gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage) || | |
92 !gpu::GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(size, | |
93 format)) { | |
94 callback.Run(gfx::GpuMemoryBufferHandle()); | |
95 return; | |
96 } | |
97 callback.Run(gpu::GpuMemoryBufferImplSharedMemory::CreateGpuMemoryBuffer( | |
98 id, size, format)); | |
99 } | |
100 | |
101 void GpuServiceProxy::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
102 const gpu::SyncToken& sync_token) { | |
103 // NOTIMPLEMENTED(); | |
104 } | |
105 | |
106 } // namespace ws | 135 } // namespace ws |
107 } // namespace ui | 136 } // namespace ui |
OLD | NEW |