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