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/common/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/shell/public/cpp/connector.h" | |
14 #include "services/ui/common/gpu_type_converters.h" | |
15 #include "services/ui/common/mojo_gpu_memory_buffer_manager.h" | |
16 #include "services/ui/common/switches.h" | |
17 #include "services/ui/public/interfaces/gpu_service.mojom.h" | |
18 | |
19 namespace ui { | |
20 | |
21 namespace { | |
22 | |
23 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, | |
24 const tracked_objects::Location& from_here, | |
25 const base::Closure& callback) { | |
26 runner->PostTask(from_here, callback); | |
27 } | |
28 | |
29 GpuService* g_gpu_service = nullptr; | |
30 } | |
31 | |
32 GpuService::GpuService(shell::Connector* connector) | |
33 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
34 connector_(connector), | |
35 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
36 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
37 io_thread_("GPUIOThread"), | |
38 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), | |
39 is_establishing_(false), | |
40 establishing_condition_(&lock_) { | |
41 DCHECK(main_task_runner_); | |
42 DCHECK(connector_); | |
43 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
44 thread_options.priority = base::ThreadPriority::NORMAL; | |
45 CHECK(io_thread_.StartWithOptions(thread_options)); | |
46 } | |
47 | |
48 GpuService::~GpuService() { | |
49 DCHECK(IsMainThread()); | |
50 if (gpu_channel_) | |
51 gpu_channel_->DestroyChannel(); | |
52 } | |
53 | |
54 // static | |
55 void GpuService::Initialize(shell::Connector* connector) { | |
56 DCHECK(!g_gpu_service); | |
57 g_gpu_service = new GpuService(connector); | |
58 } | |
59 | |
60 // static | |
61 void GpuService::Terminate() { | |
62 DCHECK(g_gpu_service); | |
63 delete g_gpu_service; | |
64 g_gpu_service = nullptr; | |
65 } | |
66 | |
67 // static | |
68 GpuService* GpuService::GetInstance() { | |
69 DCHECK(g_gpu_service); | |
70 return g_gpu_service; | |
71 } | |
72 | |
73 void GpuService::EstablishGpuChannel(const base::Closure& callback) { | |
74 base::AutoLock auto_lock(lock_); | |
75 auto runner = base::ThreadTaskRunnerHandle::Get(); | |
76 if (GetGpuChannelLocked()) { | |
77 runner->PostTask(FROM_HERE, callback); | |
78 return; | |
79 } | |
80 establish_callbacks_.push_back( | |
81 base::Bind(PostTask, runner, FROM_HERE, callback)); | |
82 if (!is_establishing_) { | |
83 is_establishing_ = true; | |
84 main_task_runner_->PostTask( | |
85 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | |
86 base::Unretained(this))); | |
87 } | |
88 } | |
89 | |
90 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync() { | |
91 base::AutoLock auto_lock(lock_); | |
92 if (GetGpuChannelLocked()) | |
93 return gpu_channel_; | |
94 | |
95 if (IsMainThread()) { | |
96 is_establishing_ = true; | |
97 EstablishGpuChannelOnMainThreadSyncLocked(); | |
98 } else { | |
99 if (!is_establishing_) { | |
100 // Create an establishing gpu channel task, if there isn't one. | |
101 is_establishing_ = true; | |
102 main_task_runner_->PostTask( | |
103 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | |
104 base::Unretained(this))); | |
105 } | |
106 | |
107 // Wait until the pending establishing task is finished. | |
108 do { | |
109 establishing_condition_.Wait(); | |
110 } while (is_establishing_); | |
111 } | |
112 return gpu_channel_; | |
113 } | |
114 | |
115 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { | |
116 base::AutoLock auto_lock(lock_); | |
117 return GetGpuChannelLocked(); | |
118 } | |
119 | |
120 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { | |
121 if (gpu_channel_ && gpu_channel_->IsLost()) { | |
122 main_task_runner_->PostTask( | |
123 FROM_HERE, | |
124 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); | |
125 gpu_channel_ = nullptr; | |
126 } | |
127 return gpu_channel_; | |
128 } | |
129 | |
130 void GpuService::EstablishGpuChannelOnMainThread() { | |
131 base::AutoLock auto_lock(lock_); | |
132 DCHECK(IsMainThread()); | |
133 | |
134 // In GpuService::EstablishGpuChannelOnMainThreadSyncLocked(), we use the sync | |
135 // mojo EstablishGpuChannel call, after that call the gpu_service_ will be | |
136 // reset immediatelly. So gpu_service_ should be always null here. | |
137 DCHECK(!gpu_service_); | |
138 | |
139 // is_establishing_ is false, it means GpuService::EstablishGpuChannelSync() | |
140 // has been used, and we don't need try to establish a new GPU channel | |
141 // anymore. | |
142 if (!is_establishing_) | |
143 return; | |
144 | |
145 connector_->ConnectToInterface("mojo:ui", &gpu_service_); | |
146 const bool locked = false; | |
147 gpu_service_->EstablishGpuChannel( | |
148 base::Bind(&GpuService::EstablishGpuChannelOnMainThreadDone, | |
149 base::Unretained(this), locked)); | |
150 } | |
151 | |
152 void GpuService::EstablishGpuChannelOnMainThreadSyncLocked() { | |
153 DCHECK(IsMainThread()); | |
154 DCHECK(is_establishing_); | |
155 | |
156 // In browser process, EstablishGpuChannelSync() is only used by testing & | |
157 // GpuProcessTransportFactory::GetGLHelper(). For GetGLHelper(), it expects | |
158 // the gpu channel has been established, so it should not reach here. | |
159 // For testing, the asyc method should not be used. | |
160 // In renderer process, we only use EstablishGpuChannelSync(). | |
161 // So the gpu_service_ should be null here. | |
162 DCHECK(!gpu_service_); | |
163 | |
164 int client_id = 0; | |
165 mojom::ChannelHandlePtr channel_handle; | |
166 mojom::GpuInfoPtr gpu_info; | |
167 connector_->ConnectToInterface("mojo:ui", &gpu_service_); | |
168 { | |
169 base::AutoUnlock auto_unlock(lock_); | |
170 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | |
171 if (!gpu_service_->EstablishGpuChannel(&client_id, &channel_handle, | |
172 &gpu_info)) { | |
173 DLOG(WARNING) | |
174 << "Channel encountered error while establishing gpu channel."; | |
175 return; | |
176 } | |
177 } | |
178 const bool locked = true; | |
179 EstablishGpuChannelOnMainThreadDone( | |
180 locked, client_id, std::move(channel_handle), std::move(gpu_info)); | |
181 } | |
182 | |
183 void GpuService::EstablishGpuChannelOnMainThreadDone( | |
184 bool locked, | |
185 int client_id, | |
186 mojom::ChannelHandlePtr channel_handle, | |
187 mojom::GpuInfoPtr gpu_info) { | |
188 DCHECK(IsMainThread()); | |
189 scoped_refptr<gpu::GpuChannelHost> gpu_channel; | |
190 if (client_id) { | |
191 // TODO(penghuang): Get the real gpu info from mus. | |
192 gpu_channel = gpu::GpuChannelHost::Create( | |
193 this, client_id, gpu::GPUInfo(), | |
194 channel_handle.To<IPC::ChannelHandle>(), &shutdown_event_, | |
195 gpu_memory_buffer_manager_.get()); | |
196 } | |
197 | |
198 auto auto_lock = base::WrapUnique<base::AutoLock>( | |
199 locked ? nullptr : new base::AutoLock(lock_)); | |
200 DCHECK(is_establishing_); | |
201 DCHECK(!gpu_channel_); | |
202 | |
203 is_establishing_ = false; | |
204 gpu_channel_ = gpu_channel; | |
205 establishing_condition_.Broadcast(); | |
206 gpu_service_.reset(); | |
207 | |
208 for (const auto& i : establish_callbacks_) | |
209 i.Run(); | |
210 establish_callbacks_.clear(); | |
211 } | |
212 | |
213 bool GpuService::IsMainThread() { | |
214 return main_task_runner_->BelongsToCurrentThread(); | |
215 } | |
216 | |
217 scoped_refptr<base::SingleThreadTaskRunner> | |
218 GpuService::GetIOThreadTaskRunner() { | |
219 return io_thread_.task_runner(); | |
220 } | |
221 | |
222 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( | |
223 size_t size) { | |
224 mojo::ScopedSharedBufferHandle handle = | |
225 mojo::SharedBufferHandle::Create(size); | |
226 if (!handle.is_valid()) | |
227 return nullptr; | |
228 | |
229 base::SharedMemoryHandle platform_handle; | |
230 size_t shared_memory_size; | |
231 bool readonly; | |
232 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
233 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | |
234 if (result != MOJO_RESULT_OK) | |
235 return nullptr; | |
236 DCHECK_EQ(shared_memory_size, size); | |
237 | |
238 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | |
239 } | |
240 | |
241 } // namespace ui | |
OLD | NEW |