Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / gpu / gpu_main.cc
blobd05b6d84e51a91132d02a30ccf6d68f403d01ff2
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/metrics/histogram.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/threading/platform_thread.h"
20 #include "build/build_config.h"
21 #include "content/child/child_process.h"
22 #include "content/common/content_constants_internal.h"
23 #include "content/common/gpu/gpu_config.h"
24 #include "content/common/gpu/gpu_messages.h"
25 #include "content/common/sandbox_linux/sandbox_linux.h"
26 #include "content/gpu/gpu_child_thread.h"
27 #include "content/gpu/gpu_process.h"
28 #include "content/gpu/gpu_watchdog_thread.h"
29 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/common/main_function_params.h"
32 #include "gpu/command_buffer/service/gpu_switches.h"
33 #include "gpu/config/gpu_info_collector.h"
34 #include "gpu/config/gpu_util.h"
35 #include "ui/events/platform/platform_event_source.h"
36 #include "ui/gl/gl_implementation.h"
37 #include "ui/gl/gl_surface.h"
38 #include "ui/gl/gl_switches.h"
39 #include "ui/gl/gpu_switching_manager.h"
41 #if defined(OS_WIN)
42 #include "base/win/windows_version.h"
43 #include "base/win/scoped_com_initializer.h"
44 #include "sandbox/win/src/sandbox.h"
45 #endif
47 #if defined(USE_X11)
48 #include "ui/base/x/x11_util.h"
49 #endif
51 #if defined(OS_LINUX)
52 #include "content/public/common/sandbox_init.h"
53 #endif
55 #if defined(OS_MACOSX)
56 #include "base/message_loop/message_pump_mac.h"
57 #include "content/common/sandbox_mac.h"
58 #endif
60 #if defined(ADDRESS_SANITIZER)
61 #include <sanitizer/asan_interface.h>
62 #endif
64 const int kGpuTimeout = 10000;
66 namespace content {
68 namespace {
70 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
71 const CommandLine& command_line);
72 bool WarmUpSandbox(const CommandLine& command_line);
74 #if !defined(OS_MACOSX)
75 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info);
76 #endif
78 #if defined(OS_LINUX)
79 #if !defined(OS_CHROMEOS)
80 bool CanAccessNvidiaDeviceFile();
81 #endif
82 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
83 #elif defined(OS_WIN)
84 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
85 #endif
87 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
88 LAZY_INSTANCE_INITIALIZER;
90 bool GpuProcessLogMessageHandler(int severity,
91 const char* file, int line,
92 size_t message_start,
93 const std::string& str) {
94 std::string header = str.substr(0, message_start);
95 std::string message = str.substr(message_start);
96 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
97 severity, header, message));
98 return false;
101 } // namespace anonymous
103 // Main function for starting the Gpu process.
104 int GpuMain(const MainFunctionParams& parameters) {
105 TRACE_EVENT0("gpu", "GpuMain");
106 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
107 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
108 kTraceEventGpuProcessSortIndex);
110 const CommandLine& command_line = parameters.command_line;
111 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
112 ChildProcess::WaitForDebugger("Gpu");
115 base::Time start_time = base::Time::Now();
117 #if defined(OS_WIN)
118 // Prevent Windows from displaying a modal dialog on failures like not being
119 // able to load a DLL.
120 SetErrorMode(
121 SEM_FAILCRITICALERRORS |
122 SEM_NOGPFAULTERRORBOX |
123 SEM_NOOPENFILEERRORBOX);
124 #elif defined(USE_X11)
125 ui::SetDefaultX11ErrorHandlers();
126 #endif
128 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
130 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
131 std::string types = command_line.GetSwitchValueASCII(
132 switches::kGpuDriverBugWorkarounds);
133 std::set<int> workarounds;
134 gpu::StringToFeatureSet(types, &workarounds);
135 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
136 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
137 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
138 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
141 // Initialization of the OpenGL bindings may fail, in which case we
142 // will need to tear down this process. However, we can not do so
143 // safely until the IPC channel is set up, because the detection of
144 // early return of a child process is implemented using an IPC
145 // channel error. If the IPC channel is not fully set up between the
146 // browser and GPU process, and the GPU process crashes or exits
147 // early, the browser process will never detect it. For this reason
148 // we defer tearing down the GPU process until receiving the
149 // GpuMsg_Initialize message from the browser.
150 bool dead_on_arrival = false;
152 #if defined(OS_WIN)
153 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
154 // Unless we're running on desktop GL, we don't need a UI message
155 // loop, so avoid its use to work around apparent problems with some
156 // third-party software.
157 if (command_line.HasSwitch(switches::kUseGL) &&
158 command_line.GetSwitchValueASCII(switches::kUseGL) ==
159 gfx::kGLImplementationDesktopName) {
160 message_loop_type = base::MessageLoop::TYPE_UI;
162 base::MessageLoop main_message_loop(message_loop_type);
163 #elif defined(OS_LINUX) && defined(USE_X11)
164 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
165 // and https://crbug.com/326995.
166 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
167 scoped_ptr<ui::PlatformEventSource> event_source =
168 ui::PlatformEventSource::CreateDefault();
169 #elif defined(OS_LINUX)
170 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_DEFAULT);
171 #elif defined(OS_MACOSX)
172 // This is necessary for CoreAnimation layers hosted in the GPU process to be
173 // drawn. See http://crbug.com/312462.
174 scoped_ptr<base::MessagePump> pump(new base::MessagePumpCFRunLoop());
175 base::MessageLoop main_message_loop(pump.Pass());
176 #else
177 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_IO);
178 #endif
180 base::PlatformThread::SetName("CrGpuMain");
182 // In addition to disabling the watchdog if the command line switch is
183 // present, disable the watchdog on valgrind because the code is expected
184 // to run slowly in that case.
185 bool enable_watchdog =
186 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
187 !RunningOnValgrind();
189 // Disable the watchdog in debug builds because they tend to only be run by
190 // developers who will not appreciate the watchdog killing the GPU process.
191 #ifndef NDEBUG
192 enable_watchdog = false;
193 #endif
195 bool delayed_watchdog_enable = false;
197 #if defined(OS_CHROMEOS)
198 // Don't start watchdog immediately, to allow developers to switch to VT2 on
199 // startup.
200 delayed_watchdog_enable = true;
201 #endif
203 scoped_refptr<GpuWatchdogThread> watchdog_thread;
205 // Start the GPU watchdog only after anything that is expected to be time
206 // consuming has completed, otherwise the process is liable to be aborted.
207 if (enable_watchdog && !delayed_watchdog_enable) {
208 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
209 watchdog_thread->Start();
212 gpu::GPUInfo gpu_info;
213 // Get vendor_id, device_id, driver_version from browser process through
214 // commandline switches.
215 GetGpuInfoFromCommandLine(gpu_info, command_line);
217 base::TimeDelta collect_context_time;
218 base::TimeDelta initialize_one_off_time;
220 // Warm up resources that don't need access to GPUInfo.
221 if (WarmUpSandbox(command_line)) {
222 #if defined(OS_LINUX)
223 bool initialized_sandbox = false;
224 bool initialized_gl_context = false;
225 bool should_initialize_gl_context = false;
226 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
227 // initializing a GL context, so start the sandbox early.
228 if (command_line.HasSwitch(switches::kGpuSandboxStartEarly)) {
229 gpu_info.sandboxed = StartSandboxLinux(
230 gpu_info, watchdog_thread.get(), should_initialize_gl_context);
231 initialized_sandbox = true;
233 #endif // defined(OS_LINUX)
235 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
237 // Determine if we need to initialize GL here or it has already been done.
238 bool gl_already_initialized = false;
239 #if defined(OS_MACOSX)
240 if (!command_line.HasSwitch(switches::kNoSandbox)) {
241 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
242 // is called from the sandbox warmup code before getting here.
243 gl_already_initialized = true;
245 #endif
246 if (command_line.HasSwitch(switches::kInProcessGPU)) {
247 // With in-process GPU, GLSurface::InitializeOneOff() is called from
248 // GpuChildThread before getting here.
249 gl_already_initialized = true;
252 // Load and initialize the GL implementation and locate the GL entry points.
253 bool gl_initialized =
254 gl_already_initialized
255 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
256 : gfx::GLSurface::InitializeOneOff();
257 if (gl_initialized) {
258 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
259 // purposes. However, on Mac we don't actually use them. As documented in
260 // crbug.com/222934, due to some driver issues, glGetString could take
261 // multiple seconds to finish, which in turn cause the GPU process to
262 // crash.
263 // By skipping the following code on Mac, we don't really lose anything,
264 // because the basic GPU information is passed down from browser process
265 // and we already registered them through SetGpuInfo() above.
266 base::TimeTicks before_collect_context_graphics_info =
267 base::TimeTicks::Now();
268 #if !defined(OS_MACOSX)
269 if (!CollectGraphicsInfo(gpu_info))
270 dead_on_arrival = true;
272 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
273 // Recompute gpu driver bug workarounds - this is specifically useful
274 // on systems where vendor_id/device_id aren't available.
275 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
276 gpu::ApplyGpuDriverBugWorkarounds(
277 gpu_info, const_cast<CommandLine*>(&command_line));
279 #endif
281 #if defined(OS_LINUX)
282 initialized_gl_context = true;
283 #if !defined(OS_CHROMEOS)
284 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
285 gpu_info.driver_vendor == "NVIDIA" &&
286 !CanAccessNvidiaDeviceFile())
287 dead_on_arrival = true;
288 #endif // !defined(OS_CHROMEOS)
289 #endif // defined(OS_LINUX)
290 #endif // !defined(OS_MACOSX)
291 collect_context_time =
292 base::TimeTicks::Now() - before_collect_context_graphics_info;
293 } else { // gl_initialized
294 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
295 dead_on_arrival = true;
298 initialize_one_off_time =
299 base::TimeTicks::Now() - before_initialize_one_off;
301 if (enable_watchdog && delayed_watchdog_enable) {
302 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
303 watchdog_thread->Start();
306 // OSMesa is expected to run very slowly, so disable the watchdog in that
307 // case.
308 if (enable_watchdog &&
309 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
310 watchdog_thread->Stop();
311 watchdog_thread = NULL;
314 #if defined(OS_LINUX)
315 should_initialize_gl_context = !initialized_gl_context &&
316 !dead_on_arrival;
318 if (!initialized_sandbox) {
319 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
320 should_initialize_gl_context);
322 #elif defined(OS_WIN)
323 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
324 #elif defined(OS_MACOSX)
325 gpu_info.sandboxed = Sandbox::SandboxIsCurrentlyActive();
326 #endif
327 } else {
328 dead_on_arrival = true;
331 logging::SetLogMessageHandler(NULL);
333 GpuProcess gpu_process;
335 // These UMA must be stored after GpuProcess is constructed as it
336 // initializes StatisticsRecorder which tracks the histograms.
337 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
338 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
340 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
341 dead_on_arrival,
342 gpu_info,
343 deferred_messages.Get());
344 while (!deferred_messages.Get().empty())
345 deferred_messages.Get().pop();
347 child_thread->Init(start_time);
349 gpu_process.set_main_thread(child_thread);
351 if (watchdog_thread.get())
352 watchdog_thread->AddPowerObserver();
355 TRACE_EVENT0("gpu", "Run Message Loop");
356 main_message_loop.Run();
359 child_thread->StopWatchdog();
361 return 0;
364 namespace {
366 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
367 const CommandLine& command_line) {
368 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
369 command_line.HasSwitch(switches::kGpuDeviceID) &&
370 command_line.HasSwitch(switches::kGpuDriverVersion));
371 bool success = base::HexStringToUInt(
372 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
373 &gpu_info.gpu.vendor_id);
374 DCHECK(success);
375 success = base::HexStringToUInt(
376 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
377 &gpu_info.gpu.device_id);
378 DCHECK(success);
379 gpu_info.driver_vendor =
380 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
381 gpu_info.driver_version =
382 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
383 GetContentClient()->SetGpuInfo(gpu_info);
386 bool WarmUpSandbox(const CommandLine& command_line) {
388 TRACE_EVENT0("gpu", "Warm up rand");
389 // Warm up the random subsystem, which needs to be done pre-sandbox on all
390 // platforms.
391 (void) base::RandUint64();
393 return true;
396 #if !defined(OS_MACOSX)
397 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info) {
398 bool res = true;
399 gpu::CollectInfoResult result = gpu::CollectContextGraphicsInfo(&gpu_info);
400 switch (result) {
401 case gpu::kCollectInfoFatalFailure:
402 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
403 res = false;
404 break;
405 case gpu::kCollectInfoNonFatalFailure:
406 VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
407 break;
408 case gpu::kCollectInfoSuccess:
409 break;
411 GetContentClient()->SetGpuInfo(gpu_info);
412 return res;
414 #endif
416 #if defined(OS_LINUX)
417 #if !defined(OS_CHROMEOS)
418 bool CanAccessNvidiaDeviceFile() {
419 bool res = true;
420 base::ThreadRestrictions::AssertIOAllowed();
421 if (access("/dev/nvidiactl", R_OK) != 0) {
422 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
423 res = false;
425 return res;
427 #endif
429 void CreateDummyGlContext() {
430 scoped_refptr<gfx::GLSurface> surface(
431 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
432 if (!surface.get()) {
433 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
434 return;
437 // On Linux, this is needed to make sure /dev/nvidiactl has
438 // been opened and its descriptor cached.
439 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
440 NULL, surface.get(), gfx::PreferDiscreteGpu));
441 if (!context.get()) {
442 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
443 return;
446 // Similarly, this is needed for /dev/nvidia0.
447 if (context->MakeCurrent(surface.get())) {
448 context->ReleaseCurrent(surface.get());
449 } else {
450 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
454 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
455 bool should_initialize_gl_context) {
456 // We special case Optimus since the vendor_id we see may not be Nvidia.
457 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
458 gpu_info.driver_vendor == "NVIDIA") ||
459 gpu_info.optimus;
460 if (uses_nvidia_driver && should_initialize_gl_context) {
461 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
462 CreateDummyGlContext();
466 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
467 GpuWatchdogThread* watchdog_thread,
468 bool should_initialize_gl_context) {
469 TRACE_EVENT0("gpu", "Initialize sandbox");
471 bool res = false;
473 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
475 if (watchdog_thread) {
476 // LinuxSandbox needs to be able to ensure that the thread
477 // has really been stopped.
478 LinuxSandbox::StopThread(watchdog_thread);
481 #if defined(ADDRESS_SANITIZER)
482 const std::string sancov_file_name =
483 "gpu." + base::Uint64ToString(base::RandUint64());
484 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
485 linux_sandbox->sanitizer_args()->coverage_sandboxed = 1;
486 linux_sandbox->sanitizer_args()->coverage_fd =
487 __sanitizer_maybe_open_cov_file(sancov_file_name.c_str());
488 linux_sandbox->sanitizer_args()->coverage_max_block_size = 0;
489 #endif
491 // LinuxSandbox::InitializeSandbox() must always be called
492 // with only one thread.
493 res = LinuxSandbox::InitializeSandbox();
494 if (watchdog_thread) {
495 watchdog_thread->Start();
498 return res;
500 #endif // defined(OS_LINUX)
502 #if defined(OS_WIN)
503 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
504 TRACE_EVENT0("gpu", "Lower token");
506 // For Windows, if the target_services interface is not zero, the process
507 // is sandboxed and we must call LowerToken() before rendering untrusted
508 // content.
509 sandbox::TargetServices* target_services = sandbox_info->target_services;
510 if (target_services) {
511 target_services->LowerToken();
512 return true;
515 return false;
517 #endif // defined(OS_WIN)
519 } // namespace.
521 } // namespace content