This source file includes following definitions.
- GpuProcessLogMessageHandler
- GpuMain
- CreateDummyGlContext
- WarmUpSandbox
- WarmUpSandboxNvidia
- StartSandboxLinux
- StartSandboxWindows
#include <stdlib.h>
#if defined(OS_WIN)
#include <dwmapi.h>
#include <windows.h>
#endif
#include "base/debug/trace_event.h"
#include "base/lazy_instance.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "content/child/child_process.h"
#include "content/common/content_constants_internal.h"
#include "content/common/gpu/gpu_config.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/common/sandbox_linux/sandbox_linux.h"
#include "content/gpu/gpu_child_thread.h"
#include "content/gpu/gpu_process.h"
#include "content/gpu/gpu_watchdog_thread.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "gpu/config/gpu_info_collector.h"
#include "gpu/config/gpu_util.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface.h"
#include "ui/gl/gl_switches.h"
#include "ui/gl/gpu_switching_manager.h"
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#include "base/win/scoped_com_initializer.h"
#include "sandbox/win/src/sandbox.h"
#endif
#if defined(USE_X11)
#include "ui/base/x/x11_util.h"
#endif
#if defined(OS_LINUX)
#include "content/public/common/sandbox_init.h"
#endif
const int kGpuTimeout = 10000;
namespace content {
namespace {
bool WarmUpSandbox(const CommandLine& command_line);
#if defined(OS_LINUX)
bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
#elif defined(OS_WIN)
bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
#endif
base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
LAZY_INSTANCE_INITIALIZER;
bool GpuProcessLogMessageHandler(int severity,
const char* file, int line,
size_t message_start,
const std::string& str) {
std::string header = str.substr(0, message_start);
std::string message = str.substr(message_start);
deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
severity, header, message));
return false;
}
}
int GpuMain(const MainFunctionParams& parameters) {
TRACE_EVENT0("gpu", "GpuMain");
base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
kTraceEventGpuProcessSortIndex);
const CommandLine& command_line = parameters.command_line;
if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
ChildProcess::WaitForDebugger("Gpu");
}
base::Time start_time = base::Time::Now();
#if defined(OS_WIN)
SetErrorMode(
SEM_FAILCRITICALERRORS |
SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX);
#elif defined(USE_X11)
ui::SetDefaultX11ErrorHandlers();
#endif
logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
std::string types = command_line.GetSwitchValueASCII(
switches::kGpuDriverBugWorkarounds);
std::set<int> workarounds;
gpu::StringToFeatureSet(types, &workarounds);
if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
}
bool dead_on_arrival = false;
base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
#if defined(OS_WIN)
if (command_line.HasSwitch(switches::kUseGL) &&
command_line.GetSwitchValueASCII(switches::kUseGL) ==
gfx::kGLImplementationDesktopName) {
message_loop_type = base::MessageLoop::TYPE_UI;
}
#elif defined(TOOLKIT_GTK)
message_loop_type = base::MessageLoop::TYPE_GPU;
#elif defined(OS_LINUX)
message_loop_type = base::MessageLoop::TYPE_DEFAULT;
#endif
base::MessageLoop main_message_loop(message_loop_type);
base::PlatformThread::SetName("CrGpuMain");
bool enable_watchdog =
!command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
!RunningOnValgrind();
#ifndef NDEBUG
enable_watchdog = false;
#endif
bool delayed_watchdog_enable = false;
#if defined(OS_CHROMEOS)
delayed_watchdog_enable = true;
#endif
scoped_refptr<GpuWatchdogThread> watchdog_thread;
if (enable_watchdog && !delayed_watchdog_enable) {
watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
watchdog_thread->Start();
}
gpu::GPUInfo gpu_info;
DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
command_line.HasSwitch(switches::kGpuDeviceID) &&
command_line.HasSwitch(switches::kGpuDriverVersion));
bool success = base::HexStringToUInt(
command_line.GetSwitchValueASCII(switches::kGpuVendorID),
&gpu_info.gpu.vendor_id);
DCHECK(success);
success = base::HexStringToUInt(
command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
&gpu_info.gpu.device_id);
DCHECK(success);
gpu_info.driver_vendor =
command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
gpu_info.driver_version =
command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
GetContentClient()->SetGpuInfo(gpu_info);
base::TimeDelta collect_context_time;
base::TimeDelta initialize_one_off_time;
if (WarmUpSandbox(command_line)) {
#if defined(OS_LINUX)
bool initialized_sandbox = false;
bool initialized_gl_context = false;
bool should_initialize_gl_context = false;
#if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
if (!command_line.HasSwitch(
switches::kGpuSandboxStartAfterInitialization)) {
gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
should_initialize_gl_context);
initialized_sandbox = true;
}
#endif
#endif
base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
bool gl_already_initialized = false;
#if defined(OS_MACOSX)
if (!command_line.HasSwitch(switches::kNoSandbox)) {
gl_already_initialized = true;
}
#endif
if (command_line.HasSwitch(switches::kInProcessGPU)) {
gl_already_initialized = true;
}
bool gl_initialized =
gl_already_initialized
? gfx::GetGLImplementation() != gfx::kGLImplementationNone
: gfx::GLSurface::InitializeOneOff();
if (gl_initialized) {
base::TimeTicks before_collect_context_graphics_info =
base::TimeTicks::Now();
#if !defined(OS_MACOSX)
gpu::CollectInfoResult result =
gpu::CollectContextGraphicsInfo(&gpu_info);
switch (result) {
case gpu::kCollectInfoFatalFailure:
LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
dead_on_arrival = true;
break;
case gpu::kCollectInfoNonFatalFailure:
VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
break;
case gpu::kCollectInfoSuccess:
break;
}
GetContentClient()->SetGpuInfo(gpu_info);
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
gpu::ApplyGpuDriverBugWorkarounds(
gpu_info, const_cast<CommandLine*>(&command_line));
}
#endif
#if defined(OS_LINUX)
initialized_gl_context = true;
#if !defined(OS_CHROMEOS)
if (gpu_info.gpu.vendor_id == 0x10de &&
gpu_info.driver_vendor == "NVIDIA") {
base::ThreadRestrictions::AssertIOAllowed();
if (access("/dev/nvidiactl", R_OK) != 0) {
VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
dead_on_arrival = true;
}
}
#endif
#endif
#endif
collect_context_time =
base::TimeTicks::Now() - before_collect_context_graphics_info;
} else {
VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
dead_on_arrival = true;
}
initialize_one_off_time =
base::TimeTicks::Now() - before_initialize_one_off;
if (enable_watchdog && delayed_watchdog_enable) {
watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
watchdog_thread->Start();
}
if (enable_watchdog &&
gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
watchdog_thread->Stop();
watchdog_thread = NULL;
}
#if defined(OS_LINUX)
should_initialize_gl_context = !initialized_gl_context &&
!dead_on_arrival;
if (!initialized_sandbox) {
gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
should_initialize_gl_context);
}
#elif defined(OS_WIN)
gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
#endif
} else {
dead_on_arrival = true;
}
logging::SetLogMessageHandler(NULL);
GpuProcess gpu_process;
UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
dead_on_arrival,
gpu_info,
deferred_messages.Get());
while (!deferred_messages.Get().empty())
deferred_messages.Get().pop();
child_thread->Init(start_time);
gpu_process.set_main_thread(child_thread);
if (watchdog_thread)
watchdog_thread->AddPowerObserver();
{
TRACE_EVENT0("gpu", "Run Message Loop");
main_message_loop.Run();
}
child_thread->StopWatchdog();
return 0;
}
namespace {
#if defined(OS_LINUX)
void CreateDummyGlContext() {
scoped_refptr<gfx::GLSurface> surface(
gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
if (!surface.get()) {
VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
return;
}
scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
NULL, surface.get(), gfx::PreferDiscreteGpu));
if (!context.get()) {
VLOG(1) << "gfx::GLContext::CreateGLContext failed";
return;
}
if (context->MakeCurrent(surface.get())) {
context->ReleaseCurrent(surface.get());
} else {
VLOG(1) << "gfx::GLContext::MakeCurrent failed";
}
}
#endif
bool WarmUpSandbox(const CommandLine& command_line) {
{
TRACE_EVENT0("gpu", "Warm up rand");
(void) base::RandUint64();
}
return true;
}
#if defined(OS_LINUX)
void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
bool should_initialize_gl_context) {
bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de &&
gpu_info.driver_vendor == "NVIDIA") ||
gpu_info.optimus;
if (uses_nvidia_driver && should_initialize_gl_context) {
CreateDummyGlContext();
}
}
bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
GpuWatchdogThread* watchdog_thread,
bool should_initialize_gl_context) {
TRACE_EVENT0("gpu", "Initialize sandbox");
bool res = false;
WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
if (watchdog_thread) {
LinuxSandbox::StopThread(watchdog_thread);
}
res = LinuxSandbox::InitializeSandbox();
if (watchdog_thread) {
watchdog_thread->Start();
}
return res;
}
#endif
#if defined(OS_WIN)
bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
TRACE_EVENT0("gpu", "Lower token");
sandbox::TargetServices* target_services = sandbox_info->target_services;
if (target_services) {
target_services->LowerToken();
return true;
}
return false;
}
#endif
}
}