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

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

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