blob: a5a5cfcb4abda8c4642d2904728a882e1e85db23 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[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
Arthur Sonzognia570236352022-12-19 13:18:5412#include "base/allocator/partition_alloc_support.h"
Joe Mason94bebf12022-06-03 15:03:5313#include "base/check.h"
Gabriel Charettef3851332022-04-28 23:39:1814#include "base/command_line.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3215#include "base/feature_list.h"
Avi Drissmanadac21992023-01-11 23:46:3916#include "base/functional/bind.h"
Keishi Hattori0e45c022021-11-27 09:25:5217#include "base/memory/raw_ptr.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1518#include "base/message_loop/message_pump_type.h"
asvitkine8d51e9d2016-09-02 23:55:4319#include "base/metrics/histogram_macros.h"
Jeff Chenfce90f32022-01-31 19:49:3420#include "base/numerics/clamped_math.h"
Aman Verma0bf8c112022-11-15 13:01:2421#include "base/process/current_process.h"
Jeff Chenfce90f32022-01-31 19:49:3422#include "base/process/process_metrics.h"
[email protected]2436a6b2012-04-13 21:08:5123#include "base/rand_util.h"
fdoraye716a902016-07-05 16:05:4924#include "base/run_loop.h"
[email protected]d0ea4782013-06-11 04:58:2425#include "base/strings/string_number_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0326#include "base/system/sys_info.h"
Alex Clarke636e7052019-05-30 10:49:3727#include "base/task/single_thread_task_executor.h"
Joe Mason94bebf12022-06-03 15:03:5328#include "base/task/thread_pool/thread_pool_instance.h"
[email protected]ce072a72010-12-31 20:02:1629#include "base/threading/platform_thread.h"
Gabriel Charetted87f10f2022-03-31 00:44:2230#include "base/time/time.h"
stanisc61507092017-07-06 16:36:5831#include "base/timer/hi_res_timer_manager.h"
primiano50b7444c2015-01-28 04:17:0032#include "base/trace_event/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3733#include "build/build_config.h"
Yuta Hijikata76d5cb62020-12-09 09:51:5334#include "build/chromeos_buildflags.h"
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:0635#include "components/viz/service/main/viz_main_impl.h"
Joe Mason94bebf12022-06-03 15:03:5336#include "content/child/child_process.h"
[email protected]91a2aea2013-07-08 23:14:3937#include "content/common/content_constants_internal.h"
John Abd-El-Malek884291c2017-08-09 06:43:4838#include "content/common/content_switches_internal.h"
Khushala4e236f2018-06-01 03:00:4639#include "content/common/skia_utils.h"
[email protected]7a31f7c2011-03-21 23:22:0440#include "content/gpu/gpu_child_thread.h"
[email protected]c9e2cbbb2012-05-12 21:17:2741#include "content/public/common/content_client.h"
42#include "content/public/common/content_switches.h"
43#include "content/public/common/main_function_params.h"
jbaumana19f1df2017-01-18 03:01:1744#include "content/public/common/result_codes.h"
Zhenyao Moc76e9032018-01-19 21:15:4645#include "content/public/gpu/content_gpu_client.h"
[email protected]40c19e722013-11-05 23:51:2446#include "gpu/command_buffer/service/gpu_switches.h"
sadruled395922016-09-07 17:32:5847#include "gpu/config/gpu_driver_bug_list.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3248#include "gpu/config/gpu_finch_features.h"
[email protected]d7b5cc72013-05-23 20:05:0049#include "gpu/config/gpu_info_collector.h"
Jonathan Backer16cc8fd2018-05-31 19:59:2250#include "gpu/config/gpu_preferences.h"
tfarina15525c42015-04-28 19:04:1651#include "gpu/config/gpu_switches.h"
[email protected]40c19e722013-11-05 23:51:2452#include "gpu/config/gpu_util.h"
fsamuelc2774222016-03-24 00:27:1253#include "gpu/ipc/common/gpu_memory_buffer_support.h"
markdittmerd88b8352016-04-08 15:28:4554#include "gpu/ipc/service/gpu_config.h"
sadrul454af3332016-09-09 18:14:3255#include "gpu/ipc/service/gpu_init.h"
sadrul2fb7e152016-08-30 05:21:4556#include "gpu/ipc/service/gpu_watchdog_thread.h"
Scott Violeta35f9a42018-03-22 22:00:4457#include "media/gpu/buildflags.h"
kylechar60e12ed2021-03-15 15:56:0358#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
Dale Curtis12cfe022020-02-21 01:18:4259#include "services/tracing/public/cpp/trace_startup.h"
Tom Sepeza0950c62017-10-18 20:39:1460#include "third_party/angle/src/gpu_info_util/SystemInfo.h"
Maksim Sisov459eada2020-06-25 08:16:3161#include "ui/base/ui_base_features.h"
[email protected]c0939182014-05-24 00:09:1962#include "ui/events/platform/platform_event_source.h"
skyostilb354f882016-12-13 18:42:4563#include "ui/gfx/switches.h"
martina.kollarovaa34211d2015-06-25 11:49:0764#include "ui/gl/gl_context.h"
Scott Violet833ff162023-04-19 00:32:3165#include "ui/gl/gl_features.h"
[email protected]db6101db2012-10-25 15:20:0866#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2767#include "ui/gl/gl_surface.h"
68#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2469#include "ui/gl/gpu_switching_manager.h"
kylechar5b9dec12016-05-16 15:40:5770#include "ui/gl/init/gl_factory.h"
[email protected]c0fc0942010-01-13 00:55:3771
Xiaohan Wang62737b52022-01-15 18:09:0272#if BUILDFLAG(IS_WIN)
fdoraye716a902016-07-05 16:05:4973#include <dwmapi.h>
Benoit Lize7ee77d32021-03-04 17:26:1474#include <windows.h>
avi66a07722015-12-25 23:38:1275#endif
76
Xiaohan Wang62737b52022-01-15 18:09:0277#if BUILDFLAG(IS_ANDROID)
Benoit Lizeb6fa4ac2023-02-13 11:59:4378#include "base/android/meminfo_dump_provider.h"
primianob3fb6412015-10-14 16:03:5179#include "base/trace_event/memory_dump_manager.h"
primianoccb26c62016-06-01 21:50:0280#include "components/tracing/common/graphics_memory_dump_provider_android.h"
primianob3fb6412015-10-14 16:03:5181#endif
82
Xiaohan Wang62737b52022-01-15 18:09:0283#if BUILDFLAG(IS_WIN)
Daniel Libby8f7e6262019-01-08 22:35:5584#include "base/trace_event/trace_event_etw_export_win.h"
[email protected]5f7e4512012-10-01 20:51:3785#include "base/win/scoped_com_initializer.h"
Bill Carrba4cbb972022-10-10 18:24:1186#include "base/win/win_util.h"
kylechar5b9dec12016-05-16 15:40:5787#include "base/win/windows_version.h"
Dale Curtis56f208fc2023-04-07 18:55:2588#include "media/base/win/mf_initializer.h"
Alex Gough76f652f2023-06-16 14:00:1789#include "sandbox/policy/win/sandbox_warmup.h"
[email protected]181491782012-07-18 00:59:1590#include "sandbox/win/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3791#endif
92
Xiaohan Wang62737b52022-01-15 18:09:0293#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Tom Sepez085507ab2017-10-18 22:36:0094#include "content/gpu/gpu_sandbox_hook_linux.h"
Robert Sesek7d0b49b2020-07-08 18:31:2795#include "sandbox/policy/linux/sandbox_linux.h"
Alex Gougheb6a38f2021-10-22 01:55:1396#include "sandbox/policy/sandbox_type.h"
[email protected]2436a6b2012-04-13 21:08:5197#endif
98
Xiaohan Wang62737b52022-01-15 18:09:0299#if BUILDFLAG(IS_MAC)
[email protected]826aab02014-05-14 02:58:59100#include "base/message_loop/message_pump_mac.h"
Kai Ninomiyae2c55582019-12-19 23:51:03101#include "components/metal_util/device_removal.h"
Scott Violet833ff162023-04-19 00:32:31102#include "gpu/ipc/service/built_in_shader_cache_loader.h"
Robert Sesek3bd6e4b32019-11-26 16:02:27103#include "media/gpu/mac/vt_video_decode_accelerator_mac.h"
Greg Kerr3480aa82018-02-01 00:53:03104#include "sandbox/mac/seatbelt.h"
[email protected]826aab02014-05-14 02:58:59105#endif
106
Alexandre Courbotc13a5972017-07-23 03:48:48107#if BUILDFLAG(USE_VAAPI)
Miguel Casas42e955c2017-12-04 14:18:12108#include "media/gpu/vaapi/vaapi_wrapper.h"
hshi95837052015-05-12 15:39:51109#endif
110
[email protected]eb398192012-10-22 20:16:19111namespace content {
[email protected]ec4bda62013-06-14 15:51:03112
[email protected]6ec3a572012-08-17 02:09:51113namespace {
[email protected]ec4bda62013-06-14 15:51:03114
Xiaohan Wang62737b52022-01-15 18:09:02115#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33116bool StartSandboxLinux(gpu::GpuWatchdogThread*,
117 const gpu::GPUInfo*,
118 const gpu::GpuPreferences&);
Xiaohan Wang62737b52022-01-15 18:09:02119#elif BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54120bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
121#endif
[email protected]ec4bda62013-06-14 15:51:03122
sadrul454af3332016-09-09 18:14:32123class ContentSandboxHelper : public gpu::GpuSandboxHelper {
124 public:
125 ContentSandboxHelper() {}
Peter Boström828b9022021-09-21 02:28:43126
127 ContentSandboxHelper(const ContentSandboxHelper&) = delete;
128 ContentSandboxHelper& operator=(const ContentSandboxHelper&) = delete;
129
sadrul454af3332016-09-09 18:14:32130 ~ContentSandboxHelper() override {}
131
Xiaohan Wang62737b52022-01-15 18:09:02132#if BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32133 void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) {
134 sandbox_info_ = info;
135 }
136#endif
137
sadrul454af3332016-09-09 18:14:32138 private:
139 // SandboxHelper:
Ted Meyer6801a072020-10-28 03:25:02140 void PreSandboxStartup(const gpu::GpuPreferences& gpu_prefs) override {
sadrul454af3332016-09-09 18:14:32141 // Warm up resources that don't need access to GPUInfo.
142 {
143 TRACE_EVENT0("gpu", "Warm up rand");
144 // Warm up the random subsystem, which needs to be done pre-sandbox on all
145 // platforms.
Alex Gough76f652f2023-06-16 14:00:17146#if BUILDFLAG(IS_WIN)
147 sandbox::policy::WarmupRandomnessInfrastructure();
148#else
Avi Drissman9a3ed4e2022-01-26 14:15:37149 std::ignore = base::RandUint64();
Alex Gough76f652f2023-06-16 14:00:17150#endif // BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32151 }
152
Alexandre Courbotc13a5972017-07-23 03:48:48153#if BUILDFLAG(USE_VAAPI)
Xiaohan Wang62737b52022-01-15 18:09:02154#if BUILDFLAG(IS_CHROMEOS)
sadrul454af3332016-09-09 18:14:32155 media::VaapiWrapper::PreSandboxInitialization();
Pilar Molina Lopezfb5103b72021-02-26 23:35:52156#else // For Linux with VA-API support.
Ted Meyer6801a072020-10-28 03:25:02157 if (!gpu_prefs.disable_accelerated_video_decode)
Ted Meyer6f266fd2020-10-23 01:54:06158 media::VaapiWrapper::PreSandboxInitialization();
sadrul454af3332016-09-09 18:14:32159#endif
Ted Meyer6f266fd2020-10-23 01:54:06160#endif // BUILDFLAG(USE_VAAPI)
Xiaohan Wang62737b52022-01-15 18:09:02161#if BUILDFLAG(IS_WIN)
Dale Curtis51f418c2023-06-02 16:31:57162 media::PreSandboxMediaFoundationInitialization();
sadrul454af3332016-09-09 18:14:32163#endif
Eric Karle35050e2017-09-07 01:44:35164
Xiaohan Wang62737b52022-01-15 18:09:02165#if BUILDFLAG(IS_MAC)
Robert Sesek766439c92021-03-23 21:01:31166 {
Robert Sesek3bd6e4b32019-11-26 16:02:27167 TRACE_EVENT0("gpu", "Initialize VideoToolbox");
168 media::InitializeVideoToolbox();
169 }
170#endif
171
Eric Karle35050e2017-09-07 01:44:35172 // On Linux, reading system memory doesn't work through the GPU sandbox.
173 // This value is cached, so access it here to populate the cache.
174 base::SysInfo::AmountOfPhysicalMemory();
sadrul454af3332016-09-09 18:14:32175 }
176
Satyajit Sahu82a76e02017-09-18 14:50:14177 bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33178 const gpu::GPUInfo* gpu_info,
179 const gpu::GpuPreferences& gpu_prefs) override {
Xiaohan Wang62737b52022-01-15 18:09:02180#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33181 return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs);
Xiaohan Wang62737b52022-01-15 18:09:02182#elif BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32183 return StartSandboxWindows(sandbox_info_);
Xiaohan Wang62737b52022-01-15 18:09:02184#elif BUILDFLAG(IS_MAC)
Greg Kerr3480aa82018-02-01 00:53:03185 return sandbox::Seatbelt::IsSandboxed();
sadrul454af3332016-09-09 18:14:32186#else
187 return false;
188#endif
189 }
190
Xiaohan Wang62737b52022-01-15 18:09:02191#if BUILDFLAG(IS_WIN)
Keishi Hattori0e45c022021-11-27 09:25:52192 raw_ptr<const sandbox::SandboxInterfaceInfo> sandbox_info_ = nullptr;
sadrul454af3332016-09-09 18:14:32193#endif
sadrul454af3332016-09-09 18:14:32194};
195
Scott Violet833ff162023-04-19 00:32:31196void LoadMetalShaderCacheIfNecessary() {
197#if BUILDFLAG(IS_MAC)
198 if (base::FeatureList::IsEnabled(features::kUseBuiltInMetalShaderCache)) {
199 gpu::BuiltInShaderCacheLoader::StartLoading();
200 }
201#endif
202}
203
kylechar476993472016-09-14 16:03:48204} // namespace
[email protected]ec4bda62013-06-14 15:51:03205
[email protected]c0fc0942010-01-13 00:55:37206// Main function for starting the Gpu process.
Gabriel Charettefbeeb1c2021-11-10 20:50:06207int GpuMain(MainFunctionParams parameters) {
[email protected]d13f35d2012-05-18 02:28:15208 TRACE_EVENT0("gpu", "GpuMain");
Aman Verma0bf8c112022-11-15 13:01:24209 base::CurrentProcess::GetInstance().SetProcessType(
210 base::CurrentProcessType::PROCESS_GPU);
ssidb2e3ece2015-02-09 16:02:20211 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:39212 kTraceEventGpuProcessSortIndex);
[email protected]d13f35d2012-05-18 02:28:15213
Gabriel Charettefbeeb1c2021-11-10 20:50:06214 const base::CommandLine& command_line = *parameters.command_line;
Zhenyao Mo910beb82017-10-25 03:23:00215
Scott Violet833ff162023-04-19 00:32:31216 // Start this early on as it reads from a file (in the background) and full
217 // startup is gated by this completing.
218 LoadMetalShaderCacheIfNecessary();
219
Zhenyao Mo910beb82017-10-25 03:23:00220 gpu::GpuPreferences gpu_preferences;
221 if (command_line.HasSwitch(switches::kGpuPreferences)) {
222 std::string value =
223 command_line.GetSwitchValueASCII(switches::kGpuPreferences);
Jonathan Backer16cc8fd2018-05-31 19:59:22224 bool success = gpu_preferences.FromSwitchValue(value);
Zhenyao Mo910beb82017-10-25 03:23:00225 CHECK(success);
226 }
227
kylechar60e12ed2021-03-15 15:56:03228 // Disallow sending sync IPCs from the GPU process, in particular CrGpuMain
229 // and VizCompositorThreads. Incoming sync IPCs can be received out of order
230 // when waiting on response to an outgoing sync IPC. Both viz and gpu
231 // interfaces rely on receiving messages in order so this message reordering
232 // would break things.
233 mojo::SyncCallRestrictions::DisallowSyncCall();
234
Zhenyao Mo910beb82017-10-25 03:23:00235 if (gpu_preferences.gpu_startup_dialog)
John Abd-El-Malek884291c2017-08-09 06:43:48236 WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:49237
Sean Maherf36d8122022-08-05 02:33:35238 base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]ca23992b02013-06-13 17:25:19239
Xiaohan Wang62737b52022-01-15 18:09:02240#if BUILDFLAG(IS_WIN)
Bill Carrba4cbb972022-10-10 18:24:11241 base::win::EnableHighDPISupport();
Daniel Libby1700bbe82019-01-30 22:22:36242 base::trace_event::TraceEventETWExport::EnableETWExport();
243
[email protected]52819472013-11-24 22:49:55244 // Prevent Windows from displaying a modal dialog on failures like not being
245 // able to load a DLL.
Benoit Lize7ee77d32021-03-04 17:26:14246 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
247 SEM_NOOPENFILEERRORBOX);
Robert Liao98bb92d2017-06-15 22:20:39248
249 // COM is used by some Windows Media Foundation calls made on this thread and
250 // must be MTA so we don't have to worry about pumping messages to handle
251 // COM callbacks.
252 base::win::ScopedCOMInitializer com_initializer(
253 base::win::ScopedCOMInitializer::kMTA);
Sunny Sachanandani35c727c2019-07-09 13:12:52254
255 if (base::FeatureList::IsEnabled(features::kGpuProcessHighPriorityWin))
Sunny Sachanandani49bdbad2019-07-17 19:15:10256 ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
[email protected]23f46562011-09-07 01:42:39257#endif
[email protected]ec4bda62013-06-14 15:51:03258
Dale Curtis1b6becebb2020-03-30 20:13:35259 // Installs a base::LogMessageHandlerFunction which ensures messages are sent
260 // to the GpuProcessHost once the GpuServiceImpl has started.
261 viz::GpuServiceImpl::InstallPreInitializeLogHandler();
[email protected]23f46562011-09-07 01:42:39262
ericrk1d9e17f2016-11-30 01:51:28263 // We are experiencing what appear to be memory-stomp issues in the GPU
Alex Clarkef7fb8a82019-06-06 15:41:53264 // process. These issues seem to be impacting the task executor and listeners
265 // registered to it. Create the task executor on the heap to guard against
ericrk1d9e17f2016-11-30 01:51:28266 // this.
267 // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802
268 // and crbug.com/609252.
Alex Clarke636e7052019-05-30 10:49:37269 std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor;
skyostil82befc52016-12-19 13:48:59270 std::unique_ptr<ui::PlatformEventSource> event_source;
skyostilb354f882016-12-13 18:42:45271 if (command_line.HasSwitch(switches::kHeadless)) {
Alex Clarke636e7052019-05-30 10:49:37272 main_thread_task_executor =
273 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15274 base::MessagePumpType::DEFAULT);
skyostilb354f882016-12-13 18:42:45275 } else {
Xiaohan Wang62737b52022-01-15 18:09:02276#if BUILDFLAG(IS_WIN)
Robert Liao98bb92d2017-06-15 22:20:39277 // The GpuMain thread should not be pumping Windows messages because no UI
278 // is expected to run on this thread.
Alex Clarke636e7052019-05-30 10:49:37279 main_thread_task_executor =
280 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15281 base::MessagePumpType::DEFAULT);
kylechar2d463872022-11-02 16:14:30282#elif BUILDFLAG(IS_OZONE)
Alex Clarke636e7052019-05-30 10:49:37283 // The MessagePump type required depends on the Ozone platform selected at
tonikitoofb807b102017-02-08 19:52:03284 // runtime.
Maksim Sisov459eada2020-06-25 08:16:31285 if (!main_thread_task_executor) {
286 main_thread_task_executor =
287 std::make_unique<base::SingleThreadTaskExecutor>(
288 gpu_preferences.message_pump_type);
289 }
Xiaohan Wang62737b52022-01-15 18:09:02290#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
kylechar476993472016-09-14 16:03:48291#error "Unsupported Linux platform."
Xiaohan Wang62737b52022-01-15 18:09:02292#elif BUILDFLAG(IS_MAC)
Christopher Cameron1732f2b02017-11-17 10:56:50293 // Cross-process CoreAnimation requires a CFRunLoop to function at all, and
294 // requires a NSRunLoop to not starve under heavy load. See:
295 // https://crbug.com/312462#c51 and https://crbug.com/783298
Alex Clarke636e7052019-05-30 10:49:37296 main_thread_task_executor =
297 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15298 base::MessagePumpType::NS_RUNLOOP);
Etienne Pierre-doray2163f3012020-04-02 21:37:14299 // As part of the migration to DoWork(), this policy is required to keep
Etienne Pierre-doray9eb4f5a2020-01-15 16:29:24300 // previous behavior and avoid regressions.
301 // TODO(crbug.com/1041853): Consider updating the policy.
302 main_thread_task_executor->SetWorkBatchSize(2);
[email protected]826aab02014-05-14 02:58:59303#else
Alex Clarke636e7052019-05-30 10:49:37304 main_thread_task_executor =
305 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15306 base::MessagePumpType::DEFAULT);
[email protected]db6101db2012-10-25 15:20:08307#endif
skyostilb354f882016-12-13 18:42:45308 }
[email protected]db6101db2012-10-25 15:20:08309
[email protected]db6101db2012-10-25 15:20:08310 base::PlatformThread::SetName("CrGpuMain");
311
Le Hoang Quyend6ceade62022-05-20 07:13:10312 // Set thread priority before sandbox initialization.
Zhibo Wangd9e4a002022-07-07 04:34:59313 if (!features::IsGpuMainThreadForcedToNormalPriorityDrDc()) {
314 base::PlatformThread::SetCurrentThreadType(base::ThreadType::kCompositing);
Sunny Sachanandani5cd10e962019-06-13 22:55:32315 }
revemane7acf842016-02-05 08:24:32316
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57317 auto gpu_init = std::make_unique<gpu::GpuInit>();
sadrul454af3332016-09-09 18:14:32318 ContentSandboxHelper sandbox_helper;
Xiaohan Wang62737b52022-01-15 18:09:02319#if BUILDFLAG(IS_WIN)
sadrul454af3332016-09-09 18:14:32320 sandbox_helper.set_sandbox_info(parameters.sandbox_info);
[email protected]af7c5d92014-02-03 19:53:15321#endif
[email protected]af7c5d92014-02-03 19:53:15322
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57323 gpu_init->set_sandbox_helper(&sandbox_helper);
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24324
Gabriel Charettec7e363c62020-06-24 17:39:35325 // Since GPU initialization calls into skia, it's important to initialize skia
Khushal1d055592018-07-28 02:00:39326 // before it.
327 InitializeSkia();
328
Joe Mason94bebf12022-06-03 15:03:53329 // The ThreadPool must have been created before invoking |gpu_init| as it
330 // needs the ThreadPool (in angle::InitializePlatform()). Do not start it
331 // until after the sandbox is initialized however to avoid creating threads
332 // outside the sandbox.
333 DCHECK(base::ThreadPoolInstance::Get());
Gabriel Charettec7e363c62020-06-24 17:39:35334
sadrul454af3332016-09-09 18:14:32335 // Gpu initialization may fail for various reasons, in which case we will need
336 // to tear down this process. However, we can not do so safely until the IPC
337 // channel is set up, because the detection of early return of a child process
338 // is implemented using an IPC channel error. If the IPC channel is not fully
339 // set up between the browser and GPU process, and the GPU process crashes or
340 // exits early, the browser process will never detect it. For this reason we
sadrul72aae8a2017-01-24 04:52:32341 // defer tearing down the GPU process until receiving the initialization
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:06342 // message from the browser (through mojom::VizMain::CreateGpuService()).
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57343 const bool init_success = gpu_init->InitializeAndStartSandbox(
Zhenyao Moe23f75262018-02-07 02:15:00344 const_cast<base::CommandLine*>(&command_line), gpu_preferences);
sadrul454af3332016-09-09 18:14:32345 const bool dead_on_arrival = !init_success;
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24346
Ian Barkley-Yeung48418c12022-08-16 01:10:16347 auto* client = GetContentClient()->gpu();
348 if (client) {
349 client->PostSandboxInitialized();
350 }
351
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57352 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
[email protected]ec4bda62013-06-14 15:51:03353
Zhibo Wangd9e4a002022-07-07 04:34:59354 base::ThreadType io_thread_type = base::ThreadType::kCompositing;
Xiaohan Wang62737b52022-01-15 18:09:02355#if BUILDFLAG(IS_MAC)
behdad8b57eaad2019-07-18 13:58:48356 // Increase the thread priority to get more reliable values in performance
357 // test of mac_os.
Joe Mason94bebf12022-06-03 15:03:53358 if (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests))
Zhibo Wangd9e4a002022-07-07 04:34:59359 io_thread_type = base::ThreadType::kRealtimeAudio;
behdad8b57eaad2019-07-18 13:58:48360#endif
Joe Mason94bebf12022-06-03 15:03:53361 // ChildProcess will start the ThreadPoolInstance now that the sandbox is
362 // initialized.
Zhibo Wangd9e4a002022-07-07 04:34:59363 ChildProcess gpu_process(io_thread_type);
Joe Mason94bebf12022-06-03 15:03:53364 DCHECK(base::ThreadPoolInstance::Get()->WasStarted());
Xi Chengd6390812018-01-24 00:01:50365
Ian Barkley-Yeung48418c12022-08-16 01:10:16366 if (client) {
Xi Chengd6390812018-01-24 00:01:50367 client->PostIOThreadCreated(gpu_process.io_task_runner());
Ian Barkley-Yeung48418c12022-08-16 01:10:16368 }
Xi Chengd6390812018-01-24 00:01:50369
Wez6979109b2018-09-07 17:30:56370 base::RunLoop run_loop;
371 GpuChildThread* child_thread =
Dale Curtis1b6becebb2020-03-30 20:13:35372 new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init));
[email protected]7a31f7c2011-03-21 23:22:04373 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17374
[email protected]7a31f7c2011-03-21 23:22:04375 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44376
Matthew Denton487e8772023-02-17 23:21:49377 // Mojo IPC support is brought up by GpuChildThread, so startup tracing is
378 // enabled here if it needs to start after mojo init (normally so the mojo
379 // broker can bypass the sandbox to allocate startup tracing's SMB).
380 if (parameters.needs_startup_tracing_after_mojo_init) {
Dale Curtis12cfe022020-02-21 01:18:42381 tracing::EnableStartupTracingIfNeeded();
Matthew Denton487e8772023-02-17 23:21:49382 }
Dale Curtis12cfe022020-02-21 01:18:42383
Xiaohan Wang62737b52022-01-15 18:09:02384#if BUILDFLAG(IS_MAC)
Kai Ninomiyae2c55582019-12-19 23:51:03385 // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser
386 // process, meaning it is "responsible" for making sure it and its
387 // subordinate processes (i.e. the GPU process) drop references to the
388 // external GPU. Despite this, the system still sends the device removal
389 // notifications to the GPU process, so the GPU process handles its own
390 // graceful shutdown without help from the browser process.
391 //
392 // Using the "SafeEjectGPU" tool, we can see that when the browser process
393 // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject
394 // actions apply to the responsible process, who in turn deals with
395 // subordinates to eliminate their ejecting eGPU references" [man 8
396 // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU
397 // process exits, it appears that the browser process is no longer considered
398 // to be using the GPU, so it "succeeds" the 'wait'.
399 metal::RegisterGracefulExitOnDeviceRemoval();
Christopher Cameron21c4abc2019-11-15 04:06:15400#endif
401
Xiaohan Wang62737b52022-01-15 18:09:02402#if BUILDFLAG(IS_ANDROID)
primianob3fb6412015-10-14 16:03:51403 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40404 tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics",
405 nullptr);
Benoit Lizeb6fa4ac2023-02-13 11:59:43406
407 base::android::MeminfoDumpProvider::Initialize();
primianob3fb6412015-10-14 16:03:51408#endif
409
Arthur Sonzognia570236352022-12-19 13:18:54410 base::allocator::PartitionAllocSupport::Get()->ReconfigureAfterTaskRunnerInit(
Benoit Lize7ee77d32021-03-04 17:26:14411 switches::kGpuProcess);
412
stanisc61507092017-07-06 16:36:58413 base::HighResolutionTimerManager hi_res_timer_manager;
414
[email protected]d13f35d2012-05-18 02:28:15415 {
416 TRACE_EVENT0("gpu", "Run Message Loop");
Wez6979109b2018-09-07 17:30:56417 run_loop.Run();
[email protected]d13f35d2012-05-18 02:28:15418 }
[email protected]c0fc0942010-01-13 00:55:37419
jbaumana19f1df2017-01-18 03:01:17420 return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0;
[email protected]c0fc0942010-01-13 00:55:37421}
[email protected]6ec3a572012-08-17 02:09:51422
423namespace {
424
Xiaohan Wang62737b52022-01-15 18:09:02425#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Satyajit Sahu82a76e02017-09-18 14:50:14426bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33427 const gpu::GPUInfo* gpu_info,
428 const gpu::GpuPreferences& gpu_prefs) {
fdorayf30bf3a2015-10-28 21:47:00429 TRACE_EVENT0("gpu,startup", "Initialize sandbox");
[email protected]663c4b32013-04-18 05:52:54430
[email protected]f330b762014-02-08 04:52:46431 if (watchdog_thread) {
Tom Sepez437e2202017-10-24 21:26:47432 // SandboxLinux needs to be able to ensure that the thread
[email protected]f330b762014-02-08 04:52:46433 // has really been stopped.
Robert Sesek7d0b49b2020-07-08 18:31:27434 sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread);
[email protected]f330b762014-02-08 04:52:46435 }
[email protected]655abd522014-06-02 15:23:43436
Tom Sepez437e2202017-10-24 21:26:47437 // SandboxLinux::InitializeSandbox() must always be called
[email protected]663c4b32013-04-18 05:52:54438 // with only one thread.
Robert Sesek7d0b49b2020-07-08 18:31:27439 sandbox::policy::SandboxLinux::Options sandbox_options;
Dominik Behr1ce8bff2022-07-13 00:54:11440 if (gpu_info) {
441 // We have to enable sandbox settings for all GPUs in the system
442 // for Chrome to be able to access/use them.
443 sandbox_options.use_amd_specific_policies =
444 angle::IsAMD(gpu_info->active_gpu().vendor_id);
445 sandbox_options.use_intel_specific_policies =
446 angle::IsIntel(gpu_info->active_gpu().vendor_id);
Kramer Ge545ba6d2023-01-17 17:14:48447 sandbox_options.use_virtio_specific_policies =
448 angle::IsVirtIO(gpu_info->active_gpu().vendor_id);
Dominik Behr1ce8bff2022-07-13 00:54:11449 sandbox_options.use_nvidia_specific_policies =
450 angle::IsNVIDIA(gpu_info->active_gpu().vendor_id);
451 for (const auto& gpu : gpu_info->secondary_gpus) {
452 if (angle::IsAMD(gpu.vendor_id))
453 sandbox_options.use_amd_specific_policies = true;
454 else if (angle::IsIntel(gpu.vendor_id))
455 sandbox_options.use_intel_specific_policies = true;
456 else if (angle::IsNVIDIA(gpu.vendor_id))
457 sandbox_options.use_nvidia_specific_policies = true;
458 }
459 }
Zhenyao Moe9187a862017-10-20 04:26:33460 sandbox_options.accelerated_video_decode_enabled =
461 !gpu_prefs.disable_accelerated_video_decode;
Sheng-Hao Tsaoea6aa852018-01-03 06:48:16462 sandbox_options.accelerated_video_encode_enabled =
463 !gpu_prefs.disable_accelerated_video_encode;
Tom Sepez085507ab2017-10-18 22:36:00464
Yaowei Zhoudbaba1b2023-05-24 05:23:55465#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
Peter McNeeley114d30182023-02-17 22:09:10466 // Video decoding of many video streams can use thousands of FDs as well as
467 // Exo clients like Lacros.
468 // See https://crbug.com/1417237
Jeff Chenfce90f32022-01-31 19:49:34469 const auto current_max_fds =
470 base::saturated_cast<unsigned int>(base::GetMaxFds());
Peter McNeeley114d30182023-02-17 22:09:10471 constexpr unsigned int kMaxFDsDelta = 1u << 13;
Jeff Chenfce90f32022-01-31 19:49:34472 const auto new_max_fds =
Peter McNeeley114d30182023-02-17 22:09:10473 static_cast<unsigned int>(base::ClampMax(current_max_fds, kMaxFDsDelta));
Hirokazu Honda53243142023-01-25 01:45:52474 base::IncreaseFdLimitTo(new_max_fds);
Jeff Chenfce90f32022-01-31 19:49:34475#endif
476
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) {
Etienne Pierre-dorayef5dcca2023-07-11 16:46:27483 watchdog_thread->Start();
[email protected]f330b762014-02-08 04:52:46484 }
[email protected]663c4b32013-04-18 05:52:54485
486 return res;
487}
Xiaohan Wang62737b52022-01-15 18:09:02488#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]663c4b32013-04-18 05:52:54489
Xiaohan Wang62737b52022-01-15 18:09:02490#if BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54491bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
fdorayf30bf3a2015-10-28 21:47:00492 TRACE_EVENT0("gpu,startup", "Lower token");
[email protected]663c4b32013-04-18 05:52:54493
494 // For Windows, if the target_services interface is not zero, the process
495 // is sandboxed and we must call LowerToken() before rendering untrusted
496 // content.
497 sandbox::TargetServices* target_services = sandbox_info->target_services;
498 if (target_services) {
499 target_services->LowerToken();
500 return true;
501 }
502
503 return false;
504}
Xiaohan Wang62737b52022-01-15 18:09:02505#endif // BUILDFLAG(IS_WIN)
[email protected]663c4b32013-04-18 05:52:54506
[email protected]6ec3a572012-08-17 02:09:51507} // namespace.
508
[email protected]eb398192012-10-22 20:16:19509} // namespace content