blob: 10228db874971c86531b213f5a15abddd514373c [file] [log] [blame]
[email protected]2e3d9e42012-02-14 03:23:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c0fc0942010-01-13 00:55:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi66a07722015-12-25 23:38:125#include <stddef.h>
[email protected]e09cee42010-11-09 01:50:086#include <stdlib.h>
7
mostynb6682b1c42016-04-19 10:17:308#include <memory>
dchengf26eed32016-01-13 10:58:149#include <utility>
10
Sebastien Marchandf8cbfab2019-01-25 16:02:3011#include "base/bind.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3212#include "base/feature_list.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1513#include "base/message_loop/message_pump_type.h"
asvitkine8d51e9d2016-09-02 23:55:4314#include "base/metrics/histogram_macros.h"
[email protected]2436a6b2012-04-13 21:08:5115#include "base/rand_util.h"
fdoraye716a902016-07-05 16:05:4916#include "base/run_loop.h"
[email protected]d0ea4782013-06-11 04:58:2417#include "base/strings/string_number_conversions.h"
18#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0319#include "base/system/sys_info.h"
Alex Clarke636e7052019-05-30 10:49:3720#include "base/task/single_thread_task_executor.h"
Gabriel Charette6f9509b2020-05-27 20:39:5721#include "base/task/thread_pool.h"
[email protected]ce072a72010-12-31 20:02:1622#include "base/threading/platform_thread.h"
stanisc61507092017-07-06 16:36:5823#include "base/timer/hi_res_timer_manager.h"
primiano50b7444c2015-01-28 04:17:0024#include "base/trace_event/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3725#include "build/build_config.h"
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:0626#include "components/viz/service/main/viz_main_impl.h"
[email protected]91a2aea2013-07-08 23:14:3927#include "content/common/content_constants_internal.h"
John Abd-El-Malek884291c2017-08-09 06:43:4828#include "content/common/content_switches_internal.h"
Khushala4e236f2018-06-01 03:00:4629#include "content/common/skia_utils.h"
[email protected]7a31f7c2011-03-21 23:22:0430#include "content/gpu/gpu_child_thread.h"
[email protected]623c0bd2011-03-12 01:00:4131#include "content/gpu/gpu_process.h"
[email protected]c9e2cbbb2012-05-12 21:17:2732#include "content/public/common/content_client.h"
33#include "content/public/common/content_switches.h"
34#include "content/public/common/main_function_params.h"
jbaumana19f1df2017-01-18 03:01:1735#include "content/public/common/result_codes.h"
Zhenyao Moc76e9032018-01-19 21:15:4636#include "content/public/gpu/content_gpu_client.h"
[email protected]40c19e722013-11-05 23:51:2437#include "gpu/command_buffer/service/gpu_switches.h"
sadruled395922016-09-07 17:32:5838#include "gpu/config/gpu_driver_bug_list.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3239#include "gpu/config/gpu_finch_features.h"
[email protected]d7b5cc72013-05-23 20:05:0040#include "gpu/config/gpu_info_collector.h"
Jonathan Backer16cc8fd2018-05-31 19:59:2241#include "gpu/config/gpu_preferences.h"
tfarina15525c42015-04-28 19:04:1642#include "gpu/config/gpu_switches.h"
[email protected]40c19e722013-11-05 23:51:2443#include "gpu/config/gpu_util.h"
fsamuelc2774222016-03-24 00:27:1244#include "gpu/ipc/common/gpu_memory_buffer_support.h"
markdittmerd88b8352016-04-08 15:28:4545#include "gpu/ipc/service/gpu_config.h"
sadrul454af3332016-09-09 18:14:3246#include "gpu/ipc/service/gpu_init.h"
sadrul2fb7e152016-08-30 05:21:4547#include "gpu/ipc/service/gpu_watchdog_thread.h"
Scott Violeta35f9a42018-03-22 22:00:4448#include "media/gpu/buildflags.h"
Dale Curtis12cfe022020-02-21 01:18:4249#include "services/tracing/public/cpp/trace_startup.h"
Tom Sepeza0950c62017-10-18 20:39:1450#include "third_party/angle/src/gpu_info_util/SystemInfo.h"
Maksim Sisov459eada2020-06-25 08:16:3151#include "ui/base/ui_base_features.h"
[email protected]c0939182014-05-24 00:09:1952#include "ui/events/platform/platform_event_source.h"
skyostilb354f882016-12-13 18:42:4553#include "ui/gfx/switches.h"
martina.kollarovaa34211d2015-06-25 11:49:0754#include "ui/gl/gl_context.h"
[email protected]db6101db2012-10-25 15:20:0855#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2756#include "ui/gl/gl_surface.h"
57#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2458#include "ui/gl/gpu_switching_manager.h"
kylechar5b9dec12016-05-16 15:40:5759#include "ui/gl/init/gl_factory.h"
[email protected]c0fc0942010-01-13 00:55:3760
avi66a07722015-12-25 23:38:1261#if defined(OS_WIN)
avi66a07722015-12-25 23:38:1262#include <windows.h>
fdoraye716a902016-07-05 16:05:4963#include <dwmapi.h>
avi66a07722015-12-25 23:38:1264#endif
65
primianob3fb6412015-10-14 16:03:5166#if defined(OS_ANDROID)
67#include "base/trace_event/memory_dump_manager.h"
primianoccb26c62016-06-01 21:50:0268#include "components/tracing/common/graphics_memory_dump_provider_android.h"
primianob3fb6412015-10-14 16:03:5169#endif
70
[email protected]d7de57872011-12-06 23:32:4371#if defined(OS_WIN)
Daniel Libby8f7e6262019-01-08 22:35:5572#include "base/trace_event/trace_event_etw_export_win.h"
[email protected]5f7e4512012-10-01 20:51:3773#include "base/win/scoped_com_initializer.h"
kylechar5b9dec12016-05-16 15:40:5774#include "base/win/windows_version.h"
Miguel Casasd697adf32018-02-26 19:06:3175#include "media/gpu/windows/dxva_video_decode_accelerator_win.h"
76#include "media/gpu/windows/media_foundation_video_encode_accelerator_win.h"
[email protected]181491782012-07-18 00:59:1577#include "sandbox/win/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3778#endif
79
[email protected]02ec37a2010-09-20 22:32:1680#if defined(USE_X11)
Tom Andersonff8d22e2020-01-30 03:19:1681#include "ui/base/x/x11_util.h" // nogncheck
82#include "ui/gfx/linux/gpu_memory_buffer_support_x11.h" // nogncheck
Tom Anderson1016c3b2020-05-19 03:13:0583#include "ui/gfx/x/x11.h" // nogncheck
Tom Andersonff8d22e2020-01-30 03:19:1684#include "ui/gfx/x/x11_switches.h" // nogncheck
Tom Anderson061b2812020-04-23 00:46:3685#include "ui/gfx/x/x11_types.h" // nogncheck
[email protected]02ec37a2010-09-20 22:32:1686#endif
87
[email protected]2436a6b2012-04-13 21:08:5188#if defined(OS_LINUX)
Tom Sepez085507ab2017-10-18 22:36:0089#include "content/gpu/gpu_sandbox_hook_linux.h"
[email protected]2436a6b2012-04-13 21:08:5190#include "content/public/common/sandbox_init.h"
Robert Sesek7d0b49b2020-07-08 18:31:2791#include "sandbox/policy/linux/sandbox_linux.h"
[email protected]2436a6b2012-04-13 21:08:5192#endif
93
Avi Drissman7c57be72020-07-29 20:09:4694#if defined(OS_MAC)
[email protected]826aab02014-05-14 02:58:5995#include "base/message_loop/message_pump_mac.h"
Kai Ninomiyae2c55582019-12-19 23:51:0396#include "components/metal_util/device_removal.h"
Christopher Cameron21c4abc2019-11-15 04:06:1597#include "components/metal_util/test_shader.h"
Robert Sesek3bd6e4b32019-11-26 16:02:2798#include "content/public/common/content_features.h"
99#include "media/gpu/mac/vt_video_decode_accelerator_mac.h"
Greg Kerr3480aa82018-02-01 00:53:03100#include "sandbox/mac/seatbelt.h"
Robert Sesek7d0b49b2020-07-08 18:31:27101#include "sandbox/policy/mac/sandbox_mac.h"
[email protected]826aab02014-05-14 02:58:59102#endif
103
Alexandre Courbotc13a5972017-07-23 03:48:48104#if BUILDFLAG(USE_VAAPI)
Miguel Casas42e955c2017-12-04 14:18:12105#include "media/gpu/vaapi/vaapi_wrapper.h"
hshi95837052015-05-12 15:39:51106#endif
107
[email protected]eb398192012-10-22 20:16:19108namespace content {
[email protected]ec4bda62013-06-14 15:51:03109
[email protected]6ec3a572012-08-17 02:09:51110namespace {
[email protected]ec4bda62013-06-14 15:51:03111
[email protected]663c4b32013-04-18 05:52:54112#if defined(OS_LINUX)
Zhenyao Moe9187a862017-10-20 04:26:33113bool StartSandboxLinux(gpu::GpuWatchdogThread*,
114 const gpu::GPUInfo*,
115 const gpu::GpuPreferences&);
[email protected]663c4b32013-04-18 05:52:54116#elif defined(OS_WIN)
117bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
118#endif
[email protected]ec4bda62013-06-14 15:51:03119
sadrul454af3332016-09-09 18:14:32120class ContentSandboxHelper : public gpu::GpuSandboxHelper {
121 public:
122 ContentSandboxHelper() {}
123 ~ContentSandboxHelper() override {}
124
125#if defined(OS_WIN)
126 void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) {
127 sandbox_info_ = info;
128 }
129#endif
130
sadrul454af3332016-09-09 18:14:32131 private:
132 // SandboxHelper:
133 void PreSandboxStartup() override {
134 // Warm up resources that don't need access to GPUInfo.
135 {
136 TRACE_EVENT0("gpu", "Warm up rand");
137 // Warm up the random subsystem, which needs to be done pre-sandbox on all
138 // platforms.
139 (void)base::RandUint64();
140 }
141
Alexandre Courbotc13a5972017-07-23 03:48:48142#if BUILDFLAG(USE_VAAPI)
sadrul454af3332016-09-09 18:14:32143 media::VaapiWrapper::PreSandboxInitialization();
144#endif
145#if defined(OS_WIN)
146 media::DXVAVideoDecodeAccelerator::PreSandboxInitialization();
147 media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization();
148#endif
Eric Karle35050e2017-09-07 01:44:35149
Avi Drissman7c57be72020-07-29 20:09:46150#if defined(OS_MAC)
Robert Sesek3bd6e4b32019-11-26 16:02:27151 if (base::FeatureList::IsEnabled(features::kMacV2GPUSandbox)) {
152 TRACE_EVENT0("gpu", "Initialize VideoToolbox");
153 media::InitializeVideoToolbox();
154 }
155#endif
156
Eric Karle35050e2017-09-07 01:44:35157 // On Linux, reading system memory doesn't work through the GPU sandbox.
158 // This value is cached, so access it here to populate the cache.
159 base::SysInfo::AmountOfPhysicalMemory();
sadrul454af3332016-09-09 18:14:32160 }
161
Satyajit Sahu82a76e02017-09-18 14:50:14162 bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33163 const gpu::GPUInfo* gpu_info,
164 const gpu::GpuPreferences& gpu_prefs) override {
sadrul454af3332016-09-09 18:14:32165#if defined(OS_LINUX)
Zhenyao Moe9187a862017-10-20 04:26:33166 return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs);
sadrul454af3332016-09-09 18:14:32167#elif defined(OS_WIN)
168 return StartSandboxWindows(sandbox_info_);
Avi Drissman7c57be72020-07-29 20:09:46169#elif defined(OS_MAC)
Greg Kerr3480aa82018-02-01 00:53:03170 return sandbox::Seatbelt::IsSandboxed();
sadrul454af3332016-09-09 18:14:32171#else
172 return false;
173#endif
174 }
175
176#if defined(OS_WIN)
177 const sandbox::SandboxInterfaceInfo* sandbox_info_ = nullptr;
sadrul454af3332016-09-09 18:14:32178#endif
179
180 DISALLOW_COPY_AND_ASSIGN(ContentSandboxHelper);
181};
182
Avi Drissman7c57be72020-07-29 20:09:46183#if defined(OS_MAC)
Christopher Camerond22d0152020-05-20 16:47:41184void TestShaderCallback(metal::TestShaderComponent component,
185 metal::TestShaderResult result,
186 const base::TimeDelta& callback_time) {
Christopher Cameron21c4abc2019-11-15 04:06:15187 switch (result) {
188 case metal::TestShaderResult::kNotAttempted:
189 case metal::TestShaderResult::kFailed:
190 // Don't include data if no Metal device was created (e.g, due to hardware
191 // or macOS version reasons).
192 return;
193 case metal::TestShaderResult::kTimedOut:
Christopher Cameronbd7837b42019-11-18 19:34:02194 break;
Christopher Cameron21c4abc2019-11-15 04:06:15195 case metal::TestShaderResult::kSucceeded:
Christopher Cameron21c4abc2019-11-15 04:06:15196 break;
197 }
Christopher Camerond22d0152020-05-20 16:47:41198 switch (component) {
199 case metal::TestShaderComponent::kCompile:
200 UMA_HISTOGRAM_MEDIUM_TIMES("Gpu.Metal.TestShaderCompileTime",
201 callback_time);
202 break;
203 case metal::TestShaderComponent::kLink:
204 UMA_HISTOGRAM_MEDIUM_TIMES("Gpu.Metal.TestShaderLinkTime", callback_time);
205 break;
206 }
Christopher Cameron21c4abc2019-11-15 04:06:15207}
208#endif
209
kylechar476993472016-09-14 16:03:48210} // namespace
[email protected]ec4bda62013-06-14 15:51:03211
[email protected]c0fc0942010-01-13 00:55:37212// Main function for starting the Gpu process.
[email protected]eb398192012-10-22 20:16:19213int GpuMain(const MainFunctionParams& parameters) {
[email protected]d13f35d2012-05-18 02:28:15214 TRACE_EVENT0("gpu", "GpuMain");
Ehsan Chiniforooshana8c8dad2017-11-03 07:23:09215 base::trace_event::TraceLog::GetInstance()->set_process_name("GPU Process");
ssidb2e3ece2015-02-09 16:02:20216 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:39217 kTraceEventGpuProcessSortIndex);
[email protected]d13f35d2012-05-18 02:28:15218
avi83883c82014-12-23 00:08:49219 const base::CommandLine& command_line = parameters.command_line;
Zhenyao Mo910beb82017-10-25 03:23:00220
221 gpu::GpuPreferences gpu_preferences;
222 if (command_line.HasSwitch(switches::kGpuPreferences)) {
223 std::string value =
224 command_line.GetSwitchValueASCII(switches::kGpuPreferences);
Jonathan Backer16cc8fd2018-05-31 19:59:22225 bool success = gpu_preferences.FromSwitchValue(value);
Zhenyao Mo910beb82017-10-25 03:23:00226 CHECK(success);
227 }
228
229 if (gpu_preferences.gpu_startup_dialog)
John Abd-El-Malek884291c2017-08-09 06:43:48230 WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:49231
[email protected]ca23992b02013-06-13 17:25:19232 base::Time start_time = base::Time::Now();
233
[email protected]23f46562011-09-07 01:42:39234#if defined(OS_WIN)
Daniel Libby1700bbe82019-01-30 22:22:36235 base::trace_event::TraceEventETWExport::EnableETWExport();
236
[email protected]52819472013-11-24 22:49:55237 // Prevent Windows from displaying a modal dialog on failures like not being
238 // able to load a DLL.
239 SetErrorMode(
240 SEM_FAILCRITICALERRORS |
241 SEM_NOGPFAULTERRORBOX |
242 SEM_NOOPENFILEERRORBOX);
Robert Liao98bb92d2017-06-15 22:20:39243
244 // COM is used by some Windows Media Foundation calls made on this thread and
245 // must be MTA so we don't have to worry about pumping messages to handle
246 // COM callbacks.
247 base::win::ScopedCOMInitializer com_initializer(
248 base::win::ScopedCOMInitializer::kMTA);
Sunny Sachanandani35c727c2019-07-09 13:12:52249
250 if (base::FeatureList::IsEnabled(features::kGpuProcessHighPriorityWin))
Sunny Sachanandani49bdbad2019-07-17 19:15:10251 ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
[email protected]23f46562011-09-07 01:42:39252#endif
[email protected]ec4bda62013-06-14 15:51:03253
Dale Curtis1b6becebb2020-03-30 20:13:35254 // Installs a base::LogMessageHandlerFunction which ensures messages are sent
255 // to the GpuProcessHost once the GpuServiceImpl has started.
256 viz::GpuServiceImpl::InstallPreInitializeLogHandler();
[email protected]23f46562011-09-07 01:42:39257
ericrk1d9e17f2016-11-30 01:51:28258 // We are experiencing what appear to be memory-stomp issues in the GPU
Alex Clarkef7fb8a82019-06-06 15:41:53259 // process. These issues seem to be impacting the task executor and listeners
260 // registered to it. Create the task executor on the heap to guard against
ericrk1d9e17f2016-11-30 01:51:28261 // this.
262 // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802
263 // and crbug.com/609252.
Alex Clarke636e7052019-05-30 10:49:37264 std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor;
skyostil82befc52016-12-19 13:48:59265 std::unique_ptr<ui::PlatformEventSource> event_source;
skyostilb354f882016-12-13 18:42:45266 if (command_line.HasSwitch(switches::kHeadless)) {
Alex Clarke636e7052019-05-30 10:49:37267 main_thread_task_executor =
268 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15269 base::MessagePumpType::DEFAULT);
skyostilb354f882016-12-13 18:42:45270 } else {
[email protected]db6101db2012-10-25 15:20:08271#if defined(OS_WIN)
Robert Liao98bb92d2017-06-15 22:20:39272 // The GpuMain thread should not be pumping Windows messages because no UI
273 // is expected to run on this thread.
Alex Clarke636e7052019-05-30 10:49:37274 main_thread_task_executor =
275 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15276 base::MessagePumpType::DEFAULT);
Maksim Sisov459eada2020-06-25 08:16:31277#elif defined(USE_X11) || defined(USE_OZONE)
278#if defined(USE_X11)
279 if (!features::IsUsingOzonePlatform()) {
280 // We need a UI loop so that we can grab the Expose events. See
281 // GLSurfaceGLX and https://crbug.com/326995.
282 ui::SetDefaultX11ErrorHandlers();
283 if (!gfx::GetXDisplay())
284 return RESULT_CODE_GPU_DEAD_ON_ARRIVAL;
285 main_thread_task_executor =
286 std::make_unique<base::SingleThreadTaskExecutor>(
287 base::MessagePumpType::UI);
288 event_source = ui::PlatformEventSource::CreateDefault();
289 }
290#endif
291#if defined(USE_OZONE)
Alex Clarke636e7052019-05-30 10:49:37292 // The MessagePump type required depends on the Ozone platform selected at
tonikitoofb807b102017-02-08 19:52:03293 // runtime.
Maksim Sisov459eada2020-06-25 08:16:31294 if (!main_thread_task_executor) {
295 main_thread_task_executor =
296 std::make_unique<base::SingleThreadTaskExecutor>(
297 gpu_preferences.message_pump_type);
298 }
299#endif
kylechar476993472016-09-14 16:03:48300#elif defined(OS_LINUX)
301#error "Unsupported Linux platform."
Avi Drissman7c57be72020-07-29 20:09:46302#elif defined(OS_MAC)
Christopher Cameron1732f2b02017-11-17 10:56:50303 // Cross-process CoreAnimation requires a CFRunLoop to function at all, and
304 // requires a NSRunLoop to not starve under heavy load. See:
305 // https://crbug.com/312462#c51 and https://crbug.com/783298
Alex Clarke636e7052019-05-30 10:49:37306 main_thread_task_executor =
307 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15308 base::MessagePumpType::NS_RUNLOOP);
Etienne Pierre-doray2163f3012020-04-02 21:37:14309 // As part of the migration to DoWork(), this policy is required to keep
Etienne Pierre-doray9eb4f5a2020-01-15 16:29:24310 // previous behavior and avoid regressions.
311 // TODO(crbug.com/1041853): Consider updating the policy.
312 main_thread_task_executor->SetWorkBatchSize(2);
[email protected]826aab02014-05-14 02:58:59313#else
Alex Clarke636e7052019-05-30 10:49:37314 main_thread_task_executor =
315 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15316 base::MessagePumpType::DEFAULT);
[email protected]db6101db2012-10-25 15:20:08317#endif
skyostilb354f882016-12-13 18:42:45318 }
[email protected]db6101db2012-10-25 15:20:08319
[email protected]db6101db2012-10-25 15:20:08320 base::PlatformThread::SetName("CrGpuMain");
321
Avi Drissman7c57be72020-07-29 20:09:46322#if !defined(OS_MAC)
Sunny Sachanandani5cd10e962019-06-13 22:55:32323 if (base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)) {
324 // Set thread priority before sandbox initialization.
325 base::PlatformThread::SetCurrentThreadPriority(
326 base::ThreadPriority::DISPLAY);
327 }
Michael Spang50ed1ff2019-07-02 22:26:56328#endif
revemane7acf842016-02-05 08:24:32329
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57330 auto gpu_init = std::make_unique<gpu::GpuInit>();
sadrul454af3332016-09-09 18:14:32331 ContentSandboxHelper sandbox_helper;
332#if defined(OS_WIN)
333 sandbox_helper.set_sandbox_info(parameters.sandbox_info);
[email protected]af7c5d92014-02-03 19:53:15334#endif
[email protected]af7c5d92014-02-03 19:53:15335
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57336 gpu_init->set_sandbox_helper(&sandbox_helper);
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24337
Gabriel Charettec7e363c62020-06-24 17:39:35338 // Since GPU initialization calls into skia, it's important to initialize skia
Khushal1d055592018-07-28 02:00:39339 // before it.
340 InitializeSkia();
341
Gabriel Charettec7e363c62020-06-24 17:39:35342 // Create the ThreadPool before invoking |gpu_init| as it needs the ThreadPool
343 // (in angle::InitializePlatform()). Do not start it until after the sandbox
344 // is initialized however to avoid creating threads outside the sandbox.
345 base::ThreadPoolInstance::Create("GPU");
346
sadrul454af3332016-09-09 18:14:32347 // Gpu initialization may fail for various reasons, in which case we will need
348 // to tear down this process. However, we can not do so safely until the IPC
349 // channel is set up, because the detection of early return of a child process
350 // is implemented using an IPC channel error. If the IPC channel is not fully
351 // set up between the browser and GPU process, and the GPU process crashes or
352 // exits early, the browser process will never detect it. For this reason we
sadrul72aae8a2017-01-24 04:52:32353 // defer tearing down the GPU process until receiving the initialization
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:06354 // message from the browser (through mojom::VizMain::CreateGpuService()).
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57355 const bool init_success = gpu_init->InitializeAndStartSandbox(
Zhenyao Moe23f75262018-02-07 02:15:00356 const_cast<base::CommandLine*>(&command_line), gpu_preferences);
sadrul454af3332016-09-09 18:14:32357 const bool dead_on_arrival = !init_success;
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24358
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57359 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
[email protected]ec4bda62013-06-14 15:51:03360
Gabriel Charettec7e363c62020-06-24 17:39:35361 // Start the ThreadPoolInstance now that the sandbox is initialized.
362 base::ThreadPoolInstance::Get()->StartWithDefaultParams();
363
Sunny Sachanandani5cd10e962019-06-13 22:55:32364 const base::ThreadPriority io_thread_priority =
365 base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)
366 ? base::ThreadPriority::DISPLAY
367 : base::ThreadPriority::NORMAL;
Avi Drissman7c57be72020-07-29 20:09:46368#if defined(OS_MAC)
behdad8b57eaad2019-07-18 13:58:48369 // Increase the thread priority to get more reliable values in performance
370 // test of mac_os.
371 GpuProcess gpu_process(
372 (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests)
373 ? base::ThreadPriority::REALTIME_AUDIO
374 : io_thread_priority));
375#else
reveman7caf8cf2016-02-16 02:39:05376 GpuProcess gpu_process(io_thread_priority);
behdad8b57eaad2019-07-18 13:58:48377#endif
Xi Chengd6390812018-01-24 00:01:50378
Tom Andersonff8d22e2020-01-30 03:19:16379#if defined(USE_X11)
380 // ui::GbmDevice() takes >50ms with amdgpu, so kick off
381 // GpuMemoryBufferSupportX11 creation on another thread now.
Maksim Sisove8bf9222020-06-24 07:53:02382 if (!features::IsUsingOzonePlatform() &&
383 gpu_preferences.enable_native_gpu_memory_buffers) {
Gabriel Charette6f9509b2020-05-27 20:39:57384 base::ThreadPool::PostTask(
Tom Andersonbe0638f2020-05-21 01:20:33385 FROM_HERE, base::BindOnce([]() {
386 SCOPED_UMA_HISTOGRAM_TIMER("Linux.X11.GbmSupportX11CreationTime");
387 ui::GpuMemoryBufferSupportX11::GetInstance();
388 }));
389 }
Tom Andersonff8d22e2020-01-30 03:19:16390#endif
391
Zhenyao Moe23f75262018-02-07 02:15:00392 auto* client = GetContentClient()->gpu();
Xi Chengd6390812018-01-24 00:01:50393 if (client)
394 client->PostIOThreadCreated(gpu_process.io_task_runner());
395
Wez6979109b2018-09-07 17:30:56396 base::RunLoop run_loop;
397 GpuChildThread* child_thread =
Dale Curtis1b6becebb2020-03-30 20:13:35398 new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init));
[email protected]7a31f7c2011-03-21 23:22:04399 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17400
[email protected]7a31f7c2011-03-21 23:22:04401 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44402
Avi Drissman7c57be72020-07-29 20:09:46403#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MAC)
Dale Curtis12cfe022020-02-21 01:18:42404 // Startup tracing is usually enabled earlier, but if we forked from a zygote,
405 // we can only enable it after mojo IPC support is brought up initialized by
406 // GpuChildThread, because the mojo broker has to create the tracing SMB on
407 // our behalf due to the zygote sandbox.
408 if (parameters.zygote_child)
409 tracing::EnableStartupTracingIfNeeded();
410#endif // OS_POSIX && !OS_ANDROID && !!OS_MACOSX
411
Avi Drissman7c57be72020-07-29 20:09:46412#if defined(OS_MAC)
Kai Ninomiyae2c55582019-12-19 23:51:03413 // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser
414 // process, meaning it is "responsible" for making sure it and its
415 // subordinate processes (i.e. the GPU process) drop references to the
416 // external GPU. Despite this, the system still sends the device removal
417 // notifications to the GPU process, so the GPU process handles its own
418 // graceful shutdown without help from the browser process.
419 //
420 // Using the "SafeEjectGPU" tool, we can see that when the browser process
421 // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject
422 // actions apply to the responsible process, who in turn deals with
423 // subordinates to eliminate their ejecting eGPU references" [man 8
424 // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU
425 // process exits, it appears that the browser process is no longer considered
426 // to be using the GPU, so it "succeeds" the 'wait'.
427 metal::RegisterGracefulExitOnDeviceRemoval();
428
Christopher Cameron21c4abc2019-11-15 04:06:15429 // Launch a test metal shader compile to see how long it takes to complete (if
430 // it ever completes).
431 // https://crbug.com/974219
Christopher Camerond362a662019-11-27 17:45:18432 metal::TestShader(base::BindOnce(TestShaderCallback));
Christopher Cameron21c4abc2019-11-15 04:06:15433#endif
434
primianob3fb6412015-10-14 16:03:51435#if defined(OS_ANDROID)
436 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40437 tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics",
438 nullptr);
primianob3fb6412015-10-14 16:03:51439#endif
440
stanisc61507092017-07-06 16:36:58441 base::HighResolutionTimerManager hi_res_timer_manager;
442
[email protected]d13f35d2012-05-18 02:28:15443 {
444 TRACE_EVENT0("gpu", "Run Message Loop");
Wez6979109b2018-09-07 17:30:56445 run_loop.Run();
[email protected]d13f35d2012-05-18 02:28:15446 }
[email protected]c0fc0942010-01-13 00:55:37447
jbaumana19f1df2017-01-18 03:01:17448 return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0;
[email protected]c0fc0942010-01-13 00:55:37449}
[email protected]6ec3a572012-08-17 02:09:51450
451namespace {
452
[email protected]59a7ae4e2012-10-01 23:54:44453#if defined(OS_LINUX)
Satyajit Sahu82a76e02017-09-18 14:50:14454bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33455 const gpu::GPUInfo* gpu_info,
456 const gpu::GpuPreferences& gpu_prefs) {
fdorayf30bf3a2015-10-28 21:47:00457 TRACE_EVENT0("gpu,startup", "Initialize sandbox");
[email protected]663c4b32013-04-18 05:52:54458
[email protected]f330b762014-02-08 04:52:46459 if (watchdog_thread) {
Tom Sepez437e2202017-10-24 21:26:47460 // SandboxLinux needs to be able to ensure that the thread
[email protected]f330b762014-02-08 04:52:46461 // has really been stopped.
Robert Sesek7d0b49b2020-07-08 18:31:27462 sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread);
[email protected]f330b762014-02-08 04:52:46463 }
[email protected]655abd522014-06-02 15:23:43464
Tom Sepez437e2202017-10-24 21:26:47465 // SandboxLinux::InitializeSandbox() must always be called
[email protected]663c4b32013-04-18 05:52:54466 // with only one thread.
Robert Sesek7d0b49b2020-07-08 18:31:27467 sandbox::policy::SandboxLinux::Options sandbox_options;
Tom Sepeza0950c62017-10-18 20:39:14468 sandbox_options.use_amd_specific_policies =
469 gpu_info && angle::IsAMD(gpu_info->active_gpu().vendor_id);
Brian Hoe7c074832019-12-11 20:20:52470 sandbox_options.use_intel_specific_policies =
471 gpu_info && angle::IsIntel(gpu_info->active_gpu().vendor_id);
Zhenyao Moe9187a862017-10-20 04:26:33472 sandbox_options.accelerated_video_decode_enabled =
473 !gpu_prefs.disable_accelerated_video_decode;
Sheng-Hao Tsaoea6aa852018-01-03 06:48:16474 sandbox_options.accelerated_video_encode_enabled =
475 !gpu_prefs.disable_accelerated_video_encode;
Tom Sepez085507ab2017-10-18 22:36:00476
Robert Sesek7d0b49b2020-07-08 18:31:27477 bool res = sandbox::policy::SandboxLinux::GetInstance()->InitializeSandbox(
478 sandbox::policy::SandboxTypeFromCommandLine(
Tom Sepez2255db72017-10-26 18:50:09479 *base::CommandLine::ForCurrentProcess()),
Tom Sepez4b101712017-11-08 19:39:58480 base::BindOnce(GpuProcessPreSandboxHook), sandbox_options);
Tom Sepeza0950c62017-10-18 20:39:14481
[email protected]f330b762014-02-08 04:52:46482 if (watchdog_thread) {
Tom Sepeza0950c62017-10-18 20:39:14483 base::Thread::Options thread_options;
484 thread_options.timer_slack = base::TIMER_SLACK_MAXIMUM;
485 watchdog_thread->StartWithOptions(thread_options);
[email protected]f330b762014-02-08 04:52:46486 }
[email protected]663c4b32013-04-18 05:52:54487
488 return res;
489}
490#endif // defined(OS_LINUX)
491
492#if defined(OS_WIN)
493bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
fdorayf30bf3a2015-10-28 21:47:00494 TRACE_EVENT0("gpu,startup", "Lower token");
[email protected]663c4b32013-04-18 05:52:54495
496 // For Windows, if the target_services interface is not zero, the process
497 // is sandboxed and we must call LowerToken() before rendering untrusted
498 // content.
499 sandbox::TargetServices* target_services = sandbox_info->target_services;
500 if (target_services) {
501 target_services->LowerToken();
502 return true;
503 }
504
505 return false;
506}
507#endif // defined(OS_WIN)
508
[email protected]6ec3a572012-08-17 02:09:51509} // namespace.
510
[email protected]eb398192012-10-22 20:16:19511} // namespace content