cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / content / gpu / gpu_main.cc
blobe7ce06e5a26c77121d07c5db178ceba7c8a250c7
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_memory_buffer_factory.h"
27 #include "content/common/gpu/gpu_messages.h"
28 #include "content/common/gpu/media/gpu_jpeg_decode_accelerator.h"
29 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
30 #include "content/common/gpu/media/gpu_video_encode_accelerator.h"
31 #include "content/common/sandbox_linux/sandbox_linux.h"
32 #include "content/gpu/gpu_child_thread.h"
33 #include "content/gpu/gpu_process.h"
34 #include "content/gpu/gpu_watchdog_thread.h"
35 #include "content/public/common/content_client.h"
36 #include "content/public/common/content_switches.h"
37 #include "content/public/common/main_function_params.h"
38 #include "gpu/command_buffer/service/gpu_switches.h"
39 #include "gpu/command_buffer/service/sync_point_manager.h"
40 #include "gpu/config/gpu_info_collector.h"
41 #include "gpu/config/gpu_switches.h"
42 #include "gpu/config/gpu_util.h"
43 #include "ui/events/platform/platform_event_source.h"
44 #include "ui/gl/gl_context.h"
45 #include "ui/gl/gl_implementation.h"
46 #include "ui/gl/gl_surface.h"
47 #include "ui/gl/gl_switches.h"
48 #include "ui/gl/gpu_switching_manager.h"
50 #if defined(OS_WIN)
51 #include "base/win/windows_version.h"
52 #include "base/win/scoped_com_initializer.h"
53 #include "sandbox/win/src/sandbox.h"
54 #endif
56 #if defined(USE_X11)
57 #include "ui/base/x/x11_util.h"
58 #endif
60 #if defined(OS_LINUX)
61 #include "content/public/common/sandbox_init.h"
62 #endif
64 #if defined(OS_MACOSX)
65 #include "base/message_loop/message_pump_mac.h"
66 #include "content/common/sandbox_mac.h"
67 #endif
69 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
70 #include "content/common/gpu/media/vaapi_wrapper.h"
71 #endif
73 #if defined(SANITIZER_COVERAGE)
74 #include <sanitizer/common_interface_defs.h>
75 #include <sanitizer/coverage_interface.h>
76 #endif
78 #if defined(CYGPROFILE_INSTRUMENTATION)
79 const int kGpuTimeout = 30000;
80 #else
81 const int kGpuTimeout = 10000;
82 #endif
84 namespace content {
86 namespace {
88 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
89 const base::CommandLine& command_line);
90 bool WarmUpSandbox(const base::CommandLine& command_line);
92 #if !defined(OS_MACOSX)
93 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info);
94 #endif
96 #if defined(OS_LINUX)
97 #if !defined(OS_CHROMEOS)
98 bool CanAccessNvidiaDeviceFile();
99 #endif
100 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
101 #elif defined(OS_WIN)
102 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
103 #endif
105 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
106 LAZY_INSTANCE_INITIALIZER;
108 bool GpuProcessLogMessageHandler(int severity,
109 const char* file, int line,
110 size_t message_start,
111 const std::string& str) {
112 std::string header = str.substr(0, message_start);
113 std::string message = str.substr(message_start);
114 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
115 severity, header, message));
116 return false;
119 } // namespace anonymous
121 // Main function for starting the Gpu process.
122 int GpuMain(const MainFunctionParams& parameters) {
123 TRACE_EVENT0("gpu", "GpuMain");
124 base::trace_event::TraceLog::GetInstance()->SetProcessName("GPU Process");
125 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
126 kTraceEventGpuProcessSortIndex);
128 const base::CommandLine& command_line = parameters.command_line;
129 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
130 ChildProcess::WaitForDebugger("Gpu");
133 base::Time start_time = base::Time::Now();
135 #if defined(OS_WIN)
136 // Prevent Windows from displaying a modal dialog on failures like not being
137 // able to load a DLL.
138 SetErrorMode(
139 SEM_FAILCRITICALERRORS |
140 SEM_NOGPFAULTERRORBOX |
141 SEM_NOOPENFILEERRORBOX);
142 #elif defined(USE_X11)
143 ui::SetDefaultX11ErrorHandlers();
144 #endif
146 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
148 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
149 std::string types = command_line.GetSwitchValueASCII(
150 switches::kGpuDriverBugWorkarounds);
151 std::set<int> workarounds;
152 gpu::StringToFeatureSet(types, &workarounds);
153 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
154 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
155 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
156 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
159 // Initialization of the OpenGL bindings may fail, in which case we
160 // will need to tear down this process. However, we can not do so
161 // safely until the IPC channel is set up, because the detection of
162 // early return of a child process is implemented using an IPC
163 // channel error. If the IPC channel is not fully set up between the
164 // browser and GPU process, and the GPU process crashes or exits
165 // early, the browser process will never detect it. For this reason
166 // we defer tearing down the GPU process until receiving the
167 // GpuMsg_Initialize message from the browser.
168 bool dead_on_arrival = false;
170 #if defined(OS_WIN)
171 // Use a UI message loop because ANGLE and the desktop GL platform can
172 // create child windows to render to.
173 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
174 #elif defined(OS_LINUX) && defined(USE_X11)
175 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
176 // and https://crbug.com/326995.
177 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
178 scoped_ptr<ui::PlatformEventSource> event_source =
179 ui::PlatformEventSource::CreateDefault();
180 #elif defined(OS_LINUX)
181 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_DEFAULT);
182 #elif defined(OS_MACOSX)
183 // This is necessary for CoreAnimation layers hosted in the GPU process to be
184 // drawn. See http://crbug.com/312462.
185 scoped_ptr<base::MessagePump> pump(new base::MessagePumpCFRunLoop());
186 base::MessageLoop main_message_loop(pump.Pass());
187 #else
188 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_IO);
189 #endif
191 base::PlatformThread::SetName("CrGpuMain");
193 // In addition to disabling the watchdog if the command line switch is
194 // present, disable the watchdog on valgrind because the code is expected
195 // to run slowly in that case.
196 bool enable_watchdog =
197 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
198 !RunningOnValgrind();
200 // Disable the watchdog in debug builds because they tend to only be run by
201 // developers who will not appreciate the watchdog killing the GPU process.
202 #ifndef NDEBUG
203 enable_watchdog = false;
204 #endif
206 bool delayed_watchdog_enable = false;
208 #if defined(OS_CHROMEOS)
209 // Don't start watchdog immediately, to allow developers to switch to VT2 on
210 // startup.
211 delayed_watchdog_enable = true;
212 #endif
214 scoped_refptr<GpuWatchdogThread> watchdog_thread;
216 // Start the GPU watchdog only after anything that is expected to be time
217 // consuming has completed, otherwise the process is liable to be aborted.
218 if (enable_watchdog && !delayed_watchdog_enable) {
219 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
220 base::Thread::Options options;
221 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
222 watchdog_thread->StartWithOptions(options);
225 // Initializes StatisticsRecorder which tracks UMA histograms.
226 base::StatisticsRecorder::Initialize();
228 gpu::GPUInfo gpu_info;
229 // Get vendor_id, device_id, driver_version from browser process through
230 // commandline switches.
231 GetGpuInfoFromCommandLine(gpu_info, command_line);
232 gpu_info.in_process_gpu = false;
234 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
235 VaapiWrapper::PreSandboxInitialization();
236 #endif
238 // Warm up resources that don't need access to GPUInfo.
239 if (WarmUpSandbox(command_line)) {
240 #if defined(OS_LINUX)
241 bool initialized_sandbox = false;
242 bool initialized_gl_context = false;
243 bool should_initialize_gl_context = false;
244 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
245 // initializing a GL context, so start the sandbox early.
246 if (command_line.HasSwitch(switches::kGpuSandboxStartEarly)) {
247 gpu_info.sandboxed = StartSandboxLinux(
248 gpu_info, watchdog_thread.get(), should_initialize_gl_context);
249 initialized_sandbox = true;
251 #endif // defined(OS_LINUX)
253 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
255 // Determine if we need to initialize GL here or it has already been done.
256 bool gl_already_initialized = false;
257 #if defined(OS_MACOSX)
258 if (!command_line.HasSwitch(switches::kNoSandbox)) {
259 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
260 // is called from the sandbox warmup code before getting here.
261 gl_already_initialized = true;
263 #endif
264 if (command_line.HasSwitch(switches::kInProcessGPU)) {
265 // With in-process GPU, GLSurface::InitializeOneOff() is called from
266 // GpuChildThread before getting here.
267 gl_already_initialized = true;
270 // Load and initialize the GL implementation and locate the GL entry points.
271 bool gl_initialized =
272 gl_already_initialized
273 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
274 : gfx::GLSurface::InitializeOneOff();
275 if (gl_initialized) {
276 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
277 // purposes. However, on Mac we don't actually use them. As documented in
278 // crbug.com/222934, due to some driver issues, glGetString could take
279 // multiple seconds to finish, which in turn cause the GPU process to
280 // crash.
281 // By skipping the following code on Mac, we don't really lose anything,
282 // because the basic GPU information is passed down from browser process
283 // and we already registered them through SetGpuInfo() above.
284 base::TimeTicks before_collect_context_graphics_info =
285 base::TimeTicks::Now();
286 #if !defined(OS_MACOSX)
287 if (!CollectGraphicsInfo(gpu_info))
288 dead_on_arrival = true;
290 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
291 // Recompute gpu driver bug workarounds - this is specifically useful
292 // on systems where vendor_id/device_id aren't available.
293 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
294 gpu::ApplyGpuDriverBugWorkarounds(
295 gpu_info, const_cast<base::CommandLine*>(&command_line));
297 #endif
299 #if defined(OS_LINUX)
300 initialized_gl_context = true;
301 #if !defined(OS_CHROMEOS)
302 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
303 gpu_info.driver_vendor == "NVIDIA" &&
304 !CanAccessNvidiaDeviceFile())
305 dead_on_arrival = true;
306 #endif // !defined(OS_CHROMEOS)
307 #endif // defined(OS_LINUX)
308 #endif // !defined(OS_MACOSX)
309 base::TimeDelta collect_context_time =
310 base::TimeTicks::Now() - before_collect_context_graphics_info;
311 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo",
312 collect_context_time);
313 } else { // gl_initialized
314 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
315 dead_on_arrival = true;
318 base::TimeDelta initialize_one_off_time =
319 base::TimeTicks::Now() - before_initialize_one_off;
320 UMA_HISTOGRAM_MEDIUM_TIMES("GPU.InitializeOneOffMediumTime",
321 initialize_one_off_time);
323 if (enable_watchdog && delayed_watchdog_enable) {
324 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
325 base::Thread::Options options;
326 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
327 watchdog_thread->StartWithOptions(options);
330 // OSMesa is expected to run very slowly, so disable the watchdog in that
331 // case.
332 if (enable_watchdog &&
333 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
334 watchdog_thread->Stop();
335 watchdog_thread = NULL;
338 #if defined(OS_LINUX)
339 should_initialize_gl_context = !initialized_gl_context &&
340 !dead_on_arrival;
342 if (!initialized_sandbox) {
343 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
344 should_initialize_gl_context);
346 #elif defined(OS_WIN)
347 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
348 #elif defined(OS_MACOSX)
349 gpu_info.sandboxed = Sandbox::SandboxIsCurrentlyActive();
350 #endif
352 gpu_info.video_decode_accelerator_supported_profiles =
353 content::GpuVideoDecodeAccelerator::GetSupportedProfiles();
354 gpu_info.video_encode_accelerator_supported_profiles =
355 content::GpuVideoEncodeAccelerator::GetSupportedProfiles();
356 gpu_info.jpeg_decode_accelerator_supported =
357 content::GpuJpegDecodeAccelerator::IsSupported();
358 } else {
359 dead_on_arrival = true;
362 logging::SetLogMessageHandler(NULL);
364 scoped_ptr<GpuMemoryBufferFactory> gpu_memory_buffer_factory =
365 GpuMemoryBufferFactory::Create(
366 GpuChildThread::GetGpuMemoryBufferFactoryType());
367 gpu::SyncPointManager sync_point_manager(false);
369 GpuProcess gpu_process;
371 GpuChildThread* child_thread = new GpuChildThread(
372 watchdog_thread.get(), dead_on_arrival, gpu_info, deferred_messages.Get(),
373 gpu_memory_buffer_factory.get(),
374 &sync_point_manager);
375 while (!deferred_messages.Get().empty())
376 deferred_messages.Get().pop();
378 child_thread->Init(start_time);
380 gpu_process.set_main_thread(child_thread);
382 if (watchdog_thread.get())
383 watchdog_thread->AddPowerObserver();
386 TRACE_EVENT0("gpu", "Run Message Loop");
387 main_message_loop.Run();
390 child_thread->StopWatchdog();
392 return 0;
395 namespace {
397 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
398 const base::CommandLine& command_line) {
399 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
400 command_line.HasSwitch(switches::kGpuDeviceID) &&
401 command_line.HasSwitch(switches::kGpuDriverVersion));
402 bool success = base::HexStringToUInt(
403 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
404 &gpu_info.gpu.vendor_id);
405 DCHECK(success);
406 success = base::HexStringToUInt(
407 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
408 &gpu_info.gpu.device_id);
409 DCHECK(success);
410 gpu_info.driver_vendor =
411 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
412 gpu_info.driver_version =
413 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
414 GetContentClient()->SetGpuInfo(gpu_info);
417 bool WarmUpSandbox(const base::CommandLine& command_line) {
419 TRACE_EVENT0("gpu", "Warm up rand");
420 // Warm up the random subsystem, which needs to be done pre-sandbox on all
421 // platforms.
422 (void) base::RandUint64();
424 return true;
427 #if !defined(OS_MACOSX)
428 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info) {
429 bool res = true;
430 gpu::CollectInfoResult result = gpu::CollectContextGraphicsInfo(&gpu_info);
431 switch (result) {
432 case gpu::kCollectInfoFatalFailure:
433 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
434 res = false;
435 break;
436 case gpu::kCollectInfoNonFatalFailure:
437 DVLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
438 break;
439 case gpu::kCollectInfoNone:
440 NOTREACHED();
441 break;
442 case gpu::kCollectInfoSuccess:
443 break;
445 GetContentClient()->SetGpuInfo(gpu_info);
446 return res;
448 #endif
450 #if defined(OS_LINUX)
451 #if !defined(OS_CHROMEOS)
452 bool CanAccessNvidiaDeviceFile() {
453 bool res = true;
454 base::ThreadRestrictions::AssertIOAllowed();
455 if (access("/dev/nvidiactl", R_OK) != 0) {
456 DVLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
457 res = false;
459 return res;
461 #endif
463 void CreateDummyGlContext() {
464 scoped_refptr<gfx::GLSurface> surface(
465 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
466 if (!surface.get()) {
467 DVLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
468 return;
471 // On Linux, this is needed to make sure /dev/nvidiactl has
472 // been opened and its descriptor cached.
473 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
474 NULL, surface.get(), gfx::PreferDiscreteGpu));
475 if (!context.get()) {
476 DVLOG(1) << "gfx::GLContext::CreateGLContext failed";
477 return;
480 // Similarly, this is needed for /dev/nvidia0.
481 if (context->MakeCurrent(surface.get())) {
482 context->ReleaseCurrent(surface.get());
483 } else {
484 DVLOG(1) << "gfx::GLContext::MakeCurrent failed";
488 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
489 bool should_initialize_gl_context) {
490 // We special case Optimus since the vendor_id we see may not be Nvidia.
491 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
492 gpu_info.driver_vendor == "NVIDIA") ||
493 gpu_info.optimus;
494 if (uses_nvidia_driver && should_initialize_gl_context) {
495 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
496 CreateDummyGlContext();
500 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
501 GpuWatchdogThread* watchdog_thread,
502 bool should_initialize_gl_context) {
503 TRACE_EVENT0("gpu", "Initialize sandbox");
505 bool res = false;
507 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
509 if (watchdog_thread) {
510 // LinuxSandbox needs to be able to ensure that the thread
511 // has really been stopped.
512 LinuxSandbox::StopThread(watchdog_thread);
515 #if defined(SANITIZER_COVERAGE)
516 const std::string sancov_file_name =
517 "gpu." + base::Uint64ToString(base::RandUint64());
518 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
519 linux_sandbox->sanitizer_args()->coverage_sandboxed = 1;
520 linux_sandbox->sanitizer_args()->coverage_fd =
521 __sanitizer_maybe_open_cov_file(sancov_file_name.c_str());
522 linux_sandbox->sanitizer_args()->coverage_max_block_size = 0;
523 #endif
525 // LinuxSandbox::InitializeSandbox() must always be called
526 // with only one thread.
527 res = LinuxSandbox::InitializeSandbox();
528 if (watchdog_thread) {
529 base::Thread::Options options;
530 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
531 watchdog_thread->StartWithOptions(options);
534 return res;
536 #endif // defined(OS_LINUX)
538 #if defined(OS_WIN)
539 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
540 TRACE_EVENT0("gpu", "Lower token");
542 // For Windows, if the target_services interface is not zero, the process
543 // is sandboxed and we must call LowerToken() before rendering untrusted
544 // content.
545 sandbox::TargetServices* target_services = sandbox_info->target_services;
546 if (target_services) {
547 target_services->LowerToken();
548 return true;
551 return false;
553 #endif // defined(OS_WIN)
555 } // namespace.
557 } // namespace content