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/gpu/gpu_service_internal.h" | 5 #include "services/ui/gpu/gpu_service_internal.h" |
6 | 6 |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "gpu/command_buffer/service/gpu_switches.h" | 10 #include "gpu/command_buffer/service/gpu_switches.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" | 21 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" |
22 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h" | 22 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h" |
23 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h" | 23 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h" |
24 #include "media/gpu/ipc/service/media_service.h" | 24 #include "media/gpu/ipc/service/media_service.h" |
25 #include "ui/gl/gl_implementation.h" | 25 #include "ui/gl/gl_implementation.h" |
26 #include "ui/gl/gl_switches.h" | 26 #include "ui/gl/gl_switches.h" |
27 #include "ui/gl/gpu_switching_manager.h" | 27 #include "ui/gl/gpu_switching_manager.h" |
28 #include "ui/gl/init/gl_factory.h" | 28 #include "ui/gl/init/gl_factory.h" |
29 #include "url/gurl.h" | 29 #include "url/gurl.h" |
30 | 30 |
31 namespace { | |
32 | |
33 #if defined(OS_WIN) | |
34 std::unique_ptr<base::MessagePump> CreateMessagePumpWin() { | |
35 base::MessagePumpForGpu::InitFactory(); | |
36 return base::MessageLoop::CreateMessagePumpForType( | |
37 base::MessageLoop::TYPE_UI); | |
38 } | |
39 #endif // defined(OS_WIN) | |
40 | |
41 #if defined(USE_X11) | |
42 std::unique_ptr<base::MessagePump> CreateMessagePumpX11() { | |
43 // TODO(sad): This should create a TYPE_UI message pump, and create a | |
44 // PlatformEventSource when gpu process split happens. | |
45 return base::MessageLoop::CreateMessagePumpForType( | |
46 base::MessageLoop::TYPE_DEFAULT); | |
47 } | |
48 #endif // defined(USE_X11) | |
49 | |
50 #if defined(OS_MACOSX) | |
51 std::unique_ptr<base::MessagePump> CreateMessagePumpMac() { | |
52 return base::MakeUnique<base::MessagePumpCFRunLoop>(); | |
53 } | |
54 #endif // defined(OS_MACOSX) | |
55 | |
56 } // namespace | |
57 | |
58 namespace ui { | 31 namespace ui { |
59 | 32 |
60 GpuServiceInternal::GpuServiceInternal( | 33 GpuServiceInternal::GpuServiceInternal( |
61 const gpu::GPUInfo& gpu_info, | 34 const gpu::GPUInfo& gpu_info, |
62 gpu::GpuWatchdogThread* watchdog_thread, | 35 gpu::GpuWatchdogThread* watchdog_thread, |
63 gpu::GpuMemoryBufferFactory* gpu_memory_buffer_factory) | 36 gpu::GpuMemoryBufferFactory* gpu_memory_buffer_factory, |
64 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 37 scoped_refptr<base::SingleThreadTaskRunner> io_runner) |
| 38 : io_runner_(std::move(io_runner)), |
65 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, | 39 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
66 base::WaitableEvent::InitialState::NOT_SIGNALED), | 40 base::WaitableEvent::InitialState::NOT_SIGNALED), |
67 gpu_thread_("GpuThread"), | |
68 io_thread_("GpuIOThread"), | |
69 watchdog_thread_(watchdog_thread), | 41 watchdog_thread_(watchdog_thread), |
70 gpu_memory_buffer_factory_(gpu_memory_buffer_factory), | 42 gpu_memory_buffer_factory_(gpu_memory_buffer_factory), |
71 gpu_info_(gpu_info), | 43 gpu_info_(gpu_info), |
72 binding_(this) { | 44 binding_(this) {} |
73 base::Thread::Options thread_options; | |
74 | |
75 #if defined(OS_WIN) | |
76 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpWin); | |
77 #elif defined(USE_X11) | |
78 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpX11); | |
79 #elif defined(OS_LINUX) | |
80 thread_options.message_loop_type = base::MessageLoop::TYPE_DEFAULT; | |
81 #elif defined(OS_MACOSX) | |
82 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpMac); | |
83 #else | |
84 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | |
85 #endif | |
86 | |
87 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
88 thread_options.priority = base::ThreadPriority::DISPLAY; | |
89 #endif | |
90 CHECK(gpu_thread_.StartWithOptions(thread_options)); | |
91 | |
92 // TODO(sad): We do not need the IO thread once gpu has a separate process. It | |
93 // should be possible to use |main_task_runner_| for doing IO tasks. | |
94 thread_options = base::Thread::Options(base::MessageLoop::TYPE_IO, 0); | |
95 thread_options.priority = base::ThreadPriority::NORMAL; | |
96 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
97 // TODO(reveman): Remove this in favor of setting it explicitly for each type | |
98 // of process. | |
99 thread_options.priority = base::ThreadPriority::DISPLAY; | |
100 #endif | |
101 CHECK(io_thread_.StartWithOptions(thread_options)); | |
102 } | |
103 | 45 |
104 GpuServiceInternal::~GpuServiceInternal() { | 46 GpuServiceInternal::~GpuServiceInternal() { |
105 // Tear down the binding in the gpu thread. | 47 binding_.Close(); |
106 gpu_thread_.task_runner()->PostTask( | 48 media_service_.reset(); |
107 FROM_HERE, base::Bind(&GpuServiceInternal::TearDownGpuThread, | 49 gpu_channel_manager_.reset(); |
108 base::Unretained(this))); | 50 owned_sync_point_manager_.reset(); |
109 gpu_thread_.Stop(); | |
110 | 51 |
111 // Signal this event before destroying the child process. That way all | 52 // Signal this event before destroying the child process. That way all |
112 // background threads can cleanup. | 53 // background threads can cleanup. |
113 // For example, in the renderer the RenderThread instances will be able to | 54 // For example, in the renderer the RenderThread instances will be able to |
114 // notice shutdown before the render process begins waiting for them to exit. | 55 // notice shutdown before the render process begins waiting for them to exit. |
115 shutdown_event_.Signal(); | 56 shutdown_event_.Signal(); |
116 io_thread_.Stop(); | |
117 } | 57 } |
118 | 58 |
119 void GpuServiceInternal::Add(mojom::GpuServiceInternalRequest request) { | 59 void GpuServiceInternal::Add(mojom::GpuServiceInternalRequest request) { |
120 // Unretained() is OK here since the thread/task runner is owned by |this|. | |
121 gpu_thread_.task_runner()->PostTask( | |
122 FROM_HERE, | |
123 base::Bind(&GpuServiceInternal::BindOnGpuThread, base::Unretained(this), | |
124 base::Passed(std::move(request)))); | |
125 } | |
126 | |
127 void GpuServiceInternal::BindOnGpuThread( | |
128 mojom::GpuServiceInternalRequest request) { | |
129 binding_.Close(); | 60 binding_.Close(); |
130 binding_.Bind(std::move(request)); | 61 binding_.Bind(std::move(request)); |
131 } | 62 } |
132 | 63 |
133 void GpuServiceInternal::TearDownGpuThread() { | |
134 binding_.Close(); | |
135 media_service_.reset(); | |
136 gpu_channel_manager_.reset(); | |
137 owned_sync_point_manager_.reset(); | |
138 } | |
139 | |
140 gfx::GpuMemoryBufferHandle GpuServiceInternal::CreateGpuMemoryBuffer( | 64 gfx::GpuMemoryBufferHandle GpuServiceInternal::CreateGpuMemoryBuffer( |
141 gfx::GpuMemoryBufferId id, | 65 gfx::GpuMemoryBufferId id, |
142 const gfx::Size& size, | 66 const gfx::Size& size, |
143 gfx::BufferFormat format, | 67 gfx::BufferFormat format, |
144 gfx::BufferUsage usage, | 68 gfx::BufferUsage usage, |
145 int client_id, | 69 int client_id, |
146 gpu::SurfaceHandle surface_handle) { | 70 gpu::SurfaceHandle surface_handle) { |
147 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread()); | 71 DCHECK(CalledOnValidThread()); |
148 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer( | 72 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer( |
149 id, size, format, usage, client_id, surface_handle); | 73 id, size, format, usage, client_id, surface_handle); |
150 } | 74 } |
151 | 75 |
152 void GpuServiceInternal::DestroyGpuMemoryBuffer( | 76 void GpuServiceInternal::DestroyGpuMemoryBuffer( |
153 gfx::GpuMemoryBufferId id, | 77 gfx::GpuMemoryBufferId id, |
154 int client_id, | 78 int client_id, |
155 const gpu::SyncToken& sync_token) { | 79 const gpu::SyncToken& sync_token) { |
156 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread()); | 80 DCHECK(CalledOnValidThread()); |
157 if (gpu_channel_manager_) | 81 if (gpu_channel_manager_) |
158 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); | 82 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); |
159 } | 83 } |
160 | 84 |
161 void GpuServiceInternal::DidCreateOffscreenContext(const GURL& active_url) { | 85 void GpuServiceInternal::DidCreateOffscreenContext(const GURL& active_url) { |
162 NOTIMPLEMENTED(); | 86 NOTIMPLEMENTED(); |
163 } | 87 } |
164 | 88 |
165 void GpuServiceInternal::DidDestroyChannel(int client_id) { | 89 void GpuServiceInternal::DidDestroyChannel(int client_id) { |
166 media_service_->RemoveChannel(client_id); | 90 media_service_->RemoveChannel(client_id); |
(...skipping 22 matching lines...) Expand all Loading... |
189 gpu::SurfaceHandle child_window) { | 113 gpu::SurfaceHandle child_window) { |
190 ::SetParent(child_window, parent_window); | 114 ::SetParent(child_window, parent_window); |
191 } | 115 } |
192 #endif | 116 #endif |
193 | 117 |
194 void GpuServiceInternal::SetActiveURL(const GURL& url) { | 118 void GpuServiceInternal::SetActiveURL(const GURL& url) { |
195 // TODO(penghuang): implement this function. | 119 // TODO(penghuang): implement this function. |
196 } | 120 } |
197 | 121 |
198 void GpuServiceInternal::Initialize(const InitializeCallback& callback) { | 122 void GpuServiceInternal::Initialize(const InitializeCallback& callback) { |
199 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread()); | 123 DCHECK(CalledOnValidThread()); |
200 gpu_info_.video_decode_accelerator_capabilities = | 124 gpu_info_.video_decode_accelerator_capabilities = |
201 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_); | 125 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_); |
202 gpu_info_.video_encode_accelerator_supported_profiles = | 126 gpu_info_.video_encode_accelerator_supported_profiles = |
203 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_); | 127 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_); |
204 gpu_info_.jpeg_decode_accelerator_supported = | 128 gpu_info_.jpeg_decode_accelerator_supported = |
205 media::GpuJpegDecodeAccelerator::IsSupported(); | 129 media::GpuJpegDecodeAccelerator::IsSupported(); |
206 | 130 |
207 DCHECK(!owned_sync_point_manager_); | 131 DCHECK(!owned_sync_point_manager_); |
208 const bool allow_threaded_wait = false; | 132 const bool allow_threaded_wait = false; |
209 owned_sync_point_manager_.reset( | 133 owned_sync_point_manager_.reset( |
210 new gpu::SyncPointManager(allow_threaded_wait)); | 134 new gpu::SyncPointManager(allow_threaded_wait)); |
211 | 135 |
212 // Defer creation of the render thread. This is to prevent it from handling | 136 // Defer creation of the render thread. This is to prevent it from handling |
213 // IPC messages before the sandbox has been enabled and all other necessary | 137 // IPC messages before the sandbox has been enabled and all other necessary |
214 // initialization has succeeded. | 138 // initialization has succeeded. |
215 gpu_channel_manager_.reset(new gpu::GpuChannelManager( | 139 gpu_channel_manager_.reset(new gpu::GpuChannelManager( |
216 gpu_preferences_, this, watchdog_thread_, | 140 gpu_preferences_, this, watchdog_thread_, |
217 base::ThreadTaskRunnerHandle::Get().get(), io_thread_.task_runner().get(), | 141 base::ThreadTaskRunnerHandle::Get().get(), io_runner_.get(), |
218 &shutdown_event_, owned_sync_point_manager_.get(), | 142 &shutdown_event_, owned_sync_point_manager_.get(), |
219 gpu_memory_buffer_factory_)); | 143 gpu_memory_buffer_factory_)); |
220 | 144 |
221 media_service_.reset(new media::MediaService(gpu_channel_manager_.get())); | 145 media_service_.reset(new media::MediaService(gpu_channel_manager_.get())); |
222 callback.Run(gpu_info_); | 146 callback.Run(gpu_info_); |
223 } | 147 } |
224 | 148 |
225 void GpuServiceInternal::EstablishGpuChannel( | 149 void GpuServiceInternal::EstablishGpuChannel( |
226 int32_t client_id, | 150 int32_t client_id, |
227 uint64_t client_tracing_id, | 151 uint64_t client_tracing_id, |
228 bool is_gpu_host, | 152 bool is_gpu_host, |
229 const EstablishGpuChannelCallback& callback) { | 153 const EstablishGpuChannelCallback& callback) { |
230 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread()); | 154 DCHECK(CalledOnValidThread()); |
231 | 155 |
232 if (!gpu_channel_manager_) { | 156 if (!gpu_channel_manager_) { |
233 callback.Run(mojo::ScopedMessagePipeHandle()); | 157 callback.Run(mojo::ScopedMessagePipeHandle()); |
234 return; | 158 return; |
235 } | 159 } |
236 | 160 |
237 const bool preempts = is_gpu_host; | 161 const bool preempts = is_gpu_host; |
238 const bool allow_view_command_buffers = is_gpu_host; | 162 const bool allow_view_command_buffers = is_gpu_host; |
239 const bool allow_real_time_streams = is_gpu_host; | 163 const bool allow_real_time_streams = is_gpu_host; |
240 mojo::ScopedMessagePipeHandle channel_handle; | 164 mojo::ScopedMessagePipeHandle channel_handle; |
241 IPC::ChannelHandle handle = gpu_channel_manager_->EstablishChannel( | 165 IPC::ChannelHandle handle = gpu_channel_manager_->EstablishChannel( |
242 client_id, client_tracing_id, preempts, allow_view_command_buffers, | 166 client_id, client_tracing_id, preempts, allow_view_command_buffers, |
243 allow_real_time_streams); | 167 allow_real_time_streams); |
244 channel_handle.reset(handle.mojo_handle); | 168 channel_handle.reset(handle.mojo_handle); |
245 media_service_->AddChannel(client_id); | 169 media_service_->AddChannel(client_id); |
246 callback.Run(std::move(channel_handle)); | 170 callback.Run(std::move(channel_handle)); |
247 } | 171 } |
248 | 172 |
249 } // namespace ui | 173 } // namespace ui |
OLD | NEW |