blob: c6fada5cefbf9ca359be880c5ab747884c7ba4ce [file] [log] [blame]
[email protected]2e3d9e42012-02-14 03:23:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c0fc0942010-01-13 00:55:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi66a07722015-12-25 23:38:125#include <stddef.h>
[email protected]e09cee42010-11-09 01:50:086#include <stdlib.h>
7
mostynb6682b1c42016-04-19 10:17:308#include <memory>
dchengf26eed32016-01-13 10:58:149#include <utility>
10
Sebastien Marchandf8cbfab2019-01-25 16:02:3011#include "base/bind.h"
Sunny Sachanandani5cd10e962019-06-13 22:55:3212#include "base/feature_list.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1513#include "base/message_loop/message_pump_type.h"
asvitkine8d51e9d2016-09-02 23:55:4314#include "base/metrics/histogram_macros.h"
[email protected]2436a6b2012-04-13 21:08:5115#include "base/rand_util.h"
fdoraye716a902016-07-05 16:05:4916#include "base/run_loop.h"
[email protected]d0ea4782013-06-11 04:58:2417#include "base/strings/string_number_conversions.h"
18#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0319#include "base/system/sys_info.h"
Alex Clarke636e7052019-05-30 10:49:3720#include "base/task/single_thread_task_executor.h"
Gabriel Charette6f9509b2020-05-27 20:39:5721#include "base/task/thread_pool.h"
[email protected]ce072a72010-12-31 20:02:1622#include "base/threading/platform_thread.h"
stanisc61507092017-07-06 16:36:5823#include "base/timer/hi_res_timer_manager.h"
primiano50b7444c2015-01-28 04:17:0024#include "base/trace_event/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3725#include "build/build_config.h"
Yuta Hijikata76d5cb62020-12-09 09:51:5326#include "build/chromeos_buildflags.h"
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:0627#include "components/viz/service/main/viz_main_impl.h"
[email protected]91a2aea2013-07-08 23:14:3928#include "content/common/content_constants_internal.h"
John Abd-El-Malek884291c2017-08-09 06:43:4829#include "content/common/content_switches_internal.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"
Dale Curtis12cfe022020-02-21 01:18:4250#include "services/tracing/public/cpp/trace_startup.h"
Tom Sepeza0950c62017-10-18 20:39:1451#include "third_party/angle/src/gpu_info_util/SystemInfo.h"
Maksim Sisov459eada2020-06-25 08:16:3152#include "ui/base/ui_base_features.h"
[email protected]c0939182014-05-24 00:09:1953#include "ui/events/platform/platform_event_source.h"
skyostilb354f882016-12-13 18:42:4554#include "ui/gfx/switches.h"
martina.kollarovaa34211d2015-06-25 11:49:0755#include "ui/gl/gl_context.h"
[email protected]db6101db2012-10-25 15:20:0856#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2757#include "ui/gl/gl_surface.h"
58#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2459#include "ui/gl/gpu_switching_manager.h"
kylechar5b9dec12016-05-16 15:40:5760#include "ui/gl/init/gl_factory.h"
[email protected]c0fc0942010-01-13 00:55:3761
avi66a07722015-12-25 23:38:1262#if defined(OS_WIN)
avi66a07722015-12-25 23:38:1263#include <windows.h>
fdoraye716a902016-07-05 16:05:4964#include <dwmapi.h>
avi66a07722015-12-25 23:38:1265#endif
66
primianob3fb6412015-10-14 16:03:5167#if defined(OS_ANDROID)
68#include "base/trace_event/memory_dump_manager.h"
primianoccb26c62016-06-01 21:50:0269#include "components/tracing/common/graphics_memory_dump_provider_android.h"
primianob3fb6412015-10-14 16:03:5170#endif
71
[email protected]d7de57872011-12-06 23:32:4372#if defined(OS_WIN)
Daniel Libby8f7e6262019-01-08 22:35:5573#include "base/trace_event/trace_event_etw_export_win.h"
[email protected]5f7e4512012-10-01 20:51:3774#include "base/win/scoped_com_initializer.h"
kylechar5b9dec12016-05-16 15:40:5775#include "base/win/windows_version.h"
Miguel Casasd697adf32018-02-26 19:06:3176#include "media/gpu/windows/dxva_video_decode_accelerator_win.h"
77#include "media/gpu/windows/media_foundation_video_encode_accelerator_win.h"
[email protected]181491782012-07-18 00:59:1578#include "sandbox/win/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3779#endif
80
[email protected]02ec37a2010-09-20 22:32:1681#if defined(USE_X11)
Tom Andersonb960daf2020-08-19 17:26:1782#include "ui/base/x/x11_ui_thread.h" // nogncheck
Tom Andersonff8d22e2020-01-30 03:19:1683#include "ui/base/x/x11_util.h" // nogncheck
84#include "ui/gfx/linux/gpu_memory_buffer_support_x11.h" // nogncheck
Tom Andersonff8d22e2020-01-30 03:19:1685#include "ui/gfx/x/x11_switches.h" // nogncheck
[email protected]02ec37a2010-09-20 22:32:1686#endif
87
Sean McAllisterbd181782020-08-13 21:05:5188#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Tom Sepez085507ab2017-10-18 22:36:0089#include "content/gpu/gpu_sandbox_hook_linux.h"
[email protected]2436a6b2012-04-13 21:08:5190#include "content/public/common/sandbox_init.h"
Robert Sesek7d0b49b2020-07-08 18:31:2791#include "sandbox/policy/linux/sandbox_linux.h"
[email protected]2436a6b2012-04-13 21:08:5192#endif
93
Avi Drissman7c57be72020-07-29 20:09:4694#if defined(OS_MAC)
[email protected]826aab02014-05-14 02:58:5995#include "base/message_loop/message_pump_mac.h"
Kai Ninomiyae2c55582019-12-19 23:51:0396#include "components/metal_util/device_removal.h"
Christopher Cameron21c4abc2019-11-15 04:06:1597#include "components/metal_util/test_shader.h"
Robert Sesek3bd6e4b32019-11-26 16:02:2798#include "content/public/common/content_features.h"
99#include "media/gpu/mac/vt_video_decode_accelerator_mac.h"
Greg Kerr3480aa82018-02-01 00:53:03100#include "sandbox/mac/seatbelt.h"
Robert Sesek7d0b49b2020-07-08 18:31:27101#include "sandbox/policy/mac/sandbox_mac.h"
[email protected]826aab02014-05-14 02:58:59102#endif
103
Alexandre Courbotc13a5972017-07-23 03:48:48104#if BUILDFLAG(USE_VAAPI)
Miguel Casas42e955c2017-12-04 14:18:12105#include "media/gpu/vaapi/vaapi_wrapper.h"
hshi95837052015-05-12 15:39:51106#endif
107
[email protected]eb398192012-10-22 20:16:19108namespace content {
[email protected]ec4bda62013-06-14 15:51:03109
[email protected]6ec3a572012-08-17 02:09:51110namespace {
[email protected]ec4bda62013-06-14 15:51:03111
Sean McAllisterbd181782020-08-13 21:05:51112#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33113bool StartSandboxLinux(gpu::GpuWatchdogThread*,
114 const gpu::GPUInfo*,
115 const gpu::GpuPreferences&);
[email protected]663c4b32013-04-18 05:52:54116#elif defined(OS_WIN)
117bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
118#endif
[email protected]ec4bda62013-06-14 15:51:03119
sadrul454af332016-09-09 18:14:32120class ContentSandboxHelper : public gpu::GpuSandboxHelper {
121 public:
122 ContentSandboxHelper() {}
123 ~ContentSandboxHelper() override {}
124
125#if defined(OS_WIN)
126 void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) {
127 sandbox_info_ = info;
128 }
129#endif
130
sadrul454af332016-09-09 18:14:32131 private:
132 // SandboxHelper:
Ted Meyer6801a072020-10-28 03:25:02133 void PreSandboxStartup(const gpu::GpuPreferences& gpu_prefs) override {
sadrul454af332016-09-09 18:14:32134 // Warm up resources that don't need access to GPUInfo.
135 {
136 TRACE_EVENT0("gpu", "Warm up rand");
137 // Warm up the random subsystem, which needs to be done pre-sandbox on all
138 // platforms.
139 (void)base::RandUint64();
140 }
141
Alexandre Courbotc13a5972017-07-23 03:48:48142#if BUILDFLAG(USE_VAAPI)
Pilar Molina Lopezfb5103b72021-02-26 23:35:52143#if defined(OS_CHROMEOS)
sadrul454af332016-09-09 18:14:32144 media::VaapiWrapper::PreSandboxInitialization();
Pilar Molina Lopezfb5103b72021-02-26 23:35:52145#else // For Linux with VA-API support.
Ted Meyer6801a072020-10-28 03:25:02146 if (!gpu_prefs.disable_accelerated_video_decode)
Ted Meyer6f266fd2020-10-23 01:54:06147 media::VaapiWrapper::PreSandboxInitialization();
sadrul454af332016-09-09 18:14:32148#endif
Ted Meyer6f266fd2020-10-23 01:54:06149#endif // BUILDFLAG(USE_VAAPI)
sadrul454af332016-09-09 18:14:32150#if defined(OS_WIN)
151 media::DXVAVideoDecodeAccelerator::PreSandboxInitialization();
152 media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization();
153#endif
Eric Karle35050e2017-09-07 01:44:35154
Avi Drissman7c57be72020-07-29 20:09:46155#if defined(OS_MAC)
Robert Sesek3bd6e4b32019-11-26 16:02:27156 if (base::FeatureList::IsEnabled(features::kMacV2GPUSandbox)) {
157 TRACE_EVENT0("gpu", "Initialize VideoToolbox");
158 media::InitializeVideoToolbox();
159 }
160#endif
161
Eric Karle35050e2017-09-07 01:44:35162 // On Linux, reading system memory doesn't work through the GPU sandbox.
163 // This value is cached, so access it here to populate the cache.
164 base::SysInfo::AmountOfPhysicalMemory();
sadrul454af332016-09-09 18:14:32165 }
166
Satyajit Sahu82a76e02017-09-18 14:50:14167 bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33168 const gpu::GPUInfo* gpu_info,
169 const gpu::GpuPreferences& gpu_prefs) override {
Sean McAllisterbd181782020-08-13 21:05:51170#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Zhenyao Moe9187a862017-10-20 04:26:33171 return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs);
sadrul454af332016-09-09 18:14:32172#elif defined(OS_WIN)
173 return StartSandboxWindows(sandbox_info_);
Avi Drissman7c57be72020-07-29 20:09:46174#elif defined(OS_MAC)
Greg Kerr3480aa82018-02-01 00:53:03175 return sandbox::Seatbelt::IsSandboxed();
sadrul454af332016-09-09 18:14:32176#else
177 return false;
178#endif
179 }
180
181#if defined(OS_WIN)
182 const sandbox::SandboxInterfaceInfo* sandbox_info_ = nullptr;
sadrul454af332016-09-09 18:14:32183#endif
184
185 DISALLOW_COPY_AND_ASSIGN(ContentSandboxHelper);
186};
187
Avi Drissman7c57be72020-07-29 20:09:46188#if defined(OS_MAC)
Christopher Camerond22d0152020-05-20 16:47:41189void TestShaderCallback(metal::TestShaderComponent component,
190 metal::TestShaderResult result,
191 const base::TimeDelta& callback_time) {
Christopher Cameron21c4abc2019-11-15 04:06:15192 switch (result) {
193 case metal::TestShaderResult::kNotAttempted:
194 case metal::TestShaderResult::kFailed:
195 // Don't include data if no Metal device was created (e.g, due to hardware
196 // or macOS version reasons).
197 return;
198 case metal::TestShaderResult::kTimedOut:
Christopher Cameronbd7837b42019-11-18 19:34:02199 break;
Christopher Cameron21c4abc2019-11-15 04:06:15200 case metal::TestShaderResult::kSucceeded:
Christopher Cameron21c4abc2019-11-15 04:06:15201 break;
202 }
Christopher Camerond22d0152020-05-20 16:47:41203 switch (component) {
204 case metal::TestShaderComponent::kCompile:
205 UMA_HISTOGRAM_MEDIUM_TIMES("Gpu.Metal.TestShaderCompileTime",
206 callback_time);
207 break;
208 case metal::TestShaderComponent::kLink:
209 UMA_HISTOGRAM_MEDIUM_TIMES("Gpu.Metal.TestShaderLinkTime", callback_time);
210 break;
211 }
Christopher Cameron21c4abc2019-11-15 04:06:15212}
213#endif
214
kylechar476993472016-09-14 16:03:48215} // namespace
[email protected]ec4bda62013-06-14 15:51:03216
[email protected]c0fc0942010-01-13 00:55:37217// Main function for starting the Gpu process.
[email protected]eb398192012-10-22 20:16:19218int GpuMain(const MainFunctionParams& parameters) {
[email protected]d13f35d2012-05-18 02:28:15219 TRACE_EVENT0("gpu", "GpuMain");
Ehsan Chiniforooshana8c8dad2017-11-03 07:23:09220 base::trace_event::TraceLog::GetInstance()->set_process_name("GPU Process");
ssidb2e3ece2015-02-09 16:02:20221 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:39222 kTraceEventGpuProcessSortIndex);
[email protected]d13f35d2012-05-18 02:28:15223
avi83883c82014-12-23 00:08:49224 const base::CommandLine& command_line = parameters.command_line;
Zhenyao Mo910beb82017-10-25 03:23:00225
226 gpu::GpuPreferences gpu_preferences;
227 if (command_line.HasSwitch(switches::kGpuPreferences)) {
228 std::string value =
229 command_line.GetSwitchValueASCII(switches::kGpuPreferences);
Jonathan Backer16cc8fd2018-05-31 19:59:22230 bool success = gpu_preferences.FromSwitchValue(value);
Zhenyao Mo910beb82017-10-25 03:23:00231 CHECK(success);
232 }
233
234 if (gpu_preferences.gpu_startup_dialog)
John Abd-El-Malek884291c2017-08-09 06:43:48235 WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:49236
[email protected]ca23992b02013-06-13 17:25:19237 base::Time start_time = base::Time::Now();
238
[email protected]23f46562011-09-07 01:42:39239#if defined(OS_WIN)
Daniel Libby1700bbe82019-01-30 22:22:36240 base::trace_event::TraceEventETWExport::EnableETWExport();
241
[email protected]52819472013-11-24 22:49:55242 // Prevent Windows from displaying a modal dialog on failures like not being
243 // able to load a DLL.
244 SetErrorMode(
245 SEM_FAILCRITICALERRORS |
246 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 {
[email protected]db6101db2012-10-25 15:20:08276#if defined(OS_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);
Maksim Sisov459eada2020-06-25 08:16:31282#elif defined(USE_X11) || defined(USE_OZONE)
283#if defined(USE_X11)
284 if (!features::IsUsingOzonePlatform()) {
285 // We need a UI loop so that we can grab the Expose events. See
286 // GLSurfaceGLX and https://crbug.com/326995.
Tom Andersone4fc22a2020-10-28 07:14:14287 if (!x11::Connection::Get()->Ready())
Maksim Sisov459eada2020-06-25 08:16:31288 return RESULT_CODE_GPU_DEAD_ON_ARRIVAL;
289 main_thread_task_executor =
290 std::make_unique<base::SingleThreadTaskExecutor>(
291 base::MessagePumpType::UI);
292 event_source = ui::PlatformEventSource::CreateDefault();
Tom Andersonb960daf2020-08-19 17:26:17293 // Set up the X11UiThread before the sandbox gets set up. This cannot be
294 // done later since opening the connection requires socket() and
295 // connect().
296 ui::X11UiThread::SetConnection(x11::Connection::Get()->Clone().release());
Maksim Sisov459eada2020-06-25 08:16:31297 }
298#endif
299#if defined(USE_OZONE)
Alex Clarke636e7052019-05-30 10:49:37300 // The MessagePump type required depends on the Ozone platform selected at
tonikitoofb807b102017-02-08 19:52:03301 // runtime.
Maksim Sisov459eada2020-06-25 08:16:31302 if (!main_thread_task_executor) {
303 main_thread_task_executor =
304 std::make_unique<base::SingleThreadTaskExecutor>(
305 gpu_preferences.message_pump_type);
306 }
307#endif
Sean McAllisterbd181782020-08-13 21:05:51308#elif defined(OS_LINUX) || defined(OS_CHROMEOS)
kylechar476993472016-09-14 16:03:48309#error "Unsupported Linux platform."
Avi Drissman7c57be72020-07-29 20:09:46310#elif defined(OS_MAC)
Christopher Cameron1732f2b02017-11-17 10:56:50311 // Cross-process CoreAnimation requires a CFRunLoop to function at all, and
312 // requires a NSRunLoop to not starve under heavy load. See:
313 // https://crbug.com/312462#c51 and https://crbug.com/783298
Alex Clarke636e7052019-05-30 10:49:37314 main_thread_task_executor =
315 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15316 base::MessagePumpType::NS_RUNLOOP);
Etienne Pierre-doray2163f3012020-04-02 21:37:14317 // As part of the migration to DoWork(), this policy is required to keep
Etienne Pierre-doray9eb4f5a2020-01-15 16:29:24318 // previous behavior and avoid regressions.
319 // TODO(crbug.com/1041853): Consider updating the policy.
320 main_thread_task_executor->SetWorkBatchSize(2);
[email protected]826aab02014-05-14 02:58:59321#else
Alex Clarke636e7052019-05-30 10:49:37322 main_thread_task_executor =
323 std::make_unique<base::SingleThreadTaskExecutor>(
Carlos Caballerodd8bf7b042019-07-30 14:14:15324 base::MessagePumpType::DEFAULT);
[email protected]db6101db2012-10-25 15:20:08325#endif
skyostilb354f882016-12-13 18:42:45326 }
[email protected]db6101db2012-10-25 15:20:08327
[email protected]db6101db2012-10-25 15:20:08328 base::PlatformThread::SetName("CrGpuMain");
329
Avi Drissman7c57be72020-07-29 20:09:46330#if !defined(OS_MAC)
Sunny Sachanandani5cd10e962019-06-13 22:55:32331 if (base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)) {
332 // Set thread priority before sandbox initialization.
333 base::PlatformThread::SetCurrentThreadPriority(
334 base::ThreadPriority::DISPLAY);
335 }
Michael Spang50ed1ff2019-07-02 22:26:56336#endif
revemane7acf842016-02-05 08:24:32337
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57338 auto gpu_init = std::make_unique<gpu::GpuInit>();
sadrul454af332016-09-09 18:14:32339 ContentSandboxHelper sandbox_helper;
340#if defined(OS_WIN)
341 sandbox_helper.set_sandbox_info(parameters.sandbox_info);
[email protected]af7c5d92014-02-03 19:53:15342#endif
[email protected]af7c5d92014-02-03 19:53:15343
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57344 gpu_init->set_sandbox_helper(&sandbox_helper);
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24345
Gabriel Charettec7e363c62020-06-24 17:39:35346 // Since GPU initialization calls into skia, it's important to initialize skia
Khushal1d055592018-07-28 02:00:39347 // before it.
348 InitializeSkia();
349
Gabriel Charettec7e363c62020-06-24 17:39:35350 // Create the ThreadPool before invoking |gpu_init| as it needs the ThreadPool
351 // (in angle::InitializePlatform()). Do not start it until after the sandbox
352 // is initialized however to avoid creating threads outside the sandbox.
353 base::ThreadPoolInstance::Create("GPU");
354
sadrul454af332016-09-09 18:14:32355 // Gpu initialization may fail for various reasons, in which case we will need
356 // to tear down this process. However, we can not do so safely until the IPC
357 // channel is set up, because the detection of early return of a child process
358 // is implemented using an IPC channel error. If the IPC channel is not fully
359 // set up between the browser and GPU process, and the GPU process crashes or
360 // exits early, the browser process will never detect it. For this reason we
sadrul72aae8a2017-01-24 04:52:32361 // defer tearing down the GPU process until receiving the initialization
Sadrul Habib Chowdhury55fc2f02017-10-27 02:40:06362 // message from the browser (through mojom::VizMain::CreateGpuService()).
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57363 const bool init_success = gpu_init->InitializeAndStartSandbox(
Zhenyao Moe23f75262018-02-07 02:15:00364 const_cast<base::CommandLine*>(&command_line), gpu_preferences);
sadrul454af332016-09-09 18:14:32365 const bool dead_on_arrival = !init_success;
Sadrul Habib Chowdhuryc0a4a9b92016-08-29 21:43:24366
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57367 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
[email protected]ec4bda62013-06-14 15:51:03368
Gabriel Charettec7e363c62020-06-24 17:39:35369 // Start the ThreadPoolInstance now that the sandbox is initialized.
370 base::ThreadPoolInstance::Get()->StartWithDefaultParams();
371
Sunny Sachanandani5cd10e962019-06-13 22:55:32372 const base::ThreadPriority io_thread_priority =
373 base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)
374 ? base::ThreadPriority::DISPLAY
375 : base::ThreadPriority::NORMAL;
Avi Drissman7c57be72020-07-29 20:09:46376#if defined(OS_MAC)
behdad8b57eaad2019-07-18 13:58:48377 // Increase the thread priority to get more reliable values in performance
378 // test of mac_os.
379 GpuProcess gpu_process(
380 (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests)
381 ? base::ThreadPriority::REALTIME_AUDIO
382 : io_thread_priority));
383#else
reveman7caf8cf2016-02-16 02:39:05384 GpuProcess gpu_process(io_thread_priority);
behdad8b57eaad2019-07-18 13:58:48385#endif
Xi Chengd6390812018-01-24 00:01:50386
Tom Andersonff8d22e2020-01-30 03:19:16387#if defined(USE_X11)
388 // ui::GbmDevice() takes >50ms with amdgpu, so kick off
389 // GpuMemoryBufferSupportX11 creation on another thread now.
Maksim Sisove8bf9222020-06-24 07:53:02390 if (!features::IsUsingOzonePlatform() &&
391 gpu_preferences.enable_native_gpu_memory_buffers) {
Gabriel Charette6f9509b2020-05-27 20:39:57392 base::ThreadPool::PostTask(
Tom Andersonbe0638f2020-05-21 01:20:33393 FROM_HERE, base::BindOnce([]() {
394 SCOPED_UMA_HISTOGRAM_TIMER("Linux.X11.GbmSupportX11CreationTime");
395 ui::GpuMemoryBufferSupportX11::GetInstance();
396 }));
397 }
Tom Andersonff8d22e2020-01-30 03:19:16398#endif
399
Zhenyao Moe23f75262018-02-07 02:15:00400 auto* client = GetContentClient()->gpu();
Xi Chengd6390812018-01-24 00:01:50401 if (client)
402 client->PostIOThreadCreated(gpu_process.io_task_runner());
403
Wez6979109b2018-09-07 17:30:56404 base::RunLoop run_loop;
405 GpuChildThread* child_thread =
Dale Curtis1b6becebb2020-03-30 20:13:35406 new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init));
[email protected]7a31f7c2011-03-21 23:22:04407 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17408
[email protected]7a31f7c2011-03-21 23:22:04409 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44410
Avi Drissman7c57be72020-07-29 20:09:46411#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MAC)
Dale Curtis12cfe022020-02-21 01:18:42412 // Startup tracing is usually enabled earlier, but if we forked from a zygote,
413 // we can only enable it after mojo IPC support is brought up initialized by
414 // GpuChildThread, because the mojo broker has to create the tracing SMB on
415 // our behalf due to the zygote sandbox.
416 if (parameters.zygote_child)
417 tracing::EnableStartupTracingIfNeeded();
Avi Drissmand4d79f72020-07-30 18:35:18418#endif // OS_POSIX && !OS_ANDROID && !OS_MAC
Dale Curtis12cfe022020-02-21 01:18:42419
Avi Drissman7c57be72020-07-29 20:09:46420#if defined(OS_MAC)
Kai Ninomiyae2c55582019-12-19 23:51:03421 // A GPUEjectPolicy of 'wait' is set in the Info.plist of the browser
422 // process, meaning it is "responsible" for making sure it and its
423 // subordinate processes (i.e. the GPU process) drop references to the
424 // external GPU. Despite this, the system still sends the device removal
425 // notifications to the GPU process, so the GPU process handles its own
426 // graceful shutdown without help from the browser process.
427 //
428 // Using the "SafeEjectGPU" tool, we can see that when the browser process
429 // has a policy of 'wait', the GPU process gets the 'rwait' policy: "Eject
430 // actions apply to the responsible process, who in turn deals with
431 // subordinates to eliminate their ejecting eGPU references" [man 8
432 // SafeEjectGPU]. Empirically, the browser does not relaunch. Once the GPU
433 // process exits, it appears that the browser process is no longer considered
434 // to be using the GPU, so it "succeeds" the 'wait'.
435 metal::RegisterGracefulExitOnDeviceRemoval();
436
Christopher Cameron21c4abc2019-11-15 04:06:15437 // Launch a test metal shader compile to see how long it takes to complete (if
438 // it ever completes).
439 // https://crbug.com/974219
Christopher Camerond362a662019-11-27 17:45:18440 metal::TestShader(base::BindOnce(TestShaderCallback));
Christopher Cameron21c4abc2019-11-15 04:06:15441#endif
442
primianob3fb6412015-10-14 16:03:51443#if defined(OS_ANDROID)
444 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40445 tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics",
446 nullptr);
primianob3fb6412015-10-14 16:03:51447#endif
448
stanisc61507092017-07-06 16:36:58449 base::HighResolutionTimerManager hi_res_timer_manager;
450
[email protected]d13f35d2012-05-18 02:28:15451 {
452 TRACE_EVENT0("gpu", "Run Message Loop");
Wez6979109b2018-09-07 17:30:56453 run_loop.Run();
[email protected]d13f35d2012-05-18 02:28:15454 }
[email protected]c0fc0942010-01-13 00:55:37455
jbaumana19f1df2017-01-18 03:01:17456 return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0;
[email protected]c0fc0942010-01-13 00:55:37457}
[email protected]6ec3a572012-08-17 02:09:51458
459namespace {
460
Sean McAllisterbd181782020-08-13 21:05:51461#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Satyajit Sahu82a76e02017-09-18 14:50:14462bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
Zhenyao Moe9187a862017-10-20 04:26:33463 const gpu::GPUInfo* gpu_info,
464 const gpu::GpuPreferences& gpu_prefs) {
fdorayf30bf3a2015-10-28 21:47:00465 TRACE_EVENT0("gpu,startup", "Initialize sandbox");
[email protected]663c4b32013-04-18 05:52:54466
[email protected]f330b762014-02-08 04:52:46467 if (watchdog_thread) {
Tom Sepez437e2202017-10-24 21:26:47468 // SandboxLinux needs to be able to ensure that the thread
[email protected]f330b762014-02-08 04:52:46469 // has really been stopped.
Robert Sesek7d0b49b2020-07-08 18:31:27470 sandbox::policy::SandboxLinux::GetInstance()->StopThread(watchdog_thread);
[email protected]f330b762014-02-08 04:52:46471 }
[email protected]655abd522014-06-02 15:23:43472
Tom Sepez437e2202017-10-24 21:26:47473 // SandboxLinux::InitializeSandbox() must always be called
[email protected]663c4b32013-04-18 05:52:54474 // with only one thread.
Robert Sesek7d0b49b2020-07-08 18:31:27475 sandbox::policy::SandboxLinux::Options sandbox_options;
Tom Sepeza0950c62017-10-18 20:39:14476 sandbox_options.use_amd_specific_policies =
477 gpu_info && angle::IsAMD(gpu_info->active_gpu().vendor_id);
Brian Hoe7c074832019-12-11 20:20:52478 sandbox_options.use_intel_specific_policies =
479 gpu_info && angle::IsIntel(gpu_info->active_gpu().vendor_id);
Tom Anderson0fd12d92020-10-31 00:55:52480 sandbox_options.use_nvidia_specific_policies =
481 gpu_info && angle::IsNVIDIA(gpu_info->active_gpu().vendor_id);
Zhenyao Moe9187a862017-10-20 04:26:33482 sandbox_options.accelerated_video_decode_enabled =
483 !gpu_prefs.disable_accelerated_video_decode;
Sheng-Hao Tsaoea6aa852018-01-03 06:48:16484 sandbox_options.accelerated_video_encode_enabled =
485 !gpu_prefs.disable_accelerated_video_encode;
Tom Sepez085507ab2017-10-18 22:36:00486
Robert Sesek7d0b49b2020-07-08 18:31:27487 bool res = sandbox::policy::SandboxLinux::GetInstance()->InitializeSandbox(
488 sandbox::policy::SandboxTypeFromCommandLine(
Tom Sepez2255db72017-10-26 18:50:09489 *base::CommandLine::ForCurrentProcess()),
Tom Sepez4b101712017-11-08 19:39:58490 base::BindOnce(GpuProcessPreSandboxHook), sandbox_options);
Tom Sepeza0950c62017-10-18 20:39:14491
[email protected]f330b762014-02-08 04:52:46492 if (watchdog_thread) {
Tom Sepeza0950c62017-10-18 20:39:14493 base::Thread::Options thread_options;
494 thread_options.timer_slack = base::TIMER_SLACK_MAXIMUM;
495 watchdog_thread->StartWithOptions(thread_options);
[email protected]f330b762014-02-08 04:52:46496 }
[email protected]663c4b32013-04-18 05:52:54497
498 return res;
499}
Sean McAllisterbd181782020-08-13 21:05:51500#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
[email protected]663c4b32013-04-18 05:52:54501
502#if defined(OS_WIN)
503bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
fdorayf30bf3a2015-10-28 21:47:00504 TRACE_EVENT0("gpu,startup", "Lower token");
[email protected]663c4b32013-04-18 05:52:54505
506 // For Windows, if the target_services interface is not zero, the process
507 // is sandboxed and we must call LowerToken() before rendering untrusted
508 // content.
509 sandbox::TargetServices* target_services = sandbox_info->target_services;
510 if (target_services) {
511 target_services->LowerToken();
512 return true;
513 }
514
515 return false;
516}
517#endif // defined(OS_WIN)
518
[email protected]6ec3a572012-08-17 02:09:51519} // namespace.
520
[email protected]eb398192012-10-22 20:16:19521} // namespace content