Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 5 | #include <stddef.h> |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 8 | #include <memory> |
Avi Drissman | 9a3ed4e | 2022-01-26 14:15:37 | [diff] [blame] | 9 | #include <tuple> |
dcheng | f26eed3 | 2016-01-13 10:58:14 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Arthur Sonzogni | a57023635 | 2022-12-19 13:18:54 | [diff] [blame] | 12 | #include "base/allocator/partition_alloc_support.h" |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 13 | #include "base/check.h" |
Gabriel Charette | f385133 | 2022-04-28 23:39:18 | [diff] [blame] | 14 | #include "base/command_line.h" |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 15 | #include "base/feature_list.h" |
Zhuang Xu | e24dfc3 | 2025-05-26 02:45:05 | [diff] [blame] | 16 | #include "base/files/scoped_file.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 17 | #include "base/functional/bind.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 18 | #include "base/memory/raw_ptr.h" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 19 | #include "base/message_loop/message_pump_type.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros.h" |
Jeff Chen | fce90f3 | 2022-01-31 19:49:34 | [diff] [blame] | 21 | #include "base/numerics/clamped_math.h" |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 22 | #include "base/process/current_process.h" |
Jeff Chen | fce90f3 | 2022-01-31 19:49:34 | [diff] [blame] | 23 | #include "base/process/process_metrics.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 24 | #include "base/rand_util.h" |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame] | 25 | #include "base/run_loop.h" |
[email protected] | d0ea478 | 2013-06-11 04:58:24 | [diff] [blame] | 26 | #include "base/strings/string_number_conversions.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 27 | #include "base/system/sys_info.h" |
henrika | 3cee59b | 2024-05-03 16:20:52 | [diff] [blame] | 28 | #include "base/task/current_thread.h" |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 29 | #include "base/task/single_thread_task_executor.h" |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 30 | #include "base/task/thread_pool/thread_pool_instance.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 31 | #include "base/threading/platform_thread.h" |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 32 | #include "base/threading/thread.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 33 | #include "base/time/time.h" |
stanisc | 6150709 | 2017-07-06 16:36:58 | [diff] [blame] | 34 | #include "base/timer/hi_res_timer_manager.h" |
primiano | 50b7444c | 2015-01-28 04:17:00 | [diff] [blame] | 35 | #include "base/trace_event/trace_event.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 36 | #include "build/build_config.h" |
Zhuang Xu | e24dfc3 | 2025-05-26 02:45:05 | [diff] [blame] | 37 | #include "components/viz/service/gl/gpu_log_message_manager.h" |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 38 | #include "components/viz/service/main/viz_main_impl.h" |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 39 | #include "content/child/child_process.h" |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 40 | #include "content/common/content_constants_internal.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 41 | #include "content/common/content_switches_internal.h" |
Matthew Denton | 66b2fa5 | 2023-10-17 00:54:22 | [diff] [blame] | 42 | #include "content/common/features.h" |
Khushal | a4e236f | 2018-06-01 03:00:46 | [diff] [blame] | 43 | #include "content/common/skia_utils.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 44 | #include "content/gpu/gpu_child_thread.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 45 | #include "content/public/common/content_client.h" |
Shintaro Kawamura | 65a3306 | 2024-03-28 03:40:08 | [diff] [blame] | 46 | #include "content/public/common/content_features.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 47 | #include "content/public/common/content_switches.h" |
| 48 | #include "content/public/common/main_function_params.h" |
jbauman | a19f1df | 2017-01-18 03:01:17 | [diff] [blame] | 49 | #include "content/public/common/result_codes.h" |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 50 | #include "content/public/common/zygote/zygote_buildflags.h" |
Zhenyao Mo | c76e903 | 2018-01-19 21:15:46 | [diff] [blame] | 51 | #include "content/public/gpu/content_gpu_client.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame] | 52 | #include "gpu/command_buffer/service/gpu_switches.h" |
sadrul | ed39592 | 2016-09-07 17:32:58 | [diff] [blame] | 53 | #include "gpu/config/gpu_driver_bug_list.h" |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 54 | #include "gpu/config/gpu_finch_features.h" |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 55 | #include "gpu/config/gpu_info_collector.h" |
Jonathan Backer | 16cc8fd | 2018-05-31 19:59:22 | [diff] [blame] | 56 | #include "gpu/config/gpu_preferences.h" |
tfarina | 15525c4 | 2015-04-28 19:04:16 | [diff] [blame] | 57 | #include "gpu/config/gpu_switches.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame] | 58 | #include "gpu/config/gpu_util.h" |
markdittmer | d88b835 | 2016-04-08 15:28:45 | [diff] [blame] | 59 | #include "gpu/ipc/service/gpu_config.h" |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 60 | #include "gpu/ipc/service/gpu_init.h" |
sadrul | 2fb7e15 | 2016-08-30 05:21:45 | [diff] [blame] | 61 | #include "gpu/ipc/service/gpu_watchdog_thread.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 62 | #include "media/gpu/buildflags.h" |
Michael Thiessen | dd70b61 | 2024-08-17 18:25:25 | [diff] [blame] | 63 | #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
kylechar | 60e12ed | 2021-03-15 15:56:03 | [diff] [blame] | 64 | #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 65 | #include "services/tracing/public/cpp/perfetto/perfetto_traced_process.h" |
Dale Curtis | 12cfe02 | 2020-02-21 01:18:42 | [diff] [blame] | 66 | #include "services/tracing/public/cpp/trace_startup.h" |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 67 | #include "services/tracing/public/cpp/trace_startup_config.h" |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 68 | #include "third_party/angle/src/gpu_info_util/SystemInfo.h" |
Maksim Sisov | 459eada | 2020-06-25 08:16:31 | [diff] [blame] | 69 | #include "ui/base/ui_base_features.h" |
[email protected] | c093918 | 2014-05-24 00:09:19 | [diff] [blame] | 70 | #include "ui/events/platform/platform_event_source.h" |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 71 | #include "ui/gfx/switches.h" |
martina.kollarova | a34211d | 2015-06-25 11:49:07 | [diff] [blame] | 72 | #include "ui/gl/gl_context.h" |
Scott Violet | 833ff16 | 2023-04-19 00:32:31 | [diff] [blame] | 73 | #include "ui/gl/gl_features.h" |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 74 | #include "ui/gl/gl_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 75 | #include "ui/gl/gl_surface.h" |
| 76 | #include "ui/gl/gl_switches.h" |
[email protected] | 1bb06b0 | 2012-09-23 19:37:24 | [diff] [blame] | 77 | #include "ui/gl/gpu_switching_manager.h" |
kylechar | 5b9dec1 | 2016-05-16 15:40:57 | [diff] [blame] | 78 | #include "ui/gl/init/gl_factory.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 79 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 80 | #if BUILDFLAG(IS_WIN) |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 81 | #include <windows.h> |
Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 82 | |
| 83 | #include <dwmapi.h> |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 84 | #endif |
| 85 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 86 | #if BUILDFLAG(IS_ANDROID) |
Benoit Lize | b6fa4ac | 2023-02-13 11:59:43 | [diff] [blame] | 87 | #include "base/android/meminfo_dump_provider.h" |
Ben Scarlato | 5ece30c7 | 2025-05-09 22:13:20 | [diff] [blame] | 88 | #include "base/posix/eintr_wrapper.h" |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 89 | #include "base/trace_event/memory_dump_manager.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 90 | #include "components/tracing/common/graphics_memory_dump_provider_android.h" |
Ben Scarlato | 5ece30c7 | 2025-05-09 22:13:20 | [diff] [blame] | 91 | #include "sandbox/linux/services/thread_helpers.h" // nogncheck |
| 92 | #include "sandbox/policy/features.h" |
| 93 | #include "sandbox/policy/linux/landlock_gpu_policy_android.h" |
| 94 | #include "sandbox/policy/sandbox_type.h" |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 95 | #endif |
| 96 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 97 | #if BUILDFLAG(IS_WIN) |
[email protected] | 5f7e451 | 2012-10-01 20:51:37 | [diff] [blame] | 98 | #include "base/win/scoped_com_initializer.h" |
Bill Carr | ba4cbb97 | 2022-10-10 18:24:11 | [diff] [blame] | 99 | #include "base/win/win_util.h" |
kylechar | 5b9dec1 | 2016-05-16 15:40:57 | [diff] [blame] | 100 | #include "base/win/windows_version.h" |
Dale Curtis | 56f208fc | 2023-04-07 18:55:25 | [diff] [blame] | 101 | #include "media/base/win/mf_initializer.h" |
Alex Gough | 76f652f | 2023-06-16 14:00:17 | [diff] [blame] | 102 | #include "sandbox/policy/win/sandbox_warmup.h" |
[email protected] | 18149178 | 2012-07-18 00:59:15 | [diff] [blame] | 103 | #include "sandbox/win/src/sandbox.h" |
[email protected] | 802a13a0 | 2010-12-02 01:48:37 | [diff] [blame] | 104 | #endif |
| 105 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 106 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Matthew Denton | 66b2fa5 | 2023-10-17 00:54:22 | [diff] [blame] | 107 | #include "content/child/sandboxed_process_thread_type_handler.h" |
Ken Rockot | 9c9d3da | 2023-11-28 22:58:32 | [diff] [blame] | 108 | #include "content/common/gpu_pre_sandbox_hook_linux.h" |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 109 | #include "sandbox/policy/linux/sandbox_linux.h" |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 110 | #include "sandbox/policy/sandbox_type.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 111 | #endif |
| 112 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 113 | #if BUILDFLAG(IS_MAC) |
Avi Drissman | 5d32f18 | 2023-08-16 15:26:39 | [diff] [blame] | 114 | #include "base/message_loop/message_pump_apple.h" |
Kai Ninomiya | e2c5558 | 2019-12-19 23:51:03 | [diff] [blame] | 115 | #include "components/metal_util/device_removal.h" |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 116 | #include "sandbox/mac/seatbelt.h" |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 117 | #endif |
| 118 | |
Alexandre Courbot | c13a597 | 2017-07-23 03:48:48 | [diff] [blame] | 119 | #if BUILDFLAG(USE_VAAPI) |
Miguel Casas | 42e955c | 2017-12-04 14:18:12 | [diff] [blame] | 120 | #include "media/gpu/vaapi/vaapi_wrapper.h" |
hshi | 9583705 | 2015-05-12 15:39:51 | [diff] [blame] | 121 | #endif |
| 122 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 123 | namespace content { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 124 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 125 | namespace { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 126 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 127 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 128 | bool StartSandboxLinux(gpu::GpuWatchdogThread*, |
| 129 | const gpu::GPUInfo*, |
| 130 | const gpu::GpuPreferences&); |
Ben Scarlato | 5ece30c7 | 2025-05-09 22:13:20 | [diff] [blame] | 131 | #elif BUILDFLAG(IS_ANDROID) |
| 132 | bool StartSandboxAndroid(gpu::GpuWatchdogThread*); |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 133 | #elif BUILDFLAG(IS_WIN) |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 134 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*); |
| 135 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 136 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 137 | class ContentSandboxHelper : public gpu::GpuSandboxHelper { |
| 138 | public: |
| 139 | ContentSandboxHelper() {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 140 | |
| 141 | ContentSandboxHelper(const ContentSandboxHelper&) = delete; |
| 142 | ContentSandboxHelper& operator=(const ContentSandboxHelper&) = delete; |
| 143 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 144 | ~ContentSandboxHelper() override {} |
| 145 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 146 | #if BUILDFLAG(IS_WIN) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 147 | void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) { |
| 148 | sandbox_info_ = info; |
| 149 | } |
| 150 | #endif |
| 151 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 152 | private: |
| 153 | // SandboxHelper: |
Ted Meyer | 6801a07 | 2020-10-28 03:25:02 | [diff] [blame] | 154 | void PreSandboxStartup(const gpu::GpuPreferences& gpu_prefs) override { |
Kramer Ge | 0de97b15 | 2025-06-05 16:28:55 | [diff] [blame] | 155 | TRACE_EVENT("gpu,startup", "gpu_main::PreSandboxStartup"); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 156 | // Warm up resources that don't need access to GPUInfo. |
| 157 | { |
| 158 | TRACE_EVENT0("gpu", "Warm up rand"); |
| 159 | // Warm up the random subsystem, which needs to be done pre-sandbox on all |
| 160 | // platforms. |
Alex Gough | 76f652f | 2023-06-16 14:00:17 | [diff] [blame] | 161 | #if BUILDFLAG(IS_WIN) |
| 162 | sandbox::policy::WarmupRandomnessInfrastructure(); |
| 163 | #else |
Avi Drissman | 9a3ed4e | 2022-01-26 14:15:37 | [diff] [blame] | 164 | std::ignore = base::RandUint64(); |
Alex Gough | 76f652f | 2023-06-16 14:00:17 | [diff] [blame] | 165 | #endif // BUILDFLAG(IS_WIN) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 166 | } |
| 167 | |
Alexandre Courbot | c13a597 | 2017-07-23 03:48:48 | [diff] [blame] | 168 | #if BUILDFLAG(USE_VAAPI) |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 169 | #if BUILDFLAG(IS_CHROMEOS) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 170 | media::VaapiWrapper::PreSandboxInitialization(); |
Pilar Molina Lopez | fb5103b7 | 2021-02-26 23:35:52 | [diff] [blame] | 171 | #else // For Linux with VA-API support. |
Ted Meyer | 6801a07 | 2020-10-28 03:25:02 | [diff] [blame] | 172 | if (!gpu_prefs.disable_accelerated_video_decode) |
Ted Meyer | 6f266fd | 2020-10-23 01:54:06 | [diff] [blame] | 173 | media::VaapiWrapper::PreSandboxInitialization(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 174 | #endif |
Ted Meyer | 6f266fd | 2020-10-23 01:54:06 | [diff] [blame] | 175 | #endif // BUILDFLAG(USE_VAAPI) |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 176 | #if BUILDFLAG(IS_WIN) |
Dale Curtis | 51f418c | 2023-06-02 16:31:57 | [diff] [blame] | 177 | media::PreSandboxMediaFoundationInitialization(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 178 | #endif |
Eric Karl | e35050e | 2017-09-07 01:44:35 | [diff] [blame] | 179 | |
| 180 | // On Linux, reading system memory doesn't work through the GPU sandbox. |
| 181 | // This value is cached, so access it here to populate the cache. |
| 182 | base::SysInfo::AmountOfPhysicalMemory(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 183 | } |
| 184 | |
Satyajit Sahu | 82a76e0 | 2017-09-18 14:50:14 | [diff] [blame] | 185 | bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread, |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 186 | const gpu::GPUInfo* gpu_info, |
| 187 | const gpu::GpuPreferences& gpu_prefs) override { |
Kramer Ge | 0de97b15 | 2025-06-05 16:28:55 | [diff] [blame] | 188 | TRACE_EVENT("gpu,startup", "gpu_main::EnsureSandboxInitialized"); |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 189 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 190 | return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs); |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 191 | #elif BUILDFLAG(IS_WIN) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 192 | return StartSandboxWindows(sandbox_info_); |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 193 | #elif BUILDFLAG(IS_MAC) |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 194 | return sandbox::Seatbelt::IsSandboxed(); |
Ben Scarlato | 5ece30c7 | 2025-05-09 22:13:20 | [diff] [blame] | 195 | #elif BUILDFLAG(IS_ANDROID) |
| 196 | if (base::FeatureList::IsEnabled( |
| 197 | sandbox::policy::features::kAndroidGpuSandbox)) { |
| 198 | return StartSandboxAndroid(watchdog_thread); |
| 199 | } |
| 200 | return false; |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 201 | #else |
| 202 | return false; |
| 203 | #endif |
| 204 | } |
| 205 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 206 | #if BUILDFLAG(IS_WIN) |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 207 | raw_ptr<const sandbox::SandboxInterfaceInfo> sandbox_info_ = nullptr; |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 208 | #endif |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 209 | }; |
| 210 | |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 211 | } // namespace |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 212 | |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 213 | // Main function for starting the Gpu process. |
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 214 | int GpuMain(MainFunctionParams parameters) { |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 215 | TRACE_EVENT("gpu,startup", "GpuMain"); |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 216 | |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 217 | base::CurrentProcess::GetInstance().SetProcessType( |
| 218 | base::CurrentProcessType::PROCESS_GPU); |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 219 | |
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 220 | const base::CommandLine& command_line = *parameters.command_line; |
Zhenyao Mo | 910beb8 | 2017-10-25 03:23:00 | [diff] [blame] | 221 | |
| 222 | gpu::GpuPreferences gpu_preferences; |
| 223 | if (command_line.HasSwitch(switches::kGpuPreferences)) { |
| 224 | std::string value = |
| 225 | command_line.GetSwitchValueASCII(switches::kGpuPreferences); |
Jonathan Backer | 16cc8fd | 2018-05-31 19:59:22 | [diff] [blame] | 226 | bool success = gpu_preferences.FromSwitchValue(value); |
Zhenyao Mo | 910beb8 | 2017-10-25 03:23:00 | [diff] [blame] | 227 | CHECK(success); |
| 228 | } |
| 229 | |
kylechar | 60e12ed | 2021-03-15 15:56:03 | [diff] [blame] | 230 | // Disallow sending sync IPCs from the GPU process, in particular CrGpuMain |
| 231 | // and VizCompositorThreads. Incoming sync IPCs can be received out of order |
| 232 | // when waiting on response to an outgoing sync IPC. Both viz and gpu |
| 233 | // interfaces rely on receiving messages in order so this message reordering |
| 234 | // would break things. |
| 235 | mojo::SyncCallRestrictions::DisallowSyncCall(); |
| 236 | |
Zhenyao Mo | 910beb8 | 2017-10-25 03:23:00 | [diff] [blame] | 237 | if (gpu_preferences.gpu_startup_dialog) |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 238 | WaitForDebugger("Gpu"); |
[email protected] | 6b889fb | 2010-03-23 20:09:49 | [diff] [blame] | 239 | |
Sean Maher | f36d812 | 2022-08-05 02:33:35 | [diff] [blame] | 240 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 241 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 242 | #if BUILDFLAG(IS_WIN) |
Bill Carr | ba4cbb97 | 2022-10-10 18:24:11 | [diff] [blame] | 243 | base::win::EnableHighDPISupport(); |
Daniel Libby | 1700bbe8 | 2019-01-30 22:22:36 | [diff] [blame] | 244 | |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 245 | // Prevent Windows from displaying a modal dialog on failures like not being |
| 246 | // able to load a DLL. |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 247 | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | |
| 248 | SEM_NOOPENFILEERRORBOX); |
Robert Liao | 98bb92d | 2017-06-15 22:20:39 | [diff] [blame] | 249 | |
Chris Davis | 3360a37 | 2025-02-06 23:21:30 | [diff] [blame] | 250 | // Disable high resolution timer throttling to prevent the OS from degrading |
| 251 | // performance. |
| 252 | base::win::SetProcessTimerThrottleState( |
| 253 | base::GetCurrentProcessHandle(), base::win::ProcessPowerState::kDisabled); |
| 254 | |
Robert Liao | 98bb92d | 2017-06-15 22:20:39 | [diff] [blame] | 255 | // COM is used by some Windows Media Foundation calls made on this thread and |
| 256 | // must be MTA so we don't have to worry about pumping messages to handle |
| 257 | // COM callbacks. |
| 258 | base::win::ScopedCOMInitializer com_initializer( |
| 259 | base::win::ScopedCOMInitializer::kMTA); |
Sunny Sachanandani | 35c727c | 2019-07-09 13:12:52 | [diff] [blame] | 260 | |
Zhenyao Mo | dca79a2 | 2024-07-16 01:11:59 | [diff] [blame] | 261 | // A higher priority class is used for the GPU process so that it remains at |
| 262 | // a higher priority than renderer processes. |
| 263 | ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 264 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 265 | |
Dale Curtis | 1b6becebb | 2020-03-30 20:13:35 | [diff] [blame] | 266 | // Installs a base::LogMessageHandlerFunction which ensures messages are sent |
| 267 | // to the GpuProcessHost once the GpuServiceImpl has started. |
Zhuang Xu | e24dfc3 | 2025-05-26 02:45:05 | [diff] [blame] | 268 | viz::GpuLogMessageManager::GetInstance()->InstallPreInitializeLogHandler(); |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 269 | |
ericrk | 1d9e17f | 2016-11-30 01:51:28 | [diff] [blame] | 270 | // We are experiencing what appear to be memory-stomp issues in the GPU |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 271 | // process. These issues seem to be impacting the task executor and listeners |
| 272 | // registered to it. Create the task executor on the heap to guard against |
ericrk | 1d9e17f | 2016-11-30 01:51:28 | [diff] [blame] | 273 | // this. |
| 274 | // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802 |
| 275 | // and crbug.com/609252. |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 276 | std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor; |
skyostil | 82befc5 | 2016-12-19 13:48:59 | [diff] [blame] | 277 | std::unique_ptr<ui::PlatformEventSource> event_source; |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 278 | if (command_line.HasSwitch(switches::kHeadless)) { |
Maggie Chen | b539c3b | 2024-10-01 20:08:37 | [diff] [blame] | 279 | #if BUILDFLAG(IS_MAC) |
| 280 | // CADisplayLink (Mac HW VSync) callback only works with NS_RUNLOOP. |
| 281 | main_thread_task_executor = |
| 282 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 283 | base::MessagePumpType::NS_RUNLOOP); |
| 284 | main_thread_task_executor->SetWorkBatchSize(2); |
| 285 | #else |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 286 | main_thread_task_executor = |
| 287 | std::make_unique<base::SingleThreadTaskExecutor>( |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 288 | base::MessagePumpType::DEFAULT); |
Maggie Chen | b539c3b | 2024-10-01 20:08:37 | [diff] [blame] | 289 | #endif |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 290 | } else { |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 291 | #if BUILDFLAG(IS_WIN) |
Robert Liao | 98bb92d | 2017-06-15 22:20:39 | [diff] [blame] | 292 | // The GpuMain thread should not be pumping Windows messages because no UI |
| 293 | // is expected to run on this thread. |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 294 | main_thread_task_executor = |
| 295 | std::make_unique<base::SingleThreadTaskExecutor>( |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 296 | base::MessagePumpType::DEFAULT); |
kylechar | 2d46387 | 2022-11-02 16:14:30 | [diff] [blame] | 297 | #elif BUILDFLAG(IS_OZONE) |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 298 | // The MessagePump type required depends on the Ozone platform selected at |
tonikitoo | fb807b10 | 2017-02-08 19:52:03 | [diff] [blame] | 299 | // runtime. |
Maksim Sisov | 459eada | 2020-06-25 08:16:31 | [diff] [blame] | 300 | if (!main_thread_task_executor) { |
| 301 | main_thread_task_executor = |
| 302 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 303 | gpu_preferences.message_pump_type); |
| 304 | } |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 305 | #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 306 | #error "Unsupported Linux platform." |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 307 | #elif BUILDFLAG(IS_MAC) |
Christopher Cameron | 1732f2b0 | 2017-11-17 10:56:50 | [diff] [blame] | 308 | // Cross-process CoreAnimation requires a CFRunLoop to function at all, and |
| 309 | // requires a NSRunLoop to not starve under heavy load. See: |
| 310 | // https://crbug.com/312462#c51 and https://crbug.com/783298 |
Maggie Chen | b539c3b | 2024-10-01 20:08:37 | [diff] [blame] | 311 | // CADisplayLink (Mac HW VSync) callback only works with NS_RUNLOOP. DEFAULT |
| 312 | // type does not support NSObject. |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 313 | main_thread_task_executor = |
| 314 | std::make_unique<base::SingleThreadTaskExecutor>( |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 315 | base::MessagePumpType::NS_RUNLOOP); |
Etienne Pierre-doray | 2163f301 | 2020-04-02 21:37:14 | [diff] [blame] | 316 | // As part of the migration to DoWork(), this policy is required to keep |
Etienne Pierre-doray | 9eb4f5a | 2020-01-15 16:29:24 | [diff] [blame] | 317 | // previous behavior and avoid regressions. |
Alison Gale | 59c007a | 2024-04-20 03:05:40 | [diff] [blame] | 318 | // TODO(crbug.com/40668161): Consider updating the policy. |
Etienne Pierre-doray | 9eb4f5a | 2020-01-15 16:29:24 | [diff] [blame] | 319 | main_thread_task_executor->SetWorkBatchSize(2); |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 320 | #else |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 321 | main_thread_task_executor = |
| 322 | std::make_unique<base::SingleThreadTaskExecutor>( |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 323 | base::MessagePumpType::DEFAULT); |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 324 | #endif |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 325 | } |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 326 | |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 327 | base::PlatformThread::SetName("CrGpuMain"); |
Michael Thiessen | dd70b61 | 2024-08-17 18:25:25 | [diff] [blame] | 328 | mojo::InterfaceEndpointClient::SetThreadNameSuffixForMetrics("GpuMain"); |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 329 | |
Shintaro Kawamura | 65a3306 | 2024-03-28 03:40:08 | [diff] [blame] | 330 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
| 331 | // Thread type delegate of the process should be registered before |
| 332 | // thread type change below for the main thread and for thread pool in |
| 333 | // ChildProcess constructor. |
| 334 | // It also needs to be registered before the process has multiple threads, |
| 335 | // which may race with application of the sandbox. InitializeAndStartSandbox() |
| 336 | // sandboxes the process and starts threads so this has to happen first. |
Nathan Memmott | 6ce8cb0 | 2024-12-13 18:16:51 | [diff] [blame] | 337 | SandboxedProcessThreadTypeHandler::Create(); |
Shintaro Kawamura | 65a3306 | 2024-03-28 03:40:08 | [diff] [blame] | 338 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
| 339 | |
Etienne Pierre-doray | 8439625b8b | 2024-08-21 16:42:37 | [diff] [blame] | 340 | base::PlatformThread::SetCurrentThreadType( |
| 341 | base::ThreadType::kDisplayCritical); |
reveman | e7acf84 | 2016-02-05 08:24:32 | [diff] [blame] | 342 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 343 | auto gpu_init = std::make_unique<gpu::GpuInit>(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 344 | ContentSandboxHelper sandbox_helper; |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 345 | #if BUILDFLAG(IS_WIN) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 346 | sandbox_helper.set_sandbox_info(parameters.sandbox_info); |
[email protected] | af7c5d9 | 2014-02-03 19:53:15 | [diff] [blame] | 347 | #endif |
[email protected] | af7c5d9 | 2014-02-03 19:53:15 | [diff] [blame] | 348 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 349 | gpu_init->set_sandbox_helper(&sandbox_helper); |
Sadrul Habib Chowdhury | c0a4a9b9 | 2016-08-29 21:43:24 | [diff] [blame] | 350 | |
Gabriel Charette | c7e363c6 | 2020-06-24 17:39:35 | [diff] [blame] | 351 | // Since GPU initialization calls into skia, it's important to initialize skia |
Khushal | 1d05559 | 2018-07-28 02:00:39 | [diff] [blame] | 352 | // before it. |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 353 | { |
Kramer Ge | 0de97b15 | 2025-06-05 16:28:55 | [diff] [blame] | 354 | TRACE_EVENT("gpu,startup", "gpu_main::InitializeSkia"); |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 355 | InitializeSkia(); |
| 356 | } |
Khushal | 1d05559 | 2018-07-28 02:00:39 | [diff] [blame] | 357 | |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 358 | // The ThreadPool must have been created before invoking |gpu_init| as it |
| 359 | // needs the ThreadPool (in angle::InitializePlatform()). Do not start it |
| 360 | // until after the sandbox is initialized however to avoid creating threads |
| 361 | // outside the sandbox. |
| 362 | DCHECK(base::ThreadPoolInstance::Get()); |
Gabriel Charette | c7e363c6 | 2020-06-24 17:39:35 | [diff] [blame] | 363 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 364 | // Gpu initialization may fail for various reasons, in which case we will need |
| 365 | // to tear down this process. However, we can not do so safely until the IPC |
| 366 | // channel is set up, because the detection of early return of a child process |
| 367 | // is implemented using an IPC channel error. If the IPC channel is not fully |
| 368 | // set up between the browser and GPU process, and the GPU process crashes or |
| 369 | // exits early, the browser process will never detect it. For this reason we |
sadrul | 72aae8a | 2017-01-24 04:52:32 | [diff] [blame] | 370 | // defer tearing down the GPU process until receiving the initialization |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 371 | // message from the browser (through mojom::VizMain::CreateGpuService()). |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 372 | const bool init_success = gpu_init->InitializeAndStartSandbox( |
Zhenyao Mo | e23f7526 | 2018-02-07 02:15:00 | [diff] [blame] | 373 | const_cast<base::CommandLine*>(&command_line), gpu_preferences); |
Georg Neis | 18fc514 | 2024-12-18 00:58:41 | [diff] [blame] | 374 | #if BUILDFLAG(IS_CHROMEOS) |
Jenny Zhang | 06beeb0 | 2024-03-19 23:24:16 | [diff] [blame] | 375 | LOG(WARNING) << "gpu initialization completed init_success:" << init_success; |
| 376 | #endif |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 377 | const bool dead_on_arrival = !init_success; |
Sadrul Habib Chowdhury | c0a4a9b9 | 2016-08-29 21:43:24 | [diff] [blame] | 378 | |
Ian Barkley-Yeung | 48418c1 | 2022-08-16 01:10:16 | [diff] [blame] | 379 | auto* client = GetContentClient()->gpu(); |
| 380 | if (client) { |
| 381 | client->PostSandboxInitialized(); |
| 382 | } |
| 383 | |
Etienne Pierre-doray | 06b3b784 | 2025-07-04 17:01:39 | [diff] [blame] | 384 | // Startup tracing creates a tracing thread, which is incompatible on |
| 385 | // platforms that require single-threaded sandbox initialization. In these |
| 386 | // cases, startup tracing is either initialized right after sandbox |
| 387 | // initialization, or we restart the tracing thread during sandbox |
| 388 | // initialization. |
| 389 | if (parameters.needs_startup_tracing_after_sandbox_init) { |
| 390 | tracing::InitTracingPostFeatureList(/*enable_consumer=*/false, |
| 391 | /*will_trace_thread_restart=*/false); |
| 392 | } |
| 393 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 394 | GetContentClient()->SetGpuInfo(gpu_init->gpu_info()); |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 395 | |
Etienne Pierre-doray | 8439625b8b | 2024-08-21 16:42:37 | [diff] [blame] | 396 | base::ThreadType io_thread_type = base::ThreadType::kDisplayCritical; |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 397 | // ChildProcess will start the ThreadPoolInstance now that the sandbox is |
| 398 | // initialized. |
Zhibo Wang | d9e4a00 | 2022-07-07 04:34:59 | [diff] [blame] | 399 | ChildProcess gpu_process(io_thread_type); |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 400 | DCHECK(base::ThreadPoolInstance::Get()->WasStarted()); |
Xi Cheng | d639081 | 2018-01-24 00:01:50 | [diff] [blame] | 401 | |
Ian Barkley-Yeung | 48418c1 | 2022-08-16 01:10:16 | [diff] [blame] | 402 | if (client) { |
Xi Cheng | d639081 | 2018-01-24 00:01:50 | [diff] [blame] | 403 | client->PostIOThreadCreated(gpu_process.io_task_runner()); |
Ian Barkley-Yeung | 48418c1 | 2022-08-16 01:10:16 | [diff] [blame] | 404 | } |
Xi Cheng | d639081 | 2018-01-24 00:01:50 | [diff] [blame] | 405 | |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 406 | base::RunLoop run_loop; |
| 407 | GpuChildThread* child_thread = |
Dale Curtis | 1b6becebb | 2020-03-30 20:13:35 | [diff] [blame] | 408 | new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init)); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 409 | child_thread->Init(start_time); |
[email protected] | 995a7f1 | 2011-02-11 23:07:17 | [diff] [blame] | 410 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 411 | gpu_process.set_main_thread(child_thread); |
[email protected] | 983c33d | 2010-11-16 22:38:44 | [diff] [blame] | 412 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 413 | #if BUILDFLAG(IS_MAC) |
Kai Ninomiya | e2c5558 | 2019-12-19 23:51:03 | [diff] [blame] | 414 | // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser |
| 415 | // process, meaning it is "responsible" for making sure it and its |
| 416 | // subordinate processes (i.e. the GPU process) drop references to the |
| 417 | // external GPU. Despite this, the system still sends the device removal |
| 418 | // notifications to the GPU process, so the GPU process handles its own |
| 419 | // graceful shutdown without help from the browser process. |
| 420 | // |
| 421 | // Using the "SafeEjectGPU" tool, we can see that when the browser process |
| 422 | // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject |
| 423 | // actions apply to the responsible process, who in turn deals with |
| 424 | // subordinates to eliminate their ejecting eGPU references" [man 8 |
| 425 | // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU |
| 426 | // process exits, it appears that the browser process is no longer considered |
| 427 | // to be using the GPU, so it "succeeds" the 'wait'. |
| 428 | metal::RegisterGracefulExitOnDeviceRemoval(); |
Christopher Cameron | 21c4abc | 2019-11-15 04:06:15 | [diff] [blame] | 429 | #endif |
| 430 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 431 | #if BUILDFLAG(IS_ANDROID) |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 432 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
primiano | 186d6bfe | 2015-10-30 13:21:40 | [diff] [blame] | 433 | tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics", |
| 434 | nullptr); |
Benoit Lize | b6fa4ac | 2023-02-13 11:59:43 | [diff] [blame] | 435 | |
| 436 | base::android::MeminfoDumpProvider::Initialize(); |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 437 | #endif |
| 438 | |
Arthur Sonzogni | a57023635 | 2022-12-19 13:18:54 | [diff] [blame] | 439 | base::allocator::PartitionAllocSupport::Get()->ReconfigureAfterTaskRunnerInit( |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 440 | switches::kGpuProcess); |
| 441 | |
stanisc | 6150709 | 2017-07-06 16:36:58 | [diff] [blame] | 442 | base::HighResolutionTimerManager hi_res_timer_manager; |
| 443 | |
henrika | 3cee59b | 2024-05-03 16:20:52 | [diff] [blame] | 444 | // Adds support of wall-time based TimerKeeper metrics for the main GPU thread |
| 445 | // when command-line flag is set. CrGpuMain will be used as suffix for each |
| 446 | // metric. |
| 447 | if (command_line.HasSwitch(switches::kEnableGpuMainTimeKeeperMetrics)) { |
| 448 | base::CurrentThread::Get()->EnableMessagePumpTimeKeeperMetrics( |
| 449 | "CrGpuMain", |
| 450 | /*wall_time_based_metrics_enabled_for_testing=*/true); |
| 451 | } |
| 452 | |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 453 | DCHECK(tracing::IsTracingInitialized()); |
Kramer Ge | b79116d | 2024-12-12 21:41:20 | [diff] [blame] | 454 | |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 455 | { |
| 456 | TRACE_EVENT0("gpu", "Run Message Loop"); |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 457 | run_loop.Run(); |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 458 | } |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 459 | |
jbauman | a19f1df | 2017-01-18 03:01:17 | [diff] [blame] | 460 | return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0; |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 461 | } |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 462 | |
| 463 | namespace { |
| 464 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 465 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Satyajit Sahu | 82a76e0 | 2017-09-18 14:50:14 | [diff] [blame] | 466 | bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread, |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 467 | const gpu::GPUInfo* gpu_info, |
| 468 | const gpu::GpuPreferences& gpu_prefs) { |
Kramer Ge | 0de97b15 | 2025-06-05 16:28:55 | [diff] [blame] | 469 | TRACE_EVENT("gpu,startup", "Initialize sandbox"); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 470 | |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 471 | if (watchdog_thread) { |
Tom Sepez | 437e220 | 2017-10-24 21:26:47 | [diff] [blame] | 472 | // SandboxLinux needs to be able to ensure that the thread |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 473 | // has really been stopped. |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 474 | sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread); |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 475 | } |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 476 | |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 477 | base::Thread* trace_thread = |
| 478 | tracing::IsTracingInitialized() |
| 479 | ? tracing::PerfettoTracedProcess::GetTraceThread() |
| 480 | : nullptr; |
| 481 | if (trace_thread) { |
| 482 | sandbox::policy::SandboxLinux::GetInstance()->StopThread(trace_thread); |
| 483 | } |
| 484 | |
Tom Sepez | 437e220 | 2017-10-24 21:26:47 | [diff] [blame] | 485 | // SandboxLinux::InitializeSandbox() must always be called |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 486 | // with only one thread. |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 487 | sandbox::policy::SandboxLinux::Options sandbox_options; |
Dominik Behr | 1ce8bff | 2022-07-13 00:54:11 | [diff] [blame] | 488 | if (gpu_info) { |
| 489 | // We have to enable sandbox settings for all GPUs in the system |
| 490 | // for Chrome to be able to access/use them. |
| 491 | sandbox_options.use_amd_specific_policies = |
| 492 | angle::IsAMD(gpu_info->active_gpu().vendor_id); |
| 493 | sandbox_options.use_intel_specific_policies = |
| 494 | angle::IsIntel(gpu_info->active_gpu().vendor_id); |
Kramer Ge | 545ba6d | 2023-01-17 17:14:48 | [diff] [blame] | 495 | sandbox_options.use_virtio_specific_policies = |
| 496 | angle::IsVirtIO(gpu_info->active_gpu().vendor_id); |
Dominik Behr | 1ce8bff | 2022-07-13 00:54:11 | [diff] [blame] | 497 | sandbox_options.use_nvidia_specific_policies = |
| 498 | angle::IsNVIDIA(gpu_info->active_gpu().vendor_id); |
| 499 | for (const auto& gpu : gpu_info->secondary_gpus) { |
| 500 | if (angle::IsAMD(gpu.vendor_id)) |
| 501 | sandbox_options.use_amd_specific_policies = true; |
| 502 | else if (angle::IsIntel(gpu.vendor_id)) |
| 503 | sandbox_options.use_intel_specific_policies = true; |
| 504 | else if (angle::IsNVIDIA(gpu.vendor_id)) |
| 505 | sandbox_options.use_nvidia_specific_policies = true; |
| 506 | } |
| 507 | } |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 508 | sandbox_options.accelerated_video_decode_enabled = |
| 509 | !gpu_prefs.disable_accelerated_video_decode; |
Sheng-Hao Tsao | ea6aa85 | 2018-01-03 06:48:16 | [diff] [blame] | 510 | sandbox_options.accelerated_video_encode_enabled = |
| 511 | !gpu_prefs.disable_accelerated_video_encode; |
Tom Sepez | 085507ab | 2017-10-18 22:36:00 | [diff] [blame] | 512 | |
Yaowei Zhou | dbaba1b | 2023-05-24 05:23:55 | [diff] [blame] | 513 | #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) |
Peter McNeeley | 114d3018 | 2023-02-17 22:09:10 | [diff] [blame] | 514 | // Video decoding of many video streams can use thousands of FDs as well as |
Georg Neis | 18fc514 | 2024-12-18 00:58:41 | [diff] [blame] | 515 | // Exo clients. |
Peter McNeeley | 114d3018 | 2023-02-17 22:09:10 | [diff] [blame] | 516 | // See https://crbug.com/1417237 |
Jeff Chen | fce90f3 | 2022-01-31 19:49:34 | [diff] [blame] | 517 | const auto current_max_fds = |
| 518 | base::saturated_cast<unsigned int>(base::GetMaxFds()); |
Peter McNeeley | 114d3018 | 2023-02-17 22:09:10 | [diff] [blame] | 519 | constexpr unsigned int kMaxFDsDelta = 1u << 13; |
Jeff Chen | fce90f3 | 2022-01-31 19:49:34 | [diff] [blame] | 520 | const auto new_max_fds = |
Peter McNeeley | 114d3018 | 2023-02-17 22:09:10 | [diff] [blame] | 521 | static_cast<unsigned int>(base::ClampMax(current_max_fds, kMaxFDsDelta)); |
Hirokazu Honda | 5324314 | 2023-01-25 01:45:52 | [diff] [blame] | 522 | base::IncreaseFdLimitTo(new_max_fds); |
Jeff Chen | fce90f3 | 2022-01-31 19:49:34 | [diff] [blame] | 523 | #endif |
| 524 | |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 525 | bool res = sandbox::policy::SandboxLinux::GetInstance()->InitializeSandbox( |
| 526 | sandbox::policy::SandboxTypeFromCommandLine( |
Tom Sepez | 2255db7 | 2017-10-26 18:50:09 | [diff] [blame] | 527 | *base::CommandLine::ForCurrentProcess()), |
Ken Rockot | 9c9d3da | 2023-11-28 22:58:32 | [diff] [blame] | 528 | base::BindOnce(GpuPreSandboxHook), sandbox_options); |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 529 | |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 530 | if (watchdog_thread) { |
Etienne Pierre-doray | ef5dcca | 2023-07-11 16:46:27 | [diff] [blame] | 531 | watchdog_thread->Start(); |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 532 | } |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 533 | |
Kramer Ge | a805bb01 | 2025-04-30 07:08:51 | [diff] [blame] | 534 | if (trace_thread) { |
| 535 | tracing::PerfettoTracedProcess::Get().RestartThreadInSandbox(); |
| 536 | } |
| 537 | |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 538 | return res; |
| 539 | } |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 540 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 541 | |
Ben Scarlato | 5ece30c7 | 2025-05-09 22:13:20 | [diff] [blame] | 542 | #if BUILDFLAG(IS_ANDROID) |
| 543 | bool StartSandboxAndroid(gpu::GpuWatchdogThread* watchdog_thread) { |
| 544 | if (watchdog_thread) { |
| 545 | // Stop the watchdog thread temporarily. |
| 546 | base::ScopedFD proc_fd( |
| 547 | HANDLE_EINTR(open("/proc", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); |
| 548 | |
| 549 | sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.get(), |
| 550 | watchdog_thread); |
| 551 | } |
| 552 | |
| 553 | bool res = sandbox::landlock::ApplyLandlock( |
| 554 | sandbox::policy::SandboxTypeFromCommandLine( |
| 555 | *base::CommandLine::ForCurrentProcess())); |
| 556 | |
| 557 | if (watchdog_thread) { |
| 558 | watchdog_thread->Start(); |
| 559 | } |
| 560 | |
| 561 | return res; |
| 562 | } |
| 563 | #endif // BUILDFLAG(IS_ANDROID) |
| 564 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 565 | #if BUILDFLAG(IS_WIN) |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 566 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) { |
Kramer Ge | 0de97b15 | 2025-06-05 16:28:55 | [diff] [blame] | 567 | TRACE_EVENT("gpu,startup", "Lower token"); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 568 | |
| 569 | // For Windows, if the target_services interface is not zero, the process |
| 570 | // is sandboxed and we must call LowerToken() before rendering untrusted |
| 571 | // content. |
| 572 | sandbox::TargetServices* target_services = sandbox_info->target_services; |
| 573 | if (target_services) { |
| 574 | target_services->LowerToken(); |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | return false; |
| 579 | } |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 580 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 581 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 582 | } // namespace. |
| 583 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 584 | } // namespace content |