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/ws/gpu_service_proxy_delegate.h" | 17 #include "services/ui/ws/gpu_service_proxy_delegate.h" |
17 #include "services/ui/ws/mus_gpu_memory_buffer_manager.h" | 18 #include "services/ui/ws/mus_gpu_memory_buffer_manager.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 const int32_t kInternalGpuChannelClientId = 1; | 26 const int32_t kInternalGpuChannelClientId = 1; |
26 const uint64_t kInternalGpuChannelClientTracingId = 1; | 27 const uint64_t kInternalGpuChannelClientTracingId = 1; |
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 { | |
reveman
2016/11/22 08:18:56
I'm confused by this. Who is supposed to use this
sadrul
2016/11/22 18:45:41
I have shared a doc [1] with you that hopefully ex
reveman
2016/11/22 23:25:04
Ok, makes sense. I guess MojoGMBManager can't talk
sadrul
2016/11/24 02:02:41
It could if we really wanted to. But that would me
reveman
2016/11/30 17:32:45
Ok, what happens if CreateGpuMemoryBuffer is calle
sadrul
2016/11/30 19:54:31
The short answer is: yes. But let me explain:
if
| |
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 | |
46 private: | |
47 void OnGpuChannelEstablished(const EstablishGpuChannelCallback& callback, | |
48 mojo::ScopedMessagePipeHandle channel_handle) { | |
49 callback.Run(client_id_, std::move(channel_handle), *gpu_info_); | |
50 } | |
51 | |
52 // mojom::GpuService overrides: | |
53 void EstablishGpuChannel( | |
54 const EstablishGpuChannelCallback& callback) override { | |
55 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful | |
56 // tracing id. | |
57 const uint64_t client_tracing_id = 0; | |
58 constexpr bool is_gpu_host = false; | |
59 gpu_service_internal_->EstablishGpuChannel( | |
60 client_id_, client_tracing_id, is_gpu_host, | |
61 base::Bind(&GpuServiceImpl::OnGpuChannelEstablished, | |
62 base::Unretained(this), callback)); | |
63 } | |
64 | |
65 void CreateGpuMemoryBuffer( | |
66 gfx::GpuMemoryBufferId id, | |
67 const gfx::Size& size, | |
68 gfx::BufferFormat format, | |
69 gfx::BufferUsage usage, | |
70 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) | |
71 override { | |
72 auto handle = gpu_memory_buffer_manager_->AllocateGpuMemoryBufferHandle( | |
73 id, client_id_, size, format, usage, gpu::kNullSurfaceHandle); | |
74 callback.Run(handle); | |
75 } | |
76 | |
77 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
78 const gpu::SyncToken& sync_token) override { | |
79 gpu_memory_buffer_manager_->DestroyGpuMemoryBuffer(id, client_id_, | |
80 sync_token); | |
81 } | |
82 | |
83 const int client_id_; | |
84 | |
85 // The objects these pointers refer to are owned by the GpuServiceProxy | |
86 // object. | |
87 const gpu::GPUInfo* gpu_info_; | |
88 MusGpuMemoryBufferManager* gpu_memory_buffer_manager_; | |
89 mojom::GpuServiceInternal* gpu_service_internal_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(GpuServiceImpl); | |
92 }; | |
93 | |
28 } // namespace | 94 } // namespace |
29 | 95 |
30 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) | 96 GpuServiceProxy::GpuServiceProxy(GpuServiceProxyDelegate* delegate) |
31 : delegate_(delegate), | 97 : delegate_(delegate), |
32 next_client_id_(kInternalGpuChannelClientId + 1), | 98 next_client_id_(kInternalGpuChannelClientId + 1), |
33 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 99 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
34 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 100 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
35 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 101 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
36 gpu_main_.OnStart(); | 102 gpu_main_.OnStart(); |
37 // TODO(sad): Once GPU process is split, this would look like: | 103 // TODO(sad): Once GPU process is split, this would look like: |
38 // connector->ConnectToInterface("gpu", &gpu_service_); | 104 // connector->ConnectToInterface("gpu", &gpu_service_); |
39 gpu_main_.Create(GetProxy(&gpu_service_)); | 105 gpu_main_.Create(GetProxy(&gpu_service_)); |
40 gpu_service_->Initialize( | 106 gpu_service_->Initialize( |
41 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); | 107 base::Bind(&GpuServiceProxy::OnInitialized, base::Unretained(this))); |
108 | |
109 gpu_memory_buffer_manager_ = base::MakeUnique<MusGpuMemoryBufferManager>( | |
110 gpu_service_.get(), kInternalGpuChannelClientId); | |
42 } | 111 } |
43 | 112 |
44 GpuServiceProxy::~GpuServiceProxy() { | 113 GpuServiceProxy::~GpuServiceProxy() { |
45 if (gpu_channel_) | 114 if (gpu_channel_) |
46 gpu_channel_->DestroyChannel(); | 115 gpu_channel_->DestroyChannel(); |
47 } | 116 } |
48 | 117 |
49 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { | 118 void GpuServiceProxy::Add(mojom::GpuServiceRequest request) { |
50 bindings_.AddBinding(this, std::move(request)); | 119 mojo::MakeStrongBinding( |
120 base::MakeUnique<GpuServiceImpl>(next_client_id_++, &gpu_info_, | |
121 gpu_memory_buffer_manager_.get(), | |
122 gpu_service_.get()), | |
123 std::move(request)); | |
51 } | 124 } |
52 | 125 |
53 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { | 126 void GpuServiceProxy::OnInitialized(const gpu::GPUInfo& gpu_info) { |
54 gpu_info_ = gpu_info; | 127 gpu_info_ = gpu_info; |
55 | 128 |
56 constexpr bool is_gpu_host = true; | 129 constexpr bool is_gpu_host = true; |
57 gpu_service_->EstablishGpuChannel( | 130 gpu_service_->EstablishGpuChannel( |
58 kInternalGpuChannelClientId, kInternalGpuChannelClientTracingId, | 131 kInternalGpuChannelClientId, kInternalGpuChannelClientTracingId, |
59 is_gpu_host, base::Bind(&GpuServiceProxy::OnInternalGpuChannelEstablished, | 132 is_gpu_host, base::Bind(&GpuServiceProxy::OnInternalGpuChannelEstablished, |
60 base::Unretained(this))); | 133 base::Unretained(this))); |
61 } | 134 } |
62 | 135 |
63 void GpuServiceProxy::OnInternalGpuChannelEstablished( | 136 void GpuServiceProxy::OnInternalGpuChannelEstablished( |
64 mojo::ScopedMessagePipeHandle channel_handle) { | 137 mojo::ScopedMessagePipeHandle channel_handle) { |
65 io_thread_ = base::MakeUnique<base::Thread>("GPUIOThread"); | 138 io_thread_ = base::MakeUnique<base::Thread>("GPUIOThread"); |
66 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 139 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
67 thread_options.priority = base::ThreadPriority::NORMAL; | 140 thread_options.priority = base::ThreadPriority::NORMAL; |
68 CHECK(io_thread_->StartWithOptions(thread_options)); | 141 CHECK(io_thread_->StartWithOptions(thread_options)); |
69 | |
70 gpu_memory_buffer_manager_ = base::MakeUnique<MusGpuMemoryBufferManager>( | |
71 gpu_service_.get(), kInternalGpuChannelClientId); | |
72 gpu_channel_ = gpu::GpuChannelHost::Create( | 142 gpu_channel_ = gpu::GpuChannelHost::Create( |
73 this, kInternalGpuChannelClientId, gpu_info_, | 143 this, kInternalGpuChannelClientId, gpu_info_, |
74 IPC::ChannelHandle(channel_handle.release()), &shutdown_event_, | 144 IPC::ChannelHandle(channel_handle.release()), &shutdown_event_, |
75 gpu_memory_buffer_manager_.get()); | 145 gpu_memory_buffer_manager_.get()); |
76 if (delegate_) | 146 if (delegate_) |
77 delegate_->OnGpuChannelEstablished(gpu_channel_); | 147 delegate_->OnGpuChannelEstablished(gpu_channel_); |
78 } | 148 } |
79 | 149 |
80 void GpuServiceProxy::OnGpuChannelEstablished( | |
81 const EstablishGpuChannelCallback& callback, | |
82 int32_t client_id, | |
83 mojo::ScopedMessagePipeHandle channel_handle) { | |
84 callback.Run(client_id, std::move(channel_handle), gpu_info_); | |
85 } | |
86 | |
87 void GpuServiceProxy::EstablishGpuChannel( | |
88 const EstablishGpuChannelCallback& callback) { | |
89 const int client_id = next_client_id_++; | |
90 // TODO(sad): crbug.com/617415 figure out how to generate a meaningful tracing | |
91 // id. | |
92 const uint64_t client_tracing_id = 0; | |
93 constexpr bool is_gpu_host = false; | |
94 gpu_service_->EstablishGpuChannel( | |
95 client_id, client_tracing_id, is_gpu_host, | |
96 base::Bind(&GpuServiceProxy::OnGpuChannelEstablished, | |
97 base::Unretained(this), callback, client_id)); | |
98 } | |
99 | |
100 void GpuServiceProxy::CreateGpuMemoryBuffer( | |
101 gfx::GpuMemoryBufferId id, | |
102 const gfx::Size& size, | |
103 gfx::BufferFormat format, | |
104 gfx::BufferUsage usage, | |
105 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) { | |
106 // TODO(sad): Check to see if native gpu memory buffer can be used first. | |
107 if (!gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage) || | |
108 !gpu::GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(size, | |
109 format)) { | |
110 callback.Run(gfx::GpuMemoryBufferHandle()); | |
111 return; | |
112 } | |
113 callback.Run(gpu::GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( | |
114 id, size, format)); | |
115 } | |
116 | |
117 void GpuServiceProxy::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
118 const gpu::SyncToken& sync_token) { | |
119 // NOTIMPLEMENTED(); | |
120 } | |
121 | |
122 bool GpuServiceProxy::IsMainThread() { | 150 bool GpuServiceProxy::IsMainThread() { |
123 return main_thread_task_runner_->BelongsToCurrentThread(); | 151 return main_thread_task_runner_->BelongsToCurrentThread(); |
124 } | 152 } |
125 | 153 |
126 scoped_refptr<base::SingleThreadTaskRunner> | 154 scoped_refptr<base::SingleThreadTaskRunner> |
127 GpuServiceProxy::GetIOThreadTaskRunner() { | 155 GpuServiceProxy::GetIOThreadTaskRunner() { |
128 return io_thread_->task_runner(); | 156 return io_thread_->task_runner(); |
129 } | 157 } |
130 | 158 |
131 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( | 159 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( |
132 size_t size) { | 160 size_t size) { |
133 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 161 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
134 if (!shm->CreateAnonymous(size)) | 162 if (!shm->CreateAnonymous(size)) |
135 shm.reset(); | 163 shm.reset(); |
136 return shm; | 164 return shm; |
137 } | 165 } |
138 | 166 |
139 } // namespace ws | 167 } // namespace ws |
140 } // namespace ui | 168 } // namespace ui |
OLD | NEW |