Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: services/ui/gpu/gpu_service_internal.cc

Issue 2329043002: services/ui: Avoid thread hopping in the gpu process. (Closed)
Patch Set: fix debug Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/ui/gpu/gpu_service_internal.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/synchronization/waitable_event.h"
9 #include "base/threading/thread_task_runner_handle.h" 8 #include "base/threading/thread_task_runner_handle.h"
10 #include "build/build_config.h" 9 #include "build/build_config.h"
11 #include "gpu/command_buffer/service/gpu_switches.h" 10 #include "gpu/command_buffer/service/gpu_switches.h"
12 #include "gpu/command_buffer/service/sync_point_manager.h" 11 #include "gpu/command_buffer/service/sync_point_manager.h"
13 #include "gpu/config/gpu_info_collector.h" 12 #include "gpu/config/gpu_info_collector.h"
14 #include "gpu/config/gpu_switches.h" 13 #include "gpu/config/gpu_switches.h"
15 #include "gpu/config/gpu_util.h" 14 #include "gpu/config/gpu_util.h"
16 #include "gpu/ipc/common/gpu_memory_buffer_support.h" 15 #include "gpu/ipc/common/gpu_memory_buffer_support.h"
17 #include "gpu/ipc/common/memory_stats.h" 16 #include "gpu/ipc/common/memory_stats.h"
18 #include "gpu/ipc/service/gpu_memory_buffer_factory.h" 17 #include "gpu/ipc/service/gpu_memory_buffer_factory.h"
19 #include "gpu/ipc/service/gpu_watchdog_thread.h" 18 #include "gpu/ipc/service/gpu_watchdog_thread.h"
20 #include "ipc/ipc_channel_handle.h" 19 #include "ipc/ipc_channel_handle.h"
21 #include "ipc/ipc_sync_message_filter.h" 20 #include "ipc/ipc_sync_message_filter.h"
22 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" 21 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h"
23 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h" 22 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h"
24 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h" 23 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h"
25 #include "media/gpu/ipc/service/media_service.h" 24 #include "media/gpu/ipc/service/media_service.h"
26 #include "ui/gl/gl_implementation.h" 25 #include "ui/gl/gl_implementation.h"
27 #include "ui/gl/gl_switches.h" 26 #include "ui/gl/gl_switches.h"
28 #include "ui/gl/gpu_switching_manager.h" 27 #include "ui/gl/gpu_switching_manager.h"
29 #include "ui/gl/init/gl_factory.h" 28 #include "ui/gl/init/gl_factory.h"
30 #include "url/gurl.h" 29 #include "url/gurl.h"
31 30
32 #if defined(USE_OZONE) 31 #if defined(USE_OZONE)
33 #include "ui/ozone/public/ozone_platform.h" 32 #include "ui/ozone/public/ozone_platform.h"
34 #endif 33 #endif
35 34
36 namespace ui { 35 namespace ui {
37 namespace {
38
39 void EstablishGpuChannelDone(
40 mojo::ScopedMessagePipeHandle* channel_handle,
41 const GpuServiceInternal::EstablishGpuChannelCallback& callback) {
42 callback.Run(std::move(*channel_handle));
43 }
44
45 } // namespace
46 36
47 GpuServiceInternal::GpuServiceInternal( 37 GpuServiceInternal::GpuServiceInternal(
48 const gpu::GPUInfo& gpu_info, 38 const gpu::GPUInfo& gpu_info,
49 gpu::GpuWatchdogThread* watchdog_thread, 39 gpu::GpuWatchdogThread* watchdog_thread,
50 gpu::GpuMemoryBufferFactory* gpu_memory_buffer_factory) 40 gpu::GpuMemoryBufferFactory* gpu_memory_buffer_factory)
51 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), 41 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
52 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, 42 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
53 base::WaitableEvent::InitialState::NOT_SIGNALED), 43 base::WaitableEvent::InitialState::NOT_SIGNALED),
54 gpu_thread_("GpuThread"), 44 gpu_thread_("GpuThread"),
55 io_thread_("GpuIOThread"), 45 io_thread_("GpuIOThread"),
56 watchdog_thread_(watchdog_thread), 46 watchdog_thread_(watchdog_thread),
57 gpu_memory_buffer_factory_(gpu_memory_buffer_factory), 47 gpu_memory_buffer_factory_(gpu_memory_buffer_factory),
58 gpu_info_(gpu_info), 48 gpu_info_(gpu_info),
59 binding_(this) {} 49 binding_(this) {
50 base::Thread::Options thread_options(base::MessageLoop::TYPE_DEFAULT, 0);
51 thread_options.priority = base::ThreadPriority::NORMAL;
52 CHECK(gpu_thread_.StartWithOptions(thread_options));
53
54 // TODO(sad): We do not need the IO thread once gpu has a separate process. It
55 // should be possible to use |main_task_runner_| for doing IO tasks.
56 thread_options = base::Thread::Options(base::MessageLoop::TYPE_IO, 0);
57 thread_options.priority = base::ThreadPriority::NORMAL;
58 #if defined(OS_ANDROID)
59 // TODO(reveman): Remove this in favor of setting it explicitly for each type
60 // of process.
61 thread_options.priority = base::ThreadPriority::DISPLAY;
62 #endif
63 CHECK(io_thread_.StartWithOptions(thread_options));
64 }
60 65
61 GpuServiceInternal::~GpuServiceInternal() { 66 GpuServiceInternal::~GpuServiceInternal() {
67 // Tear down the binding in the gpu thread.
68 gpu_thread_.task_runner()->PostTask(FROM_HERE,
69 base::Bind(&mojo::Binding<mojom::GpuServiceInternal>::Close,
70 base::Unretained(&binding_)));
71 gpu_thread_.Stop();
72
62 // Signal this event before destroying the child process. That way all 73 // Signal this event before destroying the child process. That way all
63 // background threads can cleanup. 74 // background threads can cleanup.
64 // For example, in the renderer the RenderThread instances will be able to 75 // 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. 76 // notice shutdown before the render process begins waiting for them to exit.
66 shutdown_event_.Signal(); 77 shutdown_event_.Signal();
67 io_thread_.Stop(); 78 io_thread_.Stop();
68 } 79 }
69 80
70 void GpuServiceInternal::Add(mojom::GpuServiceInternalRequest request) { 81 void GpuServiceInternal::Add(mojom::GpuServiceInternalRequest request) {
82 // Unretained() is OK here since the thread/task runner is owned by |this|.
83 gpu_thread_.task_runner()->PostTask(
84 FROM_HERE,
85 base::Bind(&GpuServiceInternal::BindOnGpuThread, base::Unretained(this),
86 base::Passed(std::move(request))));
87 }
88
89 void GpuServiceInternal::BindOnGpuThread(
90 mojom::GpuServiceInternalRequest request) {
71 binding_.Close(); 91 binding_.Close();
72 binding_.Bind(std::move(request)); 92 binding_.Bind(std::move(request));
73 } 93 }
74 94
75 void GpuServiceInternal::EstablishGpuChannelInternal(
76 int32_t client_id,
77 uint64_t client_tracing_id,
78 bool preempts,
79 bool allow_view_command_buffers,
80 bool allow_real_time_streams,
81 const EstablishGpuChannelCallback& callback) {
82 DCHECK(CalledOnValidThread());
83
84 if (!gpu_channel_manager_) {
85 callback.Run(mojo::ScopedMessagePipeHandle());
86 return;
87 }
88
89 auto* channel_handle = new mojo::ScopedMessagePipeHandle;
90 gpu_thread_.task_runner()->PostTaskAndReply(
91 FROM_HERE,
92 base::Bind(&GpuServiceInternal::EstablishGpuChannelOnGpuThread,
93 base::Unretained(this), client_id, client_tracing_id, preempts,
94 allow_view_command_buffers, allow_real_time_streams,
95 base::Unretained(channel_handle)),
96 base::Bind(&EstablishGpuChannelDone, base::Owned(channel_handle),
97 callback));
98 }
99
100 gfx::GpuMemoryBufferHandle GpuServiceInternal::CreateGpuMemoryBuffer( 95 gfx::GpuMemoryBufferHandle GpuServiceInternal::CreateGpuMemoryBuffer(
101 gfx::GpuMemoryBufferId id, 96 gfx::GpuMemoryBufferId id,
102 const gfx::Size& size, 97 const gfx::Size& size,
103 gfx::BufferFormat format, 98 gfx::BufferFormat format,
104 gfx::BufferUsage usage, 99 gfx::BufferUsage usage,
105 int client_id, 100 int client_id,
106 gpu::SurfaceHandle surface_handle) { 101 gpu::SurfaceHandle surface_handle) {
107 DCHECK(CalledOnValidThread()); 102 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread());
108 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer( 103 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer(
109 id, size, format, usage, client_id, surface_handle); 104 id, size, format, usage, client_id, surface_handle);
110 } 105 }
111 106
112 void GpuServiceInternal::DestroyGpuMemoryBuffer( 107 void GpuServiceInternal::DestroyGpuMemoryBuffer(
113 gfx::GpuMemoryBufferId id, 108 gfx::GpuMemoryBufferId id,
114 int client_id, 109 int client_id,
115 const gpu::SyncToken& sync_token) { 110 const gpu::SyncToken& sync_token) {
116 DCHECK(CalledOnValidThread()); 111 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread());
117
118 if (gpu_channel_manager_) 112 if (gpu_channel_manager_)
119 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); 113 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token);
120 } 114 }
121 115
122 void GpuServiceInternal::DidCreateOffscreenContext(const GURL& active_url) { 116 void GpuServiceInternal::DidCreateOffscreenContext(const GURL& active_url) {
123 NOTIMPLEMENTED(); 117 NOTIMPLEMENTED();
124 } 118 }
125 119
126 void GpuServiceInternal::DidDestroyChannel(int client_id) { 120 void GpuServiceInternal::DidDestroyChannel(int client_id) {
127 media_service_->RemoveChannel(client_id); 121 media_service_->RemoveChannel(client_id);
(...skipping 21 matching lines...) Expand all
149 gpu::SurfaceHandle parent_window, 143 gpu::SurfaceHandle parent_window,
150 gpu::SurfaceHandle child_window) { 144 gpu::SurfaceHandle child_window) {
151 ::SetParent(child_window, parent_window); 145 ::SetParent(child_window, parent_window);
152 } 146 }
153 #endif 147 #endif
154 148
155 void GpuServiceInternal::SetActiveURL(const GURL& url) { 149 void GpuServiceInternal::SetActiveURL(const GURL& url) {
156 // TODO(penghuang): implement this function. 150 // TODO(penghuang): implement this function.
157 } 151 }
158 152
159 void GpuServiceInternal::InitializeOnGpuThread(base::WaitableEvent* event) { 153 void GpuServiceInternal::Initialize(const InitializeCallback& callback) {
154 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread());
160 gpu_info_.video_decode_accelerator_capabilities = 155 gpu_info_.video_decode_accelerator_capabilities =
161 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_); 156 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_);
162 gpu_info_.video_encode_accelerator_supported_profiles = 157 gpu_info_.video_encode_accelerator_supported_profiles =
163 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_); 158 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_);
164 gpu_info_.jpeg_decode_accelerator_supported = 159 gpu_info_.jpeg_decode_accelerator_supported =
165 media::GpuJpegDecodeAccelerator::IsSupported(); 160 media::GpuJpegDecodeAccelerator::IsSupported();
166 161
167 DCHECK(!owned_sync_point_manager_); 162 DCHECK(!owned_sync_point_manager_);
168 const bool allow_threaded_wait = false; 163 const bool allow_threaded_wait = false;
169 owned_sync_point_manager_.reset( 164 owned_sync_point_manager_.reset(
170 new gpu::SyncPointManager(allow_threaded_wait)); 165 new gpu::SyncPointManager(allow_threaded_wait));
171 166
172 // Defer creation of the render thread. This is to prevent it from handling 167 // Defer creation of the render thread. This is to prevent it from handling
173 // IPC messages before the sandbox has been enabled and all other necessary 168 // IPC messages before the sandbox has been enabled and all other necessary
174 // initialization has succeeded. 169 // initialization has succeeded.
175 gpu_channel_manager_.reset(new gpu::GpuChannelManager( 170 gpu_channel_manager_.reset(new gpu::GpuChannelManager(
176 gpu_preferences_, this, watchdog_thread_, 171 gpu_preferences_, this, watchdog_thread_,
177 base::ThreadTaskRunnerHandle::Get().get(), io_thread_.task_runner().get(), 172 base::ThreadTaskRunnerHandle::Get().get(), io_thread_.task_runner().get(),
178 &shutdown_event_, owned_sync_point_manager_.get(), 173 &shutdown_event_, owned_sync_point_manager_.get(),
179 gpu_memory_buffer_factory_)); 174 gpu_memory_buffer_factory_));
180 175
181 media_service_.reset(new media::MediaService(gpu_channel_manager_.get())); 176 media_service_.reset(new media::MediaService(gpu_channel_manager_.get()));
182 event->Signal();
183 }
184
185 void GpuServiceInternal::EstablishGpuChannelOnGpuThread(
186 int client_id,
187 uint64_t client_tracing_id,
188 bool preempts,
189 bool allow_view_command_buffers,
190 bool allow_real_time_streams,
191 mojo::ScopedMessagePipeHandle* channel_handle) {
192 if (gpu_channel_manager_) {
193 auto handle = gpu_channel_manager_->EstablishChannel(
194 client_id, client_tracing_id, preempts, allow_view_command_buffers,
195 allow_real_time_streams);
196 channel_handle->reset(handle.mojo_handle);
197 media_service_->AddChannel(client_id);
198 }
199 }
200
201 void GpuServiceInternal::Initialize(const InitializeCallback& callback) {
202 DCHECK(CalledOnValidThread());
203 base::Thread::Options thread_options(base::MessageLoop::TYPE_DEFAULT, 0);
204 thread_options.priority = base::ThreadPriority::NORMAL;
205 CHECK(gpu_thread_.StartWithOptions(thread_options));
206
207 thread_options = base::Thread::Options(base::MessageLoop::TYPE_IO, 0);
208 thread_options.priority = base::ThreadPriority::NORMAL;
209 #if defined(OS_ANDROID)
210 // TODO(reveman): Remove this in favor of setting it explicitly for each type
211 // of process.
212 thread_options.priority = base::ThreadPriority::DISPLAY;
213 #endif
214 CHECK(io_thread_.StartWithOptions(thread_options));
215
216 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL,
217 base::WaitableEvent::InitialState::NOT_SIGNALED);
218 gpu_thread_.task_runner()->PostTask(
219 FROM_HERE, base::Bind(&GpuServiceInternal::InitializeOnGpuThread,
220 base::Unretained(this), &event));
221 event.Wait();
222
223 callback.Run(gpu_info_); 177 callback.Run(gpu_info_);
224 } 178 }
225 179
226 void GpuServiceInternal::EstablishGpuChannel( 180 void GpuServiceInternal::EstablishGpuChannel(
227 int32_t client_id, 181 int32_t client_id,
228 uint64_t client_tracing_id, 182 uint64_t client_tracing_id,
229 bool is_gpu_host, 183 bool is_gpu_host,
230 const EstablishGpuChannelCallback& callback) { 184 const EstablishGpuChannelCallback& callback) {
185 DCHECK(gpu_thread_.task_runner()->BelongsToCurrentThread());
186
187 if (!gpu_channel_manager_) {
188 callback.Run(mojo::ScopedMessagePipeHandle());
189 return;
190 }
191
231 const bool preempts = is_gpu_host; 192 const bool preempts = is_gpu_host;
232 const bool allow_view_command_buffers = is_gpu_host; 193 const bool allow_view_command_buffers = is_gpu_host;
233 const bool allow_real_time_streams = is_gpu_host; 194 const bool allow_real_time_streams = is_gpu_host;
234 EstablishGpuChannelInternal(client_id, client_tracing_id, preempts, 195 mojo::ScopedMessagePipeHandle channel_handle;
235 allow_view_command_buffers, 196 IPC::ChannelHandle handle = gpu_channel_manager_->EstablishChannel(
236 allow_real_time_streams, callback); 197 client_id, client_tracing_id, preempts, allow_view_command_buffers,
198 allow_real_time_streams);
199 channel_handle.reset(handle.mojo_handle);
200 media_service_->AddChannel(client_id);
201 callback.Run(std::move(channel_handle));
237 } 202 }
238 203
239 } // namespace ui 204 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/gpu/gpu_service_internal.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698