blob: 0e84d3e431adab223afaac11b7f254fa56d706b9 [file] [log] [blame]
[email protected]2e3d9e42012-02-14 03:23:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c0fc0942010-01-13 00:55:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e09cee42010-11-09 01:50:085#include <stdlib.h>
6
[email protected]cafd0b62011-01-19 01:22:057#if defined(OS_WIN)
8#include <windows.h>
9#endif
10
[email protected]d13f35d2012-05-18 02:28:1511#include "base/debug/trace_event.h"
[email protected]c0fc0942010-01-13 00:55:3712#include "base/message_loop.h"
[email protected]2436a6b2012-04-13 21:08:5113#include "base/rand_util.h"
[email protected]085170ca2012-05-17 20:27:2814#include "base/string_number_conversions.h"
[email protected]e09cee42010-11-09 01:50:0815#include "base/stringprintf.h"
[email protected]ce072a72010-12-31 20:02:1616#include "base/threading/platform_thread.h"
[email protected]c0fc0942010-01-13 00:55:3717#include "build/build_config.h"
[email protected]f24a1e2b2011-04-08 01:48:4818#include "content/common/gpu/gpu_config.h"
[email protected]7a31f7c2011-03-21 23:22:0419#include "content/gpu/gpu_child_thread.h"
[email protected]f0918242012-02-18 00:30:5020#include "content/gpu/gpu_info_collector.h"
[email protected]623c0bd2011-03-12 01:00:4121#include "content/gpu/gpu_process.h"
[email protected]db6101db2012-10-25 15:20:0822#include "content/gpu/gpu_watchdog_thread.h"
[email protected]c9e2cbbb2012-05-12 21:17:2723#include "content/public/common/content_client.h"
24#include "content/public/common/content_switches.h"
[email protected]1bb06b02012-09-23 19:37:2425#include "content/public/common/gpu_switching_option.h"
[email protected]c9e2cbbb2012-05-12 21:17:2726#include "content/public/common/main_function_params.h"
[email protected]5fa097a2012-05-10 21:59:0727#include "crypto/hmac.h"
[email protected]db6101db2012-10-25 15:20:0828#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2729#include "ui/gl/gl_surface.h"
30#include "ui/gl/gl_switches.h"
[email protected]1bb06b02012-09-23 19:37:2431#include "ui/gl/gpu_switching_manager.h"
[email protected]c0fc0942010-01-13 00:55:3732
[email protected]d7de57872011-12-06 23:32:4333#if defined(OS_WIN)
[email protected]5f7e4512012-10-01 20:51:3734#include "base/win/scoped_com_initializer.h"
[email protected]7ad3a4352011-12-22 22:18:0035#include "content/common/gpu/media/dxva_video_decode_accelerator.h"
[email protected]181491782012-07-18 00:59:1536#include "sandbox/win/src/sandbox.h"
[email protected]d254e7e2012-08-04 08:38:1037#elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
38#include "content/common/gpu/media/omx_video_decode_accelerator.h"
39#elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
40#include "content/common/gpu/media/vaapi_video_decode_accelerator.h"
[email protected]802a13a02010-12-02 01:48:3741#endif
42
[email protected]02ec37a2010-09-20 22:32:1643#if defined(USE_X11)
[email protected]57b5d7dc2011-03-09 14:11:3444#include "ui/base/x/x11_util.h"
[email protected]02ec37a2010-09-20 22:32:1645#endif
46
[email protected]2436a6b2012-04-13 21:08:5147#if defined(OS_LINUX)
48#include "content/public/common/sandbox_init.h"
49#endif
50
[email protected]db6101db2012-10-25 15:20:0851const int kGpuTimeout = 10000;
52
[email protected]eb398192012-10-22 20:16:1953namespace content {
[email protected]6ec3a572012-08-17 02:09:5154namespace {
[email protected]eb398192012-10-22 20:16:1955void WarmUpSandbox(const GPUInfo&, bool);
[email protected]6ec3a572012-08-17 02:09:5156}
57
[email protected]c0fc0942010-01-13 00:55:3758// Main function for starting the Gpu process.
[email protected]eb398192012-10-22 20:16:1959int GpuMain(const MainFunctionParams& parameters) {
[email protected]d13f35d2012-05-18 02:28:1560 TRACE_EVENT0("gpu", "GpuMain");
61
[email protected]e09cee42010-11-09 01:50:0862 base::Time start_time = base::Time::Now();
63
[email protected]badf5cf2011-10-29 03:44:4464 const CommandLine& command_line = parameters.command_line;
[email protected]6b889fb2010-03-23 20:09:4965 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
[email protected]75fcc272011-03-08 20:50:4866 ChildProcess::WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:4967 }
68
[email protected]23f46562011-09-07 01:42:3969 if (!command_line.HasSwitch(switches::kSingleProcess)) {
70#if defined(OS_WIN)
71 // Prevent Windows from displaying a modal dialog on failures like not being
72 // able to load a DLL.
73 SetErrorMode(
74 SEM_FAILCRITICALERRORS |
75 SEM_NOGPFAULTERRORBOX |
76 SEM_NOOPENFILEERRORBOX);
77#elif defined(USE_X11)
78 ui::SetDefaultX11ErrorHandlers();
79#endif
80 }
81
[email protected]48fe7e42012-10-01 23:06:0482 if (command_line.HasSwitch(switches::kSupportsDualGpus) &&
83 command_line.HasSwitch(switches::kGpuSwitching)) {
[email protected]1bb06b02012-09-23 19:37:2484 std::string option = command_line.GetSwitchValueASCII(
85 switches::kGpuSwitching);
86 if (option == switches::kGpuSwitchingOptionNameForceDiscrete)
[email protected]a2221e82012-10-08 22:33:1487 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
[email protected]1bb06b02012-09-23 19:37:2488 else if (option == switches::kGpuSwitchingOptionNameForceIntegrated)
[email protected]a2221e82012-10-08 22:33:1489 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
[email protected]1bb06b02012-09-23 19:37:2490 }
91
[email protected]0b2cec62011-07-22 18:13:2892 // Initialization of the OpenGL bindings may fail, in which case we
93 // will need to tear down this process. However, we can not do so
94 // safely until the IPC channel is set up, because the detection of
95 // early return of a child process is implemented using an IPC
96 // channel error. If the IPC channel is not fully set up between the
97 // browser and GPU process, and the GPU process crashes or exits
98 // early, the browser process will never detect it. For this reason
99 // we defer tearing down the GPU process until receiving the
100 // GpuMsg_Initialize message from the browser.
101 bool dead_on_arrival = false;
102
[email protected]db6101db2012-10-25 15:20:08103 MessageLoop::Type message_loop_type = MessageLoop::TYPE_IO;
104#if defined(OS_WIN)
105 // Unless we're running on desktop GL, we don't need a UI message
106 // loop, so avoid its use to work around apparent problems with some
107 // third-party software.
108 if (command_line.HasSwitch(switches::kUseGL) &&
109 command_line.GetSwitchValueASCII(switches::kUseGL) ==
110 gfx::kGLImplementationDesktopName) {
111 message_loop_type = MessageLoop::TYPE_UI;
112 }
113#elif defined(OS_LINUX)
114 message_loop_type = MessageLoop::TYPE_DEFAULT;
115#endif
116
117 MessageLoop main_message_loop(message_loop_type);
118 base::PlatformThread::SetName("CrGpuMain");
119
120 // In addition to disabling the watchdog if the command line switch is
121 // present, disable the watchdog on valgrind because the code is expected
122 // to run slowly in that case.
123 bool enable_watchdog =
124 !CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kDisableGpuWatchdog) &&
126 !RunningOnValgrind();
127
128 // Disable the watchdog in debug builds because they tend to only be run by
129 // developers who will not appreciate the watchdog killing the GPU process.
130#ifndef NDEBUG
131 enable_watchdog = false;
132#endif
133
134 scoped_refptr<GpuWatchdogThread> watchdog_thread;
135
136 // Start the GPU watchdog only after anything that is expected to be time
137 // consuming has completed, otherwise the process is liable to be aborted.
138 if (enable_watchdog) {
139 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
140 watchdog_thread->Start();
141 }
142
[email protected]eb398192012-10-22 20:16:19143 GPUInfo gpu_info;
[email protected]085170ca2012-05-17 20:27:28144 // Get vendor_id, device_id, driver_version from browser process through
145 // commandline switches.
146 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
147 command_line.HasSwitch(switches::kGpuDeviceID) &&
148 command_line.HasSwitch(switches::kGpuDriverVersion));
149 bool success = base::HexStringToInt(
150 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
151 reinterpret_cast<int*>(&(gpu_info.gpu.vendor_id)));
152 DCHECK(success);
153 success = base::HexStringToInt(
154 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
155 reinterpret_cast<int*>(&(gpu_info.gpu.device_id)));
[email protected]8114edfb2012-06-22 01:07:21156 DCHECK(success);
157 gpu_info.driver_vendor =
158 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
[email protected]085170ca2012-05-17 20:27:28159 gpu_info.driver_version =
160 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
[email protected]eb398192012-10-22 20:16:19161 GetContentClient()->SetGpuInfo(gpu_info);
[email protected]f0918242012-02-18 00:30:50162
[email protected]6ec3a572012-08-17 02:09:51163 // We need to track that information for the WarmUpSandbox function.
164 bool initialized_gl_context = false;
[email protected]085170ca2012-05-17 20:27:28165 // Load and initialize the GL implementation and locate the GL entry points.
166 if (gfx::GLSurface::InitializeOneOff()) {
[email protected]3a62be092012-10-09 20:58:19167 if (!command_line.HasSwitch(switches::kSkipGpuFullInfoCollection)) {
168 if (!gpu_info_collector::CollectGraphicsInfo(&gpu_info))
169 VLOG(1) << "gpu_info_collector::CollectGraphicsInfo failed";
[email protected]eb398192012-10-22 20:16:19170 GetContentClient()->SetGpuInfo(gpu_info);
[email protected]3a62be092012-10-09 20:58:19171
[email protected]6ec3a572012-08-17 02:09:51172 // We know that CollectGraphicsInfo will initialize a GLContext.
173 initialized_gl_context = true;
[email protected]8114edfb2012-06-22 01:07:21174 }
[email protected]085170ca2012-05-17 20:27:28175
[email protected]3a62be092012-10-09 20:58:19176#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
[email protected]a094e2c2012-05-10 23:02:42177 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
[email protected]4b550b1a2012-04-16 22:01:25178 gpu_info.driver_vendor == "NVIDIA") {
[email protected]42dc3c02012-03-28 00:28:55179 base::ThreadRestrictions::AssertIOAllowed();
180 if (access("/dev/nvidiactl", R_OK) != 0) {
[email protected]085170ca2012-05-17 20:27:28181 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
[email protected]42dc3c02012-03-28 00:28:55182 gpu_info.gpu_accessible = false;
183 dead_on_arrival = true;
184 }
185 }
[email protected]0204bf12012-05-25 22:31:12186#endif // OS_CHROMEOS
[email protected]f0918242012-02-18 00:30:50187 } else {
[email protected]085170ca2012-05-17 20:27:28188 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
[email protected]42dc3c02012-03-28 00:28:55189 gpu_info.gpu_accessible = false;
[email protected]085170ca2012-05-17 20:27:28190 gpu_info.finalized = true;
[email protected]4a2a4d22011-07-25 23:20:34191 dead_on_arrival = true;
192 }
193
[email protected]db6101db2012-10-25 15:20:08194 // OSMesa is expected to run very slowly, so disable the watchdog in that
195 // case.
196 if (enable_watchdog &&
197 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
198 watchdog_thread->Stop();
199
200 watchdog_thread = NULL;
201 }
202
[email protected]d13f35d2012-05-18 02:28:15203 {
[email protected]6ec3a572012-08-17 02:09:51204 const bool should_initialize_gl_context = !initialized_gl_context &&
205 !dead_on_arrival;
206 // Warm up the current process before enabling the sandbox.
207 WarmUpSandbox(gpu_info, should_initialize_gl_context);
[email protected]d13f35d2012-05-18 02:28:15208 }
[email protected]5fa097a2012-05-10 21:59:07209
[email protected]c90e84d2012-06-12 17:55:00210#if defined(OS_LINUX)
[email protected]d13f35d2012-05-18 02:28:15211 {
212 TRACE_EVENT0("gpu", "Initialize sandbox");
[email protected]c90e84d2012-06-12 17:55:00213 bool do_init_sandbox = true;
214
215#if defined(OS_CHROMEOS) && defined(NDEBUG)
216 // On Chrome OS and when not on a debug build, initialize
217 // the GPU process' sandbox only for Intel GPUs.
218 do_init_sandbox = gpu_info.gpu.vendor_id == 0x8086; // Intel GPU.
219#endif
220
221 if (do_init_sandbox) {
[email protected]eb398192012-10-22 20:16:19222 gpu_info.sandboxed = InitializeSandbox();
[email protected]c90e84d2012-06-12 17:55:00223 }
[email protected]d13f35d2012-05-18 02:28:15224 }
[email protected]2436a6b2012-04-13 21:08:51225#endif
226
[email protected]0b2cec62011-07-22 18:13:28227#if defined(OS_WIN)
[email protected]d13f35d2012-05-18 02:28:15228 {
[email protected]d13f35d2012-05-18 02:28:15229 TRACE_EVENT0("gpu", "Lower token");
230 // For windows, if the target_services interface is not zero, the process
231 // is sandboxed and we must call LowerToken() before rendering untrusted
232 // content.
233 sandbox::TargetServices* target_services =
234 parameters.sandbox_info->target_services;
[email protected]fad3ccf2012-09-11 22:36:00235 if (target_services) {
[email protected]d13f35d2012-05-18 02:28:15236 target_services->LowerToken();
[email protected]fad3ccf2012-09-11 22:36:00237 gpu_info.sandboxed = true;
238 }
[email protected]d13f35d2012-05-18 02:28:15239 }
[email protected]0b2cec62011-07-22 18:13:28240#endif
241
[email protected]983c33d2010-11-16 22:38:44242 GpuProcess gpu_process;
[email protected]8fe0ec522011-03-03 00:31:33243
[email protected]db6101db2012-10-25 15:20:08244 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
245 dead_on_arrival, gpu_info);
[email protected]8fe0ec522011-03-03 00:31:33246
[email protected]7a31f7c2011-03-21 23:22:04247 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17248
[email protected]7a31f7c2011-03-21 23:22:04249 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44250
[email protected]d13f35d2012-05-18 02:28:15251 {
252 TRACE_EVENT0("gpu", "Run Message Loop");
253 main_message_loop.Run();
254 }
[email protected]c0fc0942010-01-13 00:55:37255
[email protected]7a31f7c2011-03-21 23:22:04256 child_thread->StopWatchdog();
[email protected]e09cee42010-11-09 01:50:08257
[email protected]c0fc0942010-01-13 00:55:37258 return 0;
259}
[email protected]6ec3a572012-08-17 02:09:51260
261namespace {
262
[email protected]59a7ae4e2012-10-01 23:54:44263#if defined(OS_LINUX)
[email protected]6ec3a572012-08-17 02:09:51264void CreateDummyGlContext() {
265 scoped_refptr<gfx::GLSurface> surface(
266 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)));
267 if (!surface.get()) {
268 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
269 return;
270 }
271
272 // On Linux, this is needed to make sure /dev/nvidiactl has
273 // been opened and its descriptor cached.
274 scoped_refptr<gfx::GLContext> context(
275 gfx::GLContext::CreateGLContext(NULL,
276 surface,
277 gfx::PreferDiscreteGpu));
278 if (!context.get()) {
279 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
280 return;
281 }
282
283 // Similarly, this is needed for /dev/nvidia0.
284 if (context->MakeCurrent(surface)) {
285 context->ReleaseCurrent(surface.get());
286 } else {
287 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
288 }
289}
[email protected]59a7ae4e2012-10-01 23:54:44290#endif
[email protected]6ec3a572012-08-17 02:09:51291
[email protected]eb398192012-10-22 20:16:19292void WarmUpSandbox(const GPUInfo& gpu_info,
[email protected]6ec3a572012-08-17 02:09:51293 bool should_initialize_gl_context) {
294 {
295 TRACE_EVENT0("gpu", "Warm up rand");
296 // Warm up the random subsystem, which needs to be done pre-sandbox on all
297 // platforms.
298 (void) base::RandUint64();
299 }
300 {
301 TRACE_EVENT0("gpu", "Warm up HMAC");
302 // Warm up the crypto subsystem, which needs to done pre-sandbox on all
303 // platforms.
304 crypto::HMAC hmac(crypto::HMAC::SHA256);
305 unsigned char key = '\0';
306 bool ret = hmac.Init(&key, sizeof(key));
307 (void) ret;
308 }
309
310#if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
311 OmxVideoDecodeAccelerator::PreSandboxInitialization();
312#elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
313 VaapiVideoDecodeAccelerator::PreSandboxInitialization();
314#endif
315
316#if defined(OS_LINUX)
[email protected]9b5d2d32012-08-23 00:53:54317 // We special case Optimus since the vendor_id we see may not be Nvidia.
318 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
319 gpu_info.driver_vendor == "NVIDIA") ||
320 gpu_info.optimus;
321 if (uses_nvidia_driver && should_initialize_gl_context) {
[email protected]6ec3a572012-08-17 02:09:51322 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
323 CreateDummyGlContext();
324 }
325#endif
326
[email protected]5f7e4512012-10-01 20:51:37327#if defined(OS_WIN)
[email protected]6ec3a572012-08-17 02:09:51328 {
329 TRACE_EVENT0("gpu", "Initialize COM");
330 base::win::ScopedCOMInitializer com_initializer;
331 }
332
[email protected]6ec3a572012-08-17 02:09:51333 {
334 TRACE_EVENT0("gpu", "Preload setupapi.dll");
335 // Preload this DLL because the sandbox prevents it from loading.
336 LoadLibrary(L"setupapi.dll");
337 }
338
339 {
340 TRACE_EVENT0("gpu", "Initialize DXVA");
341 // Initialize H/W video decoding stuff which fails in the sandbox.
342 DXVAVideoDecodeAccelerator::PreSandboxInitialization();
343 }
344#endif
345}
346
[email protected]6ec3a572012-08-17 02:09:51347} // namespace.
348
[email protected]eb398192012-10-22 20:16:19349} // namespace content