blob: a4bb4944351f30597373781ac84134a172c7afd7 [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]f24a1e2b2011-04-08 01:48:4817#include "content/common/gpu/gpu_config.h"
[email protected]415c2cd2011-03-11 21:56:1118#include "content/common/main_function_params.h"
[email protected]c08950d22011-10-13 22:20:2919#include "content/public/common/content_switches.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]badf5cf2011-10-29 03:44:4439 const CommandLine& command_line = parameters.command_line;
[email protected]6b889fb2010-03-23 20:09:4940 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]23f46562011-09-07 01:42:3944 if (!command_line.HasSwitch(switches::kSingleProcess)) {
45#if defined(OS_WIN)
46 // Prevent Windows from displaying a modal dialog on failures like not being
47 // able to load a DLL.
48 SetErrorMode(
49 SEM_FAILCRITICALERRORS |
50 SEM_NOGPFAULTERRORBOX |
51 SEM_NOOPENFILEERRORBOX);
52#elif defined(USE_X11)
53 ui::SetDefaultX11ErrorHandlers();
54#endif
55 }
56
[email protected]0b2cec62011-07-22 18:13:2857 // Initialization of the OpenGL bindings may fail, in which case we
58 // will need to tear down this process. However, we can not do so
59 // safely until the IPC channel is set up, because the detection of
60 // early return of a child process is implemented using an IPC
61 // channel error. If the IPC channel is not fully set up between the
62 // browser and GPU process, and the GPU process crashes or exits
63 // early, the browser process will never detect it. For this reason
64 // we defer tearing down the GPU process until receiving the
65 // GpuMsg_Initialize message from the browser.
66 bool dead_on_arrival = false;
67
[email protected]4a2a4d22011-07-25 23:20:3468 // Load the GL implementation and locate the bindings before starting the GPU
69 // watchdog because this can take a lot of time and the GPU watchdog might
70 // terminate the GPU process.
71 if (!gfx::GLSurface::InitializeOneOff()) {
[email protected]51dd1b482011-10-18 19:35:1972 LOG(INFO) << "gfx::GLSurface::InitializeOneOff failed";
[email protected]4a2a4d22011-07-25 23:20:3473 dead_on_arrival = true;
74 }
75
[email protected]bccf36e2011-08-16 16:41:0676 base::win::ScopedCOMInitializer com_initializer;
77
[email protected]0b2cec62011-07-22 18:13:2878#if defined(OS_WIN)
79 sandbox::TargetServices* target_services =
[email protected]badf5cf2011-10-29 03:44:4480 parameters.sandbox_info->target_services;
[email protected]0b2cec62011-07-22 18:13:2881 // For windows, if the target_services interface is not zero, the process
82 // is sandboxed and we must call LowerToken() before rendering untrusted
83 // content.
84 if (target_services)
85 target_services->LowerToken();
86#endif
87
[email protected]de9e0b52010-12-23 22:01:1788#if defined(OS_MACOSX)
89 chrome_application_mac::RegisterCrApp();
90#endif
91
[email protected]d2a6c0e32011-08-08 21:53:1692 MessageLoop::Type message_loop_type = MessageLoop::TYPE_UI;
93#if defined(OS_WIN)
94 // Unless we're running on desktop GL, we don't need a UI message
95 // loop, so avoid its use to work around apparent problems with some
96 // third-party software.
97 message_loop_type = MessageLoop::TYPE_IO;
98 if (command_line.HasSwitch(switches::kUseGL) &&
99 command_line.GetSwitchValueASCII(switches::kUseGL) ==
100 gfx::kGLImplementationDesktopName) {
101 message_loop_type = MessageLoop::TYPE_UI;
102 }
103#endif
104
105 MessageLoop main_message_loop(message_loop_type);
[email protected]ce072a72010-12-31 20:02:16106 base::PlatformThread::SetName("CrGpuMain");
[email protected]c0fc0942010-01-13 00:55:37107
[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}