[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 | |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | |
[email protected] | cafd0b6 | 2011-01-19 01:22:05 | [diff] [blame] | 7 | #if defined(OS_WIN) |
[email protected] | b5b9bfc | 2013-09-07 02:09:07 | [diff] [blame] | 8 | #include <dwmapi.h> |
[email protected] | cafd0b6 | 2011-01-19 01:22:05 | [diff] [blame] | 9 | #include <windows.h> |
| 10 | #endif |
| 11 | |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 12 | #include "base/debug/trace_event.h" |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
[email protected] | e55f564 | 2013-07-18 00:22:54 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 15 | #include "base/rand_util.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" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 18 | #include "base/threading/platform_thread.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 10208ea | 2013-06-06 20:08:03 | [diff] [blame] | 20 | #include "content/child/child_process.h" |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 21 | #include "content/common/content_constants_internal.h" |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 22 | #include "content/common/gpu/gpu_config.h" |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 23 | #include "content/common/gpu/gpu_messages.h" |
[email protected] | 1b73f77e | 2013-04-13 02:00:47 | [diff] [blame] | 24 | #include "content/common/sandbox_linux.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 25 | #include "content/gpu/gpu_child_thread.h" |
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 26 | #include "content/gpu/gpu_process.h" |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 27 | #include "content/gpu/gpu_watchdog_thread.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 28 | #include "content/public/common/content_client.h" |
| 29 | #include "content/public/common/content_switches.h" |
| 30 | #include "content/public/common/main_function_params.h" |
[email protected] | 5fa097a | 2012-05-10 21:59:07 | [diff] [blame] | 31 | #include "crypto/hmac.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame^] | 32 | #include "gpu/command_buffer/service/gpu_switches.h" |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 33 | #include "gpu/config/gpu_info_collector.h" |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame^] | 34 | #include "gpu/config/gpu_util.h" |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 35 | #include "ui/gl/gl_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 36 | #include "ui/gl/gl_surface.h" |
| 37 | #include "ui/gl/gl_switches.h" |
[email protected] | 1bb06b0 | 2012-09-23 19:37:24 | [diff] [blame] | 38 | #include "ui/gl/gpu_switching_manager.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 39 | |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 40 | #if defined(OS_WIN) |
[email protected] | b5b9bfc | 2013-09-07 02:09:07 | [diff] [blame] | 41 | #include "base/win/windows_version.h" |
[email protected] | 5f7e451 | 2012-10-01 20:51:37 | [diff] [blame] | 42 | #include "base/win/scoped_com_initializer.h" |
[email protected] | 7ad3a435 | 2011-12-22 22:18:00 | [diff] [blame] | 43 | #include "content/common/gpu/media/dxva_video_decode_accelerator.h" |
[email protected] | 18149178 | 2012-07-18 00:59:15 | [diff] [blame] | 44 | #include "sandbox/win/src/sandbox.h" |
[email protected] | 106e38b | 2013-03-11 23:16:45 | [diff] [blame] | 45 | #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
[email protected] | 10f5d59 | 2013-01-16 13:21:12 | [diff] [blame] | 46 | #include "content/common/gpu/media/exynos_video_decode_accelerator.h" |
[email protected] | 106e38b | 2013-03-11 23:16:45 | [diff] [blame] | 47 | #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
[email protected] | fd99f1f | 2013-05-31 06:52:41 | [diff] [blame] | 48 | #include "content/common/gpu/media/vaapi_wrapper.h" |
[email protected] | 802a13a0 | 2010-12-02 01:48:37 | [diff] [blame] | 49 | #endif |
| 50 | |
[email protected] | 02ec37a | 2010-09-20 22:32:16 | [diff] [blame] | 51 | #if defined(USE_X11) |
[email protected] | 57b5d7dc | 2011-03-09 14:11:34 | [diff] [blame] | 52 | #include "ui/base/x/x11_util.h" |
[email protected] | 02ec37a | 2010-09-20 22:32:16 | [diff] [blame] | 53 | #endif |
| 54 | |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 55 | #if defined(OS_LINUX) |
| 56 | #include "content/public/common/sandbox_init.h" |
| 57 | #endif |
| 58 | |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 59 | const int kGpuTimeout = 10000; |
| 60 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 61 | namespace content { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 62 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 63 | namespace { |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 64 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 65 | bool WarmUpSandbox(const CommandLine& command_line); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 66 | #if defined(OS_LINUX) |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 67 | bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool); |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 68 | #elif defined(OS_WIN) |
| 69 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*); |
| 70 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 71 | |
| 72 | base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages = |
| 73 | LAZY_INSTANCE_INITIALIZER; |
| 74 | |
| 75 | bool GpuProcessLogMessageHandler(int severity, |
| 76 | const char* file, int line, |
| 77 | size_t message_start, |
| 78 | const std::string& str) { |
| 79 | std::string header = str.substr(0, message_start); |
| 80 | std::string message = str.substr(message_start); |
| 81 | deferred_messages.Get().push(new GpuHostMsg_OnLogMessage( |
| 82 | severity, header, message)); |
| 83 | return false; |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 86 | } // namespace anonymous |
| 87 | |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 88 | // Main function for starting the Gpu process. |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 89 | int GpuMain(const MainFunctionParams& parameters) { |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 90 | TRACE_EVENT0("gpu", "GpuMain"); |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 91 | base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process"); |
| 92 | base::debug::TraceLog::GetInstance()->SetProcessSortIndex( |
| 93 | kTraceEventGpuProcessSortIndex); |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 94 | |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 95 | const CommandLine& command_line = parameters.command_line; |
[email protected] | 6b889fb | 2010-03-23 20:09:49 | [diff] [blame] | 96 | if (command_line.HasSwitch(switches::kGpuStartupDialog)) { |
[email protected] | 75fcc27 | 2011-03-08 20:50:48 | [diff] [blame] | 97 | ChildProcess::WaitForDebugger("Gpu"); |
[email protected] | 6b889fb | 2010-03-23 20:09:49 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 100 | base::Time start_time = base::Time::Now(); |
| 101 | |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 102 | bool in_browser_process = command_line.HasSwitch(switches::kSingleProcess) || |
| 103 | command_line.HasSwitch(switches::kInProcessGPU); |
| 104 | |
| 105 | if (!in_browser_process) { |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 106 | #if defined(OS_WIN) |
| 107 | // Prevent Windows from displaying a modal dialog on failures like not being |
| 108 | // able to load a DLL. |
| 109 | SetErrorMode( |
| 110 | SEM_FAILCRITICALERRORS | |
| 111 | SEM_NOGPFAULTERRORBOX | |
| 112 | SEM_NOOPENFILEERRORBOX); |
| 113 | #elif defined(USE_X11) |
| 114 | ui::SetDefaultX11ErrorHandlers(); |
| 115 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 116 | |
| 117 | logging::SetLogMessageHandler(GpuProcessLogMessageHandler); |
[email protected] | 23f4656 | 2011-09-07 01:42:39 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame^] | 120 | if (command_line.HasSwitch(switches::kSupportsDualGpus)) { |
| 121 | std::string types = command_line.GetSwitchValueASCII( |
| 122 | switches::kGpuDriverBugWorkarounds); |
| 123 | std::set<int> workarounds; |
| 124 | gpu::StringToFeatureSet(types, &workarounds); |
| 125 | if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1) |
[email protected] | a2221e8 | 2012-10-08 22:33:14 | [diff] [blame] | 126 | ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu(); |
[email protected] | 40c19e72 | 2013-11-05 23:51:24 | [diff] [blame^] | 127 | else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1) |
[email protected] | a2221e8 | 2012-10-08 22:33:14 | [diff] [blame] | 128 | ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu(); |
[email protected] | 1bb06b0 | 2012-09-23 19:37:24 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 131 | // Initialization of the OpenGL bindings may fail, in which case we |
| 132 | // will need to tear down this process. However, we can not do so |
| 133 | // safely until the IPC channel is set up, because the detection of |
| 134 | // early return of a child process is implemented using an IPC |
| 135 | // channel error. If the IPC channel is not fully set up between the |
| 136 | // browser and GPU process, and the GPU process crashes or exits |
| 137 | // early, the browser process will never detect it. For this reason |
| 138 | // we defer tearing down the GPU process until receiving the |
| 139 | // GpuMsg_Initialize message from the browser. |
| 140 | bool dead_on_arrival = false; |
| 141 | |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 142 | base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 143 | #if defined(OS_WIN) |
| 144 | // Unless we're running on desktop GL, we don't need a UI message |
| 145 | // loop, so avoid its use to work around apparent problems with some |
| 146 | // third-party software. |
| 147 | if (command_line.HasSwitch(switches::kUseGL) && |
| 148 | command_line.GetSwitchValueASCII(switches::kUseGL) == |
| 149 | gfx::kGLImplementationDesktopName) { |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 150 | message_loop_type = base::MessageLoop::TYPE_UI; |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 151 | } |
[email protected] | 4fc7cc5 | 2013-09-12 22:51:10 | [diff] [blame] | 152 | #elif defined(TOOLKIT_GTK) |
| 153 | message_loop_type = base::MessageLoop::TYPE_GPU; |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 154 | #elif defined(OS_LINUX) |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 155 | message_loop_type = base::MessageLoop::TYPE_DEFAULT; |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 156 | #endif |
| 157 | |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 158 | base::MessageLoop main_message_loop(message_loop_type); |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 159 | base::PlatformThread::SetName("CrGpuMain"); |
| 160 | |
| 161 | // In addition to disabling the watchdog if the command line switch is |
| 162 | // present, disable the watchdog on valgrind because the code is expected |
| 163 | // to run slowly in that case. |
| 164 | bool enable_watchdog = |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 165 | !command_line.HasSwitch(switches::kDisableGpuWatchdog) && |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 166 | !RunningOnValgrind(); |
| 167 | |
| 168 | // Disable the watchdog in debug builds because they tend to only be run by |
| 169 | // developers who will not appreciate the watchdog killing the GPU process. |
| 170 | #ifndef NDEBUG |
| 171 | enable_watchdog = false; |
| 172 | #endif |
| 173 | |
[email protected] | 4271960 | 2012-11-08 02:51:12 | [diff] [blame] | 174 | bool delayed_watchdog_enable = false; |
| 175 | |
| 176 | #if defined(OS_CHROMEOS) |
| 177 | // Don't start watchdog immediately, to allow developers to switch to VT2 on |
| 178 | // startup. |
| 179 | delayed_watchdog_enable = true; |
| 180 | #endif |
| 181 | |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 182 | scoped_refptr<GpuWatchdogThread> watchdog_thread; |
| 183 | |
| 184 | // Start the GPU watchdog only after anything that is expected to be time |
| 185 | // consuming has completed, otherwise the process is liable to be aborted. |
[email protected] | 4271960 | 2012-11-08 02:51:12 | [diff] [blame] | 186 | if (enable_watchdog && !delayed_watchdog_enable) { |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 187 | watchdog_thread = new GpuWatchdogThread(kGpuTimeout); |
| 188 | watchdog_thread->Start(); |
| 189 | } |
| 190 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 191 | gpu::GPUInfo gpu_info; |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 192 | // Get vendor_id, device_id, driver_version from browser process through |
| 193 | // commandline switches. |
| 194 | DCHECK(command_line.HasSwitch(switches::kGpuVendorID) && |
| 195 | command_line.HasSwitch(switches::kGpuDeviceID) && |
| 196 | command_line.HasSwitch(switches::kGpuDriverVersion)); |
[email protected] | 3fb0169 | 2013-10-23 13:37:04 | [diff] [blame] | 197 | bool success = base::HexStringToUInt( |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 198 | command_line.GetSwitchValueASCII(switches::kGpuVendorID), |
[email protected] | 3fb0169 | 2013-10-23 13:37:04 | [diff] [blame] | 199 | &gpu_info.gpu.vendor_id); |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 200 | DCHECK(success); |
[email protected] | 3fb0169 | 2013-10-23 13:37:04 | [diff] [blame] | 201 | success = base::HexStringToUInt( |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 202 | command_line.GetSwitchValueASCII(switches::kGpuDeviceID), |
[email protected] | 3fb0169 | 2013-10-23 13:37:04 | [diff] [blame] | 203 | &gpu_info.gpu.device_id); |
[email protected] | 8114edfb | 2012-06-22 01:07:21 | [diff] [blame] | 204 | DCHECK(success); |
| 205 | gpu_info.driver_vendor = |
| 206 | command_line.GetSwitchValueASCII(switches::kGpuDriverVendor); |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 207 | gpu_info.driver_version = |
| 208 | command_line.GetSwitchValueASCII(switches::kGpuDriverVersion); |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 209 | GetContentClient()->SetGpuInfo(gpu_info); |
[email protected] | f091824 | 2012-02-18 00:30:50 | [diff] [blame] | 210 | |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 211 | // Warm up resources that don't need access to GPUInfo. |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 212 | if (WarmUpSandbox(command_line)) { |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 213 | #if defined(OS_LINUX) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 214 | bool initialized_sandbox = false; |
| 215 | bool initialized_gl_context = false; |
| 216 | bool should_initialize_gl_context = false; |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 217 | #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 218 | // On Chrome OS ARM, GPU driver userspace creates threads when initializing |
| 219 | // a GL context, so start the sandbox early. |
| 220 | gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(), |
| 221 | should_initialize_gl_context); |
| 222 | initialized_sandbox = true; |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 223 | #endif |
| 224 | #endif // defined(OS_LINUX) |
| 225 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 226 | // Load and initialize the GL implementation and locate the GL entry points. |
| 227 | if (gfx::GLSurface::InitializeOneOff()) { |
| 228 | // We need to collect GL strings (VENDOR, RENDERER) for blacklisting |
| 229 | // purposes. However, on Mac we don't actually use them. As documented in |
| 230 | // crbug.com/222934, due to some driver issues, glGetString could take |
| 231 | // multiple seconds to finish, which in turn cause the GPU process to |
| 232 | // crash. |
| 233 | // By skipping the following code on Mac, we don't really lose anything, |
| 234 | // because the basic GPU information is passed down from browser process |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 235 | // and we already registered them through SetGpuInfo() above. |
[email protected] | e9931a4 | 2013-04-11 00:48:41 | [diff] [blame] | 236 | #if !defined(OS_MACOSX) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 237 | if (!gpu::CollectContextGraphicsInfo(&gpu_info)) |
| 238 | VLOG(1) << "gpu::CollectGraphicsInfo failed"; |
| 239 | GetContentClient()->SetGpuInfo(gpu_info); |
[email protected] | 3a62be09 | 2012-10-09 20:58:19 | [diff] [blame] | 240 | |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 241 | #if defined(OS_LINUX) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 242 | initialized_gl_context = true; |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 243 | #if !defined(OS_CHROMEOS) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 244 | if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA |
| 245 | gpu_info.driver_vendor == "NVIDIA") { |
| 246 | base::ThreadRestrictions::AssertIOAllowed(); |
| 247 | if (access("/dev/nvidiactl", R_OK) != 0) { |
| 248 | VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied"; |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 249 | dead_on_arrival = true; |
| 250 | } |
[email protected] | 42dc3c0 | 2012-03-28 00:28:55 | [diff] [blame] | 251 | } |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 252 | #endif // !defined(OS_CHROMEOS) |
| 253 | #endif // defined(OS_LINUX) |
| 254 | #endif // !defined(OS_MACOSX) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 255 | } else { |
| 256 | VLOG(1) << "gfx::GLSurface::InitializeOneOff failed"; |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 257 | dead_on_arrival = true; |
| 258 | } |
[email protected] | 4a2a4d2 | 2011-07-25 23:20:34 | [diff] [blame] | 259 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 260 | if (enable_watchdog && delayed_watchdog_enable) { |
| 261 | watchdog_thread = new GpuWatchdogThread(kGpuTimeout); |
| 262 | watchdog_thread->Start(); |
| 263 | } |
[email protected] | 4271960 | 2012-11-08 02:51:12 | [diff] [blame] | 264 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 265 | // OSMesa is expected to run very slowly, so disable the watchdog in that |
| 266 | // case. |
| 267 | if (enable_watchdog && |
| 268 | gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) { |
| 269 | watchdog_thread->Stop(); |
| 270 | watchdog_thread = NULL; |
| 271 | } |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 272 | |
[email protected] | c90e84d | 2012-06-12 17:55:00 | [diff] [blame] | 273 | #if defined(OS_LINUX) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 274 | should_initialize_gl_context = !initialized_gl_context && |
| 275 | !dead_on_arrival; |
[email protected] | c90e84d | 2012-06-12 17:55:00 | [diff] [blame] | 276 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 277 | if (!initialized_sandbox) { |
| 278 | gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(), |
| 279 | should_initialize_gl_context); |
| 280 | } |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 281 | #elif defined(OS_WIN) |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 282 | gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info); |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 283 | #endif |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 284 | } else { |
| 285 | dead_on_arrival = true; |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 286 | } |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 287 | |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 288 | logging::SetLogMessageHandler(NULL); |
| 289 | |
[email protected] | 983c33d | 2010-11-16 22:38:44 | [diff] [blame] | 290 | GpuProcess gpu_process; |
[email protected] | 8fe0ec52 | 2011-03-03 00:31:33 | [diff] [blame] | 291 | |
[email protected] | db6101db | 2012-10-25 15:20:08 | [diff] [blame] | 292 | GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(), |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 293 | dead_on_arrival, |
| 294 | gpu_info, |
| 295 | deferred_messages.Get()); |
| 296 | while (!deferred_messages.Get().empty()) |
| 297 | deferred_messages.Get().pop(); |
[email protected] | 8fe0ec52 | 2011-03-03 00:31:33 | [diff] [blame] | 298 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 299 | child_thread->Init(start_time); |
[email protected] | 995a7f1 | 2011-02-11 23:07:17 | [diff] [blame] | 300 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 301 | gpu_process.set_main_thread(child_thread); |
[email protected] | 983c33d | 2010-11-16 22:38:44 | [diff] [blame] | 302 | |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 303 | if (watchdog_thread) |
| 304 | watchdog_thread->AddPowerObserver(); |
| 305 | |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 306 | { |
| 307 | TRACE_EVENT0("gpu", "Run Message Loop"); |
| 308 | main_message_loop.Run(); |
| 309 | } |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 310 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 311 | child_thread->StopWatchdog(); |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 312 | |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 313 | return 0; |
| 314 | } |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 315 | |
| 316 | namespace { |
| 317 | |
[email protected] | 59a7ae4e | 2012-10-01 23:54:44 | [diff] [blame] | 318 | #if defined(OS_LINUX) |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 319 | void CreateDummyGlContext() { |
| 320 | scoped_refptr<gfx::GLSurface> surface( |
[email protected] | 658f03f | 2013-06-17 18:28:09 | [diff] [blame] | 321 | gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1))); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 322 | if (!surface.get()) { |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 323 | VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed"; |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // On Linux, this is needed to make sure /dev/nvidiactl has |
| 328 | // been opened and its descriptor cached. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 329 | scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext( |
| 330 | NULL, surface.get(), gfx::PreferDiscreteGpu)); |
| 331 | if (!context.get()) { |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 332 | VLOG(1) << "gfx::GLContext::CreateGLContext failed"; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | // Similarly, this is needed for /dev/nvidia0. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 337 | if (context->MakeCurrent(surface.get())) { |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 338 | context->ReleaseCurrent(surface.get()); |
| 339 | } else { |
| 340 | VLOG(1) << "gfx::GLContext::MakeCurrent failed"; |
| 341 | } |
| 342 | } |
[email protected] | 59a7ae4e | 2012-10-01 23:54:44 | [diff] [blame] | 343 | #endif |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 344 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 345 | bool WarmUpSandbox(const CommandLine& command_line) { |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 346 | { |
| 347 | TRACE_EVENT0("gpu", "Warm up rand"); |
| 348 | // Warm up the random subsystem, which needs to be done pre-sandbox on all |
| 349 | // platforms. |
| 350 | (void) base::RandUint64(); |
| 351 | } |
| 352 | { |
| 353 | TRACE_EVENT0("gpu", "Warm up HMAC"); |
| 354 | // Warm up the crypto subsystem, which needs to done pre-sandbox on all |
| 355 | // platforms. |
| 356 | crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 357 | unsigned char key = '\0'; |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 358 | if (!hmac.Init(&key, sizeof(key))) { |
| 359 | LOG(ERROR) << "WarmUpSandbox() failed with crypto::HMAC::Init()"; |
| 360 | return false; |
| 361 | } |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | 106e38b | 2013-03-11 23:16:45 | [diff] [blame] | 364 | #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
[email protected] | 3943be2 | 2013-08-06 18:11:42 | [diff] [blame] | 365 | ExynosVideoDecodeAccelerator::PreSandboxInitialization(); |
[email protected] | 106e38b | 2013-03-11 23:16:45 | [diff] [blame] | 366 | #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
[email protected] | fd99f1f | 2013-05-31 06:52:41 | [diff] [blame] | 367 | VaapiWrapper::PreSandboxInitialization(); |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 368 | #endif |
| 369 | |
[email protected] | 5f7e451 | 2012-10-01 20:51:37 | [diff] [blame] | 370 | #if defined(OS_WIN) |
[email protected] | 2abf912 | 2013-03-12 03:00:19 | [diff] [blame] | 371 | { |
[email protected] | 2abf912 | 2013-03-12 03:00:19 | [diff] [blame] | 372 | TRACE_EVENT0("gpu", "Preload setupapi.dll"); |
| 373 | // Preload this DLL because the sandbox prevents it from loading. |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 374 | if (LoadLibrary(L"setupapi.dll") == NULL) { |
| 375 | LOG(ERROR) << "WarmUpSandbox() failed with loading setupapi.dll"; |
| 376 | return false; |
| 377 | } |
[email protected] | 2abf912 | 2013-03-12 03:00:19 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 380 | if (!command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode)) { |
[email protected] | 2abf912 | 2013-03-12 03:00:19 | [diff] [blame] | 381 | TRACE_EVENT0("gpu", "Initialize DXVA"); |
| 382 | // Initialize H/W video decoding stuff which fails in the sandbox. |
| 383 | DXVAVideoDecodeAccelerator::PreSandboxInitialization(); |
| 384 | } |
[email protected] | b5b9bfc | 2013-09-07 02:09:07 | [diff] [blame] | 385 | |
| 386 | { |
| 387 | TRACE_EVENT0("gpu", "Warm up DWM"); |
| 388 | |
| 389 | // DWM was introduced with Windows Vista. DwmFlush seems to be sufficient |
| 390 | // to warm it up before lowering the token. DWM is required to present to |
| 391 | // a window with Vista and later and this allows us to do so with the |
| 392 | // GPU process sandbox enabled. |
| 393 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 394 | HMODULE module = LoadLibrary(L"dwmapi.dll"); |
| 395 | if (module) { |
| 396 | typedef HRESULT (WINAPI *DwmFlushFunc)(); |
| 397 | DwmFlushFunc dwm_flush = reinterpret_cast<DwmFlushFunc>( |
| 398 | GetProcAddress(module, "DwmFlush")); |
| 399 | if (dwm_flush) |
| 400 | dwm_flush(); |
| 401 | } |
| 402 | } |
| 403 | } |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 404 | #endif |
[email protected] | ca23992b0 | 2013-06-13 17:25:19 | [diff] [blame] | 405 | return true; |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 408 | #if defined(OS_LINUX) |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 409 | void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info, |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 410 | bool should_initialize_gl_context) { |
| 411 | // We special case Optimus since the vendor_id we see may not be Nvidia. |
| 412 | bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA. |
| 413 | gpu_info.driver_vendor == "NVIDIA") || |
| 414 | gpu_info.optimus; |
| 415 | if (uses_nvidia_driver && should_initialize_gl_context) { |
| 416 | // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0. |
| 417 | CreateDummyGlContext(); |
| 418 | } |
| 419 | } |
| 420 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 421 | bool StartSandboxLinux(const gpu::GPUInfo& gpu_info, |
[email protected] | 663c4b3 | 2013-04-18 05:52:54 | [diff] [blame] | 422 | GpuWatchdogThread* watchdog_thread, |
| 423 | bool should_initialize_gl_context) { |
| 424 | TRACE_EVENT0("gpu", "Initialize sandbox"); |
| 425 | |
| 426 | bool res = false; |
| 427 | |
| 428 | WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context); |
| 429 | |
| 430 | if (watchdog_thread) |
| 431 | watchdog_thread->Stop(); |
| 432 | // LinuxSandbox::InitializeSandbox() must always be called |
| 433 | // with only one thread. |
| 434 | res = LinuxSandbox::InitializeSandbox(); |
| 435 | if (watchdog_thread) |
| 436 | watchdog_thread->Start(); |
| 437 | |
| 438 | return res; |
| 439 | } |
| 440 | #endif // defined(OS_LINUX) |
| 441 | |
| 442 | #if defined(OS_WIN) |
| 443 | bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) { |
| 444 | TRACE_EVENT0("gpu", "Lower token"); |
| 445 | |
| 446 | // For Windows, if the target_services interface is not zero, the process |
| 447 | // is sandboxed and we must call LowerToken() before rendering untrusted |
| 448 | // content. |
| 449 | sandbox::TargetServices* target_services = sandbox_info->target_services; |
| 450 | if (target_services) { |
| 451 | target_services->LowerToken(); |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | return false; |
| 456 | } |
| 457 | #endif // defined(OS_WIN) |
| 458 | |
[email protected] | 6ec3a57 | 2012-08-17 02:09:51 | [diff] [blame] | 459 | } // namespace. |
| 460 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 461 | } // namespace content |