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