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 "components/mus/gpu/gpu_service_mus.h" | |
6 | |
7 #include "base/memory/shared_memory.h" | |
8 #include "base/memory/singleton.h" | |
9 #include "base/synchronization/waitable_event.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "build/build_config.h" | |
12 #include "components/mus/gpu/mus_gpu_memory_buffer_manager.h" | |
13 #include "gpu/command_buffer/service/gpu_switches.h" | |
14 #include "gpu/command_buffer/service/sync_point_manager.h" | |
15 #include "gpu/config/gpu_info_collector.h" | |
16 #include "gpu/config/gpu_switches.h" | |
17 #include "gpu/config/gpu_util.h" | |
18 #include "gpu/ipc/common/gpu_memory_buffer_support.h" | |
19 #include "gpu/ipc/common/memory_stats.h" | |
20 #include "gpu/ipc/service/gpu_memory_buffer_factory.h" | |
21 #include "ipc/ipc_channel_handle.h" | |
22 #include "ipc/ipc_sync_message_filter.h" | |
23 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" | |
24 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h" | |
25 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h" | |
26 #include "media/gpu/ipc/service/media_service.h" | |
27 #include "ui/gl/gl_implementation.h" | |
28 #include "ui/gl/gl_switches.h" | |
29 #include "ui/gl/gpu_switching_manager.h" | |
30 #include "ui/gl/init/gl_factory.h" | |
31 #include "url/gurl.h" | |
32 | |
33 #if defined(USE_OZONE) | |
34 #include "ui/ozone/public/ozone_platform.h" | |
35 #endif | |
36 | |
37 namespace mus { | |
38 namespace { | |
39 | |
40 const int kLocalGpuChannelClientId = 1; | |
41 const uint64_t kLocalGpuChannelClientTracingId = 1; | |
42 | |
43 void EstablishGpuChannelDone( | |
44 int client_id, | |
45 const IPC::ChannelHandle* channel_handle, | |
46 const GpuServiceMus::EstablishGpuChannelCallback& callback) { | |
47 callback.Run(channel_handle ? client_id : -1, *channel_handle); | |
48 } | |
49 } | |
50 | |
51 GpuServiceMus::GpuServiceMus() | |
52 : next_client_id_(kLocalGpuChannelClientId), | |
53 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
54 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, | |
55 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
56 gpu_thread_("GpuThread"), | |
57 io_thread_("GpuIOThread") { | |
58 Initialize(); | |
59 } | |
60 | |
61 GpuServiceMus::~GpuServiceMus() { | |
62 // Signal this event before destroying the child process. That way all | |
63 // background threads can cleanup. | |
64 // For example, in the renderer the RenderThread instances will be able to | |
65 // notice shutdown before the render process begins waiting for them to exit. | |
66 shutdown_event_.Signal(); | |
67 io_thread_.Stop(); | |
68 } | |
69 | |
70 void GpuServiceMus::EstablishGpuChannel( | |
71 uint64_t client_tracing_id, | |
72 bool preempts, | |
73 bool allow_view_command_buffers, | |
74 bool allow_real_time_streams, | |
75 const EstablishGpuChannelCallback& callback) { | |
76 DCHECK(CalledOnValidThread()); | |
77 | |
78 if (!gpu_channel_manager_) { | |
79 callback.Run(-1, IPC::ChannelHandle()); | |
80 return; | |
81 } | |
82 | |
83 const int client_id = ++next_client_id_; | |
84 IPC::ChannelHandle* channel_handle = new IPC::ChannelHandle; | |
85 gpu_thread_.task_runner()->PostTaskAndReply( | |
86 FROM_HERE, | |
87 base::Bind(&GpuServiceMus::EstablishGpuChannelOnGpuThread, | |
88 base::Unretained(this), client_id, client_tracing_id, preempts, | |
89 allow_view_command_buffers, allow_real_time_streams, | |
90 base::Unretained(channel_handle)), | |
91 base::Bind(&EstablishGpuChannelDone, client_id, | |
92 base::Owned(channel_handle), callback)); | |
93 } | |
94 | |
95 gfx::GpuMemoryBufferHandle GpuServiceMus::CreateGpuMemoryBuffer( | |
96 gfx::GpuMemoryBufferId id, | |
97 const gfx::Size& size, | |
98 gfx::BufferFormat format, | |
99 gfx::BufferUsage usage, | |
100 int client_id, | |
101 gpu::SurfaceHandle surface_handle) { | |
102 DCHECK(CalledOnValidThread()); | |
103 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer( | |
104 id, size, format, usage, client_id, surface_handle); | |
105 } | |
106 | |
107 void GpuServiceMus::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
108 int client_id, | |
109 const gpu::SyncToken& sync_token) { | |
110 DCHECK(CalledOnValidThread()); | |
111 | |
112 if (gpu_channel_manager_) | |
113 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); | |
114 } | |
115 | |
116 void GpuServiceMus::DidCreateOffscreenContext(const GURL& active_url) { | |
117 NOTIMPLEMENTED(); | |
118 } | |
119 | |
120 void GpuServiceMus::DidDestroyChannel(int client_id) { | |
121 media_service_->RemoveChannel(client_id); | |
122 NOTIMPLEMENTED(); | |
123 } | |
124 | |
125 void GpuServiceMus::DidDestroyOffscreenContext(const GURL& active_url) { | |
126 NOTIMPLEMENTED(); | |
127 } | |
128 | |
129 void GpuServiceMus::DidLoseContext(bool offscreen, | |
130 gpu::error::ContextLostReason reason, | |
131 const GURL& active_url) { | |
132 NOTIMPLEMENTED(); | |
133 } | |
134 | |
135 void GpuServiceMus::GpuMemoryUmaStats(const gpu::GPUMemoryUmaStats& params) { | |
136 NOTIMPLEMENTED(); | |
137 } | |
138 | |
139 void GpuServiceMus::StoreShaderToDisk(int client_id, | |
140 const std::string& key, | |
141 const std::string& shader) { | |
142 NOTIMPLEMENTED(); | |
143 } | |
144 | |
145 #if defined(OS_WIN) | |
146 void GpuServiceMus::SendAcceleratedSurfaceCreatedChildWindow( | |
147 gpu::SurfaceHandle parent_window, | |
148 gpu::SurfaceHandle child_window) { | |
149 NOTIMPLEMENTED(); | |
150 } | |
151 #endif | |
152 | |
153 void GpuServiceMus::SetActiveURL(const GURL& url) { | |
154 NOTIMPLEMENTED(); | |
155 } | |
156 | |
157 void GpuServiceMus::Initialize() { | |
158 DCHECK(CalledOnValidThread()); | |
159 base::Thread::Options thread_options(base::MessageLoop::TYPE_DEFAULT, 0); | |
160 thread_options.priority = base::ThreadPriority::NORMAL; | |
161 CHECK(gpu_thread_.StartWithOptions(thread_options)); | |
162 | |
163 thread_options = base::Thread::Options(base::MessageLoop::TYPE_IO, 0); | |
164 thread_options.priority = base::ThreadPriority::NORMAL; | |
165 #if defined(OS_ANDROID) | |
166 // TODO(reveman): Remove this in favor of setting it explicitly for each type | |
167 // of process. | |
168 thread_options.priority = base::ThreadPriority::DISPLAY; | |
169 #endif | |
170 CHECK(io_thread_.StartWithOptions(thread_options)); | |
171 | |
172 IPC::ChannelHandle channel_handle; | |
173 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL, | |
174 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
175 gpu_thread_.task_runner()->PostTask( | |
176 FROM_HERE, base::Bind(&GpuServiceMus::InitializeOnGpuThread, | |
177 base::Unretained(this), &channel_handle, &event)); | |
178 event.Wait(); | |
179 | |
180 gpu_memory_buffer_manager_local_.reset( | |
181 new MusGpuMemoryBufferManager(this, kLocalGpuChannelClientId)); | |
182 gpu_channel_local_ = gpu::GpuChannelHost::Create( | |
183 this, kLocalGpuChannelClientId, gpu_info_, channel_handle, | |
184 &shutdown_event_, gpu_memory_buffer_manager_local_.get()); | |
185 } | |
186 | |
187 void GpuServiceMus::InitializeOnGpuThread(IPC::ChannelHandle* channel_handle, | |
188 base::WaitableEvent* event) { | |
189 gpu_info_.video_decode_accelerator_capabilities = | |
190 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_); | |
191 gpu_info_.video_encode_accelerator_supported_profiles = | |
192 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_); | |
193 gpu_info_.jpeg_decode_accelerator_supported = | |
194 media::GpuJpegDecodeAccelerator::IsSupported(); | |
195 | |
196 #if defined(USE_OZONE) | |
197 ui::OzonePlatform::InitializeForGPU(); | |
198 #endif | |
199 | |
200 if (gpu::GetNativeGpuMemoryBufferType() != gfx::EMPTY_BUFFER) { | |
201 gpu_memory_buffer_factory_ = | |
202 gpu::GpuMemoryBufferFactory::CreateNativeType(); | |
203 } | |
204 | |
205 if (!gl::init::InitializeGLOneOff()) | |
206 VLOG(1) << "gl::init::InitializeGLOneOff failed"; | |
207 | |
208 DCHECK(!owned_sync_point_manager_); | |
209 const bool allow_threaded_wait = false; | |
210 owned_sync_point_manager_.reset( | |
211 new gpu::SyncPointManager(allow_threaded_wait)); | |
212 | |
213 // Defer creation of the render thread. This is to prevent it from handling | |
214 // IPC messages before the sandbox has been enabled and all other necessary | |
215 // initialization has succeeded. | |
216 // TODO(penghuang): implement a watchdog. | |
217 gpu::GpuWatchdog* watchdog = nullptr; | |
218 gpu_channel_manager_.reset(new gpu::GpuChannelManager( | |
219 gpu_preferences_, this, watchdog, | |
220 base::ThreadTaskRunnerHandle::Get().get(), io_thread_.task_runner().get(), | |
221 &shutdown_event_, owned_sync_point_manager_.get(), | |
222 gpu_memory_buffer_factory_.get())); | |
223 | |
224 media_service_.reset(new media::MediaService(gpu_channel_manager_.get())); | |
225 | |
226 const bool preempts = true; | |
227 const bool allow_view_command_buffers = true; | |
228 const bool allow_real_time_streams = true; | |
229 EstablishGpuChannelOnGpuThread( | |
230 kLocalGpuChannelClientId, kLocalGpuChannelClientTracingId, preempts, | |
231 allow_view_command_buffers, allow_real_time_streams, channel_handle); | |
232 event->Signal(); | |
233 } | |
234 | |
235 void GpuServiceMus::EstablishGpuChannelOnGpuThread( | |
236 int client_id, | |
237 uint64_t client_tracing_id, | |
238 bool preempts, | |
239 bool allow_view_command_buffers, | |
240 bool allow_real_time_streams, | |
241 IPC::ChannelHandle* channel_handle) { | |
242 if (gpu_channel_manager_) { | |
243 *channel_handle = gpu_channel_manager_->EstablishChannel( | |
244 client_id, client_tracing_id, preempts, allow_view_command_buffers, | |
245 allow_real_time_streams); | |
246 media_service_->AddChannel(client_id); | |
247 } | |
248 } | |
249 | |
250 bool GpuServiceMus::IsMainThread() { | |
251 return main_task_runner_->BelongsToCurrentThread(); | |
252 } | |
253 | |
254 scoped_refptr<base::SingleThreadTaskRunner> | |
255 GpuServiceMus::GetIOThreadTaskRunner() { | |
256 return io_thread_.task_runner(); | |
257 } | |
258 | |
259 std::unique_ptr<base::SharedMemory> GpuServiceMus::AllocateSharedMemory( | |
260 size_t size) { | |
261 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
262 if (!shm->CreateAnonymous(size)) | |
263 return std::unique_ptr<base::SharedMemory>(); | |
264 return shm; | |
265 } | |
266 | |
267 // static | |
268 GpuServiceMus* GpuServiceMus::GetInstance() { | |
269 return base::Singleton<GpuServiceMus, | |
270 base::LeakySingletonTraits<GpuServiceMus>>::get(); | |
271 } | |
272 | |
273 } // namespace mus | |
OLD | NEW |