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