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