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/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/gl/gl_implementation.h"
36 #include "ui/gl/gl_surface.h"
37 #include "ui/gl/gl_switches.h"
38 #include "ui/gl/gpu_switching_manager.h"
41 #include "base/win/windows_version.h"
42 #include "base/win/scoped_com_initializer.h"
43 #include "sandbox/win/src/sandbox.h"
47 #include "ui/base/x/x11_util.h"
51 #include "content/public/common/sandbox_init.h"
54 #if defined(USE_OZONE)
55 #include "ozone/content/ozone_channel.h"
58 const int kGpuTimeout = 10000;
64 bool WarmUpSandbox(const CommandLine& command_line);
66 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
68 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
71 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
72 LAZY_INSTANCE_INITIALIZER;
74 bool GpuProcessLogMessageHandler(int severity,
75 const char* file, int line,
77 const std::string& str) {
78 std::string header = str.substr(0, message_start);
79 std::string message = str.substr(message_start);
80 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
81 severity, header, message));
85 } // namespace anonymous
87 // Main function for starting the Gpu process.
88 int GpuMain(const MainFunctionParams& parameters) {
89 TRACE_EVENT0("gpu", "GpuMain");
90 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
91 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
92 kTraceEventGpuProcessSortIndex);
94 const CommandLine& command_line = parameters.command_line;
95 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
96 ChildProcess::WaitForDebugger("Gpu");
99 base::Time start_time = base::Time::Now();
102 // Prevent Windows from displaying a modal dialog on failures like not being
103 // able to load a DLL.
105 SEM_FAILCRITICALERRORS |
106 SEM_NOGPFAULTERRORBOX |
107 SEM_NOOPENFILEERRORBOX);
108 #elif defined(USE_X11)
109 ui::SetDefaultX11ErrorHandlers();
112 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
114 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
115 std::string types = command_line.GetSwitchValueASCII(
116 switches::kGpuDriverBugWorkarounds);
117 std::set<int> workarounds;
118 gpu::StringToFeatureSet(types, &workarounds);
119 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
120 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
121 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
122 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
125 // Initialization of the OpenGL bindings may fail, in which case we
126 // will need to tear down this process. However, we can not do so
127 // safely until the IPC channel is set up, because the detection of
128 // early return of a child process is implemented using an IPC
129 // channel error. If the IPC channel is not fully set up between the
130 // browser and GPU process, and the GPU process crashes or exits
131 // early, the browser process will never detect it. For this reason
132 // we defer tearing down the GPU process until receiving the
133 // GpuMsg_Initialize message from the browser.
134 bool dead_on_arrival = false;
136 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
138 // Unless we're running on desktop GL, we don't need a UI message
139 // loop, so avoid its use to work around apparent problems with some
140 // third-party software.
141 if (command_line.HasSwitch(switches::kUseGL) &&
142 command_line.GetSwitchValueASCII(switches::kUseGL) ==
143 gfx::kGLImplementationDesktopName) {
144 message_loop_type = base::MessageLoop::TYPE_UI;
146 #elif defined(OS_LINUX)
147 message_loop_type = base::MessageLoop::TYPE_DEFAULT;
150 base::MessageLoop main_message_loop(message_loop_type);
151 base::PlatformThread::SetName("CrGpuMain");
153 // In addition to disabling the watchdog if the command line switch is
154 // present, disable the watchdog on valgrind because the code is expected
155 // to run slowly in that case.
156 bool enable_watchdog =
157 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
158 !RunningOnValgrind();
160 // Disable the watchdog in debug builds because they tend to only be run by
161 // developers who will not appreciate the watchdog killing the GPU process.
163 enable_watchdog = false;
166 bool delayed_watchdog_enable = false;
168 #if defined(OS_CHROMEOS)
169 // Don't start watchdog immediately, to allow developers to switch to VT2 on
171 delayed_watchdog_enable = true;
174 scoped_refptr<GpuWatchdogThread> watchdog_thread;
176 // Start the GPU watchdog only after anything that is expected to be time
177 // consuming has completed, otherwise the process is liable to be aborted.
178 if (enable_watchdog && !delayed_watchdog_enable) {
179 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
180 watchdog_thread->Start();
183 gpu::GPUInfo gpu_info;
184 // Get vendor_id, device_id, driver_version from browser process through
185 // commandline switches.
186 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
187 command_line.HasSwitch(switches::kGpuDeviceID) &&
188 command_line.HasSwitch(switches::kGpuDriverVersion));
189 bool success = base::HexStringToUInt(
190 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
191 &gpu_info.gpu.vendor_id);
193 success = base::HexStringToUInt(
194 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
195 &gpu_info.gpu.device_id);
197 gpu_info.driver_vendor =
198 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
199 gpu_info.driver_version =
200 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
201 GetContentClient()->SetGpuInfo(gpu_info);
203 base::TimeDelta collect_context_time;
204 base::TimeDelta initialize_one_off_time;
206 // Warm up resources that don't need access to GPUInfo.
207 if (WarmUpSandbox(command_line)) {
208 #if defined(OS_LINUX)
209 bool initialized_sandbox = false;
210 bool initialized_gl_context = false;
211 bool should_initialize_gl_context = false;
212 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
213 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
214 // initializing a GL context, so start the sandbox early.
215 if (!command_line.HasSwitch(
216 switches::kGpuSandboxStartAfterInitialization)) {
217 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
218 should_initialize_gl_context);
219 initialized_sandbox = true;
222 #endif // defined(OS_LINUX)
224 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
226 // Determine if we need to initialize GL here or it has already been done.
227 bool gl_already_initialized = false;
228 #if defined(OS_MACOSX)
229 if (!command_line.HasSwitch(switches::kNoSandbox)) {
230 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
231 // is called from the sandbox warmup code before getting here.
232 gl_already_initialized = true;
235 if (command_line.HasSwitch(switches::kInProcessGPU)) {
236 // With in-process GPU, GLSurface::InitializeOneOff() is called from
237 // GpuChildThread before getting here.
238 gl_already_initialized = true;
241 // Load and initialize the GL implementation and locate the GL entry points.
242 bool gl_initialized =
243 gl_already_initialized
244 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
245 : gfx::GLSurface::InitializeOneOff();
246 if (gl_initialized) {
247 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
248 // purposes. However, on Mac we don't actually use them. As documented in
249 // crbug.com/222934, due to some driver issues, glGetString could take
250 // multiple seconds to finish, which in turn cause the GPU process to
252 // By skipping the following code on Mac, we don't really lose anything,
253 // because the basic GPU information is passed down from browser process
254 // and we already registered them through SetGpuInfo() above.
255 base::TimeTicks before_collect_context_graphics_info =
256 base::TimeTicks::Now();
257 #if !defined(OS_MACOSX)
258 gpu::CollectInfoResult result =
259 gpu::CollectContextGraphicsInfo(&gpu_info);
261 case gpu::kCollectInfoFatalFailure:
262 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
263 dead_on_arrival = true;
265 case gpu::kCollectInfoNonFatalFailure:
266 VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
268 case gpu::kCollectInfoSuccess:
271 GetContentClient()->SetGpuInfo(gpu_info);
273 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
274 // Recompute gpu driver bug workarounds - this is specifically useful
275 // on systems where vendor_id/device_id aren't available.
276 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
277 gpu::ApplyGpuDriverBugWorkarounds(
278 gpu_info, const_cast<CommandLine*>(&command_line));
282 #if defined(OS_LINUX)
283 initialized_gl_context = true;
284 #if !defined(OS_CHROMEOS)
285 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
286 gpu_info.driver_vendor == "NVIDIA") {
287 base::ThreadRestrictions::AssertIOAllowed();
288 if (access("/dev/nvidiactl", R_OK) != 0) {
289 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
290 dead_on_arrival = true;
293 #endif // !defined(OS_CHROMEOS)
294 #endif // defined(OS_LINUX)
295 #endif // !defined(OS_MACOSX)
296 collect_context_time =
297 base::TimeTicks::Now() - before_collect_context_graphics_info;
299 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
300 dead_on_arrival = true;
303 initialize_one_off_time =
304 base::TimeTicks::Now() - before_initialize_one_off;
306 if (enable_watchdog && delayed_watchdog_enable) {
307 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
308 watchdog_thread->Start();
311 // OSMesa is expected to run very slowly, so disable the watchdog in that
313 if (enable_watchdog &&
314 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
315 watchdog_thread->Stop();
316 watchdog_thread = NULL;
319 #if defined(OS_LINUX)
320 should_initialize_gl_context = !initialized_gl_context &&
323 if (!initialized_sandbox) {
324 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
325 should_initialize_gl_context);
327 #elif defined(OS_WIN)
328 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
331 dead_on_arrival = true;
334 logging::SetLogMessageHandler(NULL);
336 GpuProcess gpu_process;
338 // These UMA must be stored after GpuProcess is constructed as it
339 // initializes StatisticsRecorder which tracks the histograms.
340 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
341 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
343 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
346 deferred_messages.Get());
347 while (!deferred_messages.Get().empty())
348 deferred_messages.Get().pop();
350 child_thread->Init(start_time);
352 gpu_process.set_main_thread(child_thread);
355 watchdog_thread->AddPowerObserver();
358 #if defined(USE_OZONE)
359 OzoneChannel channel;
362 TRACE_EVENT0("gpu", "Run Message Loop");
363 main_message_loop.Run();
366 child_thread->StopWatchdog();
373 #if defined(OS_LINUX)
374 void CreateDummyGlContext() {
375 scoped_refptr<gfx::GLSurface> surface(
376 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
377 if (!surface.get()) {
378 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
382 // On Linux, this is needed to make sure /dev/nvidiactl has
383 // been opened and its descriptor cached.
384 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
385 NULL, surface.get(), gfx::PreferDiscreteGpu));
386 if (!context.get()) {
387 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
391 // Similarly, this is needed for /dev/nvidia0.
392 if (context->MakeCurrent(surface.get())) {
393 context->ReleaseCurrent(surface.get());
395 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
400 bool WarmUpSandbox(const CommandLine& command_line) {
402 TRACE_EVENT0("gpu", "Warm up rand");
403 // Warm up the random subsystem, which needs to be done pre-sandbox on all
405 (void) base::RandUint64();
410 #if defined(OS_LINUX)
411 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
412 bool should_initialize_gl_context) {
413 // We special case Optimus since the vendor_id we see may not be Nvidia.
414 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
415 gpu_info.driver_vendor == "NVIDIA") ||
417 if (uses_nvidia_driver && should_initialize_gl_context) {
418 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
419 CreateDummyGlContext();
423 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
424 GpuWatchdogThread* watchdog_thread,
425 bool should_initialize_gl_context) {
426 TRACE_EVENT0("gpu", "Initialize sandbox");
430 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
432 if (watchdog_thread) {
433 // LinuxSandbox needs to be able to ensure that the thread
434 // has really been stopped.
435 LinuxSandbox::StopThread(watchdog_thread);
437 // LinuxSandbox::InitializeSandbox() must always be called
438 // with only one thread.
439 res = LinuxSandbox::InitializeSandbox();
440 if (watchdog_thread) {
441 watchdog_thread->Start();
446 #endif // defined(OS_LINUX)
449 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
450 TRACE_EVENT0("gpu", "Lower token");
452 // For Windows, if the target_services interface is not zero, the process
453 // is sandboxed and we must call LowerToken() before rendering untrusted
455 sandbox::TargetServices* target_services = sandbox_info->target_services;
456 if (target_services) {
457 target_services->LowerToken();
463 #endif // defined(OS_WIN)
467 } // namespace content