Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / gpu / gpu_child_thread.cc
blobf47294b9d66fc29e9e1ae91348656f5af56a64df
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.
5 #include "content/gpu/gpu_child_thread.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/threading/worker_pool.h"
10 #include "build/build_config.h"
11 #include "content/child/child_process.h"
12 #include "content/child/thread_safe_sender.h"
13 #include "content/common/gpu/gpu_messages.h"
14 #include "content/gpu/gpu_watchdog_thread.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/content_switches.h"
17 #include "gpu/config/gpu_info_collector.h"
18 #include "ipc/ipc_channel_handle.h"
19 #include "ipc/ipc_sync_message_filter.h"
20 #include "ui/gl/gl_implementation.h"
21 #include "ui/gl/gpu_switching_manager.h"
23 #if defined(USE_OZONE)
24 #include "ui/ozone/public/gpu_platform_support.h"
25 #include "ui/ozone/public/ozone_platform.h"
26 #endif
28 namespace content {
29 namespace {
31 static base::LazyInstance<scoped_refptr<ThreadSafeSender> >
32 g_thread_safe_sender = LAZY_INSTANCE_INITIALIZER;
34 bool GpuProcessLogMessageHandler(int severity,
35 const char* file, int line,
36 size_t message_start,
37 const std::string& str) {
38 std::string header = str.substr(0, message_start);
39 std::string message = str.substr(message_start);
41 g_thread_safe_sender.Get()->Send(new GpuHostMsg_OnLogMessage(
42 severity, header, message));
44 return false;
47 } // namespace
49 GpuChildThread::GpuChildThread(GpuWatchdogThread* watchdog_thread,
50 bool dead_on_arrival,
51 const gpu::GPUInfo& gpu_info,
52 const DeferredMessages& deferred_messages)
53 : dead_on_arrival_(dead_on_arrival),
54 gpu_info_(gpu_info),
55 deferred_messages_(deferred_messages),
56 in_browser_process_(false) {
57 watchdog_thread_ = watchdog_thread;
58 #if defined(OS_WIN)
59 target_services_ = NULL;
60 #endif
61 g_thread_safe_sender.Get() = thread_safe_sender();
64 GpuChildThread::GpuChildThread(const std::string& channel_id)
65 : ChildThread(Options(channel_id, false)),
66 dead_on_arrival_(false),
67 in_browser_process_(true) {
68 #if defined(OS_WIN)
69 target_services_ = NULL;
70 #endif
71 DCHECK(
72 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess) ||
73 CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU));
74 #if !defined(OS_ANDROID)
75 // For single process and in-process GPU mode, we need to load and
76 // initialize the GL implementation and locate the GL entry points here.
77 // On Android, GLSurface::InitializeOneOff() is called from BrowserMainLoop
78 // before getting here. crbug.com/326295
79 if (!gfx::GLSurface::InitializeOneOff())
80 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
81 #endif
82 g_thread_safe_sender.Get() = thread_safe_sender();
85 GpuChildThread::~GpuChildThread() {
88 void GpuChildThread::Shutdown() {
89 ChildThread::Shutdown();
90 logging::SetLogMessageHandler(NULL);
93 void GpuChildThread::Init(const base::Time& process_start_time) {
94 process_start_time_ = process_start_time;
97 bool GpuChildThread::Send(IPC::Message* msg) {
98 // The GPU process must never send a synchronous IPC message to the browser
99 // process. This could result in deadlock.
100 DCHECK(!msg->is_sync());
102 return ChildThread::Send(msg);
105 bool GpuChildThread::OnControlMessageReceived(const IPC::Message& msg) {
106 bool handled = true;
107 IPC_BEGIN_MESSAGE_MAP(GpuChildThread, msg)
108 IPC_MESSAGE_HANDLER(GpuMsg_Initialize, OnInitialize)
109 IPC_MESSAGE_HANDLER(GpuMsg_CollectGraphicsInfo, OnCollectGraphicsInfo)
110 IPC_MESSAGE_HANDLER(GpuMsg_GetVideoMemoryUsageStats,
111 OnGetVideoMemoryUsageStats)
112 IPC_MESSAGE_HANDLER(GpuMsg_Clean, OnClean)
113 IPC_MESSAGE_HANDLER(GpuMsg_Crash, OnCrash)
114 IPC_MESSAGE_HANDLER(GpuMsg_Hang, OnHang)
115 IPC_MESSAGE_HANDLER(GpuMsg_DisableWatchdog, OnDisableWatchdog)
116 IPC_MESSAGE_HANDLER(GpuMsg_GpuSwitched, OnGpuSwitched)
117 IPC_MESSAGE_UNHANDLED(handled = false)
118 IPC_END_MESSAGE_MAP()
120 if (handled)
121 return true;
123 #if defined(USE_OZONE)
124 if (ui::OzonePlatform::GetInstance()
125 ->GetGpuPlatformSupport()
126 ->OnMessageReceived(msg))
127 return true;
128 #endif
130 return gpu_channel_manager_.get() &&
131 gpu_channel_manager_->OnMessageReceived(msg);
134 void GpuChildThread::OnInitialize() {
135 // Record initialization only after collecting the GPU info because that can
136 // take a significant amount of time.
137 gpu_info_.initialization_time = base::Time::Now() - process_start_time_;
138 Send(new GpuHostMsg_Initialized(!dead_on_arrival_, gpu_info_));
139 while (!deferred_messages_.empty()) {
140 Send(deferred_messages_.front());
141 deferred_messages_.pop();
144 if (dead_on_arrival_) {
145 LOG(ERROR) << "Exiting GPU process due to errors during initialization";
146 base::MessageLoop::current()->Quit();
147 return;
150 #if defined(OS_ANDROID)
151 base::PlatformThread::SetThreadPriority(
152 base::PlatformThread::CurrentHandle(),
153 base::kThreadPriority_Display);
154 #endif
156 // We don't need to pipe log messages if we are running the GPU thread in
157 // the browser process.
158 if (!in_browser_process_)
159 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
161 // Defer creation of the render thread. This is to prevent it from handling
162 // IPC messages before the sandbox has been enabled and all other necessary
163 // initialization has succeeded.
164 gpu_channel_manager_.reset(
165 new GpuChannelManager(GetRouter(),
166 watchdog_thread_.get(),
167 ChildProcess::current()->io_message_loop_proxy(),
168 ChildProcess::current()->GetShutDownEvent(),
169 channel()));
171 #if defined(USE_OZONE)
172 ui::OzonePlatform::GetInstance()
173 ->GetGpuPlatformSupport()
174 ->OnChannelEstablished(this);
175 #endif
178 void GpuChildThread::StopWatchdog() {
179 if (watchdog_thread_.get()) {
180 watchdog_thread_->Stop();
184 void GpuChildThread::OnCollectGraphicsInfo() {
185 #if defined(OS_WIN)
186 // GPU full info collection should only happen on un-sandboxed GPU process
187 // or single process/in-process gpu mode on Windows.
188 CommandLine* command_line = CommandLine::ForCurrentProcess();
189 DCHECK(command_line->HasSwitch(switches::kDisableGpuSandbox) ||
190 in_browser_process_);
191 #endif // OS_WIN
193 gpu::CollectInfoResult result =
194 gpu::CollectContextGraphicsInfo(&gpu_info_);
195 switch (result) {
196 case gpu::kCollectInfoFatalFailure:
197 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
198 // TODO(piman): can we signal overall failure?
199 break;
200 case gpu::kCollectInfoNonFatalFailure:
201 VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
202 break;
203 case gpu::kCollectInfoNone:
204 NOTREACHED();
205 break;
206 case gpu::kCollectInfoSuccess:
207 break;
209 GetContentClient()->SetGpuInfo(gpu_info_);
211 #if defined(OS_WIN)
212 // This is slow, but it's the only thing the unsandboxed GPU process does,
213 // and GpuDataManager prevents us from sending multiple collecting requests,
214 // so it's OK to be blocking.
215 gpu::GetDxDiagnostics(&gpu_info_.dx_diagnostics);
216 gpu_info_.dx_diagnostics_info_state = gpu::kCollectInfoSuccess;
217 #endif // OS_WIN
219 Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_));
221 #if defined(OS_WIN)
222 if (!in_browser_process_) {
223 // The unsandboxed GPU process fulfilled its duty. Rest in peace.
224 base::MessageLoop::current()->Quit();
226 #endif // OS_WIN
229 void GpuChildThread::OnGetVideoMemoryUsageStats() {
230 GPUVideoMemoryUsageStats video_memory_usage_stats;
231 if (gpu_channel_manager_)
232 gpu_channel_manager_->gpu_memory_manager()->GetVideoMemoryUsageStats(
233 &video_memory_usage_stats);
234 Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats));
237 void GpuChildThread::OnClean() {
238 VLOG(1) << "GPU: Removing all contexts";
239 if (gpu_channel_manager_)
240 gpu_channel_manager_->LoseAllContexts();
243 void GpuChildThread::OnCrash() {
244 VLOG(1) << "GPU: Simulating GPU crash";
245 // Good bye, cruel world.
246 volatile int* it_s_the_end_of_the_world_as_we_know_it = NULL;
247 *it_s_the_end_of_the_world_as_we_know_it = 0xdead;
250 void GpuChildThread::OnHang() {
251 VLOG(1) << "GPU: Simulating GPU hang";
252 for (;;) {
253 // Do not sleep here. The GPU watchdog timer tracks the amount of user
254 // time this thread is using and it doesn't use much while calling Sleep.
258 void GpuChildThread::OnDisableWatchdog() {
259 VLOG(1) << "GPU: Disabling watchdog thread";
260 if (watchdog_thread_.get()) {
261 // Disarm the watchdog before shutting down the message loop. This prevents
262 // the future posting of tasks to the message loop.
263 if (watchdog_thread_->message_loop())
264 watchdog_thread_->PostAcknowledge();
265 // Prevent rearming.
266 watchdog_thread_->Stop();
270 void GpuChildThread::OnGpuSwitched() {
271 VLOG(1) << "GPU: GPU has switched";
272 // Notify observers in the GPU process.
273 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched();
276 } // namespace content