Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / content / gpu / gpu_main.cc
blobc2ffd63470fbfda833198798b293c7f2a5e9272e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <stdlib.h>
7 #if defined(OS_WIN)
8 #include <dwmapi.h>
9 #include <windows.h>
10 #endif
12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/rand_util.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/threading/platform_thread.h"
19 #include "build/build_config.h"
20 #include "content/child/child_process.h"
21 #include "content/common/content_constants_internal.h"
22 #include "content/common/gpu/gpu_config.h"
23 #include "content/common/gpu/gpu_messages.h"
24 #include "content/common/sandbox_linux.h"
25 #include "content/gpu/gpu_child_thread.h"
26 #include "content/gpu/gpu_process.h"
27 #include "content/gpu/gpu_watchdog_thread.h"
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"
31 #include "crypto/hmac.h"
32 #include "gpu/config/gpu_info_collector.h"
33 #include "ui/gl/gl_implementation.h"
34 #include "ui/gl/gl_surface.h"
35 #include "ui/gl/gl_switches.h"
36 #include "ui/gl/gpu_switching_manager.h"
38 #if defined(OS_WIN)
39 #include "base/win/windows_version.h"
40 #include "base/win/scoped_com_initializer.h"
41 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
42 #include "sandbox/win/src/sandbox.h"
43 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11)
44 #include "content/common/gpu/media/exynos_video_decode_accelerator.h"
45 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
46 #include "content/common/gpu/media/vaapi_wrapper.h"
47 #endif
49 #if defined(USE_X11)
50 #include "ui/base/x/x11_util.h"
51 #endif
53 #if defined(OS_LINUX)
54 #include "content/public/common/sandbox_init.h"
55 #endif
57 const int kGpuTimeout = 10000;
59 namespace content {
61 namespace {
63 bool WarmUpSandbox(const CommandLine& command_line);
64 #if defined(OS_LINUX)
65 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
66 #elif defined(OS_WIN)
67 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
68 #endif
70 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
71 LAZY_INSTANCE_INITIALIZER;
73 bool GpuProcessLogMessageHandler(int severity,
74 const char* file, int line,
75 size_t message_start,
76 const std::string& str) {
77 std::string header = str.substr(0, message_start);
78 std::string message = str.substr(message_start);
79 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
80 severity, header, message));
81 return false;
84 } // namespace anonymous
86 // Main function for starting the Gpu process.
87 int GpuMain(const MainFunctionParams& parameters) {
88 TRACE_EVENT0("gpu", "GpuMain");
89 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
90 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
91 kTraceEventGpuProcessSortIndex);
93 const CommandLine& command_line = parameters.command_line;
94 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
95 ChildProcess::WaitForDebugger("Gpu");
98 base::Time start_time = base::Time::Now();
100 bool in_browser_process = command_line.HasSwitch(switches::kSingleProcess) ||
101 command_line.HasSwitch(switches::kInProcessGPU);
103 if (!in_browser_process) {
104 #if defined(OS_WIN)
105 // Prevent Windows from displaying a modal dialog on failures like not being
106 // able to load a DLL.
107 SetErrorMode(
108 SEM_FAILCRITICALERRORS |
109 SEM_NOGPFAULTERRORBOX |
110 SEM_NOOPENFILEERRORBOX);
111 #elif defined(USE_X11)
112 ui::SetDefaultX11ErrorHandlers();
113 #endif
115 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
118 if (command_line.HasSwitch(switches::kSupportsDualGpus) &&
119 command_line.HasSwitch(switches::kGpuSwitching)) {
120 std::string option = command_line.GetSwitchValueASCII(
121 switches::kGpuSwitching);
122 if (option == switches::kGpuSwitchingOptionNameForceDiscrete)
123 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
124 else if (option == switches::kGpuSwitchingOptionNameForceIntegrated)
125 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
128 // Initialization of the OpenGL bindings may fail, in which case we
129 // will need to tear down this process. However, we can not do so
130 // safely until the IPC channel is set up, because the detection of
131 // early return of a child process is implemented using an IPC
132 // channel error. If the IPC channel is not fully set up between the
133 // browser and GPU process, and the GPU process crashes or exits
134 // early, the browser process will never detect it. For this reason
135 // we defer tearing down the GPU process until receiving the
136 // GpuMsg_Initialize message from the browser.
137 bool dead_on_arrival = false;
139 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
140 #if defined(OS_WIN)
141 // Unless we're running on desktop GL, we don't need a UI message
142 // loop, so avoid its use to work around apparent problems with some
143 // third-party software.
144 if (command_line.HasSwitch(switches::kUseGL) &&
145 command_line.GetSwitchValueASCII(switches::kUseGL) ==
146 gfx::kGLImplementationDesktopName) {
147 message_loop_type = base::MessageLoop::TYPE_UI;
149 #elif defined(TOOLKIT_GTK)
150 message_loop_type = base::MessageLoop::TYPE_GPU;
151 #elif defined(OS_LINUX)
152 message_loop_type = base::MessageLoop::TYPE_DEFAULT;
153 #endif
155 base::MessageLoop main_message_loop(message_loop_type);
156 base::PlatformThread::SetName("CrGpuMain");
158 // In addition to disabling the watchdog if the command line switch is
159 // present, disable the watchdog on valgrind because the code is expected
160 // to run slowly in that case.
161 bool enable_watchdog =
162 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
163 !RunningOnValgrind();
165 // Disable the watchdog in debug builds because they tend to only be run by
166 // developers who will not appreciate the watchdog killing the GPU process.
167 #ifndef NDEBUG
168 enable_watchdog = false;
169 #endif
171 bool delayed_watchdog_enable = false;
173 #if defined(OS_CHROMEOS)
174 // Don't start watchdog immediately, to allow developers to switch to VT2 on
175 // startup.
176 delayed_watchdog_enable = true;
177 #endif
179 scoped_refptr<GpuWatchdogThread> watchdog_thread;
181 // Start the GPU watchdog only after anything that is expected to be time
182 // consuming has completed, otherwise the process is liable to be aborted.
183 if (enable_watchdog && !delayed_watchdog_enable) {
184 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
185 watchdog_thread->Start();
188 gpu::GPUInfo gpu_info;
189 // Get vendor_id, device_id, driver_version from browser process through
190 // commandline switches.
191 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
192 command_line.HasSwitch(switches::kGpuDeviceID) &&
193 command_line.HasSwitch(switches::kGpuDriverVersion));
194 bool success = base::HexStringToUInt(
195 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
196 &gpu_info.gpu.vendor_id);
197 DCHECK(success);
198 success = base::HexStringToUInt(
199 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
200 &gpu_info.gpu.device_id);
201 DCHECK(success);
202 gpu_info.driver_vendor =
203 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
204 gpu_info.driver_version =
205 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
206 GetContentClient()->SetGpuInfo(gpu_info);
208 // Warm up resources that don't need access to GPUInfo.
209 if (WarmUpSandbox(command_line)) {
210 #if defined(OS_LINUX)
211 bool initialized_sandbox = false;
212 bool initialized_gl_context = false;
213 bool should_initialize_gl_context = false;
214 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
215 // On Chrome OS ARM, GPU driver userspace creates threads when initializing
216 // a GL context, so start the sandbox early.
217 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
218 should_initialize_gl_context);
219 initialized_sandbox = true;
220 #endif
221 #endif // defined(OS_LINUX)
223 // Load and initialize the GL implementation and locate the GL entry points.
224 if (gfx::GLSurface::InitializeOneOff()) {
225 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
226 // purposes. However, on Mac we don't actually use them. As documented in
227 // crbug.com/222934, due to some driver issues, glGetString could take
228 // multiple seconds to finish, which in turn cause the GPU process to
229 // crash.
230 // By skipping the following code on Mac, we don't really lose anything,
231 // because the basic GPU information is passed down from browser process
232 // and we already registered them through SetGpuInfo() above.
233 #if !defined(OS_MACOSX)
234 if (!gpu::CollectContextGraphicsInfo(&gpu_info))
235 VLOG(1) << "gpu::CollectGraphicsInfo failed";
236 GetContentClient()->SetGpuInfo(gpu_info);
238 #if defined(OS_LINUX)
239 initialized_gl_context = true;
240 #if !defined(OS_CHROMEOS)
241 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
242 gpu_info.driver_vendor == "NVIDIA") {
243 base::ThreadRestrictions::AssertIOAllowed();
244 if (access("/dev/nvidiactl", R_OK) != 0) {
245 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
246 dead_on_arrival = true;
249 #endif // !defined(OS_CHROMEOS)
250 #endif // defined(OS_LINUX)
251 #endif // !defined(OS_MACOSX)
252 } else {
253 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
254 dead_on_arrival = true;
257 if (enable_watchdog && delayed_watchdog_enable) {
258 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
259 watchdog_thread->Start();
262 // OSMesa is expected to run very slowly, so disable the watchdog in that
263 // case.
264 if (enable_watchdog &&
265 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
266 watchdog_thread->Stop();
267 watchdog_thread = NULL;
270 #if defined(OS_LINUX)
271 should_initialize_gl_context = !initialized_gl_context &&
272 !dead_on_arrival;
274 if (!initialized_sandbox) {
275 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
276 should_initialize_gl_context);
278 #elif defined(OS_WIN)
279 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
280 #endif
281 } else {
282 dead_on_arrival = true;
285 logging::SetLogMessageHandler(NULL);
287 GpuProcess gpu_process;
289 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
290 dead_on_arrival,
291 gpu_info,
292 deferred_messages.Get());
293 while (!deferred_messages.Get().empty())
294 deferred_messages.Get().pop();
296 child_thread->Init(start_time);
298 gpu_process.set_main_thread(child_thread);
300 if (watchdog_thread)
301 watchdog_thread->AddPowerObserver();
304 TRACE_EVENT0("gpu", "Run Message Loop");
305 main_message_loop.Run();
308 child_thread->StopWatchdog();
310 return 0;
313 namespace {
315 #if defined(OS_LINUX)
316 void CreateDummyGlContext() {
317 scoped_refptr<gfx::GLSurface> surface(
318 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
319 if (!surface.get()) {
320 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
321 return;
324 // On Linux, this is needed to make sure /dev/nvidiactl has
325 // been opened and its descriptor cached.
326 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
327 NULL, surface.get(), gfx::PreferDiscreteGpu));
328 if (!context.get()) {
329 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
330 return;
333 // Similarly, this is needed for /dev/nvidia0.
334 if (context->MakeCurrent(surface.get())) {
335 context->ReleaseCurrent(surface.get());
336 } else {
337 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
340 #endif
342 bool WarmUpSandbox(const CommandLine& command_line) {
344 TRACE_EVENT0("gpu", "Warm up rand");
345 // Warm up the random subsystem, which needs to be done pre-sandbox on all
346 // platforms.
347 (void) base::RandUint64();
350 TRACE_EVENT0("gpu", "Warm up HMAC");
351 // Warm up the crypto subsystem, which needs to done pre-sandbox on all
352 // platforms.
353 crypto::HMAC hmac(crypto::HMAC::SHA256);
354 unsigned char key = '\0';
355 if (!hmac.Init(&key, sizeof(key))) {
356 LOG(ERROR) << "WarmUpSandbox() failed with crypto::HMAC::Init()";
357 return false;
361 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11)
362 ExynosVideoDecodeAccelerator::PreSandboxInitialization();
363 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
364 VaapiWrapper::PreSandboxInitialization();
365 #endif
367 #if defined(OS_WIN)
369 TRACE_EVENT0("gpu", "Preload setupapi.dll");
370 // Preload this DLL because the sandbox prevents it from loading.
371 if (LoadLibrary(L"setupapi.dll") == NULL) {
372 LOG(ERROR) << "WarmUpSandbox() failed with loading setupapi.dll";
373 return false;
377 if (!command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode)) {
378 TRACE_EVENT0("gpu", "Initialize DXVA");
379 // Initialize H/W video decoding stuff which fails in the sandbox.
380 DXVAVideoDecodeAccelerator::PreSandboxInitialization();
384 TRACE_EVENT0("gpu", "Warm up DWM");
386 // DWM was introduced with Windows Vista. DwmFlush seems to be sufficient
387 // to warm it up before lowering the token. DWM is required to present to
388 // a window with Vista and later and this allows us to do so with the
389 // GPU process sandbox enabled.
390 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
391 HMODULE module = LoadLibrary(L"dwmapi.dll");
392 if (module) {
393 typedef HRESULT (WINAPI *DwmFlushFunc)();
394 DwmFlushFunc dwm_flush = reinterpret_cast<DwmFlushFunc>(
395 GetProcAddress(module, "DwmFlush"));
396 if (dwm_flush)
397 dwm_flush();
401 #endif
402 return true;
405 #if defined(OS_LINUX)
406 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
407 bool should_initialize_gl_context) {
408 // We special case Optimus since the vendor_id we see may not be Nvidia.
409 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
410 gpu_info.driver_vendor == "NVIDIA") ||
411 gpu_info.optimus;
412 if (uses_nvidia_driver && should_initialize_gl_context) {
413 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
414 CreateDummyGlContext();
418 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
419 GpuWatchdogThread* watchdog_thread,
420 bool should_initialize_gl_context) {
421 TRACE_EVENT0("gpu", "Initialize sandbox");
423 bool res = false;
425 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
427 if (watchdog_thread)
428 watchdog_thread->Stop();
429 // LinuxSandbox::InitializeSandbox() must always be called
430 // with only one thread.
431 res = LinuxSandbox::InitializeSandbox();
432 if (watchdog_thread)
433 watchdog_thread->Start();
435 return res;
437 #endif // defined(OS_LINUX)
439 #if defined(OS_WIN)
440 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
441 TRACE_EVENT0("gpu", "Lower token");
443 // For Windows, if the target_services interface is not zero, the process
444 // is sandboxed and we must call LowerToken() before rendering untrusted
445 // content.
446 sandbox::TargetServices* target_services = sandbox_info->target_services;
447 if (target_services) {
448 target_services->LowerToken();
449 return true;
452 return false;
454 #endif // defined(OS_WIN)
456 } // namespace.
458 } // namespace content