blob: 660c5f35c6095b23cc483c8eb1c119c215dc681d [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>
Avi Drissman9a3ed4e2022-01-26 14:15:379#include <tuple>
dchengf26eed32016-01-13 10:58:1410#include <utility>
11
Sebastien Marchandf8cbfab2019-01-25 16:02:3012#include "base/bind.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3213#include "base/feature_list.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1515#include "base/message_loop/message_pump_type.h"
John Abd-El-Malek2ded1eb62021-10-14 20:06:4116#include "base/metrics/field_trial_params.h"
asvitkine8d51e9d2016-09-02 23:55:4317#include "base/metrics/histogram_macros.h"
Jeff Chenfce90f32022-01-31 19:49:3418#include "base/numerics/clamped_math.h"
19#include "base/process/process_metrics.h"
[email protected]2436a6b2012-04-13 21:08:5120#include "base/rand_util.h"
fdoraye716a902016-07-05 16:05:4921#include "base/run_loop.h"
[email protected]d0ea4782013-06-11 04:58:2422#include "base/strings/string_number_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0323#include "base/system/sys_info.h"
Alex Clarke636e7052019-05-30 10:49:3724#include "base/task/single_thread_task_executor.h"
Gabriel Charette6f9509b2020-05-27 20:39:5725#include "base/task/thread_pool.h"
[email protected]ce072a72010-12-31 20:02:1626#include "base/threading/platform_thread.h"
Gabriel Charetted87f10f2022-03-31 00:44:2227#include "base/time/time.h"
stanisc61507092017-07-06 16:36:5828#include "base/timer/hi_res_timer_manager.h"
primiano50b7444c2015-01-28 04:17:0029#include "base/trace_event/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3730#include "build/build_config.h"
Yuta Hijikata76d5cb62020-12-09 09:51:5331#include "build/chromeos_buildflags.h"
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:0632#include "components/viz/service/main/viz_main_impl.h"
[email protected]91a2aea2013-07-08 23:14:3933#include "content/common/content_constants_internal.h"
John Abd-El-Malek884291c2017-08-09 06:43:4834#include "content/common/content_switches_internal.h"
Benoit Lize7ee77d32021-03-04 17:26:1435#include "content/common/partition_alloc_support.h"
Khushala4e236f2018-06-01 03:00:4636#include "content/common/skia_utils.h"
[email protected]7a31f7c2011-03-21 23:22:0437#include "content/gpu/gpu_child_thread.h"
[email protected]623c0bd2011-03-12 01:00:4138#include "content/gpu/gpu_process.h"
[email protected]c9e2cbbb2012-05-12 21:17:2739#include "content/public/common/content_client.h"
John Abd-El-Malek2ded1eb62021-10-14 20:06:4140#include "content/public/common/content_features.h"
[email protected]c9e2cbbb2012-05-12 21:17:2741#include "content/public/common/content_switches.h"
42#include "content/public/common/main_function_params.h"
jbaumana19f1df2017-01-18 03:01:1743#include "content/public/common/result_codes.h"
Zhenyao Moc76e9032018-01-19 21:15:4644#include "content/public/gpu/content_gpu_client.h"
[email protected]40c19e722013-11-05 23:51:2445#include "gpu/command_buffer/service/gpu_switches.h"
sadruled395922016-09-07 17:32:5846#include "gpu/config/gpu_driver_bug_list.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3247#include "gpu/config/gpu_finch_features.h"
[email protected]d7b5cc72013-05-23 20:05:0048#include "gpu/config/gpu_info_collector.h"
Jonathan Backer16cc8fd2018-05-31 19:59:2249#include "gpu/config/gpu_preferences.h"
tfarina15525c42015-04-28 19:04:1650#include "gpu/config/gpu_switches.h"
[email protected]40c19e722013-11-05 23:51:2451#include "gpu/config/gpu_util.h"
fsamuelc2774222016-03-24 00:27:1252#include "gpu/ipc/common/gpu_memory_buffer_support.h"
markdittmerd88b8352016-04-08 15:28:4553#include "gpu/ipc/service/gpu_config.h"
sadrul454af3332016-09-09 18:14:3254#include "gpu/ipc/service/gpu_init.h"
sadrul2fb7e152016-08-30 05:21:4555#include "gpu/ipc/service/gpu_watchdog_thread.h"
Scott Violeta35f9a42018-03-22 22:00:4456#include "media/gpu/buildflags.h"
kylechar60e12ed2021-03-15 15:56:0357#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
Dale Curtis12cfe022020-02-21 01:18:4258#include "services/tracing/public/cpp/trace_startup.h"
Tom Sepeza0950c62017-10-18 20:39:1459#include "third_party/angle/src/gpu_info_util/SystemInfo.h"
Maksim Sisov459eada2020-06-25 08:16:3160#include "ui/base/ui_base_features.h"
[email protected]c0939182014-05-24 00:09:1961#include "ui/events/platform/platform_event_source.h"
skyostilb354f882016-12-13 18:42:4562#include "ui/gfx/switches.h"
martina.kollarovaa34211d2015-06-25 11:49:0763#include "ui/gl/gl_context.h"
[email protected]db6101db2012-10-25 15:20:0864#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2765#include "ui/gl/gl_surface.h"
66#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2467#include "ui/gl/gpu_switching_manager.h"
kylechar5b9dec12016-05-16 15:40:5768#include "ui/gl/init/gl_factory.h"
[email protected]c0fc0942010-01-13 00:55:3769
Xiaohan Wang62737b52022-01-15 18:09:0270#if BUILDFLAG(IS_WIN)
fdoraye716a902016-07-05 16:05:4971#include <dwmapi.h>
Benoit Lize7ee77d32021-03-04 17:26:1472#include <windows.h>
avi66a07722015-12-25 23:38:1273#endif
74
Xiaohan Wang62737b52022-01-15 18:09:0275#if BUILDFLAG(IS_ANDROID)
primianob3fb6412015-10-14 16:03:5176#include "base/trace_event/memory_dump_manager.h"
primianoccb26c62016-06-01 21:50:0277#include "components/tracing/common/graphics_memory_dump_provider_android.h"
John Abd-El-Malek7d7b8e32021-10-19 00:11:1778#include "content/common/android/cpu_affinity_setter.h"
primianob3fb6412015-10-14 16:03:5179#endif
80
Xiaohan Wang62737b52022-01-15 18:09:0281#if BUILDFLAG(IS_WIN)
Daniel Libby8f7e6262019-01-08 22:35:5582#include "base/trace_event/trace_event_etw_export_win.h"
[email protected]5f7e4512012-10-01 20:51:3783#include "base/win/scoped_com_initializer.h"
kylechar5b9dec12016-05-16 15:40:5784#include "base/win/windows_version.h"
Miguel Casasd697adf32018-02-26 19:06:3185#include "media/gpu/windows/dxva_video_decode_accelerator_win.h"
86#include "media/gpu/windows/media_foundation_video_encode_accelerator_win.h"
[email protected]181491782012-07-18 00:59:1587#include "sandbox/win/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3788#endif
89
Xiaohan Wang62737b52022-01-15 18:09:0290#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Tom Sepez085507ab2017-10-18 22:36:0091#include "content/gpu/gpu_sandbox_hook_linux.h"
Robert Sesek7d0b49b2020-07-08 18:31:2792#include "sandbox/policy/linux/sandbox_linux.h"
Alex Gougheb6a38f2021-10-22 01:55:1393#include "sandbox/policy/sandbox_type.h"
[email protected]2436a6b2012-04-13 21:08:5194#endif
95
Xiaohan Wang62737b52022-01-15 18:09:0296#if BUILDFLAG(IS_MAC)
[email protected]826aab02014-05-14 02:58:5997#include "base/message_loop/message_pump_mac.h"
Kai Ninomiyae2c55582019-12-19 23:51:0398#include "components/metal_util/device_removal.h"
Christopher Cameron21c4abc2019-11-15 04:06:1599#include "components/metal_util/test_shader.h"
Robert Sesek3bd6e4b32019-11-26 16:02:27100#include "media/gpu/mac/vt_video_decode_accelerator_mac.h"
Greg Kerr3480aa82018-02-01 00:53:03101#include "sandbox/mac/seatbelt.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
Xiaohan Wang62737b52022-01-15 18:09:02112#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33113bool StartSandboxLinux(gpu::GpuWatchdogThread*,
114 const gpu::GPUInfo*,
115 const gpu::GpuPreferences&);
Xiaohan Wang62737b52022-01-15 18:09:02116#elif BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54117bool 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() {}
Peter Boström828b9022021-09-21 02:28:43123
124 ContentSandboxHelper(const ContentSandboxHelper&) = delete;
125 ContentSandboxHelper& operator=(const ContentSandboxHelper&) = delete;
126
sadrul454af3332016-09-09 18:14:32127 ~ContentSandboxHelper() override {}
128
Xiaohan Wang62737b52022-01-15 18:09:02129#if BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32130 void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) {
131 sandbox_info_ = info;
132 }
133#endif
134
sadrul454af3332016-09-09 18:14:32135 private:
136 // SandboxHelper:
Ted Meyer6801a072020-10-28 03:25:02137 void PreSandboxStartup(const gpu::GpuPreferences& gpu_prefs) override {
sadrul454af3332016-09-09 18:14:32138 // Warm up resources that don't need access to GPUInfo.
139 {
140 TRACE_EVENT0("gpu", "Warm up rand");
141 // Warm up the random subsystem, which needs to be done pre-sandbox on all
142 // platforms.
Avi Drissman9a3ed4e2022-01-26 14:15:37143 std::ignore = base::RandUint64();
sadrul454af3332016-09-09 18:14:32144 }
145
Alexandre Courbotc13a5972017-07-23 03:48:48146#if BUILDFLAG(USE_VAAPI)
Xiaohan Wang62737b52022-01-15 18:09:02147#if BUILDFLAG(IS_CHROMEOS)
sadrul454af3332016-09-09 18:14:32148 media::VaapiWrapper::PreSandboxInitialization();
Pilar Molina Lopezfb5103b72021-02-26 23:35:52149#else // For Linux with VA-API support.
Ted Meyer6801a072020-10-28 03:25:02150 if (!gpu_prefs.disable_accelerated_video_decode)
Ted Meyer6f266fd2020-10-23 01:54:06151 media::VaapiWrapper::PreSandboxInitialization();
sadrul454af3332016-09-09 18:14:32152#endif
Ted Meyer6f266fd2020-10-23 01:54:06153#endif // BUILDFLAG(USE_VAAPI)
Xiaohan Wang62737b52022-01-15 18:09:02154#if BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32155 media::DXVAVideoDecodeAccelerator::PreSandboxInitialization();
156 media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization();
157#endif
Eric Karle35050e2017-09-07 01:44:35158
Xiaohan Wang62737b52022-01-15 18:09:02159#if BUILDFLAG(IS_MAC)
Robert Sesek766439c92021-03-23 21:01:31160 {
Robert Sesek3bd6e4b32019-11-26 16:02:27161 TRACE_EVENT0("gpu", "Initialize VideoToolbox");
162 media::InitializeVideoToolbox();
163 }
164#endif
165
Eric Karle35050e2017-09-07 01:44:35166 // On Linux, reading system memory doesn't work through the GPU sandbox.
167 // This value is cached, so access it here to populate the cache.
168 base::SysInfo::AmountOfPhysicalMemory();
sadrul454af3332016-09-09 18:14:32169 }
170
Satyajit Sahu82a76e02017-09-18 14:50:14171 bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33172 const gpu::GPUInfo* gpu_info,
173 const gpu::GpuPreferences& gpu_prefs) override {
Xiaohan Wang62737b52022-01-15 18:09:02174#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33175 return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs);
Xiaohan Wang62737b52022-01-15 18:09:02176#elif BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32177 return StartSandboxWindows(sandbox_info_);
Xiaohan Wang62737b52022-01-15 18:09:02178#elif BUILDFLAG(IS_MAC)
Greg Kerr3480aa82018-02-01 00:53:03179 return sandbox::Seatbelt::IsSandboxed();
sadrul454af3332016-09-09 18:14:32180#else
181 return false;
182#endif
183 }
184
Xiaohan Wang62737b52022-01-15 18:09:02185#if BUILDFLAG(IS_WIN)
Keishi Hattori0e45c022021-11-27 09:25:52186 raw_ptr<const sandbox::SandboxInterfaceInfo> sandbox_info_ = nullptr;
sadrul454af3332016-09-09 18:14:32187#endif
sadrul454af3332016-09-09 18:14:32188};
189
kylechar476993472016-09-14 16:03:48190} // namespace
[email protected]ec4bda62013-06-14 15:51:03191
[email protected]c0fc0942010-01-13 00:55:37192// Main function for starting the Gpu process.
Gabriel Charettefbeeb1c2021-11-10 20:50:06193int GpuMain(MainFunctionParams parameters) {
[email protected]d13f35d2012-05-18 02:28:15194 TRACE_EVENT0("gpu", "GpuMain");
Ehsan Chiniforooshana8c8dad2017-11-03 07:23:09195 base::trace_event::TraceLog::GetInstance()->set_process_name("GPU Process");
ssidb2e3ece2015-02-09 16:02:20196 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:39197 kTraceEventGpuProcessSortIndex);
[email protected]d13f35d2012-05-18 02:28:15198
Gabriel Charettefbeeb1c2021-11-10 20:50:06199 const base::CommandLine& command_line = *parameters.command_line;
Zhenyao Mo910beb82017-10-25 03:23:00200
201 gpu::GpuPreferences gpu_preferences;
202 if (command_line.HasSwitch(switches::kGpuPreferences)) {
203 std::string value =
204 command_line.GetSwitchValueASCII(switches::kGpuPreferences);
Jonathan Backer16cc8fd2018-05-31 19:59:22205 bool success = gpu_preferences.FromSwitchValue(value);
Zhenyao Mo910beb82017-10-25 03:23:00206 CHECK(success);
207 }
208
kylechar60e12ed2021-03-15 15:56:03209 // Disallow sending sync IPCs from the GPU process, in particular CrGpuMain
210 // and VizCompositorThreads. Incoming sync IPCs can be received out of order
211 // when waiting on response to an outgoing sync IPC. Both viz and gpu
212 // interfaces rely on receiving messages in order so this message reordering
213 // would break things.
214 mojo::SyncCallRestrictions::DisallowSyncCall();
215
Zhenyao Mo910beb82017-10-25 03:23:00216 if (gpu_preferences.gpu_startup_dialog)
John Abd-El-Malek884291c2017-08-09 06:43:48217 WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:49218
[email protected]ca23992b02013-06-13 17:25:19219 base::Time start_time = base::Time::Now();
220
Xiaohan Wang62737b52022-01-15 18:09:02221#if BUILDFLAG(IS_WIN)
Daniel Libby1700bbe82019-01-30 22:22:36222 base::trace_event::TraceEventETWExport::EnableETWExport();
223
[email protected]52819472013-11-24 22:49:55224 // Prevent Windows from displaying a modal dialog on failures like not being
225 // able to load a DLL.
Benoit Lize7ee77d32021-03-04 17:26:14226 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
227 SEM_NOOPENFILEERRORBOX);
Robert Liao98bb92d2017-06-15 22:20:39228
229 // COM is used by some Windows Media Foundation calls made on this thread and
230 // must be MTA so we don't have to worry about pumping messages to handle
231 // COM callbacks.
232 base::win::ScopedCOMInitializer com_initializer(
233 base::win::ScopedCOMInitializer::kMTA);
Sunny Sachanandani35c727c2019-07-09 13:12:52234
235 if (base::FeatureList::IsEnabled(features::kGpuProcessHighPriorityWin))
Sunny Sachanandani49bdbad2019-07-17 19:15:10236 ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
[email protected]23f46562011-09-07 01:42:39237#endif
[email protected]ec4bda62013-06-14 15:51:03238
Dale Curtis1b6becebb2020-03-30 20:13:35239 // Installs a base::LogMessageHandlerFunction which ensures messages are sent
240 // to the GpuProcessHost once the GpuServiceImpl has started.
241 viz::GpuServiceImpl::InstallPreInitializeLogHandler();
[email protected]23f46562011-09-07 01:42:39242
ericrk1d9e17f2016-11-30 01:51:28243 // We are experiencing what appear to be memory-stomp issues in the GPU
Alex Clarkef7fb8a82019-06-06 15:41:53244 // process. These issues seem to be impacting the task executor and listeners
245 // registered to it. Create the task executor on the heap to guard against
ericrk1d9e17f2016-11-30 01:51:28246 // this.
247 // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802
248 // and crbug.com/609252.
Alex Clarke636e7052019-05-30 10:49:37249 std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor;
skyostil82befc52016-12-19 13:48:59250 std::unique_ptr<ui::PlatformEventSource> event_source;
skyostilb354f882016-12-13 18:42:45251 if (command_line.HasSwitch(switches::kHeadless)) {
Alex Clarke636e7052019-05-30 10:49:37252 main_thread_task_executor =
253 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15254 base::MessagePumpType::DEFAULT);
skyostilb354f882016-12-13 18:42:45255 } else {
Xiaohan Wang62737b52022-01-15 18:09:02256#if BUILDFLAG(IS_WIN)
Robert Liao98bb92d2017-06-15 22:20:39257 // The GpuMain thread should not be pumping Windows messages because no UI
258 // is expected to run on this thread.
Alex Clarke636e7052019-05-30 10:49:37259 main_thread_task_executor =
260 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15261 base::MessagePumpType::DEFAULT);
Maksim Sisov2ac2c312021-11-03 08:00:12262#elif defined(USE_OZONE)
Alex Clarke636e7052019-05-30 10:49:37263 // The MessagePump type required depends on the Ozone platform selected at
tonikitoofb807b102017-02-08 19:52:03264 // runtime.
Maksim Sisov459eada2020-06-25 08:16:31265 if (!main_thread_task_executor) {
266 main_thread_task_executor =
267 std::make_unique<base::SingleThreadTaskExecutor>(
268 gpu_preferences.message_pump_type);
269 }
Xiaohan Wang62737b52022-01-15 18:09:02270#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
kylechar476993472016-09-14 16:03:48271#error "Unsupported Linux platform."
Xiaohan Wang62737b52022-01-15 18:09:02272#elif BUILDFLAG(IS_MAC)
Christopher Cameron1732f2b02017-11-17 10:56:50273 // Cross-process CoreAnimation requires a CFRunLoop to function at all, and
274 // requires a NSRunLoop to not starve under heavy load. See:
275 // https://crbug.com/312462#c51 and https://crbug.com/783298
Alex Clarke636e7052019-05-30 10:49:37276 main_thread_task_executor =
277 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15278 base::MessagePumpType::NS_RUNLOOP);
Etienne Pierre-doray2163f3012020-04-02 21:37:14279 // As part of the migration to DoWork(), this policy is required to keep
Etienne Pierre-doray9eb4f5a2020-01-15 16:29:24280 // previous behavior and avoid regressions.
281 // TODO(crbug.com/1041853): Consider updating the policy.
282 main_thread_task_executor->SetWorkBatchSize(2);
[email protected]826aab02014-05-14 02:58:59283#else
Alex Clarke636e7052019-05-30 10:49:37284 main_thread_task_executor =
285 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15286 base::MessagePumpType::DEFAULT);
[email protected]db6101db2012-10-25 15:20:08287#endif
skyostilb354f882016-12-13 18:42:45288 }
[email protected]db6101db2012-10-25 15:20:08289
[email protected]db6101db2012-10-25 15:20:08290 base::PlatformThread::SetName("CrGpuMain");
291
Xiaohan Wang62737b52022-01-15 18:09:02292#if !BUILDFLAG(IS_MAC)
Sunny Sachanandani5cd10e962019-06-13 22:55:32293 if (base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)) {
294 // Set thread priority before sandbox initialization.
295 base::PlatformThread::SetCurrentThreadPriority(
296 base::ThreadPriority::DISPLAY);
297 }
Michael Spang50ed1ff2019-07-02 22:26:56298#endif
revemane7acf842016-02-05 08:24:32299
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57300 auto gpu_init = std::make_unique<gpu::GpuInit>();
sadrul454af3332016-09-09 18:14:32301 ContentSandboxHelper sandbox_helper;
Xiaohan Wang62737b52022-01-15 18:09:02302#if BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32303 sandbox_helper.set_sandbox_info(parameters.sandbox_info);
[email protected]af7c5d92014-02-03 19:53:15304#endif
[email protected]af7c5d92014-02-03 19:53:15305
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57306 gpu_init->set_sandbox_helper(&sandbox_helper);
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24307
Gabriel Charettec7e363c62020-06-24 17:39:35308 // Since GPU initialization calls into skia, it's important to initialize skia
Khushal1d055592018-07-28 02:00:39309 // before it.
310 InitializeSkia();
311
Gabriel Charettec7e363c62020-06-24 17:39:35312 // Create the ThreadPool before invoking |gpu_init| as it needs the ThreadPool
313 // (in angle::InitializePlatform()). Do not start it until after the sandbox
314 // is initialized however to avoid creating threads outside the sandbox.
315 base::ThreadPoolInstance::Create("GPU");
316
sadrul454af3332016-09-09 18:14:32317 // Gpu initialization may fail for various reasons, in which case we will need
318 // to tear down this process. However, we can not do so safely until the IPC
319 // channel is set up, because the detection of early return of a child process
320 // is implemented using an IPC channel error. If the IPC channel is not fully
321 // set up between the browser and GPU process, and the GPU process crashes or
322 // exits early, the browser process will never detect it. For this reason we
sadrul72aae8a2017-01-24 04:52:32323 // defer tearing down the GPU process until receiving the initialization
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:06324 // message from the browser (through mojom::VizMain::CreateGpuService()).
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57325 const bool init_success = gpu_init->InitializeAndStartSandbox(
Zhenyao Moe23f75262018-02-07 02:15:00326 const_cast<base::CommandLine*>(&command_line), gpu_preferences);
sadrul454af3332016-09-09 18:14:32327 const bool dead_on_arrival = !init_success;
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24328
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57329 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
[email protected]ec4bda62013-06-14 15:51:03330
Gabriel Charettec7e363c62020-06-24 17:39:35331 // Start the ThreadPoolInstance now that the sandbox is initialized.
332 base::ThreadPoolInstance::Get()->StartWithDefaultParams();
333
Sunny Sachanandani5cd10e962019-06-13 22:55:32334 const base::ThreadPriority io_thread_priority =
335 base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)
336 ? base::ThreadPriority::DISPLAY
337 : base::ThreadPriority::NORMAL;
Xiaohan Wang62737b52022-01-15 18:09:02338#if BUILDFLAG(IS_MAC)
behdad8b57eaad2019-07-18 13:58:48339 // Increase the thread priority to get more reliable values in performance
340 // test of mac_os.
341 GpuProcess gpu_process(
342 (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests)
343 ? base::ThreadPriority::REALTIME_AUDIO
344 : io_thread_priority));
345#else
reveman7caf8cf2016-02-16 02:39:05346 GpuProcess gpu_process(io_thread_priority);
behdad8b57eaad2019-07-18 13:58:48347#endif
Xi Chengd6390812018-01-24 00:01:50348
Zhenyao Moe23f75262018-02-07 02:15:00349 auto* client = GetContentClient()->gpu();
Xi Chengd6390812018-01-24 00:01:50350 if (client)
351 client->PostIOThreadCreated(gpu_process.io_task_runner());
352
Wez6979109b2018-09-07 17:30:56353 base::RunLoop run_loop;
354 GpuChildThread* child_thread =
Dale Curtis1b6becebb2020-03-30 20:13:35355 new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init));
[email protected]7a31f7c2011-03-21 23:22:04356 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17357
[email protected]7a31f7c2011-03-21 23:22:04358 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44359
Xiaohan Wang62737b52022-01-15 18:09:02360#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
Dale Curtis12cfe022020-02-21 01:18:42361 // Startup tracing is usually enabled earlier, but if we forked from a zygote,
362 // we can only enable it after mojo IPC support is brought up initialized by
363 // GpuChildThread, because the mojo broker has to create the tracing SMB on
364 // our behalf due to the zygote sandbox.
365 if (parameters.zygote_child)
366 tracing::EnableStartupTracingIfNeeded();
Xiaohan Wang62737b52022-01-15 18:09:02367#endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
Dale Curtis12cfe022020-02-21 01:18:42368
Xiaohan Wang62737b52022-01-15 18:09:02369#if BUILDFLAG(IS_MAC)
Kai Ninomiyae2c55582019-12-19 23:51:03370 // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser
371 // process, meaning it is "responsible" for making sure it and its
372 // subordinate processes (i.e. the GPU process) drop references to the
373 // external GPU. Despite this, the system still sends the device removal
374 // notifications to the GPU process, so the GPU process handles its own
375 // graceful shutdown without help from the browser process.
376 //
377 // Using the "SafeEjectGPU" tool, we can see that when the browser process
378 // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject
379 // actions apply to the responsible process, who in turn deals with
380 // subordinates to eliminate their ejecting eGPU references" [man 8
381 // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU
382 // process exits, it appears that the browser process is no longer considered
383 // to be using the GPU, so it "succeeds" the 'wait'.
384 metal::RegisterGracefulExitOnDeviceRemoval();
Christopher Cameron21c4abc2019-11-15 04:06:15385#endif
386
Xiaohan Wang62737b52022-01-15 18:09:02387#if BUILDFLAG(IS_ANDROID)
primianob3fb6412015-10-14 16:03:51388 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40389 tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics",
390 nullptr);
John Abd-El-Malek7d7b8e32021-10-19 00:11:17391 if (base::GetFieldTrialParamByFeatureAsBool(
392 features::kBigLittleScheduling,
393 features::kBigLittleSchedulingGpuMainBigParam, false)) {
394 SetCpuAffinityForCurrentThread(base::CpuAffinityMode::kBigCoresOnly);
395 }
primianob3fb6412015-10-14 16:03:51396#endif
397
Benoit Lize7ee77d32021-03-04 17:26:14398 internal::PartitionAllocSupport::Get()->ReconfigureAfterTaskRunnerInit(
399 switches::kGpuProcess);
400
stanisc61507092017-07-06 16:36:58401 base::HighResolutionTimerManager hi_res_timer_manager;
402
[email protected]d13f35d2012-05-18 02:28:15403 {
404 TRACE_EVENT0("gpu", "Run Message Loop");
Wez6979109b2018-09-07 17:30:56405 run_loop.Run();
[email protected]d13f35d2012-05-18 02:28:15406 }
[email protected]c0fc0942010-01-13 00:55:37407
jbaumana19f1df2017-01-18 03:01:17408 return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0;
[email protected]c0fc0942010-01-13 00:55:37409}
[email protected]6ec3a572012-08-17 02:09:51410
411namespace {
412
Xiaohan Wang62737b52022-01-15 18:09:02413#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Satyajit Sahu82a76e02017-09-18 14:50:14414bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33415 const gpu::GPUInfo* gpu_info,
416 const gpu::GpuPreferences& gpu_prefs) {
fdorayf30bf3a2015-10-28 21:47:00417 TRACE_EVENT0("gpu,startup", "Initialize sandbox");
[email protected]663c4b32013-04-18 05:52:54418
[email protected]f330b762014-02-08 04:52:46419 if (watchdog_thread) {
Tom Sepez437e2202017-10-24 21:26:47420 // SandboxLinux needs to be able to ensure that the thread
[email protected]f330b762014-02-08 04:52:46421 // has really been stopped.
Robert Sesek7d0b49b2020-07-08 18:31:27422 sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread);
[email protected]f330b762014-02-08 04:52:46423 }
[email protected]655abd522014-06-02 15:23:43424
Tom Sepez437e2202017-10-24 21:26:47425 // SandboxLinux::InitializeSandbox() must always be called
[email protected]663c4b32013-04-18 05:52:54426 // with only one thread.
Robert Sesek7d0b49b2020-07-08 18:31:27427 sandbox::policy::SandboxLinux::Options sandbox_options;
Tom Sepeza0950c62017-10-18 20:39:14428 sandbox_options.use_amd_specific_policies =
429 gpu_info && angle::IsAMD(gpu_info->active_gpu().vendor_id);
Brian Hoe7c074832019-12-11 20:20:52430 sandbox_options.use_intel_specific_policies =
431 gpu_info && angle::IsIntel(gpu_info->active_gpu().vendor_id);
Tom Anderson0fd12d92020-10-31 00:55:52432 sandbox_options.use_nvidia_specific_policies =
433 gpu_info && angle::IsNVIDIA(gpu_info->active_gpu().vendor_id);
Zhenyao Moe9187a862017-10-20 04:26:33434 sandbox_options.accelerated_video_decode_enabled =
435 !gpu_prefs.disable_accelerated_video_decode;
Sheng-Hao Tsaoea6aa852018-01-03 06:48:16436 sandbox_options.accelerated_video_encode_enabled =
437 !gpu_prefs.disable_accelerated_video_encode;
Tom Sepez085507ab2017-10-18 22:36:00438
Jeff Chenfce90f32022-01-31 19:49:34439#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_VAAPI)
440 // Increase the FD limit by 512 on VA-API Chrome OS devices in order to
441 // avoid running out of FDs in cases where many decoders are running
442 // concurrently. See b/215553848.
443 // TODO(b/195769334): revisit the need for this once out-of-process video
444 // decoding has been fully implemented.
445 const auto current_max_fds =
446 base::saturated_cast<unsigned int>(base::GetMaxFds());
447 constexpr unsigned int kMaxFDsDelta = 1u << 9;
448 const auto new_max_fds =
449 static_cast<int>(base::ClampAdd(current_max_fds, kMaxFDsDelta));
450 base::IncreaseFdLimitTo(base::checked_cast<unsigned int>(new_max_fds));
451#endif
452
Robert Sesek7d0b49b2020-07-08 18:31:27453 bool res = sandbox::policy::SandboxLinux::GetInstance()->InitializeSandbox(
454 sandbox::policy::SandboxTypeFromCommandLine(
Tom Sepez2255db72017-10-26 18:50:09455 *base::CommandLine::ForCurrentProcess()),
Tom Sepez4b101712017-11-08 19:39:58456 base::BindOnce(GpuProcessPreSandboxHook), sandbox_options);
Tom Sepeza0950c62017-10-18 20:39:14457
[email protected]f330b762014-02-08 04:52:46458 if (watchdog_thread) {
Tom Sepeza0950c62017-10-18 20:39:14459 base::Thread::Options thread_options;
460 thread_options.timer_slack = base::TIMER_SLACK_MAXIMUM;
Olivier Lia3c71552021-05-12 17:05:52461 watchdog_thread->StartWithOptions(std::move(thread_options));
[email protected]f330b762014-02-08 04:52:46462 }
[email protected]663c4b32013-04-18 05:52:54463
464 return res;
465}
Xiaohan Wang62737b52022-01-15 18:09:02466#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]663c4b32013-04-18 05:52:54467
Xiaohan Wang62737b52022-01-15 18:09:02468#if BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54469bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
fdorayf30bf3a2015-10-28 21:47:00470 TRACE_EVENT0("gpu,startup", "Lower token");
[email protected]663c4b32013-04-18 05:52:54471
472 // For Windows, if the target_services interface is not zero, the process
473 // is sandboxed and we must call LowerToken() before rendering untrusted
474 // content.
475 sandbox::TargetServices* target_services = sandbox_info->target_services;
476 if (target_services) {
477 target_services->LowerToken();
478 return true;
479 }
480
481 return false;
482}
Xiaohan Wang62737b52022-01-15 18:09:02483#endif // BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54484
[email protected]6ec3a572012-08-17 02:09:51485} // namespace.
486
[email protected]eb398192012-10-22 20:16:19487} // namespace content