blob: 79ca7c49b75fb8590f68183cb75cb784e6efd19c [file] [log] [blame]
[email protected]623c0bd2011-03-12 01:00:411// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c0fc0942010-01-13 00:55:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e09cee42010-11-09 01:50:085#include <stdlib.h>
6
[email protected]cafd0b62011-01-19 01:22:057#if defined(OS_WIN)
8#include <windows.h>
9#endif
10
[email protected]e09cee42010-11-09 01:50:0811#include "base/environment.h"
[email protected]c0fc0942010-01-13 00:55:3712#include "base/message_loop.h"
[email protected]e09cee42010-11-09 01:50:0813#include "base/stringprintf.h"
[email protected]ce072a72010-12-31 20:02:1614#include "base/threading/platform_thread.h"
[email protected]dcd57762011-06-25 12:18:5115#include "base/win/scoped_com_initializer.h"
[email protected]c0fc0942010-01-13 00:55:3716#include "build/build_config.h"
[email protected]6b7a5e32011-03-11 18:34:1017#include "content/common/content_switches.h"
[email protected]f24a1e2b2011-04-08 01:48:4818#include "content/common/gpu/gpu_config.h"
[email protected]415c2cd2011-03-11 21:56:1119#include "content/common/main_function_params.h"
[email protected]7a31f7c2011-03-21 23:22:0420#include "content/gpu/gpu_child_thread.h"
[email protected]623c0bd2011-03-12 01:00:4121#include "content/gpu/gpu_process.h"
[email protected]0b2cec62011-07-22 18:13:2822#include "ui/gfx/gl/gl_surface.h"
[email protected]d2a6c0e32011-08-08 21:53:1623#include "ui/gfx/gl/gl_switches.h"
[email protected]c0fc0942010-01-13 00:55:3724
[email protected]802a13a02010-12-02 01:48:3725#if defined(OS_MACOSX)
[email protected]415c2cd2011-03-11 21:56:1126#include "content/common/chrome_application_mac.h"
[email protected]0b2cec62011-07-22 18:13:2827#elif defined(OS_WIN)
28#include "sandbox/src/sandbox.h"
[email protected]802a13a02010-12-02 01:48:3729#endif
30
[email protected]02ec37a2010-09-20 22:32:1631#if defined(USE_X11)
[email protected]57b5d7dc2011-03-09 14:11:3432#include "ui/base/x/x11_util.h"
[email protected]02ec37a2010-09-20 22:32:1633#endif
34
[email protected]c0fc0942010-01-13 00:55:3735// Main function for starting the Gpu process.
36int GpuMain(const MainFunctionParams& parameters) {
[email protected]e09cee42010-11-09 01:50:0837 base::Time start_time = base::Time::Now();
38
[email protected]6b889fb2010-03-23 20:09:4939 const CommandLine& command_line = parameters.command_line_;
40 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
[email protected]75fcc272011-03-08 20:50:4841 ChildProcess::WaitForDebugger("Gpu");
[email protected]6b889fb2010-03-23 20:09:4942 }
43
[email protected]0b2cec62011-07-22 18:13:2844 // Initialization of the OpenGL bindings may fail, in which case we
45 // will need to tear down this process. However, we can not do so
46 // safely until the IPC channel is set up, because the detection of
47 // early return of a child process is implemented using an IPC
48 // channel error. If the IPC channel is not fully set up between the
49 // browser and GPU process, and the GPU process crashes or exits
50 // early, the browser process will never detect it. For this reason
51 // we defer tearing down the GPU process until receiving the
52 // GpuMsg_Initialize message from the browser.
53 bool dead_on_arrival = false;
54
[email protected]4a2a4d22011-07-25 23:20:3455 // Load the GL implementation and locate the bindings before starting the GPU
56 // watchdog because this can take a lot of time and the GPU watchdog might
57 // terminate the GPU process.
58 if (!gfx::GLSurface::InitializeOneOff()) {
59 LOG(INFO) << "GLContext::InitializeOneOff failed";
60 dead_on_arrival = true;
61 }
62
[email protected]bccf36e2011-08-16 16:41:0663 base::win::ScopedCOMInitializer com_initializer;
64
[email protected]0b2cec62011-07-22 18:13:2865#if defined(OS_WIN)
66 sandbox::TargetServices* target_services =
67 parameters.sandbox_info_.TargetServices();
68 // For windows, if the target_services interface is not zero, the process
69 // is sandboxed and we must call LowerToken() before rendering untrusted
70 // content.
71 if (target_services)
72 target_services->LowerToken();
73#endif
74
[email protected]de9e0b52010-12-23 22:01:1775#if defined(OS_MACOSX)
76 chrome_application_mac::RegisterCrApp();
77#endif
78
[email protected]d2a6c0e32011-08-08 21:53:1679 MessageLoop::Type message_loop_type = MessageLoop::TYPE_UI;
80#if defined(OS_WIN)
81 // Unless we're running on desktop GL, we don't need a UI message
82 // loop, so avoid its use to work around apparent problems with some
83 // third-party software.
84 message_loop_type = MessageLoop::TYPE_IO;
85 if (command_line.HasSwitch(switches::kUseGL) &&
86 command_line.GetSwitchValueASCII(switches::kUseGL) ==
87 gfx::kGLImplementationDesktopName) {
88 message_loop_type = MessageLoop::TYPE_UI;
89 }
90#endif
91
92 MessageLoop main_message_loop(message_loop_type);
[email protected]ce072a72010-12-31 20:02:1693 base::PlatformThread::SetName("CrGpuMain");
[email protected]c0fc0942010-01-13 00:55:3794
[email protected]57b5d7dc2011-03-09 14:11:3495 if (!command_line.HasSwitch(switches::kSingleProcess)) {
[email protected]cafd0b62011-01-19 01:22:0596#if defined(OS_WIN)
[email protected]57b5d7dc2011-03-09 14:11:3497 // Prevent Windows from displaying a modal dialog on failures like not being
98 // able to load a DLL.
99 SetErrorMode(
100 SEM_FAILCRITICALERRORS |
101 SEM_NOGPFAULTERRORBOX |
102 SEM_NOOPENFILEERRORBOX);
103#elif defined(USE_X11)
104 ui::SetDefaultX11ErrorHandlers();
[email protected]cafd0b62011-01-19 01:22:05105#endif
[email protected]57b5d7dc2011-03-09 14:11:34106 }
[email protected]cafd0b62011-01-19 01:22:05107
[email protected]983c33d2010-11-16 22:38:44108 GpuProcess gpu_process;
[email protected]8fe0ec522011-03-03 00:31:33109
[email protected]0b2cec62011-07-22 18:13:28110 GpuChildThread* child_thread = new GpuChildThread(dead_on_arrival);
[email protected]8fe0ec522011-03-03 00:31:33111
[email protected]7a31f7c2011-03-21 23:22:04112 child_thread->Init(start_time);
[email protected]995a7f12011-02-11 23:07:17113
[email protected]7a31f7c2011-03-21 23:22:04114 gpu_process.set_main_thread(child_thread);
[email protected]983c33d2010-11-16 22:38:44115
[email protected]c0fc0942010-01-13 00:55:37116 main_message_loop.Run();
117
[email protected]7a31f7c2011-03-21 23:22:04118 child_thread->StopWatchdog();
[email protected]e09cee42010-11-09 01:50:08119
[email protected]c0fc0942010-01-13 00:55:37120 return 0;
121}