Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / content / gpu / gpu_main.cc
blob76a6020e0a2a5929f7b865f862c3807111b737b1
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/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/statistics_recorder.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
20 #include "base/threading/platform_thread.h"
21 #include "base/trace_event/trace_event.h"
22 #include "build/build_config.h"
23 #include "content/child/child_process.h"
24 #include "content/common/content_constants_internal.h"
25 #include "content/common/gpu/gpu_config.h"
26 #include "content/common/gpu/gpu_messages.h"
27 #include "content/common/gpu/media/gpu_video_encode_accelerator.h"
28 #include "content/common/sandbox_linux/sandbox_linux.h"
29 #include "content/gpu/gpu_child_thread.h"
30 #include "content/gpu/gpu_process.h"
31 #include "content/gpu/gpu_watchdog_thread.h"
32 #include "content/public/common/content_client.h"
33 #include "content/public/common/content_switches.h"
34 #include "content/public/common/main_function_params.h"
35 #include "gpu/command_buffer/service/gpu_switches.h"
36 #include "gpu/config/gpu_info_collector.h"
37 #include "gpu/config/gpu_util.h"
38 #include "ui/events/platform/platform_event_source.h"
39 #include "ui/gl/gl_implementation.h"
40 #include "ui/gl/gl_surface.h"
41 #include "ui/gl/gl_switches.h"
42 #include "ui/gl/gpu_switching_manager.h"
44 #if defined(OS_WIN)
45 #include "base/win/windows_version.h"
46 #include "base/win/scoped_com_initializer.h"
47 #include "sandbox/win/src/sandbox.h"
48 #endif
50 #if defined(USE_X11)
51 #include "ui/base/x/x11_util.h"
52 #endif
54 #if defined(OS_LINUX)
55 #include "content/public/common/sandbox_init.h"
56 #endif
58 #if defined(OS_MACOSX)
59 #include "base/message_loop/message_pump_mac.h"
60 #include "content/common/sandbox_mac.h"
61 #endif
63 #if defined(SANITIZER_COVERAGE)
64 #include <sanitizer/common_interface_defs.h>
65 #include <sanitizer/coverage_interface.h>
66 #endif
68 const int kGpuTimeout = 10000;
70 namespace content {
72 namespace {
74 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
75 const base::CommandLine& command_line);
76 bool WarmUpSandbox(const base::CommandLine& command_line);
78 #if !defined(OS_MACOSX)
79 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info);
80 #endif
82 #if defined(OS_LINUX)
83 #if !defined(OS_CHROMEOS)
84 bool CanAccessNvidiaDeviceFile();
85 #endif
86 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
87 #elif defined(OS_WIN)
88 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
89 #endif
91 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
92 LAZY_INSTANCE_INITIALIZER;
94 bool GpuProcessLogMessageHandler(int severity,
95 const char* file, int line,
96 size_t message_start,
97 const std::string& str) {
98 std::string header = str.substr(0, message_start);
99 std::string message = str.substr(message_start);
100 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
101 severity, header, message));
102 return false;
105 } // namespace anonymous
107 // Main function for starting the Gpu process.
108 int GpuMain(const MainFunctionParams& parameters) {
109 TRACE_EVENT0("gpu", "GpuMain");
110 base::trace_event::TraceLog::GetInstance()->SetProcessName("GPU Process");
111 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
112 kTraceEventGpuProcessSortIndex);
114 const base::CommandLine& command_line = parameters.command_line;
115 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
116 ChildProcess::WaitForDebugger("Gpu");
119 base::Time start_time = base::Time::Now();
121 #if defined(OS_WIN)
122 // Prevent Windows from displaying a modal dialog on failures like not being
123 // able to load a DLL.
124 SetErrorMode(
125 SEM_FAILCRITICALERRORS |
126 SEM_NOGPFAULTERRORBOX |
127 SEM_NOOPENFILEERRORBOX);
128 #elif defined(USE_X11)
129 ui::SetDefaultX11ErrorHandlers();
130 #endif
132 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
134 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
135 std::string types = command_line.GetSwitchValueASCII(
136 switches::kGpuDriverBugWorkarounds);
137 std::set<int> workarounds;
138 gpu::StringToFeatureSet(types, &workarounds);
139 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
140 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
141 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
142 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
145 // Initialization of the OpenGL bindings may fail, in which case we
146 // will need to tear down this process. However, we can not do so
147 // safely until the IPC channel is set up, because the detection of
148 // early return of a child process is implemented using an IPC
149 // channel error. If the IPC channel is not fully set up between the
150 // browser and GPU process, and the GPU process crashes or exits
151 // early, the browser process will never detect it. For this reason
152 // we defer tearing down the GPU process until receiving the
153 // GpuMsg_Initialize message from the browser.
154 bool dead_on_arrival = false;
156 #if defined(OS_WIN)
157 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
158 // Unless we're running on desktop GL, we don't need a UI message
159 // loop, so avoid its use to work around apparent problems with some
160 // third-party software.
161 if (command_line.HasSwitch(switches::kUseGL) &&
162 command_line.GetSwitchValueASCII(switches::kUseGL) ==
163 gfx::kGLImplementationDesktopName) {
164 message_loop_type = base::MessageLoop::TYPE_UI;
166 base::MessageLoop main_message_loop(message_loop_type);
167 #elif defined(OS_LINUX) && defined(USE_X11)
168 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
169 // and https://crbug.com/326995.
170 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
171 scoped_ptr<ui::PlatformEventSource> event_source =
172 ui::PlatformEventSource::CreateDefault();
173 #elif defined(OS_LINUX)
174 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_DEFAULT);
175 #elif defined(OS_MACOSX)
176 // This is necessary for CoreAnimation layers hosted in the GPU process to be
177 // drawn. See http://crbug.com/312462.
178 scoped_ptr<base::MessagePump> pump(new base::MessagePumpCFRunLoop());
179 base::MessageLoop main_message_loop(pump.Pass());
180 #else
181 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_IO);
182 #endif
184 base::PlatformThread::SetName("CrGpuMain");
186 // In addition to disabling the watchdog if the command line switch is
187 // present, disable the watchdog on valgrind because the code is expected
188 // to run slowly in that case.
189 bool enable_watchdog =
190 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
191 !RunningOnValgrind();
193 // Disable the watchdog in debug builds because they tend to only be run by
194 // developers who will not appreciate the watchdog killing the GPU process.
195 #ifndef NDEBUG
196 enable_watchdog = false;
197 #endif
199 bool delayed_watchdog_enable = false;
201 #if defined(OS_CHROMEOS)
202 // Don't start watchdog immediately, to allow developers to switch to VT2 on
203 // startup.
204 delayed_watchdog_enable = true;
205 #endif
207 scoped_refptr<GpuWatchdogThread> watchdog_thread;
209 // Start the GPU watchdog only after anything that is expected to be time
210 // consuming has completed, otherwise the process is liable to be aborted.
211 if (enable_watchdog && !delayed_watchdog_enable) {
212 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
213 base::Thread::Options options;
214 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
215 watchdog_thread->StartWithOptions(options);
218 // Initializes StatisticsRecorder which tracks UMA histograms.
219 base::StatisticsRecorder::Initialize();
221 gpu::GPUInfo gpu_info;
222 // Get vendor_id, device_id, driver_version from browser process through
223 // commandline switches.
224 GetGpuInfoFromCommandLine(gpu_info, command_line);
226 // Warm up resources that don't need access to GPUInfo.
227 if (WarmUpSandbox(command_line)) {
228 #if defined(OS_LINUX)
229 bool initialized_sandbox = false;
230 bool initialized_gl_context = false;
231 bool should_initialize_gl_context = false;
232 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
233 // initializing a GL context, so start the sandbox early.
234 if (command_line.HasSwitch(switches::kGpuSandboxStartEarly)) {
235 gpu_info.sandboxed = StartSandboxLinux(
236 gpu_info, watchdog_thread.get(), should_initialize_gl_context);
237 initialized_sandbox = true;
239 #endif // defined(OS_LINUX)
241 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
243 // Determine if we need to initialize GL here or it has already been done.
244 bool gl_already_initialized = false;
245 #if defined(OS_MACOSX)
246 if (!command_line.HasSwitch(switches::kNoSandbox)) {
247 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
248 // is called from the sandbox warmup code before getting here.
249 gl_already_initialized = true;
251 #endif
252 if (command_line.HasSwitch(switches::kInProcessGPU)) {
253 // With in-process GPU, GLSurface::InitializeOneOff() is called from
254 // GpuChildThread before getting here.
255 gl_already_initialized = true;
258 // Load and initialize the GL implementation and locate the GL entry points.
259 bool gl_initialized =
260 gl_already_initialized
261 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
262 : gfx::GLSurface::InitializeOneOff();
263 if (gl_initialized) {
264 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
265 // purposes. However, on Mac we don't actually use them. As documented in
266 // crbug.com/222934, due to some driver issues, glGetString could take
267 // multiple seconds to finish, which in turn cause the GPU process to
268 // crash.
269 // By skipping the following code on Mac, we don't really lose anything,
270 // because the basic GPU information is passed down from browser process
271 // and we already registered them through SetGpuInfo() above.
272 base::TimeTicks before_collect_context_graphics_info =
273 base::TimeTicks::Now();
274 #if !defined(OS_MACOSX)
275 if (!CollectGraphicsInfo(gpu_info))
276 dead_on_arrival = true;
278 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
279 // Recompute gpu driver bug workarounds - this is specifically useful
280 // on systems where vendor_id/device_id aren't available.
281 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
282 gpu::ApplyGpuDriverBugWorkarounds(
283 gpu_info, const_cast<base::CommandLine*>(&command_line));
285 #endif
287 #if defined(OS_LINUX)
288 initialized_gl_context = true;
289 #if !defined(OS_CHROMEOS)
290 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
291 gpu_info.driver_vendor == "NVIDIA" &&
292 !CanAccessNvidiaDeviceFile())
293 dead_on_arrival = true;
294 #endif // !defined(OS_CHROMEOS)
295 #endif // defined(OS_LINUX)
296 #endif // !defined(OS_MACOSX)
297 base::TimeDelta collect_context_time =
298 base::TimeTicks::Now() - before_collect_context_graphics_info;
299 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo",
300 collect_context_time);
301 } else { // gl_initialized
302 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
303 dead_on_arrival = true;
306 base::TimeDelta initialize_one_off_time =
307 base::TimeTicks::Now() - before_initialize_one_off;
308 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
310 if (enable_watchdog && delayed_watchdog_enable) {
311 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
312 base::Thread::Options options;
313 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
314 watchdog_thread->StartWithOptions(options);
317 // OSMesa is expected to run very slowly, so disable the watchdog in that
318 // case.
319 if (enable_watchdog &&
320 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
321 watchdog_thread->Stop();
322 watchdog_thread = NULL;
325 #if defined(OS_LINUX)
326 should_initialize_gl_context = !initialized_gl_context &&
327 !dead_on_arrival;
329 if (!initialized_sandbox) {
330 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
331 should_initialize_gl_context);
333 #elif defined(OS_WIN)
334 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
335 #elif defined(OS_MACOSX)
336 gpu_info.sandboxed = Sandbox::SandboxIsCurrentlyActive();
337 #endif
339 gpu_info.video_encode_accelerator_supported_profiles =
340 content::GpuVideoEncodeAccelerator::GetSupportedProfiles();
341 } else {
342 dead_on_arrival = true;
345 logging::SetLogMessageHandler(NULL);
347 GpuProcess gpu_process;
349 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
350 dead_on_arrival,
351 gpu_info,
352 deferred_messages.Get());
353 while (!deferred_messages.Get().empty())
354 deferred_messages.Get().pop();
356 child_thread->Init(start_time);
358 gpu_process.set_main_thread(child_thread);
360 if (watchdog_thread.get())
361 watchdog_thread->AddPowerObserver();
364 TRACE_EVENT0("gpu", "Run Message Loop");
365 main_message_loop.Run();
368 child_thread->StopWatchdog();
370 return 0;
373 namespace {
375 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
376 const base::CommandLine& command_line) {
377 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
378 command_line.HasSwitch(switches::kGpuDeviceID) &&
379 command_line.HasSwitch(switches::kGpuDriverVersion));
380 bool success = base::HexStringToUInt(
381 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
382 &gpu_info.gpu.vendor_id);
383 DCHECK(success);
384 success = base::HexStringToUInt(
385 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
386 &gpu_info.gpu.device_id);
387 DCHECK(success);
388 gpu_info.driver_vendor =
389 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
390 gpu_info.driver_version =
391 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
392 GetContentClient()->SetGpuInfo(gpu_info);
395 bool WarmUpSandbox(const base::CommandLine& command_line) {
397 TRACE_EVENT0("gpu", "Warm up rand");
398 // Warm up the random subsystem, which needs to be done pre-sandbox on all
399 // platforms.
400 (void) base::RandUint64();
402 return true;
405 #if !defined(OS_MACOSX)
406 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info) {
407 bool res = true;
408 gpu::CollectInfoResult result = gpu::CollectContextGraphicsInfo(&gpu_info);
409 switch (result) {
410 case gpu::kCollectInfoFatalFailure:
411 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
412 res = false;
413 break;
414 case gpu::kCollectInfoNonFatalFailure:
415 DVLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
416 break;
417 case gpu::kCollectInfoNone:
418 NOTREACHED();
419 break;
420 case gpu::kCollectInfoSuccess:
421 break;
423 GetContentClient()->SetGpuInfo(gpu_info);
424 return res;
426 #endif
428 #if defined(OS_LINUX)
429 #if !defined(OS_CHROMEOS)
430 bool CanAccessNvidiaDeviceFile() {
431 bool res = true;
432 base::ThreadRestrictions::AssertIOAllowed();
433 if (access("/dev/nvidiactl", R_OK) != 0) {
434 DVLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
435 res = false;
437 return res;
439 #endif
441 void CreateDummyGlContext() {
442 scoped_refptr<gfx::GLSurface> surface(
443 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
444 if (!surface.get()) {
445 DVLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
446 return;
449 // On Linux, this is needed to make sure /dev/nvidiactl has
450 // been opened and its descriptor cached.
451 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
452 NULL, surface.get(), gfx::PreferDiscreteGpu));
453 if (!context.get()) {
454 DVLOG(1) << "gfx::GLContext::CreateGLContext failed";
455 return;
458 // Similarly, this is needed for /dev/nvidia0.
459 if (context->MakeCurrent(surface.get())) {
460 context->ReleaseCurrent(surface.get());
461 } else {
462 DVLOG(1) << "gfx::GLContext::MakeCurrent failed";
466 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
467 bool should_initialize_gl_context) {
468 // We special case Optimus since the vendor_id we see may not be Nvidia.
469 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
470 gpu_info.driver_vendor == "NVIDIA") ||
471 gpu_info.optimus;
472 if (uses_nvidia_driver && should_initialize_gl_context) {
473 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
474 CreateDummyGlContext();
478 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
479 GpuWatchdogThread* watchdog_thread,
480 bool should_initialize_gl_context) {
481 TRACE_EVENT0("gpu", "Initialize sandbox");
483 bool res = false;
485 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
487 if (watchdog_thread) {
488 // LinuxSandbox needs to be able to ensure that the thread
489 // has really been stopped.
490 LinuxSandbox::StopThread(watchdog_thread);
493 #if defined(SANITIZER_COVERAGE)
494 const std::string sancov_file_name =
495 "gpu." + base::Uint64ToString(base::RandUint64());
496 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
497 linux_sandbox->sanitizer_args()->coverage_sandboxed = 1;
498 linux_sandbox->sanitizer_args()->coverage_fd =
499 __sanitizer_maybe_open_cov_file(sancov_file_name.c_str());
500 linux_sandbox->sanitizer_args()->coverage_max_block_size = 0;
501 #endif
503 // LinuxSandbox::InitializeSandbox() must always be called
504 // with only one thread.
505 res = LinuxSandbox::InitializeSandbox();
506 if (watchdog_thread) {
507 base::Thread::Options options;
508 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
509 watchdog_thread->StartWithOptions(options);
512 return res;
514 #endif // defined(OS_LINUX)
516 #if defined(OS_WIN)
517 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
518 TRACE_EVENT0("gpu", "Lower token");
520 // For Windows, if the target_services interface is not zero, the process
521 // is sandboxed and we must call LowerToken() before rendering untrusted
522 // content.
523 sandbox::TargetServices* target_services = sandbox_info->target_services;
524 if (target_services) {
525 #if defined(ADDRESS_SANITIZER)
526 // Bind and leak dbghelp.dll before the token is lowered, otherwise
527 // AddressSanitizer will crash when trying to symbolize a report.
528 if (!LoadLibraryA("dbghelp.dll"))
529 return false;
530 #endif
532 target_services->LowerToken();
533 return true;
536 return false;
538 #endif // defined(OS_WIN)
540 } // namespace.
542 } // namespace content