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"
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"
25 static base::LazyInstance
<scoped_refptr
<ThreadSafeSender
> >
26 g_thread_safe_sender
= LAZY_INSTANCE_INITIALIZER
;
28 bool GpuProcessLogMessageHandler(int severity
,
29 const char* file
, int line
,
31 const std::string
& str
) {
32 std::string header
= str
.substr(0, message_start
);
33 std::string message
= str
.substr(message_start
);
35 g_thread_safe_sender
.Get()->Send(new GpuHostMsg_OnLogMessage(
36 severity
, header
, message
));
43 GpuChildThread::GpuChildThread(GpuWatchdogThread
* watchdog_thread
,
45 const gpu::GPUInfo
& gpu_info
,
46 const DeferredMessages
& deferred_messages
)
47 : dead_on_arrival_(dead_on_arrival
),
49 deferred_messages_(deferred_messages
),
50 in_browser_process_(false) {
51 watchdog_thread_
= watchdog_thread
;
53 target_services_
= NULL
;
55 g_thread_safe_sender
.Get() = thread_safe_sender();
58 GpuChildThread::GpuChildThread(const std::string
& channel_id
)
59 : ChildThread(channel_id
),
60 dead_on_arrival_(false),
61 in_browser_process_(true) {
63 target_services_
= NULL
;
66 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
) ||
67 CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU
));
68 // For single process and in-process GPU mode, we need to load and
69 // initialize the GL implementation and locate the GL entry points here.
70 if (!gfx::GLSurface::InitializeOneOff()) {
71 VLOG(1) << "gfx::GLSurface::InitializeOneOff()";
73 g_thread_safe_sender
.Get() = thread_safe_sender();
76 GpuChildThread::~GpuChildThread() {
79 void GpuChildThread::Shutdown() {
80 logging::SetLogMessageHandler(NULL
);
83 void GpuChildThread::Init(const base::Time
& process_start_time
) {
84 process_start_time_
= process_start_time
;
87 bool GpuChildThread::Send(IPC::Message
* msg
) {
88 // The GPU process must never send a synchronous IPC message to the browser
89 // process. This could result in deadlock.
90 DCHECK(!msg
->is_sync());
92 return ChildThread::Send(msg
);
95 bool GpuChildThread::OnControlMessageReceived(const IPC::Message
& msg
) {
96 bool msg_is_ok
= true;
98 IPC_BEGIN_MESSAGE_MAP_EX(GpuChildThread
, msg
, msg_is_ok
)
99 IPC_MESSAGE_HANDLER(GpuMsg_Initialize
, OnInitialize
)
100 IPC_MESSAGE_HANDLER(GpuMsg_CollectGraphicsInfo
, OnCollectGraphicsInfo
)
101 IPC_MESSAGE_HANDLER(GpuMsg_GetVideoMemoryUsageStats
,
102 OnGetVideoMemoryUsageStats
)
103 IPC_MESSAGE_HANDLER(GpuMsg_Clean
, OnClean
)
104 IPC_MESSAGE_HANDLER(GpuMsg_Crash
, OnCrash
)
105 IPC_MESSAGE_HANDLER(GpuMsg_Hang
, OnHang
)
106 IPC_MESSAGE_HANDLER(GpuMsg_DisableWatchdog
, OnDisableWatchdog
)
107 IPC_MESSAGE_UNHANDLED(handled
= false)
108 IPC_END_MESSAGE_MAP_EX()
113 return gpu_channel_manager_
.get() &&
114 gpu_channel_manager_
->OnMessageReceived(msg
);
117 void GpuChildThread::OnInitialize() {
118 Send(new GpuHostMsg_Initialized(!dead_on_arrival_
, gpu_info_
));
119 while (!deferred_messages_
.empty()) {
120 Send(deferred_messages_
.front());
121 deferred_messages_
.pop();
124 if (dead_on_arrival_
) {
125 VLOG(1) << "Exiting GPU process due to errors during initialization";
126 base::MessageLoop::current()->Quit();
130 #if defined(OS_ANDROID)
131 base::PlatformThread::SetThreadPriority(
132 base::PlatformThread::CurrentHandle(),
133 base::kThreadPriority_Display
);
136 // We don't need to pipe log messages if we are running the GPU thread in
137 // the browser process.
138 if (!in_browser_process_
)
139 logging::SetLogMessageHandler(GpuProcessLogMessageHandler
);
141 // Record initialization only after collecting the GPU info because that can
142 // take a significant amount of time.
143 gpu_info_
.initialization_time
= base::Time::Now() - process_start_time_
;
145 // Defer creation of the render thread. This is to prevent it from handling
146 // IPC messages before the sandbox has been enabled and all other necessary
147 // initialization has succeeded.
148 gpu_channel_manager_
.reset(
149 new GpuChannelManager(this,
150 watchdog_thread_
.get(),
151 ChildProcess::current()->io_message_loop_proxy(),
152 ChildProcess::current()->GetShutDownEvent()));
154 // Ensure the browser process receives the GPU info before a reply to any
155 // subsequent IPC it might send.
156 if (!in_browser_process_
)
157 Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_
));
160 void GpuChildThread::StopWatchdog() {
161 if (watchdog_thread_
.get()) {
162 watchdog_thread_
->Stop();
166 void GpuChildThread::OnCollectGraphicsInfo() {
168 // GPU full info collection should only happen on un-sandboxed GPU process
169 // or single process/in-process gpu mode on Windows.
170 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
171 DCHECK(command_line
->HasSwitch(switches::kDisableGpuSandbox
) ||
172 in_browser_process_
);
175 if (!gpu::CollectContextGraphicsInfo(&gpu_info_
))
176 VLOG(1) << "gpu::CollectGraphicsInfo failed";
177 GetContentClient()->SetGpuInfo(gpu_info_
);
180 // This is slow, but it's the only thing the unsandboxed GPU process does,
181 // and GpuDataManager prevents us from sending multiple collecting requests,
182 // so it's OK to be blocking.
183 gpu::GetDxDiagnostics(&gpu_info_
.dx_diagnostics
);
184 gpu_info_
.finalized
= true;
187 Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_
));
190 if (!in_browser_process_
) {
191 // The unsandboxed GPU process fulfilled its duty. Rest in peace.
192 base::MessageLoop::current()->Quit();
197 void GpuChildThread::OnGetVideoMemoryUsageStats() {
198 GPUVideoMemoryUsageStats video_memory_usage_stats
;
199 if (gpu_channel_manager_
)
200 gpu_channel_manager_
->gpu_memory_manager()->GetVideoMemoryUsageStats(
201 &video_memory_usage_stats
);
202 Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats
));
205 void GpuChildThread::OnClean() {
206 VLOG(1) << "GPU: Removing all contexts";
207 if (gpu_channel_manager_
)
208 gpu_channel_manager_
->LoseAllContexts();
211 void GpuChildThread::OnCrash() {
212 VLOG(1) << "GPU: Simulating GPU crash";
213 // Good bye, cruel world.
214 volatile int* it_s_the_end_of_the_world_as_we_know_it
= NULL
;
215 *it_s_the_end_of_the_world_as_we_know_it
= 0xdead;
218 void GpuChildThread::OnHang() {
219 VLOG(1) << "GPU: Simulating GPU hang";
221 // Do not sleep here. The GPU watchdog timer tracks the amount of user
222 // time this thread is using and it doesn't use much while calling Sleep.
226 void GpuChildThread::OnDisableWatchdog() {
227 VLOG(1) << "GPU: Disabling watchdog thread";
228 if (watchdog_thread_
.get()) {
229 // Disarm the watchdog before shutting down the message loop. This prevents
230 // the future posting of tasks to the message loop.
231 if (watchdog_thread_
->message_loop())
232 watchdog_thread_
->PostAcknowledge();
234 watchdog_thread_
->Stop();
238 } // namespace content