blob: 51b004f5e459eb265627c01e9e38acd655a65481 [file] [log] [blame]
[email protected]2e3d9e42012-02-14 03:23:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c0fc0942010-01-13 00:55:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi66a07722015-12-25 23:38:125#include <stddef.h>
[email protected]e09cee42010-11-09 01:50:086#include <stdlib.h>
7
mostynb6682b1c42016-04-19 10:17:308#include <memory>
dchengf26eed32016-01-13 10:58:149#include <utility>
10
Sebastien Marchandf8cbfab2019-01-25 16:02:3011#include "base/bind.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3212#include "base/feature_list.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1513#include "base/message_loop/message_pump_type.h"
asvitkine8d51e9d2016-09-02 23:55:4314#include "base/metrics/histogram_macros.h"
[email protected]2436a6b2012-04-13 21:08:5115#include "base/rand_util.h"
fdoraye716a902016-07-05 16:05:4916#include "base/run_loop.h"
[email protected]d0ea4782013-06-11 04:58:2417#include "base/strings/string_number_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0318#include "base/system/sys_info.h"
Alex Clarke636e7052019-05-30 10:49:3719#include "base/task/single_thread_task_executor.h"
Gabriel Charette6f9509b2020-05-27 20:39:5720#include "base/task/thread_pool.h"
[email protected]ce072a72010-12-31 20:02:1621#include "base/threading/platform_thread.h"
stanisc61507092017-07-06 16:36:5822#include "base/timer/hi_res_timer_manager.h"
primiano50b7444c2015-01-28 04:17:0023#include "base/trace_event/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3724#include "build/build_config.h"
Yuta Hijikata76d5cb62020-12-09 09:51:5325#include "build/chromeos_buildflags.h"
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:0626#include "components/viz/service/main/viz_main_impl.h"
[email protected]91a2aea2013-07-08 23:14:3927#include "content/common/content_constants_internal.h"
John Abd-El-Malek884291c2017-08-09 06:43:4828#include "content/common/content_switches_internal.h"
Benoit Lize7ee77d32021-03-04 17:26:1429#include "content/common/partition_alloc_support.h"
Khushala4e236f2018-06-01 03:00:4630#include "content/common/skia_utils.h"
[email protected]7a31f7c2011-03-21 23:22:0431#include "content/gpu/gpu_child_thread.h"
[email protected]623c0bd2011-03-12 01:00:4132#include "content/gpu/gpu_process.h"
[email protected]c9e2cbbb2012-05-12 21:17:2733#include "content/public/common/content_client.h"
34#include "content/public/common/content_switches.h"
35#include "content/public/common/main_function_params.h"
jbaumana19f1df2017-01-18 03:01:1736#include "content/public/common/result_codes.h"
Zhenyao Moc76e9032018-01-19 21:15:4637#include "content/public/gpu/content_gpu_client.h"
[email protected]40c19e722013-11-05 23:51:2438#include "gpu/command_buffer/service/gpu_switches.h"
sadruled395922016-09-07 17:32:5839#include "gpu/config/gpu_driver_bug_list.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3240#include "gpu/config/gpu_finch_features.h"
[email protected]d7b5cc72013-05-23 20:05:0041#include "gpu/config/gpu_info_collector.h"
Jonathan Backer16cc8fd2018-05-31 19:59:2242#include "gpu/config/gpu_preferences.h"
tfarina15525c42015-04-28 19:04:1643#include "gpu/config/gpu_switches.h"
[email protected]40c19e722013-11-05 23:51:2444#include "gpu/config/gpu_util.h"
fsamuelc2774222016-03-24 00:27:1245#include "gpu/ipc/common/gpu_memory_buffer_support.h"
markdittmerd88b8352016-04-08 15:28:4546#include "gpu/ipc/service/gpu_config.h"
sadrul454af332016-09-09 18:14:3247#include "gpu/ipc/service/gpu_init.h"
sadrul2fb7e152016-08-30 05:21:4548#include "gpu/ipc/service/gpu_watchdog_thread.h"
Scott Violeta35f9a42018-03-22 22:00:4449#include "media/gpu/buildflags.h"
kylechar60e12ed2021-03-15 15:56:0350#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
Dale Curtis12cfe022020-02-21 01:18:4251#include "services/tracing/public/cpp/trace_startup.h"
Tom Sepeza0950c62017-10-18 20:39:1452#include "third_party/angle/src/gpu_info_util/SystemInfo.h"
Maksim Sisov459eada2020-06-25 08:16:3153#include "ui/base/ui_base_features.h"
[email protected]c0939182014-05-24 00:09:1954#include "ui/events/platform/platform_event_source.h"
skyostilb354f882016-12-13 18:42:4555#include "ui/gfx/switches.h"
martina.kollarovaa34211d2015-06-25 11:49:0756#include "ui/gl/gl_context.h"
[email protected]db6101db2012-10-25 15:20:0857#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2758#include "ui/gl/gl_surface.h"
59#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2460#include "ui/gl/gpu_switching_manager.h"
kylechar5b9dec12016-05-16 15:40:5761#include "ui/gl/init/gl_factory.h"
[email protected]c0fc0942010-01-13 00:55:3762
avi66a07722015-12-25 23:38:1263#if defined(OS_WIN)
fdoraye716a902016-07-05 16:05:4964#include <dwmapi.h>
Benoit Lize7ee77d32021-03-04 17:26:1465#include <windows.h>
avi66a07722015-12-25 23:38:1266#endif
67
primianob3fb6412015-10-14 16:03:5168#if defined(OS_ANDROID)
69#include "base/trace_event/memory_dump_manager.h"
primianoccb26c62016-06-01 21:50:0270#include "components/tracing/common/graphics_memory_dump_provider_android.h"
primianob3fb6412015-10-14 16:03:5171#endif
72
[email protected]d7de57872011-12-06 23:32:4373#if defined(OS_WIN)
Daniel Libby8f7e6262019-01-08 22:35:5574#include "base/trace_event/trace_event_etw_export_win.h"
[email protected]5f7e4512012-10-01 20:51:3775#include "base/win/scoped_com_initializer.h"
kylechar5b9dec12016-05-16 15:40:5776#include "base/win/windows_version.h"
Miguel Casasd697adf32018-02-26 19:06:3177#include "media/gpu/windows/dxva_video_decode_accelerator_win.h"
78#include "media/gpu/windows/media_foundation_video_encode_accelerator_win.h"
[email protected]181491782012-07-18 00:59:1579#include "sandbox/win/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3780#endif
81
[email protected]02ec37a2010-09-20 22:32:1682#if defined(USE_X11)
Tom Andersonb960daf2020-08-19 17:26:1783#include "ui/base/x/x11_ui_thread.h" // nogncheck
Tom Andersonff8d22e2020-01-30 03:19:1684#include "ui/base/x/x11_util.h" // nogncheck
85#include "ui/gfx/linux/gpu_memory_buffer_support_x11.h" // nogncheck
Tom Andersonff8d22e2020-01-30 03:19:1686#include "ui/gfx/x/x11_switches.h" // nogncheck
[email protected]02ec37a2010-09-20 22:32:1687#endif
88
Sean McAllisterbd181782020-08-13 21:05:5189#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Tom Sepez085507ab2017-10-18 22:36:0090#include "content/gpu/gpu_sandbox_hook_linux.h"
[email protected]2436a6b2012-04-13 21:08:5191#include "content/public/common/sandbox_init.h"
Robert Sesek7d0b49b2020-07-08 18:31:2792#include "sandbox/policy/linux/sandbox_linux.h"
[email protected]2436a6b2012-04-13 21:08:5193#endif
94
Avi Drissman7c57be72020-07-29 20:09:4695#if defined(OS_MAC)
[email protected]826aab02014-05-14 02:58:5996#include "base/message_loop/message_pump_mac.h"
Kai Ninomiyae2c55582019-12-19 23:51:0397#include "components/metal_util/device_removal.h"
Christopher Cameron21c4abc2019-11-15 04:06:1598#include "components/metal_util/test_shader.h"
Robert Sesek3bd6e4b32019-11-26 16:02:2799#include "media/gpu/mac/vt_video_decode_accelerator_mac.h"
Greg Kerr3480aa82018-02-01 00:53:03100#include "sandbox/mac/seatbelt.h"
[email protected]826aab02014-05-14 02:58:59101#endif
102
Alexandre Courbotc13a5972017-07-23 03:48:48103#if BUILDFLAG(USE_VAAPI)
Miguel Casas42e955c2017-12-04 14:18:12104#include "media/gpu/vaapi/vaapi_wrapper.h"
hshi95837052015-05-12 15:39:51105#endif
106
[email protected]eb398192012-10-22 20:16:19107namespace content {
[email protected]ec4bda62013-06-14 15:51:03108
[email protected]6ec3a572012-08-17 02:09:51109namespace {
[email protected]ec4bda62013-06-14 15:51:03110
Sean McAllisterbd181782020-08-13 21:05:51111#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33112bool StartSandboxLinux(gpu::GpuWatchdogThread*,
113 const gpu::GPUInfo*,
114 const gpu::GpuPreferences&);
[email protected]663c4b32013-04-18 05:52:54115#elif defined(OS_WIN)
116bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
117#endif
[email protected]ec4bda62013-06-14 15:51:03118
sadrul454af332016-09-09 18:14:32119class ContentSandboxHelper : public gpu::GpuSandboxHelper {
120 public:
121 ContentSandboxHelper() {}
Peter Boström828b9022021-09-21 02:28:43122
123 ContentSandboxHelper(const ContentSandboxHelper&) = delete;
124 ContentSandboxHelper& operator=(const ContentSandboxHelper&) = delete;
125
sadrul454af332016-09-09 18:14:32126 ~ContentSandboxHelper() override {}
127
128#if defined(OS_WIN)
129 void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) {
130 sandbox_info_ = info;
131 }
132#endif
133
sadrul454af332016-09-09 18:14:32134 private:
135 // SandboxHelper:
Ted Meyer6801a072020-10-28 03:25:02136 void PreSandboxStartup(const gpu::GpuPreferences& gpu_prefs) override {
sadrul454af332016-09-09 18:14:32137 // Warm up resources that don't need access to GPUInfo.
138 {
139 TRACE_EVENT0("gpu", "Warm up rand");
140 // Warm up the random subsystem, which needs to be done pre-sandbox on all
141 // platforms.
142 (void)base::RandUint64();
143 }
144
Alexandre Courbotc13a5972017-07-23 03:48:48145#if BUILDFLAG(USE_VAAPI)
Pilar Molina Lopezfb5103b72021-02-26 23:35:52146#if defined(OS_CHROMEOS)
sadrul454af332016-09-09 18:14:32147 media::VaapiWrapper::PreSandboxInitialization();
Pilar Molina Lopezfb5103b72021-02-26 23:35:52148#else // For Linux with VA-API support.
Ted Meyer6801a072020-10-28 03:25:02149 if (!gpu_prefs.disable_accelerated_video_decode)
Ted Meyer6f266fd2020-10-23 01:54:06150 media::VaapiWrapper::PreSandboxInitialization();
sadrul454af332016-09-09 18:14:32151#endif
Ted Meyer6f266fd2020-10-23 01:54:06152#endif // BUILDFLAG(USE_VAAPI)
sadrul454af332016-09-09 18:14:32153#if defined(OS_WIN)
154 media::DXVAVideoDecodeAccelerator::PreSandboxInitialization();
155 media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization();
156#endif
Eric Karle35050e2017-09-07 01:44:35157
Avi Drissman7c57be72020-07-29 20:09:46158#if defined(OS_MAC)
Robert Sesek766439c92021-03-23 21:01:31159 {
Robert Sesek3bd6e4b32019-11-26 16:02:27160 TRACE_EVENT0("gpu", "Initialize VideoToolbox");
161 media::InitializeVideoToolbox();
162 }
163#endif
164
Eric Karle35050e2017-09-07 01:44:35165 // On Linux, reading system memory doesn't work through the GPU sandbox.
166 // This value is cached, so access it here to populate the cache.
167 base::SysInfo::AmountOfPhysicalMemory();
sadrul454af332016-09-09 18:14:32168 }
169
Satyajit Sahu82a76e02017-09-18 14:50:14170 bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33171 const gpu::GPUInfo* gpu_info,
172 const gpu::GpuPreferences& gpu_prefs) override {
Sean McAllisterbd181782020-08-13 21:05:51173#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33174 return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs);
sadrul454af332016-09-09 18:14:32175#elif defined(OS_WIN)
176 return StartSandboxWindows(sandbox_info_);
Avi Drissman7c57be72020-07-29 20:09:46177#elif defined(OS_MAC)
Greg Kerr3480aa82018-02-01 00:53:03178 return sandbox::Seatbelt::IsSandboxed();
sadrul454af332016-09-09 18:14:32179#else
180 return false;
181#endif
182 }
183
184#if defined(OS_WIN)
185 const sandbox::SandboxInterfaceInfo* sandbox_info_ = nullptr;
sadrul454af332016-09-09 18:14:32186#endif
sadrul454af332016-09-09 18:14:32187};
188
kylechar476993472016-09-14 16:03:48189} // namespace
[email protected]ec4bda62013-06-14 15:51:03190
[email protected]c0fc0942010-01-13 00:55:37191// Main function for starting the Gpu process.
[email protected]eb398192012-10-22 20:16:19192int GpuMain(const MainFunctionParams& parameters) {
[email protected]d13f35d2012-05-18 02:28:15193 TRACE_EVENT0("gpu", "GpuMain");
Ehsan Chiniforooshana8c8dad2017-11-03 07:23:09194 base::trace_event::TraceLog::GetInstance()->set_process_name("GPU Process");
ssidb2e3ece2015-02-09 16:02:20195 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:39196 kTraceEventGpuProcessSortIndex);
[email protected]d13f35d2012-05-18 02:28:15197
avi83883c82014-12-23 00:08:49198 const base::CommandLine& command_line = parameters.command_line;
Zhenyao Mo910beb82017-10-25 03:23:00199
200 gpu::GpuPreferences gpu_preferences;
201 if (command_line.HasSwitch(switches::kGpuPreferences)) {
202 std::string value =
203 command_line.GetSwitchValueASCII(switches::kGpuPreferences);
Jonathan Backer16cc8fd2018-05-31 19:59:22204 bool success = gpu_preferences.FromSwitchValue(value);
Zhenyao Mo910beb82017-10-25 03:23:00205 CHECK(success);
206 }
207
kylechar60e12ed2021-03-15 15:56:03208 // Disallow sending sync IPCs from the GPU process, in particular CrGpuMain
209 // and VizCompositorThreads. Incoming sync IPCs can be received out of order
210 // when waiting on response to an outgoing sync IPC. Both viz and gpu
211 // interfaces rely on receiving messages in order so this message reordering
212 // would break things.
213 mojo::SyncCallRestrictions::DisallowSyncCall();
214
Zhenyao Mo910beb82017-10-25 03:23:00215 if (gpu_preferences.gpu_startup_dialog)
John Abd-El-Malek884291c2017-08-09 06:43:48216 WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:49217
[email protected]ca23992b02013-06-13 17:25:19218 base::Time start_time = base::Time::Now();
219
[email protected]23f46562011-09-07 01:42:39220#if defined(OS_WIN)
Daniel Libby1700bbe82019-01-30 22:22:36221 base::trace_event::TraceEventETWExport::EnableETWExport();
222
[email protected]52819472013-11-24 22:49:55223 // Prevent Windows from displaying a modal dialog on failures like not being
224 // able to load a DLL.
Benoit Lize7ee77d32021-03-04 17:26:14225 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
226 SEM_NOOPENFILEERRORBOX);
Robert Liao98bb92d2017-06-15 22:20:39227
228 // COM is used by some Windows Media Foundation calls made on this thread and
229 // must be MTA so we don't have to worry about pumping messages to handle
230 // COM callbacks.
231 base::win::ScopedCOMInitializer com_initializer(
232 base::win::ScopedCOMInitializer::kMTA);
Sunny Sachanandani35c727c2019-07-09 13:12:52233
234 if (base::FeatureList::IsEnabled(features::kGpuProcessHighPriorityWin))
Sunny Sachanandani49bdbad2019-07-17 19:15:10235 ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
[email protected]23f46562011-09-07 01:42:39236#endif
[email protected]ec4bda62013-06-14 15:51:03237
Dale Curtis1b6becebb2020-03-30 20:13:35238 // Installs a base::LogMessageHandlerFunction which ensures messages are sent
239 // to the GpuProcessHost once the GpuServiceImpl has started.
240 viz::GpuServiceImpl::InstallPreInitializeLogHandler();
[email protected]23f46562011-09-07 01:42:39241
ericrk1d9e17f2016-11-30 01:51:28242 // We are experiencing what appear to be memory-stomp issues in the GPU
Alex Clarkef7fb8a82019-06-06 15:41:53243 // process. These issues seem to be impacting the task executor and listeners
244 // registered to it. Create the task executor on the heap to guard against
ericrk1d9e17f2016-11-30 01:51:28245 // this.
246 // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802
247 // and crbug.com/609252.
Alex Clarke636e7052019-05-30 10:49:37248 std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor;
skyostil82befc52016-12-19 13:48:59249 std::unique_ptr<ui::PlatformEventSource> event_source;
skyostilb354f882016-12-13 18:42:45250 if (command_line.HasSwitch(switches::kHeadless)) {
Alex Clarke636e7052019-05-30 10:49:37251 main_thread_task_executor =
252 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15253 base::MessagePumpType::DEFAULT);
skyostilb354f882016-12-13 18:42:45254 } else {
[email protected]db6101db2012-10-25 15:20:08255#if defined(OS_WIN)
Robert Liao98bb92d2017-06-15 22:20:39256 // The GpuMain thread should not be pumping Windows messages because no UI
257 // is expected to run on this thread.
Alex Clarke636e7052019-05-30 10:49:37258 main_thread_task_executor =
259 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15260 base::MessagePumpType::DEFAULT);
Maksim Sisov459eada2020-06-25 08:16:31261#elif defined(USE_X11) || defined(USE_OZONE)
262#if defined(USE_X11)
263 if (!features::IsUsingOzonePlatform()) {
264 // We need a UI loop so that we can grab the Expose events. See
265 // GLSurfaceGLX and https://crbug.com/326995.
Tom Andersone4fc22a2020-10-28 07:14:14266 if (!x11::Connection::Get()->Ready())
Maksim Sisov459eada2020-06-25 08:16:31267 return RESULT_CODE_GPU_DEAD_ON_ARRIVAL;
268 main_thread_task_executor =
269 std::make_unique<base::SingleThreadTaskExecutor>(
270 base::MessagePumpType::UI);
271 event_source = ui::PlatformEventSource::CreateDefault();
Tom Andersonb960daf2020-08-19 17:26:17272 // Set up the X11UiThread before the sandbox gets set up. This cannot be
273 // done later since opening the connection requires socket() and
274 // connect().
275 ui::X11UiThread::SetConnection(x11::Connection::Get()->Clone().release());
Maksim Sisov459eada2020-06-25 08:16:31276 }
277#endif
278#if defined(USE_OZONE)
Alex Clarke636e7052019-05-30 10:49:37279 // The MessagePump type required depends on the Ozone platform selected at
tonikitoofb807b102017-02-08 19:52:03280 // runtime.
Maksim Sisov459eada2020-06-25 08:16:31281 if (!main_thread_task_executor) {
282 main_thread_task_executor =
283 std::make_unique<base::SingleThreadTaskExecutor>(
284 gpu_preferences.message_pump_type);
285 }
286#endif
Sean McAllisterbd181782020-08-13 21:05:51287#elif defined(OS_LINUX) || defined(OS_CHROMEOS)
kylechar476993472016-09-14 16:03:48288#error "Unsupported Linux platform."
Avi Drissman7c57be72020-07-29 20:09:46289#elif defined(OS_MAC)
Christopher Cameron1732f2b02017-11-17 10:56:50290 // Cross-process CoreAnimation requires a CFRunLoop to function at all, and
291 // requires a NSRunLoop to not starve under heavy load. See:
292 // https://crbug.com/312462#c51 and https://crbug.com/783298
Alex Clarke636e7052019-05-30 10:49:37293 main_thread_task_executor =
294 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15295 base::MessagePumpType::NS_RUNLOOP);
Etienne Pierre-doray2163f3012020-04-02 21:37:14296 // As part of the migration to DoWork(), this policy is required to keep
Etienne Pierre-doray9eb4f5a2020-01-15 16:29:24297 // previous behavior and avoid regressions.
298 // TODO(crbug.com/1041853): Consider updating the policy.
299 main_thread_task_executor->SetWorkBatchSize(2);
[email protected]826aab02014-05-14 02:58:59300#else
Alex Clarke636e7052019-05-30 10:49:37301 main_thread_task_executor =
302 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15303 base::MessagePumpType::DEFAULT);
[email protected]db6101db2012-10-25 15:20:08304#endif
skyostilb354f882016-12-13 18:42:45305 }
[email protected]db6101db2012-10-25 15:20:08306
[email protected]db6101db2012-10-25 15:20:08307 base::PlatformThread::SetName("CrGpuMain");
308
Avi Drissman7c57be72020-07-29 20:09:46309#if !defined(OS_MAC)
Sunny Sachanandani5cd10e962019-06-13 22:55:32310 if (base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)) {
311 // Set thread priority before sandbox initialization.
312 base::PlatformThread::SetCurrentThreadPriority(
313 base::ThreadPriority::DISPLAY);
314 }
Michael Spang50ed1ff2019-07-02 22:26:56315#endif
revemane7acf842016-02-05 08:24:32316
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57317 auto gpu_init = std::make_unique<gpu::GpuInit>();
sadrul454af332016-09-09 18:14:32318 ContentSandboxHelper sandbox_helper;
319#if defined(OS_WIN)
320 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
Gabriel Charettec7e363c62020-06-24 17:39:35329 // Create the ThreadPool before invoking |gpu_init| as it needs the ThreadPool
330 // (in angle::InitializePlatform()). Do not start it until after the sandbox
331 // is initialized however to avoid creating threads outside the sandbox.
332 base::ThreadPoolInstance::Create("GPU");
333
sadrul454af332016-09-09 18:14:32334 // Gpu initialization may fail for various reasons, in which case we will need
335 // to tear down this process. However, we can not do so safely until the IPC
336 // channel is set up, because the detection of early return of a child process
337 // is implemented using an IPC channel error. If the IPC channel is not fully
338 // set up between the browser and GPU process, and the GPU process crashes or
339 // exits early, the browser process will never detect it. For this reason we
sadrul72aae8a2017-01-24 04:52:32340 // defer tearing down the GPU process until receiving the initialization
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:06341 // message from the browser (through mojom::VizMain::CreateGpuService()).
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57342 const bool init_success = gpu_init->InitializeAndStartSandbox(
Zhenyao Moe23f75262018-02-07 02:15:00343 const_cast<base::CommandLine*>(&command_line), gpu_preferences);
sadrul454af332016-09-09 18:14:32344 const bool dead_on_arrival = !init_success;
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24345
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57346 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
[email protected]ec4bda62013-06-14 15:51:03347
Gabriel Charettec7e363c62020-06-24 17:39:35348 // Start the ThreadPoolInstance now that the sandbox is initialized.
349 base::ThreadPoolInstance::Get()->StartWithDefaultParams();
350
Sunny Sachanandani5cd10e962019-06-13 22:55:32351 const base::ThreadPriority io_thread_priority =
352 base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)
353 ? base::ThreadPriority::DISPLAY
354 : base::ThreadPriority::NORMAL;
Avi Drissman7c57be72020-07-29 20:09:46355#if defined(OS_MAC)
behdad8b57eaad2019-07-18 13:58:48356 // Increase the thread priority to get more reliable values in performance
357 // test of mac_os.
358 GpuProcess gpu_process(
359 (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests)
360 ? base::ThreadPriority::REALTIME_AUDIO
361 : io_thread_priority));
362#else
reveman7caf8cf2016-02-16 02:39:05363 GpuProcess gpu_process(io_thread_priority);
behdad8b57eaad2019-07-18 13:58:48364#endif
Xi Chengd6390812018-01-24 00:01:50365
Tom Andersonff8d22e2020-01-30 03:19:16366#if defined(USE_X11)
367 // ui::GbmDevice() takes >50ms with amdgpu, so kick off
368 // GpuMemoryBufferSupportX11 creation on another thread now.
Maksim Sisove8bf9222020-06-24 07:53:02369 if (!features::IsUsingOzonePlatform() &&
370 gpu_preferences.enable_native_gpu_memory_buffers) {
Gabriel Charette6f9509b2020-05-27 20:39:57371 base::ThreadPool::PostTask(
Tom Andersonbe0638f2020-05-21 01:20:33372 FROM_HERE, base::BindOnce([]() {
373 SCOPED_UMA_HISTOGRAM_TIMER("Linux.X11.GbmSupportX11CreationTime");
374 ui::GpuMemoryBufferSupportX11::GetInstance();
375 }));
376 }
Tom Andersonff8d22e2020-01-30 03:19:16377#endif
378
Zhenyao Moe23f75262018-02-07 02:15:00379 auto* client = GetContentClient()->gpu();
Xi Chengd6390812018-01-24 00:01:50380 if (client)
381 client->PostIOThreadCreated(gpu_process.io_task_runner());
382
Wez6979109b2018-09-07 17:30:56383 base::RunLoop run_loop;
384 GpuChildThread* child_thread =
Dale Curtis1b6becebb2020-03-30 20:13:35385 new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init));
[email protected]7a31f7c2011-03-21 23:22:04386 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17387
[email protected]7a31f7c2011-03-21 23:22:04388 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44389
Avi Drissman7c57be72020-07-29 20:09:46390#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MAC)
Dale Curtis12cfe022020-02-21 01:18:42391 // Startup tracing is usually enabled earlier, but if we forked from a zygote,
392 // we can only enable it after mojo IPC support is brought up initialized by
393 // GpuChildThread, because the mojo broker has to create the tracing SMB on
394 // our behalf due to the zygote sandbox.
395 if (parameters.zygote_child)
396 tracing::EnableStartupTracingIfNeeded();
Avi Drissmand4d79f72020-07-30 18:35:18397#endif // OS_POSIX && !OS_ANDROID && !OS_MAC
Dale Curtis12cfe022020-02-21 01:18:42398
Avi Drissman7c57be72020-07-29 20:09:46399#if defined(OS_MAC)
Kai Ninomiyae2c55582019-12-19 23:51:03400 // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser
401 // process, meaning it is "responsible" for making sure it and its
402 // subordinate processes (i.e. the GPU process) drop references to the
403 // external GPU. Despite this, the system still sends the device removal
404 // notifications to the GPU process, so the GPU process handles its own
405 // graceful shutdown without help from the browser process.
406 //
407 // Using the "SafeEjectGPU" tool, we can see that when the browser process
408 // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject
409 // actions apply to the responsible process, who in turn deals with
410 // subordinates to eliminate their ejecting eGPU references" [man 8
411 // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU
412 // process exits, it appears that the browser process is no longer considered
413 // to be using the GPU, so it "succeeds" the 'wait'.
414 metal::RegisterGracefulExitOnDeviceRemoval();
Christopher Cameron21c4abc2019-11-15 04:06:15415#endif
416
primianob3fb6412015-10-14 16:03:51417#if defined(OS_ANDROID)
418 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40419 tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics",
420 nullptr);
primianob3fb6412015-10-14 16:03:51421#endif
422
Benoit Lize7ee77d32021-03-04 17:26:14423 internal::PartitionAllocSupport::Get()->ReconfigureAfterTaskRunnerInit(
424 switches::kGpuProcess);
425
stanisc61507092017-07-06 16:36:58426 base::HighResolutionTimerManager hi_res_timer_manager;
427
[email protected]d13f35d2012-05-18 02:28:15428 {
429 TRACE_EVENT0("gpu", "Run Message Loop");
Wez6979109b2018-09-07 17:30:56430 run_loop.Run();
[email protected]d13f35d2012-05-18 02:28:15431 }
[email protected]c0fc0942010-01-13 00:55:37432
jbaumana19f1df2017-01-18 03:01:17433 return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0;
[email protected]c0fc0942010-01-13 00:55:37434}
[email protected]6ec3a572012-08-17 02:09:51435
436namespace {
437
Sean McAllisterbd181782020-08-13 21:05:51438#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Satyajit Sahu82a76e02017-09-18 14:50:14439bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33440 const gpu::GPUInfo* gpu_info,
441 const gpu::GpuPreferences& gpu_prefs) {
fdorayf30bf3a2015-10-28 21:47:00442 TRACE_EVENT0("gpu,startup", "Initialize sandbox");
[email protected]663c4b32013-04-18 05:52:54443
[email protected]f330b762014-02-08 04:52:46444 if (watchdog_thread) {
Tom Sepez437e2202017-10-24 21:26:47445 // SandboxLinux needs to be able to ensure that the thread
[email protected]f330b762014-02-08 04:52:46446 // has really been stopped.
Robert Sesek7d0b49b2020-07-08 18:31:27447 sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread);
[email protected]f330b762014-02-08 04:52:46448 }
[email protected]655abd522014-06-02 15:23:43449
Tom Sepez437e2202017-10-24 21:26:47450 // SandboxLinux::InitializeSandbox() must always be called
[email protected]663c4b32013-04-18 05:52:54451 // with only one thread.
Robert Sesek7d0b49b2020-07-08 18:31:27452 sandbox::policy::SandboxLinux::Options sandbox_options;
Tom Sepeza0950c62017-10-18 20:39:14453 sandbox_options.use_amd_specific_policies =
454 gpu_info && angle::IsAMD(gpu_info->active_gpu().vendor_id);
Brian Hoe7c074832019-12-11 20:20:52455 sandbox_options.use_intel_specific_policies =
456 gpu_info && angle::IsIntel(gpu_info->active_gpu().vendor_id);
Tom Anderson0fd12d92020-10-31 00:55:52457 sandbox_options.use_nvidia_specific_policies =
458 gpu_info && angle::IsNVIDIA(gpu_info->active_gpu().vendor_id);
Zhenyao Moe9187a862017-10-20 04:26:33459 sandbox_options.accelerated_video_decode_enabled =
460 !gpu_prefs.disable_accelerated_video_decode;
Sheng-Hao Tsaoea6aa852018-01-03 06:48:16461 sandbox_options.accelerated_video_encode_enabled =
462 !gpu_prefs.disable_accelerated_video_encode;
Tom Sepez085507ab2017-10-18 22:36:00463
Robert Sesek7d0b49b2020-07-08 18:31:27464 bool res = sandbox::policy::SandboxLinux::GetInstance()->InitializeSandbox(
465 sandbox::policy::SandboxTypeFromCommandLine(
Tom Sepez2255db72017-10-26 18:50:09466 *base::CommandLine::ForCurrentProcess()),
Tom Sepez4b101712017-11-08 19:39:58467 base::BindOnce(GpuProcessPreSandboxHook), sandbox_options);
Tom Sepeza0950c62017-10-18 20:39:14468
[email protected]f330b762014-02-08 04:52:46469 if (watchdog_thread) {
Tom Sepeza0950c62017-10-18 20:39:14470 base::Thread::Options thread_options;
471 thread_options.timer_slack = base::TIMER_SLACK_MAXIMUM;
Olivier Lia3c71552021-05-12 17:05:52472 watchdog_thread->StartWithOptions(std::move(thread_options));
[email protected]f330b762014-02-08 04:52:46473 }
[email protected]663c4b32013-04-18 05:52:54474
475 return res;
476}
Sean McAllisterbd181782020-08-13 21:05:51477#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
[email protected]663c4b32013-04-18 05:52:54478
479#if defined(OS_WIN)
480bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
fdorayf30bf3a2015-10-28 21:47:00481 TRACE_EVENT0("gpu,startup", "Lower token");
[email protected]663c4b32013-04-18 05:52:54482
483 // For Windows, if the target_services interface is not zero, the process
484 // is sandboxed and we must call LowerToken() before rendering untrusted
485 // content.
486 sandbox::TargetServices* target_services = sandbox_info->target_services;
487 if (target_services) {
488 target_services->LowerToken();
489 return true;
490 }
491
492 return false;
493}
494#endif // defined(OS_WIN)
495
[email protected]6ec3a572012-08-17 02:09:51496} // namespace.
497
[email protected]eb398192012-10-22 20:16:19498} // namespace content