Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / gpu / gpu_main.cc
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.
4
5 #include <stdlib.h>
6
7 #if defined(OS_WIN)
8 #include <dwmapi.h>
9 #include <windows.h>
10 #endif
11
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 "crypto/hmac.h"
33 #include "gpu/command_buffer/service/gpu_switches.h"
34 #include "gpu/config/gpu_info_collector.h"
35 #include "gpu/config/gpu_util.h"
36 #include "ui/gl/gl_implementation.h"
37 #include "ui/gl/gl_surface.h"
38 #include "ui/gl/gl_switches.h"
39 #include "ui/gl/gpu_switching_manager.h"
40
41 #if defined(OS_WIN)
42 #include "base/win/windows_version.h"
43 #include "base/win/scoped_com_initializer.h"
44 #include "sandbox/win/src/sandbox.h"
45 #endif
46
47 #if defined(USE_X11)
48 #include "ui/base/x/x11_util.h"
49 #endif
50
51 #if defined(OS_LINUX)
52 #include "content/public/common/sandbox_init.h"
53 #endif
54
55 const int kGpuTimeout = 10000;
56
57 namespace content {
58
59 namespace {
60
61 bool WarmUpSandbox(const CommandLine& command_line);
62 #if defined(OS_LINUX)
63 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
64 #elif defined(OS_WIN)
65 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
66 #endif
67
68 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
69     LAZY_INSTANCE_INITIALIZER;
70
71 bool GpuProcessLogMessageHandler(int severity,
72                                  const char* file, int line,
73                                  size_t message_start,
74                                  const std::string& str) {
75   std::string header = str.substr(0, message_start);
76   std::string message = str.substr(message_start);
77   deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
78       severity, header, message));
79   return false;
80 }
81
82 }  // namespace anonymous
83
84 // Main function for starting the Gpu process.
85 int GpuMain(const MainFunctionParams& parameters) {
86   TRACE_EVENT0("gpu", "GpuMain");
87   base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
88   base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
89       kTraceEventGpuProcessSortIndex);
90
91   const CommandLine& command_line = parameters.command_line;
92   if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
93     ChildProcess::WaitForDebugger("Gpu");
94   }
95
96   base::Time start_time = base::Time::Now();
97
98 #if defined(OS_WIN)
99   // Prevent Windows from displaying a modal dialog on failures like not being
100   // able to load a DLL.
101   SetErrorMode(
102       SEM_FAILCRITICALERRORS |
103       SEM_NOGPFAULTERRORBOX |
104       SEM_NOOPENFILEERRORBOX);
105 #elif defined(USE_X11)
106   ui::SetDefaultX11ErrorHandlers();
107 #endif
108
109   logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
110
111   if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
112     std::string types = command_line.GetSwitchValueASCII(
113         switches::kGpuDriverBugWorkarounds);
114     std::set<int> workarounds;
115     gpu::StringToFeatureSet(types, &workarounds);
116     if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
117       ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
118     else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
119       ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
120   }
121
122   // Initialization of the OpenGL bindings may fail, in which case we
123   // will need to tear down this process. However, we can not do so
124   // safely until the IPC channel is set up, because the detection of
125   // early return of a child process is implemented using an IPC
126   // channel error. If the IPC channel is not fully set up between the
127   // browser and GPU process, and the GPU process crashes or exits
128   // early, the browser process will never detect it.  For this reason
129   // we defer tearing down the GPU process until receiving the
130   // GpuMsg_Initialize message from the browser.
131   bool dead_on_arrival = false;
132
133   base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
134 #if defined(OS_WIN)
135   // Unless we're running on desktop GL, we don't need a UI message
136   // loop, so avoid its use to work around apparent problems with some
137   // third-party software.
138   if (command_line.HasSwitch(switches::kUseGL) &&
139       command_line.GetSwitchValueASCII(switches::kUseGL) ==
140           gfx::kGLImplementationDesktopName) {
141     message_loop_type = base::MessageLoop::TYPE_UI;
142   }
143 #elif defined(TOOLKIT_GTK)
144   message_loop_type = base::MessageLoop::TYPE_GPU;
145 #elif defined(OS_LINUX)
146   message_loop_type = base::MessageLoop::TYPE_DEFAULT;
147 #endif
148
149   base::MessageLoop main_message_loop(message_loop_type);
150   base::PlatformThread::SetName("CrGpuMain");
151
152   // In addition to disabling the watchdog if the command line switch is
153   // present, disable the watchdog on valgrind because the code is expected
154   // to run slowly in that case.
155   bool enable_watchdog =
156       !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
157       !RunningOnValgrind();
158
159   // Disable the watchdog in debug builds because they tend to only be run by
160   // developers who will not appreciate the watchdog killing the GPU process.
161 #ifndef NDEBUG
162   enable_watchdog = false;
163 #endif
164
165   bool delayed_watchdog_enable = false;
166
167 #if defined(OS_CHROMEOS)
168   // Don't start watchdog immediately, to allow developers to switch to VT2 on
169   // startup.
170   delayed_watchdog_enable = true;
171 #endif
172
173   scoped_refptr<GpuWatchdogThread> watchdog_thread;
174
175   // Start the GPU watchdog only after anything that is expected to be time
176   // consuming has completed, otherwise the process is liable to be aborted.
177   if (enable_watchdog && !delayed_watchdog_enable) {
178     watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
179     watchdog_thread->Start();
180   }
181
182   gpu::GPUInfo gpu_info;
183   // Get vendor_id, device_id, driver_version from browser process through
184   // commandline switches.
185   DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
186          command_line.HasSwitch(switches::kGpuDeviceID) &&
187          command_line.HasSwitch(switches::kGpuDriverVersion));
188   bool success = base::HexStringToUInt(
189       command_line.GetSwitchValueASCII(switches::kGpuVendorID),
190       &gpu_info.gpu.vendor_id);
191   DCHECK(success);
192   success = base::HexStringToUInt(
193       command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
194       &gpu_info.gpu.device_id);
195   DCHECK(success);
196   gpu_info.driver_vendor =
197       command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
198   gpu_info.driver_version =
199       command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
200   GetContentClient()->SetGpuInfo(gpu_info);
201
202   base::TimeDelta collect_context_time;
203   base::TimeDelta initialize_one_off_time;
204
205   // Warm up resources that don't need access to GPUInfo.
206   if (WarmUpSandbox(command_line)) {
207 #if defined(OS_LINUX)
208     bool initialized_sandbox = false;
209     bool initialized_gl_context = false;
210     bool should_initialize_gl_context = false;
211 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
212     // On Chrome OS ARM, GPU driver userspace creates threads when initializing
213     // a GL context, so start the sandbox early.
214     gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
215                                            should_initialize_gl_context);
216     initialized_sandbox = true;
217 #endif
218 #endif  // defined(OS_LINUX)
219
220     base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
221
222     // Load and initialize the GL implementation and locate the GL entry points.
223     if (gfx::GLSurface::InitializeOneOff()) {
224       // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
225       // purposes. However, on Mac we don't actually use them. As documented in
226       // crbug.com/222934, due to some driver issues, glGetString could take
227       // multiple seconds to finish, which in turn cause the GPU process to
228       // crash.
229       // By skipping the following code on Mac, we don't really lose anything,
230       // because the basic GPU information is passed down from browser process
231       // and we already registered them through SetGpuInfo() above.
232       base::TimeTicks before_collect_context_graphics_info =
233           base::TimeTicks::Now();
234 #if !defined(OS_MACOSX)
235       if (!gpu::CollectContextGraphicsInfo(&gpu_info))
236         VLOG(1) << "gpu::CollectGraphicsInfo failed";
237       GetContentClient()->SetGpuInfo(gpu_info);
238
239 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
240       // Recompute gpu driver bug workarounds - this is specifically useful
241       // on systems where vendor_id/device_id aren't available.
242       if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
243         gpu::ApplyGpuDriverBugWorkarounds(
244             gpu_info, const_cast<CommandLine*>(&command_line));
245       }
246 #endif
247
248 #if defined(OS_LINUX)
249       initialized_gl_context = true;
250 #if !defined(OS_CHROMEOS)
251       if (gpu_info.gpu.vendor_id == 0x10de &&  // NVIDIA
252           gpu_info.driver_vendor == "NVIDIA") {
253         base::ThreadRestrictions::AssertIOAllowed();
254         if (access("/dev/nvidiactl", R_OK) != 0) {
255           VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
256           dead_on_arrival = true;
257         }
258       }
259 #endif  // !defined(OS_CHROMEOS)
260 #endif  // defined(OS_LINUX)
261 #endif  // !defined(OS_MACOSX)
262       collect_context_time =
263           base::TimeTicks::Now() - before_collect_context_graphics_info;
264     } else {
265       VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
266       dead_on_arrival = true;
267     }
268
269     initialize_one_off_time =
270         base::TimeTicks::Now() - before_initialize_one_off;
271
272     if (enable_watchdog && delayed_watchdog_enable) {
273       watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
274       watchdog_thread->Start();
275     }
276
277     // OSMesa is expected to run very slowly, so disable the watchdog in that
278     // case.
279     if (enable_watchdog &&
280         gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
281       watchdog_thread->Stop();
282       watchdog_thread = NULL;
283     }
284
285 #if defined(OS_LINUX)
286     should_initialize_gl_context = !initialized_gl_context &&
287                                    !dead_on_arrival;
288
289     if (!initialized_sandbox) {
290       gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
291                                              should_initialize_gl_context);
292     }
293 #elif defined(OS_WIN)
294     gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
295 #endif
296   } else {
297     dead_on_arrival = true;
298   }
299
300   logging::SetLogMessageHandler(NULL);
301
302   GpuProcess gpu_process;
303
304   // These UMA must be stored after GpuProcess is constructed as it
305   // initializes StatisticsRecorder which tracks the histograms.
306   UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
307   UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
308
309   GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
310                                                     dead_on_arrival,
311                                                     gpu_info,
312                                                     deferred_messages.Get());
313   while (!deferred_messages.Get().empty())
314     deferred_messages.Get().pop();
315
316   child_thread->Init(start_time);
317
318   gpu_process.set_main_thread(child_thread);
319
320   if (watchdog_thread)
321     watchdog_thread->AddPowerObserver();
322
323   {
324     TRACE_EVENT0("gpu", "Run Message Loop");
325     main_message_loop.Run();
326   }
327
328   child_thread->StopWatchdog();
329
330   return 0;
331 }
332
333 namespace {
334
335 #if defined(OS_LINUX)
336 void CreateDummyGlContext() {
337   scoped_refptr<gfx::GLSurface> surface(
338       gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
339   if (!surface.get()) {
340     VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
341     return;
342   }
343
344   // On Linux, this is needed to make sure /dev/nvidiactl has
345   // been opened and its descriptor cached.
346   scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
347       NULL, surface.get(), gfx::PreferDiscreteGpu));
348   if (!context.get()) {
349     VLOG(1) << "gfx::GLContext::CreateGLContext failed";
350     return;
351   }
352
353   // Similarly, this is needed for /dev/nvidia0.
354   if (context->MakeCurrent(surface.get())) {
355     context->ReleaseCurrent(surface.get());
356   } else {
357     VLOG(1)  << "gfx::GLContext::MakeCurrent failed";
358   }
359 }
360 #endif
361
362 bool WarmUpSandbox(const CommandLine& command_line) {
363   {
364     TRACE_EVENT0("gpu", "Warm up rand");
365     // Warm up the random subsystem, which needs to be done pre-sandbox on all
366     // platforms.
367     (void) base::RandUint64();
368   }
369   {
370     TRACE_EVENT0("gpu", "Warm up HMAC");
371     // Warm up the crypto subsystem, which needs to done pre-sandbox on all
372     // platforms.
373     crypto::HMAC hmac(crypto::HMAC::SHA256);
374     unsigned char key = '\0';
375     if (!hmac.Init(&key, sizeof(key))) {
376       LOG(ERROR) << "WarmUpSandbox() failed with crypto::HMAC::Init()";
377       return false;
378     }
379   }
380
381   return true;
382 }
383
384 #if defined(OS_LINUX)
385 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
386                          bool should_initialize_gl_context) {
387   // We special case Optimus since the vendor_id we see may not be Nvidia.
388   bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de &&  // NVIDIA.
389                              gpu_info.driver_vendor == "NVIDIA") ||
390                             gpu_info.optimus;
391   if (uses_nvidia_driver && should_initialize_gl_context) {
392     // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
393     CreateDummyGlContext();
394   }
395 }
396
397 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
398                        GpuWatchdogThread* watchdog_thread,
399                        bool should_initialize_gl_context) {
400   TRACE_EVENT0("gpu", "Initialize sandbox");
401
402   bool res = false;
403
404   WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
405
406   if (watchdog_thread)
407     watchdog_thread->Stop();
408   // LinuxSandbox::InitializeSandbox() must always be called
409   // with only one thread.
410   res = LinuxSandbox::InitializeSandbox();
411   if (watchdog_thread)
412     watchdog_thread->Start();
413
414   return res;
415 }
416 #endif  // defined(OS_LINUX)
417
418 #if defined(OS_WIN)
419 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
420   TRACE_EVENT0("gpu", "Lower token");
421
422   // For Windows, if the target_services interface is not zero, the process
423   // is sandboxed and we must call LowerToken() before rendering untrusted
424   // content.
425   sandbox::TargetServices* target_services = sandbox_info->target_services;
426   if (target_services) {
427     target_services->LowerToken();
428     return true;
429   }
430
431   return false;
432 }
433 #endif  // defined(OS_WIN)
434
435 }  // namespace.
436
437 }  // namespace content