[email protected] | 2e3d9e4 | 2012-02-14 03:23:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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> |
dcheng | f26eed3 | 2016-01-13 10:58:14 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 11 | #include "base/bind.h" |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 12 | #include "base/feature_list.h" |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 15 | #include "base/rand_util.h" |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame] | 16 | #include "base/run_loop.h" |
[email protected] | d0ea478 | 2013-06-11 04:58:24 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
| 18 | #include "base/strings/stringprintf.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 19 | #include "base/system/sys_info.h" |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 20 | #include "base/task/single_thread_task_executor.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 21 | #include "base/threading/platform_thread.h" |
stanisc | 6150709 | 2017-07-06 16:36:58 | [diff] [blame] | 22 | #include "base/timer/hi_res_timer_manager.h" |
primiano | 50b7444c | 2015-01-28 04:17:00 | [diff] [blame] | 23 | #include "base/trace_event/trace_event.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 24 | #include "build/build_config.h" |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 25 | #include "components/viz/service/main/viz_main_impl.h" |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 26 | #include "content/common/content_constants_internal.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 27 | #include "content/common/content_switches_internal.h" |
Khushal | a4e236f | 2018-06-01 03:00:46 | [diff] [blame] | 28 | #include "content/common/skia_utils.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 29 | #include "content/gpu/gpu_child_thread.h" |
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 30 | #include "content/gpu/gpu_process.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 31 | #include "content/public/common/content_client.h" |
| 32 | #include "content/public/common/content_switches.h" |
| 33 | #include "content/public/common/main_function_params.h" |
jbauman | a19f1df | 2017-01-18 03:01:17 | [diff] [blame] | 34 | #include "content/public/common/result_codes.h" |
Zhenyao Mo | c76e903 | 2018-01-19 21:15:46 | [diff] [blame] | 35 | #include "content/public/gpu/content_gpu_client.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame] | 36 | #include "gpu/command_buffer/service/gpu_switches.h" |
sadrul | ed39592 | 2016-09-07 17:32:58 | [diff] [blame] | 37 | #include "gpu/config/gpu_driver_bug_list.h" |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 38 | #include "gpu/config/gpu_finch_features.h" |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 39 | #include "gpu/config/gpu_info_collector.h" |
Jonathan Backer | 16cc8fd | 2018-05-31 19:59:22 | [diff] [blame] | 40 | #include "gpu/config/gpu_preferences.h" |
tfarina | 15525c4 | 2015-04-28 19:04:16 | [diff] [blame] | 41 | #include "gpu/config/gpu_switches.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame] | 42 | #include "gpu/config/gpu_util.h" |
fsamuel | c277422 | 2016-03-24 00:27:12 | [diff] [blame] | 43 | #include "gpu/ipc/common/gpu_memory_buffer_support.h" |
markdittmer | d88b835 | 2016-04-08 15:28:45 | [diff] [blame] | 44 | #include "gpu/ipc/service/gpu_config.h" |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 45 | #include "gpu/ipc/service/gpu_init.h" |
sadrul | 2fb7e15 | 2016-08-30 05:21:45 | [diff] [blame] | 46 | #include "gpu/ipc/service/gpu_watchdog_thread.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 47 | #include "media/gpu/buildflags.h" |
Oystein Eftevaag | 424fd37 | 2019-06-07 17:51:03 | [diff] [blame] | 48 | #include "services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h" |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 49 | #include "third_party/angle/src/gpu_info_util/SystemInfo.h" |
[email protected] | c093918 | 2014-05-24 00:09:19 | [diff] [blame] | 50 | #include "ui/events/platform/platform_event_source.h" |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 51 | #include "ui/gfx/switches.h" |
martina.kollarova | a34211d | 2015-06-25 11:49:07 | [diff] [blame] | 52 | #include "ui/gl/gl_context.h" |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 53 | #include "ui/gl/gl_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 54 | #include "ui/gl/gl_surface.h" |
| 55 | #include "ui/gl/gl_switches.h" |
[email protected] | 1bb06b0 | 2012-09-23 19:37:24 | [diff] [blame] | 56 | #include "ui/gl/gpu_switching_manager.h" |
kylechar | 5b9dec1 | 2016-05-16 15:40:57 | [diff] [blame] | 57 | #include "ui/gl/init/gl_factory.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 58 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 59 | #if defined(OS_WIN) |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 60 | #include <windows.h> |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame] | 61 | #include <dwmapi.h> |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 62 | #endif |
| 63 | |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 64 | #if defined(OS_ANDROID) |
| 65 | #include "base/trace_event/memory_dump_manager.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 66 | #include "components/tracing/common/graphics_memory_dump_provider_android.h" |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 67 | #endif |
| 68 | |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 69 | #if defined(OS_WIN) |
Daniel Libby | 8f7e626 | 2019-01-08 22:35:55 | [diff] [blame] | 70 | #include "base/trace_event/trace_event_etw_export_win.h" |
[email protected] | 5f7e451 | 2012-10-01 20:51:37 | [diff] [blame] | 71 | #include "base/win/scoped_com_initializer.h" |
kylechar | 5b9dec1 | 2016-05-16 15:40:57 | [diff] [blame] | 72 | #include "base/win/windows_version.h" |
Miguel Casas | d697adf3 | 2018-02-26 19:06:31 | [diff] [blame] | 73 | #include "media/gpu/windows/dxva_video_decode_accelerator_win.h" |
| 74 | #include "media/gpu/windows/media_foundation_video_encode_accelerator_win.h" |
[email protected] | 18149178 | 2012-07-18 00:59:15 | [diff] [blame] | 75 | #include "sandbox/win/src/sandbox.h" |
[email protected] | 802a13a0 | 2010-12-02 01:48:37 | [diff] [blame] | 76 | #endif |
| 77 | |
[email protected] | 02ec37a | 2010-09-20 22:32:16 | [diff] [blame] | 78 | #if defined(USE_X11) |
kylechar | b8d249c | 2017-12-15 00:04:18 | [diff] [blame] | 79 | #include "ui/base/x/x11_util.h" // nogncheck |
| 80 | #include "ui/gfx/x/x11_connection.h" // nogncheck |
| 81 | #include "ui/gfx/x/x11_switches.h" // nogncheck |
[email protected] | 02ec37a | 2010-09-20 22:32:16 | [diff] [blame] | 82 | #endif |
| 83 | |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 84 | #if defined(OS_LINUX) |
Tom Sepez | 085507ab | 2017-10-18 22:36:00 | [diff] [blame] | 85 | #include "content/gpu/gpu_sandbox_hook_linux.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 86 | #include "content/public/common/sandbox_init.h" |
Tom Sepez | 903f7f1 | 2017-10-26 17:34:02 | [diff] [blame] | 87 | #include "services/service_manager/sandbox/linux/sandbox_linux.h" |
Jay Civelli | 668c097f | 2018-05-16 20:44:13 | [diff] [blame] | 88 | #include "services/service_manager/zygote/common/common_sandbox_support_linux.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 89 | #endif |
| 90 | |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 91 | #if defined(OS_MACOSX) |
| 92 | #include "base/message_loop/message_pump_mac.h" |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 93 | #include "sandbox/mac/seatbelt.h" |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame] | 94 | #include "services/service_manager/sandbox/mac/sandbox_mac.h" |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 95 | #endif |
| 96 | |
Alexandre Courbot | c13a597 | 2017-07-23 03:48:48 | [diff] [blame] | 97 | #if BUILDFLAG(USE_VAAPI) |
Miguel Casas | 42e955c | 2017-12-04 14:18:12 | [diff] [blame] | 98 | #include "media/gpu/vaapi/vaapi_wrapper.h" |
hshi | 9583705 | 2015-05-12 15:39:51 | [diff] [blame] | 99 | #endif |
| 100 | |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 101 | #if defined(OS_MACOSX) |
| 102 | extern "C" { |
| 103 | void _LSSetApplicationLaunchServicesServerConnectionStatus( |
| 104 | uint64_t flags, |
| 105 | bool (^connection_allowed)(CFDictionaryRef)); |
Nico Weber | e86ed9b3 | 2019-02-22 18:11:12 | [diff] [blame] | 106 | } |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 107 | #endif // defined(OS_MACOSX) |
| 108 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 109 | namespace content { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 110 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 111 | namespace { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 112 | |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 113 | #if defined(OS_LINUX) |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 114 | bool StartSandboxLinux(gpu::GpuWatchdogThread*, |
| 115 | const gpu::GPUInfo*, |
| 116 | const gpu::GpuPreferences&); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 117 | #elif defined(OS_WIN) |
| 118 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*); |
| 119 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 120 | |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 121 | base::LazyInstance<viz::VizMainImpl::LogMessages>::DestructorAtExit |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 122 | deferred_messages = LAZY_INSTANCE_INITIALIZER; |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 123 | |
| 124 | bool GpuProcessLogMessageHandler(int severity, |
| 125 | const char* file, int line, |
| 126 | size_t message_start, |
| 127 | const std::string& str) { |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 128 | viz::VizMainImpl::LogMessage log; |
sadrul | db04bc3 | 2017-01-04 04:50:30 | [diff] [blame] | 129 | log.severity = severity; |
| 130 | log.header = str.substr(0, message_start); |
| 131 | log.message = str.substr(message_start); |
sadrul | 041ca72 | 2017-03-24 22:35:35 | [diff] [blame] | 132 | deferred_messages.Get().push_back(std::move(log)); |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 133 | return false; |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 134 | } |
| 135 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 136 | class ContentSandboxHelper : public gpu::GpuSandboxHelper { |
| 137 | public: |
| 138 | ContentSandboxHelper() {} |
| 139 | ~ContentSandboxHelper() override {} |
| 140 | |
| 141 | #if defined(OS_WIN) |
| 142 | void set_sandbox_info(const sandbox::SandboxInterfaceInfo* info) { |
| 143 | sandbox_info_ = info; |
| 144 | } |
| 145 | #endif |
| 146 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 147 | private: |
| 148 | // SandboxHelper: |
| 149 | void PreSandboxStartup() override { |
| 150 | // Warm up resources that don't need access to GPUInfo. |
| 151 | { |
| 152 | TRACE_EVENT0("gpu", "Warm up rand"); |
| 153 | // Warm up the random subsystem, which needs to be done pre-sandbox on all |
| 154 | // platforms. |
| 155 | (void)base::RandUint64(); |
| 156 | } |
| 157 | |
Alexandre Courbot | c13a597 | 2017-07-23 03:48:48 | [diff] [blame] | 158 | #if BUILDFLAG(USE_VAAPI) |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 159 | media::VaapiWrapper::PreSandboxInitialization(); |
| 160 | #endif |
| 161 | #if defined(OS_WIN) |
| 162 | media::DXVAVideoDecodeAccelerator::PreSandboxInitialization(); |
| 163 | media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization(); |
| 164 | #endif |
Eric Karl | e35050e | 2017-09-07 01:44:35 | [diff] [blame] | 165 | |
| 166 | // On Linux, reading system memory doesn't work through the GPU sandbox. |
| 167 | // This value is cached, so access it here to populate the cache. |
| 168 | base::SysInfo::AmountOfPhysicalMemory(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 169 | } |
| 170 | |
Satyajit Sahu | 82a76e0 | 2017-09-18 14:50:14 | [diff] [blame] | 171 | bool EnsureSandboxInitialized(gpu::GpuWatchdogThread* watchdog_thread, |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 172 | const gpu::GPUInfo* gpu_info, |
| 173 | const gpu::GpuPreferences& gpu_prefs) override { |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 174 | #if defined(OS_LINUX) |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 175 | return StartSandboxLinux(watchdog_thread, gpu_info, gpu_prefs); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 176 | #elif defined(OS_WIN) |
| 177 | return StartSandboxWindows(sandbox_info_); |
| 178 | #elif defined(OS_MACOSX) |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 179 | return sandbox::Seatbelt::IsSandboxed(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 180 | #else |
| 181 | return false; |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | #if defined(OS_WIN) |
| 186 | const sandbox::SandboxInterfaceInfo* sandbox_info_ = nullptr; |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | DISALLOW_COPY_AND_ASSIGN(ContentSandboxHelper); |
| 190 | }; |
| 191 | |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 192 | } // namespace |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 193 | |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 194 | // Main function for starting the Gpu process. |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 195 | int GpuMain(const MainFunctionParams& parameters) { |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 196 | TRACE_EVENT0("gpu", "GpuMain"); |
Ehsan Chiniforooshan | a8c8dad | 2017-11-03 07:23:09 | [diff] [blame] | 197 | base::trace_event::TraceLog::GetInstance()->set_process_name("GPU Process"); |
ssid | b2e3ece | 2015-02-09 16:02:20 | [diff] [blame] | 198 | base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 199 | kTraceEventGpuProcessSortIndex); |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 200 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 201 | const base::CommandLine& command_line = parameters.command_line; |
Zhenyao Mo | 910beb8 | 2017-10-25 03:23:00 | [diff] [blame] | 202 | |
| 203 | gpu::GpuPreferences gpu_preferences; |
| 204 | if (command_line.HasSwitch(switches::kGpuPreferences)) { |
| 205 | std::string value = |
| 206 | command_line.GetSwitchValueASCII(switches::kGpuPreferences); |
Jonathan Backer | 16cc8fd | 2018-05-31 19:59:22 | [diff] [blame] | 207 | bool success = gpu_preferences.FromSwitchValue(value); |
Zhenyao Mo | 910beb8 | 2017-10-25 03:23:00 | [diff] [blame] | 208 | CHECK(success); |
| 209 | } |
| 210 | |
| 211 | if (gpu_preferences.gpu_startup_dialog) |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 212 | WaitForDebugger("Gpu"); |
[email protected] | 6b889fb | 2010-03-23 20:09:49 | [diff] [blame] | 213 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 214 | base::Time start_time = base::Time::Now(); |
| 215 | |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 216 | #if defined(OS_WIN) |
Daniel Libby | 1700bbe8 | 2019-01-30 22:22:36 | [diff] [blame] | 217 | base::trace_event::TraceEventETWExport::EnableETWExport(); |
| 218 | |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 219 | // Prevent Windows from displaying a modal dialog on failures like not being |
| 220 | // able to load a DLL. |
| 221 | SetErrorMode( |
| 222 | SEM_FAILCRITICALERRORS | |
| 223 | SEM_NOGPFAULTERRORBOX | |
| 224 | SEM_NOOPENFILEERRORBOX); |
Robert Liao | 98bb92d | 2017-06-15 22:20:39 | [diff] [blame] | 225 | |
| 226 | // COM is used by some Windows Media Foundation calls made on this thread and |
| 227 | // must be MTA so we don't have to worry about pumping messages to handle |
| 228 | // COM callbacks. |
| 229 | base::win::ScopedCOMInitializer com_initializer( |
| 230 | base::win::ScopedCOMInitializer::kMTA); |
Sunny Sachanandani | 35c727c | 2019-07-09 13:12:52 | [diff] [blame] | 231 | |
| 232 | if (base::FeatureList::IsEnabled(features::kGpuProcessHighPriorityWin)) |
Sunny Sachanandani | 49bdbad | 2019-07-17 19:15:10 | [diff] [blame] | 233 | ::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 234 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 235 | |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 236 | logging::SetLogMessageHandler(GpuProcessLogMessageHandler); |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 237 | |
ericrk | 1d9e17f | 2016-11-30 01:51:28 | [diff] [blame] | 238 | // We are experiencing what appear to be memory-stomp issues in the GPU |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 239 | // process. These issues seem to be impacting the task executor and listeners |
| 240 | // registered to it. Create the task executor on the heap to guard against |
ericrk | 1d9e17f | 2016-11-30 01:51:28 | [diff] [blame] | 241 | // this. |
| 242 | // TODO(ericrk): Revisit this once we assess its impact on crbug.com/662802 |
| 243 | // and crbug.com/609252. |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 244 | std::unique_ptr<base::SingleThreadTaskExecutor> main_thread_task_executor; |
skyostil | 82befc5 | 2016-12-19 13:48:59 | [diff] [blame] | 245 | std::unique_ptr<ui::PlatformEventSource> event_source; |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 246 | if (command_line.HasSwitch(switches::kHeadless)) { |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 247 | main_thread_task_executor = |
| 248 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 249 | base::MessagePump::Type::DEFAULT); |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 250 | } else { |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 251 | #if defined(OS_WIN) |
Robert Liao | 98bb92d | 2017-06-15 22:20:39 | [diff] [blame] | 252 | // The GpuMain thread should not be pumping Windows messages because no UI |
| 253 | // is expected to run on this thread. |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 254 | main_thread_task_executor = |
| 255 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 256 | base::MessagePump::Type::DEFAULT); |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 257 | #elif defined(USE_X11) |
kylechar | b8d249c | 2017-12-15 00:04:18 | [diff] [blame] | 258 | // Depending on how Chrome is running there are multiple threads that can |
| 259 | // make Xlib function calls. Call XInitThreads() here to be safe, even if |
| 260 | // some configurations don't strictly need it. |
| 261 | gfx::InitializeThreadedX11(); |
| 262 | |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 263 | // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX |
| 264 | // and https://crbug.com/326995. |
thomasanderson | a8d3f52b | 2017-04-17 21:57:02 | [diff] [blame] | 265 | ui::SetDefaultX11ErrorHandlers(); |
| 266 | if (!gfx::GetXDisplay()) |
| 267 | return RESULT_CODE_GPU_DEAD_ON_ARRIVAL; |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 268 | main_thread_task_executor = |
| 269 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 270 | base::MessagePump::Type::UI); |
skyostil | 82befc5 | 2016-12-19 13:48:59 | [diff] [blame] | 271 | event_source = ui::PlatformEventSource::CreateDefault(); |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 272 | #elif defined(USE_OZONE) |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 273 | // The MessagePump type required depends on the Ozone platform selected at |
tonikitoo | fb807b10 | 2017-02-08 19:52:03 | [diff] [blame] | 274 | // runtime. |
Mohsen Izadi | d66900e | 2019-07-16 13:50:15 | [diff] [blame] | 275 | main_thread_task_executor = |
| 276 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 277 | gpu_preferences.message_loop_type); |
kylechar | 47699347 | 2016-09-14 16:03:48 | [diff] [blame] | 278 | #elif defined(OS_LINUX) |
| 279 | #error "Unsupported Linux platform." |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 280 | #elif defined(OS_MACOSX) |
Christopher Cameron | 1732f2b0 | 2017-11-17 10:56:50 | [diff] [blame] | 281 | // Cross-process CoreAnimation requires a CFRunLoop to function at all, and |
| 282 | // requires a NSRunLoop to not starve under heavy load. See: |
| 283 | // https://crbug.com/312462#c51 and https://crbug.com/783298 |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 284 | main_thread_task_executor = |
| 285 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 286 | base::MessagePump::Type::NS_RUNLOOP); |
Greg Kerr | 3480aa8 | 2018-02-01 00:53:03 | [diff] [blame] | 287 | |
| 288 | // Tell LaunchServices to continue without a connection to the daemon. |
| 289 | _LSSetApplicationLaunchServicesServerConnectionStatus(0, nullptr); |
[email protected] | 826aab0 | 2014-05-14 02:58:59 | [diff] [blame] | 290 | #else |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 291 | main_thread_task_executor = |
| 292 | std::make_unique<base::SingleThreadTaskExecutor>( |
| 293 | base::MessagePump::Type::DEFAULT); |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 294 | #endif |
skyostil | b354f88 | 2016-12-13 18:42:45 | [diff] [blame] | 295 | } |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 296 | |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 297 | base::PlatformThread::SetName("CrGpuMain"); |
| 298 | |
Michael Spang | 50ed1ff | 2019-07-02 22:26:56 | [diff] [blame] | 299 | #if !defined(OS_MACOSX) |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 300 | if (base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)) { |
| 301 | // Set thread priority before sandbox initialization. |
| 302 | base::PlatformThread::SetCurrentThreadPriority( |
| 303 | base::ThreadPriority::DISPLAY); |
| 304 | } |
Michael Spang | 50ed1ff | 2019-07-02 22:26:56 | [diff] [blame] | 305 | #endif |
reveman | e7acf84 | 2016-02-05 08:24:32 | [diff] [blame] | 306 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 307 | auto gpu_init = std::make_unique<gpu::GpuInit>(); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 308 | ContentSandboxHelper sandbox_helper; |
| 309 | #if defined(OS_WIN) |
| 310 | sandbox_helper.set_sandbox_info(parameters.sandbox_info); |
[email protected] | af7c5d9 | 2014-02-03 19:53:15 | [diff] [blame] | 311 | #endif |
[email protected] | af7c5d9 | 2014-02-03 19:53:15 | [diff] [blame] | 312 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 313 | gpu_init->set_sandbox_helper(&sandbox_helper); |
Sadrul Habib Chowdhury | c0a4a9b9 | 2016-08-29 21:43:24 | [diff] [blame] | 314 | |
Khushal | 1d05559 | 2018-07-28 02:00:39 | [diff] [blame] | 315 | // Since GPU initialization calls into skia, its important to initialize skia |
| 316 | // before it. |
| 317 | InitializeSkia(); |
| 318 | |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 319 | // Gpu initialization may fail for various reasons, in which case we will need |
| 320 | // to tear down this process. However, we can not do so safely until the IPC |
| 321 | // channel is set up, because the detection of early return of a child process |
| 322 | // is implemented using an IPC channel error. If the IPC channel is not fully |
| 323 | // set up between the browser and GPU process, and the GPU process crashes or |
| 324 | // exits early, the browser process will never detect it. For this reason we |
sadrul | 72aae8a | 2017-01-24 04:52:32 | [diff] [blame] | 325 | // defer tearing down the GPU process until receiving the initialization |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 326 | // message from the browser (through mojom::VizMain::CreateGpuService()). |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 327 | const bool init_success = gpu_init->InitializeAndStartSandbox( |
Zhenyao Mo | e23f7526 | 2018-02-07 02:15:00 | [diff] [blame] | 328 | const_cast<base::CommandLine*>(&command_line), gpu_preferences); |
sadrul | 454af333 | 2016-09-09 18:14:32 | [diff] [blame] | 329 | const bool dead_on_arrival = !init_success; |
Sadrul Habib Chowdhury | c0a4a9b9 | 2016-08-29 21:43:24 | [diff] [blame] | 330 | |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 331 | logging::SetLogMessageHandler(nullptr); |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 332 | GetContentClient()->SetGpuInfo(gpu_init->gpu_info()); |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 333 | |
Sunny Sachanandani | 5cd10e96 | 2019-06-13 22:55:32 | [diff] [blame] | 334 | const base::ThreadPriority io_thread_priority = |
| 335 | base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority) |
| 336 | ? base::ThreadPriority::DISPLAY |
| 337 | : base::ThreadPriority::NORMAL; |
behdad | 8b57eaad | 2019-07-18 13:58:48 | [diff] [blame^] | 338 | #if defined(OS_MACOSX) |
| 339 | // Increase the thread priority to get more reliable values in performance |
| 340 | // test of mac_os. |
| 341 | GpuProcess gpu_process( |
| 342 | (command_line.HasSwitch(switches::kUseHighGPUThreadPriorityForPerfTests) |
| 343 | ? base::ThreadPriority::REALTIME_AUDIO |
| 344 | : io_thread_priority)); |
| 345 | #else |
reveman | 7caf8cf | 2016-02-16 02:39:05 | [diff] [blame] | 346 | GpuProcess gpu_process(io_thread_priority); |
behdad | 8b57eaad | 2019-07-18 13:58:48 | [diff] [blame^] | 347 | #endif |
Xi Cheng | d639081 | 2018-01-24 00:01:50 | [diff] [blame] | 348 | |
Zhenyao Mo | e23f7526 | 2018-02-07 02:15:00 | [diff] [blame] | 349 | auto* client = GetContentClient()->gpu(); |
Xi Cheng | d639081 | 2018-01-24 00:01:50 | [diff] [blame] | 350 | if (client) |
| 351 | client->PostIOThreadCreated(gpu_process.io_task_runner()); |
| 352 | |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 353 | base::RunLoop run_loop; |
| 354 | GpuChildThread* child_thread = |
| 355 | new GpuChildThread(run_loop.QuitClosure(), std::move(gpu_init), |
| 356 | std::move(deferred_messages.Get())); |
sadrul | 041ca72 | 2017-03-24 22:35:35 | [diff] [blame] | 357 | deferred_messages.Get().clear(); |
[email protected] | 8fe0ec52 | 2011-03-03 00:31:33 | [diff] [blame] | 358 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 359 | child_thread->Init(start_time); |
[email protected] | 995a7f1 | 2011-02-11 23:07:17 | [diff] [blame] | 360 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 361 | gpu_process.set_main_thread(child_thread); |
[email protected] | 983c33d | 2010-11-16 22:38:44 | [diff] [blame] | 362 | |
Etienne Bergeron | eba91421 | 2018-09-27 21:26:20 | [diff] [blame] | 363 | // Setup tracing sampler profiler as early as possible. |
Oystein Eftevaag | 71022d60 | 2019-07-12 02:41:29 | [diff] [blame] | 364 | tracing::TracingSamplerProfiler::CreateForCurrentThread(); |
Etienne Bergeron | eba91421 | 2018-09-27 21:26:20 | [diff] [blame] | 365 | |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 366 | #if defined(OS_ANDROID) |
| 367 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
primiano | 186d6bfe | 2015-10-30 13:21:40 | [diff] [blame] | 368 | tracing::GraphicsMemoryDumpProvider::GetInstance(), "AndroidGraphics", |
| 369 | nullptr); |
primiano | b3fb641 | 2015-10-14 16:03:51 | [diff] [blame] | 370 | #endif |
| 371 | |
stanisc | 6150709 | 2017-07-06 16:36:58 | [diff] [blame] | 372 | base::HighResolutionTimerManager hi_res_timer_manager; |
| 373 | |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 374 | { |
| 375 | TRACE_EVENT0("gpu", "Run Message Loop"); |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 376 | run_loop.Run(); |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 377 | } |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 378 | |
jbauman | a19f1df | 2017-01-18 03:01:17 | [diff] [blame] | 379 | return dead_on_arrival ? RESULT_CODE_GPU_DEAD_ON_ARRIVAL : 0; |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 380 | } |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 381 | |
| 382 | namespace { |
| 383 | |
[email protected] | 59a7ae4e | 2012-10-01 23:54:44 | [diff] [blame] | 384 | #if defined(OS_LINUX) |
Satyajit Sahu | 82a76e0 | 2017-09-18 14:50:14 | [diff] [blame] | 385 | bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread, |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 386 | const gpu::GPUInfo* gpu_info, |
| 387 | const gpu::GpuPreferences& gpu_prefs) { |
fdoray | f30bf3a | 2015-10-28 21:47:00 | [diff] [blame] | 388 | TRACE_EVENT0("gpu,startup", "Initialize sandbox"); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 389 | |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 390 | if (watchdog_thread) { |
Tom Sepez | 437e220 | 2017-10-24 21:26:47 | [diff] [blame] | 391 | // SandboxLinux needs to be able to ensure that the thread |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 392 | // has really been stopped. |
Tom Sepez | a2cba006 | 2017-11-07 17:22:43 | [diff] [blame] | 393 | service_manager::SandboxLinux::GetInstance()->StopThread(watchdog_thread); |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 394 | } |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 395 | |
Tom Sepez | 437e220 | 2017-10-24 21:26:47 | [diff] [blame] | 396 | // SandboxLinux::InitializeSandbox() must always be called |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 397 | // with only one thread. |
Tom Sepez | a2cba006 | 2017-11-07 17:22:43 | [diff] [blame] | 398 | service_manager::SandboxLinux::Options sandbox_options; |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 399 | sandbox_options.use_amd_specific_policies = |
| 400 | gpu_info && angle::IsAMD(gpu_info->active_gpu().vendor_id); |
Zhenyao Mo | e9187a86 | 2017-10-20 04:26:33 | [diff] [blame] | 401 | sandbox_options.accelerated_video_decode_enabled = |
| 402 | !gpu_prefs.disable_accelerated_video_decode; |
Sheng-Hao Tsao | ea6aa85 | 2018-01-03 06:48:16 | [diff] [blame] | 403 | sandbox_options.accelerated_video_encode_enabled = |
| 404 | !gpu_prefs.disable_accelerated_video_encode; |
Tom Sepez | 085507ab | 2017-10-18 22:36:00 | [diff] [blame] | 405 | |
Tom Sepez | a2cba006 | 2017-11-07 17:22:43 | [diff] [blame] | 406 | bool res = service_manager::SandboxLinux::GetInstance()->InitializeSandbox( |
Tom Sepez | 2255db7 | 2017-10-26 18:50:09 | [diff] [blame] | 407 | service_manager::SandboxTypeFromCommandLine( |
| 408 | *base::CommandLine::ForCurrentProcess()), |
Tom Sepez | 4b10171 | 2017-11-08 19:39:58 | [diff] [blame] | 409 | base::BindOnce(GpuProcessPreSandboxHook), sandbox_options); |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 410 | |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 411 | if (watchdog_thread) { |
Tom Sepez | a0950c6 | 2017-10-18 20:39:14 | [diff] [blame] | 412 | base::Thread::Options thread_options; |
| 413 | thread_options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
| 414 | watchdog_thread->StartWithOptions(thread_options); |
[email protected] | f330b76 | 2014-02-08 04:52:46 | [diff] [blame] | 415 | } |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 416 | |
| 417 | return res; |
| 418 | } |
| 419 | #endif // defined(OS_LINUX) |
| 420 | |
| 421 | #if defined(OS_WIN) |
| 422 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) { |
fdoray | f30bf3a | 2015-10-28 21:47:00 | [diff] [blame] | 423 | TRACE_EVENT0("gpu,startup", "Lower token"); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 424 | |
| 425 | // For Windows, if the target_services interface is not zero, the process |
| 426 | // is sandboxed and we must call LowerToken() before rendering untrusted |
| 427 | // content. |
| 428 | sandbox::TargetServices* target_services = sandbox_info->target_services; |
| 429 | if (target_services) { |
| 430 | target_services->LowerToken(); |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | return false; |
| 435 | } |
| 436 | #endif // defined(OS_WIN) |
| 437 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 438 | } // namespace. |
| 439 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 440 | } // namespace content |