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