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.
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_decode_accelerator.h"
28 #include "content/common/gpu/media/gpu_video_encode_accelerator.h"
29 #include "content/common/sandbox_linux/sandbox_linux.h"
30 #include "content/gpu/gpu_child_thread.h"
31 #include "content/gpu/gpu_process.h"
32 #include "content/gpu/gpu_watchdog_thread.h"
33 #include "content/public/common/content_client.h"
34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/main_function_params.h"
36 #include "gpu/command_buffer/service/gpu_switches.h"
37 #include "gpu/config/gpu_info_collector.h"
38 #include "gpu/config/gpu_switches.h"
39 #include "gpu/config/gpu_util.h"
40 #include "ui/events/platform/platform_event_source.h"
41 #include "ui/gl/gl_implementation.h"
42 #include "ui/gl/gl_surface.h"
43 #include "ui/gl/gl_switches.h"
44 #include "ui/gl/gpu_switching_manager.h"
47 #include "base/win/windows_version.h"
48 #include "base/win/scoped_com_initializer.h"
49 #include "sandbox/win/src/sandbox.h"
53 #include "ui/base/x/x11_util.h"
57 #include "content/public/common/sandbox_init.h"
60 #if defined(OS_MACOSX)
61 #include "base/message_loop/message_pump_mac.h"
62 #include "content/common/sandbox_mac.h"
65 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
66 #include "content/common/gpu/media/vaapi_wrapper.h"
69 #if defined(SANITIZER_COVERAGE)
70 #include <sanitizer/common_interface_defs.h>
71 #include <sanitizer/coverage_interface.h>
74 const int kGpuTimeout
= 10000;
80 void GetGpuInfoFromCommandLine(gpu::GPUInfo
& gpu_info
,
81 const base::CommandLine
& command_line
);
82 bool WarmUpSandbox(const base::CommandLine
& command_line
);
84 #if !defined(OS_MACOSX)
85 bool CollectGraphicsInfo(gpu::GPUInfo
& gpu_info
);
89 #if !defined(OS_CHROMEOS)
90 bool CanAccessNvidiaDeviceFile();
92 bool StartSandboxLinux(const gpu::GPUInfo
&, GpuWatchdogThread
*, bool);
94 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo
*);
97 base::LazyInstance
<GpuChildThread::DeferredMessages
> deferred_messages
=
98 LAZY_INSTANCE_INITIALIZER
;
100 bool GpuProcessLogMessageHandler(int severity
,
101 const char* file
, int line
,
102 size_t message_start
,
103 const std::string
& str
) {
104 std::string header
= str
.substr(0, message_start
);
105 std::string message
= str
.substr(message_start
);
106 deferred_messages
.Get().push(new GpuHostMsg_OnLogMessage(
107 severity
, header
, message
));
111 } // namespace anonymous
113 // Main function for starting the Gpu process.
114 int GpuMain(const MainFunctionParams
& parameters
) {
115 TRACE_EVENT0("gpu", "GpuMain");
116 base::trace_event::TraceLog::GetInstance()->SetProcessName("GPU Process");
117 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
118 kTraceEventGpuProcessSortIndex
);
120 const base::CommandLine
& command_line
= parameters
.command_line
;
121 if (command_line
.HasSwitch(switches::kGpuStartupDialog
)) {
122 ChildProcess::WaitForDebugger("Gpu");
125 base::Time start_time
= base::Time::Now();
128 // Prevent Windows from displaying a modal dialog on failures like not being
129 // able to load a DLL.
131 SEM_FAILCRITICALERRORS
|
132 SEM_NOGPFAULTERRORBOX
|
133 SEM_NOOPENFILEERRORBOX
);
134 #elif defined(USE_X11)
135 ui::SetDefaultX11ErrorHandlers();
138 logging::SetLogMessageHandler(GpuProcessLogMessageHandler
);
140 if (command_line
.HasSwitch(switches::kSupportsDualGpus
)) {
141 std::string types
= command_line
.GetSwitchValueASCII(
142 switches::kGpuDriverBugWorkarounds
);
143 std::set
<int> workarounds
;
144 gpu::StringToFeatureSet(types
, &workarounds
);
145 if (workarounds
.count(gpu::FORCE_DISCRETE_GPU
) == 1)
146 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
147 else if (workarounds
.count(gpu::FORCE_INTEGRATED_GPU
) == 1)
148 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
151 // Initialization of the OpenGL bindings may fail, in which case we
152 // will need to tear down this process. However, we can not do so
153 // safely until the IPC channel is set up, because the detection of
154 // early return of a child process is implemented using an IPC
155 // channel error. If the IPC channel is not fully set up between the
156 // browser and GPU process, and the GPU process crashes or exits
157 // early, the browser process will never detect it. For this reason
158 // we defer tearing down the GPU process until receiving the
159 // GpuMsg_Initialize message from the browser.
160 bool dead_on_arrival
= false;
163 base::MessageLoop::Type loop_type
= base::MessageLoop::TYPE_IO
;
164 // Use a UI message loop because ANGLE and the desktop GL platform can
165 // create child windows to render to.
166 // TODO(ananta) : Recent changes to the UI message pump class on Windows
167 // will cause delays in tasks getting processed in the GPU process which
168 // will show up on the bots in webgl conformance tests, perf tests etc.
169 // It will be great if we can work around the issues with desktop GL and
170 // avoid creating a child window in the GPU process which requires a UI
172 if ((command_line
.HasSwitch(switches::kUseGL
) &&
173 command_line
.GetSwitchValueASCII(switches::kUseGL
) == "desktop") ||
174 (command_line
.HasSwitch(switches::kUseANGLE
) &&
175 command_line
.GetSwitchValueASCII(switches::kUseANGLE
) == "gl")) {
176 loop_type
= base::MessageLoop::TYPE_UI
;
178 base::MessageLoop
main_message_loop(loop_type
);
179 #elif defined(OS_LINUX) && defined(USE_X11)
180 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
181 // and https://crbug.com/326995.
182 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_UI
);
183 scoped_ptr
<ui::PlatformEventSource
> event_source
=
184 ui::PlatformEventSource::CreateDefault();
185 #elif defined(OS_LINUX)
186 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_DEFAULT
);
187 #elif defined(OS_MACOSX)
188 // This is necessary for CoreAnimation layers hosted in the GPU process to be
189 // drawn. See http://crbug.com/312462.
190 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpCFRunLoop());
191 base::MessageLoop
main_message_loop(pump
.Pass());
193 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_IO
);
196 base::PlatformThread::SetName("CrGpuMain");
198 // In addition to disabling the watchdog if the command line switch is
199 // present, disable the watchdog on valgrind because the code is expected
200 // to run slowly in that case.
201 bool enable_watchdog
=
202 !command_line
.HasSwitch(switches::kDisableGpuWatchdog
) &&
203 !RunningOnValgrind();
205 // Disable the watchdog in debug builds because they tend to only be run by
206 // developers who will not appreciate the watchdog killing the GPU process.
208 enable_watchdog
= false;
211 bool delayed_watchdog_enable
= false;
213 #if defined(OS_CHROMEOS)
214 // Don't start watchdog immediately, to allow developers to switch to VT2 on
216 delayed_watchdog_enable
= true;
219 scoped_refptr
<GpuWatchdogThread
> watchdog_thread
;
221 // Start the GPU watchdog only after anything that is expected to be time
222 // consuming has completed, otherwise the process is liable to be aborted.
223 if (enable_watchdog
&& !delayed_watchdog_enable
) {
224 watchdog_thread
= new GpuWatchdogThread(kGpuTimeout
);
225 base::Thread::Options options
;
226 options
.timer_slack
= base::TIMER_SLACK_MAXIMUM
;
227 watchdog_thread
->StartWithOptions(options
);
230 // Initializes StatisticsRecorder which tracks UMA histograms.
231 base::StatisticsRecorder::Initialize();
233 gpu::GPUInfo gpu_info
;
234 // Get vendor_id, device_id, driver_version from browser process through
235 // commandline switches.
236 GetGpuInfoFromCommandLine(gpu_info
, command_line
);
238 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
239 VaapiWrapper::PreSandboxInitialization();
242 // Warm up resources that don't need access to GPUInfo.
243 if (WarmUpSandbox(command_line
)) {
244 #if defined(OS_LINUX)
245 bool initialized_sandbox
= false;
246 bool initialized_gl_context
= false;
247 bool should_initialize_gl_context
= false;
248 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
249 // initializing a GL context, so start the sandbox early.
250 if (command_line
.HasSwitch(switches::kGpuSandboxStartEarly
)) {
251 gpu_info
.sandboxed
= StartSandboxLinux(
252 gpu_info
, watchdog_thread
.get(), should_initialize_gl_context
);
253 initialized_sandbox
= true;
255 #endif // defined(OS_LINUX)
257 base::TimeTicks before_initialize_one_off
= base::TimeTicks::Now();
259 // Determine if we need to initialize GL here or it has already been done.
260 bool gl_already_initialized
= false;
261 #if defined(OS_MACOSX)
262 if (!command_line
.HasSwitch(switches::kNoSandbox
)) {
263 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
264 // is called from the sandbox warmup code before getting here.
265 gl_already_initialized
= true;
268 if (command_line
.HasSwitch(switches::kInProcessGPU
)) {
269 // With in-process GPU, GLSurface::InitializeOneOff() is called from
270 // GpuChildThread before getting here.
271 gl_already_initialized
= true;
274 // Load and initialize the GL implementation and locate the GL entry points.
275 bool gl_initialized
=
276 gl_already_initialized
277 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
278 : gfx::GLSurface::InitializeOneOff();
279 if (gl_initialized
) {
280 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
281 // purposes. However, on Mac we don't actually use them. As documented in
282 // crbug.com/222934, due to some driver issues, glGetString could take
283 // multiple seconds to finish, which in turn cause the GPU process to
285 // By skipping the following code on Mac, we don't really lose anything,
286 // because the basic GPU information is passed down from browser process
287 // and we already registered them through SetGpuInfo() above.
288 base::TimeTicks before_collect_context_graphics_info
=
289 base::TimeTicks::Now();
290 #if !defined(OS_MACOSX)
291 if (!CollectGraphicsInfo(gpu_info
))
292 dead_on_arrival
= true;
294 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
295 // Recompute gpu driver bug workarounds - this is specifically useful
296 // on systems where vendor_id/device_id aren't available.
297 if (!command_line
.HasSwitch(switches::kDisableGpuDriverBugWorkarounds
)) {
298 gpu::ApplyGpuDriverBugWorkarounds(
299 gpu_info
, const_cast<base::CommandLine
*>(&command_line
));
303 #if defined(OS_LINUX)
304 initialized_gl_context
= true;
305 #if !defined(OS_CHROMEOS)
306 if (gpu_info
.gpu
.vendor_id
== 0x10de && // NVIDIA
307 gpu_info
.driver_vendor
== "NVIDIA" &&
308 !CanAccessNvidiaDeviceFile())
309 dead_on_arrival
= true;
310 #endif // !defined(OS_CHROMEOS)
311 #endif // defined(OS_LINUX)
312 #endif // !defined(OS_MACOSX)
313 base::TimeDelta collect_context_time
=
314 base::TimeTicks::Now() - before_collect_context_graphics_info
;
315 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo",
316 collect_context_time
);
317 } else { // gl_initialized
318 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
319 dead_on_arrival
= true;
322 base::TimeDelta initialize_one_off_time
=
323 base::TimeTicks::Now() - before_initialize_one_off
;
324 UMA_HISTOGRAM_MEDIUM_TIMES("GPU.InitializeOneOffMediumTime",
325 initialize_one_off_time
);
327 if (enable_watchdog
&& delayed_watchdog_enable
) {
328 watchdog_thread
= new GpuWatchdogThread(kGpuTimeout
);
329 base::Thread::Options options
;
330 options
.timer_slack
= base::TIMER_SLACK_MAXIMUM
;
331 watchdog_thread
->StartWithOptions(options
);
334 // OSMesa is expected to run very slowly, so disable the watchdog in that
336 if (enable_watchdog
&&
337 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL
) {
338 watchdog_thread
->Stop();
339 watchdog_thread
= NULL
;
342 #if defined(OS_LINUX)
343 should_initialize_gl_context
= !initialized_gl_context
&&
346 if (!initialized_sandbox
) {
347 gpu_info
.sandboxed
= StartSandboxLinux(gpu_info
, watchdog_thread
.get(),
348 should_initialize_gl_context
);
350 #elif defined(OS_WIN)
351 gpu_info
.sandboxed
= StartSandboxWindows(parameters
.sandbox_info
);
352 #elif defined(OS_MACOSX)
353 gpu_info
.sandboxed
= Sandbox::SandboxIsCurrentlyActive();
356 gpu_info
.video_decode_accelerator_supported_profiles
=
357 content::GpuVideoDecodeAccelerator::GetSupportedProfiles();
358 gpu_info
.video_encode_accelerator_supported_profiles
=
359 content::GpuVideoEncodeAccelerator::GetSupportedProfiles();
361 dead_on_arrival
= true;
364 logging::SetLogMessageHandler(NULL
);
366 GpuProcess gpu_process
;
368 GpuChildThread
* child_thread
= new GpuChildThread(watchdog_thread
.get(),
371 deferred_messages
.Get());
372 while (!deferred_messages
.Get().empty())
373 deferred_messages
.Get().pop();
375 child_thread
->Init(start_time
);
377 gpu_process
.set_main_thread(child_thread
);
379 if (watchdog_thread
.get())
380 watchdog_thread
->AddPowerObserver();
383 TRACE_EVENT0("gpu", "Run Message Loop");
384 main_message_loop
.Run();
387 child_thread
->StopWatchdog();
394 void GetGpuInfoFromCommandLine(gpu::GPUInfo
& gpu_info
,
395 const base::CommandLine
& command_line
) {
396 DCHECK(command_line
.HasSwitch(switches::kGpuVendorID
) &&
397 command_line
.HasSwitch(switches::kGpuDeviceID
) &&
398 command_line
.HasSwitch(switches::kGpuDriverVersion
));
399 bool success
= base::HexStringToUInt(
400 command_line
.GetSwitchValueASCII(switches::kGpuVendorID
),
401 &gpu_info
.gpu
.vendor_id
);
403 success
= base::HexStringToUInt(
404 command_line
.GetSwitchValueASCII(switches::kGpuDeviceID
),
405 &gpu_info
.gpu
.device_id
);
407 gpu_info
.driver_vendor
=
408 command_line
.GetSwitchValueASCII(switches::kGpuDriverVendor
);
409 gpu_info
.driver_version
=
410 command_line
.GetSwitchValueASCII(switches::kGpuDriverVersion
);
411 GetContentClient()->SetGpuInfo(gpu_info
);
414 bool WarmUpSandbox(const base::CommandLine
& command_line
) {
416 TRACE_EVENT0("gpu", "Warm up rand");
417 // Warm up the random subsystem, which needs to be done pre-sandbox on all
419 (void) base::RandUint64();
424 #if !defined(OS_MACOSX)
425 bool CollectGraphicsInfo(gpu::GPUInfo
& gpu_info
) {
427 gpu::CollectInfoResult result
= gpu::CollectContextGraphicsInfo(&gpu_info
);
429 case gpu::kCollectInfoFatalFailure
:
430 LOG(ERROR
) << "gpu::CollectGraphicsInfo failed (fatal).";
433 case gpu::kCollectInfoNonFatalFailure
:
434 DVLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
436 case gpu::kCollectInfoNone
:
439 case gpu::kCollectInfoSuccess
:
442 GetContentClient()->SetGpuInfo(gpu_info
);
447 #if defined(OS_LINUX)
448 #if !defined(OS_CHROMEOS)
449 bool CanAccessNvidiaDeviceFile() {
451 base::ThreadRestrictions::AssertIOAllowed();
452 if (access("/dev/nvidiactl", R_OK
) != 0) {
453 DVLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
460 void CreateDummyGlContext() {
461 scoped_refptr
<gfx::GLSurface
> surface(
462 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
463 if (!surface
.get()) {
464 DVLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
468 // On Linux, this is needed to make sure /dev/nvidiactl has
469 // been opened and its descriptor cached.
470 scoped_refptr
<gfx::GLContext
> context(gfx::GLContext::CreateGLContext(
471 NULL
, surface
.get(), gfx::PreferDiscreteGpu
));
472 if (!context
.get()) {
473 DVLOG(1) << "gfx::GLContext::CreateGLContext failed";
477 // Similarly, this is needed for /dev/nvidia0.
478 if (context
->MakeCurrent(surface
.get())) {
479 context
->ReleaseCurrent(surface
.get());
481 DVLOG(1) << "gfx::GLContext::MakeCurrent failed";
485 void WarmUpSandboxNvidia(const gpu::GPUInfo
& gpu_info
,
486 bool should_initialize_gl_context
) {
487 // We special case Optimus since the vendor_id we see may not be Nvidia.
488 bool uses_nvidia_driver
= (gpu_info
.gpu
.vendor_id
== 0x10de && // NVIDIA.
489 gpu_info
.driver_vendor
== "NVIDIA") ||
491 if (uses_nvidia_driver
&& should_initialize_gl_context
) {
492 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
493 CreateDummyGlContext();
497 bool StartSandboxLinux(const gpu::GPUInfo
& gpu_info
,
498 GpuWatchdogThread
* watchdog_thread
,
499 bool should_initialize_gl_context
) {
500 TRACE_EVENT0("gpu", "Initialize sandbox");
504 WarmUpSandboxNvidia(gpu_info
, should_initialize_gl_context
);
506 if (watchdog_thread
) {
507 // LinuxSandbox needs to be able to ensure that the thread
508 // has really been stopped.
509 LinuxSandbox::StopThread(watchdog_thread
);
512 #if defined(SANITIZER_COVERAGE)
513 const std::string sancov_file_name
=
514 "gpu." + base::Uint64ToString(base::RandUint64());
515 LinuxSandbox
* linux_sandbox
= LinuxSandbox::GetInstance();
516 linux_sandbox
->sanitizer_args()->coverage_sandboxed
= 1;
517 linux_sandbox
->sanitizer_args()->coverage_fd
=
518 __sanitizer_maybe_open_cov_file(sancov_file_name
.c_str());
519 linux_sandbox
->sanitizer_args()->coverage_max_block_size
= 0;
522 // LinuxSandbox::InitializeSandbox() must always be called
523 // with only one thread.
524 res
= LinuxSandbox::InitializeSandbox();
525 if (watchdog_thread
) {
526 base::Thread::Options options
;
527 options
.timer_slack
= base::TIMER_SLACK_MAXIMUM
;
528 watchdog_thread
->StartWithOptions(options
);
533 #endif // defined(OS_LINUX)
536 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo
* sandbox_info
) {
537 TRACE_EVENT0("gpu", "Lower token");
539 // For Windows, if the target_services interface is not zero, the process
540 // is sandboxed and we must call LowerToken() before rendering untrusted
542 sandbox::TargetServices
* target_services
= sandbox_info
->target_services
;
543 if (target_services
) {
544 #if defined(ADDRESS_SANITIZER)
545 // Bind and leak dbghelp.dll before the token is lowered, otherwise
546 // AddressSanitizer will crash when trying to symbolize a report.
547 if (!LoadLibraryA("dbghelp.dll"))
551 target_services
->LowerToken();
557 #endif // defined(OS_WIN)
561 } // namespace content