[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / gpu / gpu_child_thread.cc
blob1064a70adc111cae2969eafde6b6368b4335d886
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 ChildThreadImpl::Options GetOptions() {
48 ChildThreadImpl::Options options;
50 #if defined(USE_OZONE)
51 IPC::MessageFilter* message_filter = ui::OzonePlatform::GetInstance()
52 ->GetGpuPlatformSupport()
53 ->GetMessageFilter();
54 if (message_filter)
55 options.startup_filters.push_back(message_filter);
56 #endif
58 return options;
61 } // namespace
63 GpuChildThread::GpuChildThread(GpuWatchdogThread* watchdog_thread,
64 bool dead_on_arrival,
65 const gpu::GPUInfo& gpu_info,
66 const DeferredMessages& deferred_messages)
67 : ChildThreadImpl(GetOptions()),
68 dead_on_arrival_(dead_on_arrival),
69 gpu_info_(gpu_info),
70 deferred_messages_(deferred_messages),
71 in_browser_process_(false) {
72 watchdog_thread_ = watchdog_thread;
73 #if defined(OS_WIN)
74 target_services_ = NULL;
75 #endif
76 g_thread_safe_sender.Get() = thread_safe_sender();
79 GpuChildThread::GpuChildThread(const std::string& channel_id)
80 : ChildThreadImpl(Options(channel_id, false)),
81 dead_on_arrival_(false),
82 in_browser_process_(true) {
83 #if defined(OS_WIN)
84 target_services_ = NULL;
85 #endif
86 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
87 switches::kSingleProcess) ||
88 base::CommandLine::ForCurrentProcess()->HasSwitch(
89 switches::kInProcessGPU));
90 #if !defined(OS_ANDROID)
91 // For single process and in-process GPU mode, we need to load and
92 // initialize the GL implementation and locate the GL entry points here.
93 // On Android, GLSurface::InitializeOneOff() is called from BrowserMainLoop
94 // before getting here. crbug.com/326295
95 if (!gfx::GLSurface::InitializeOneOff())
96 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
97 #endif
98 g_thread_safe_sender.Get() = thread_safe_sender();
101 GpuChildThread::~GpuChildThread() {
104 void GpuChildThread::Shutdown() {
105 ChildThreadImpl::Shutdown();
106 logging::SetLogMessageHandler(NULL);
109 void GpuChildThread::Init(const base::Time& process_start_time) {
110 process_start_time_ = process_start_time;
113 bool GpuChildThread::Send(IPC::Message* msg) {
114 // The GPU process must never send a synchronous IPC message to the browser
115 // process. This could result in deadlock.
116 DCHECK(!msg->is_sync());
118 return ChildThreadImpl::Send(msg);
121 bool GpuChildThread::OnControlMessageReceived(const IPC::Message& msg) {
122 bool handled = true;
123 IPC_BEGIN_MESSAGE_MAP(GpuChildThread, msg)
124 IPC_MESSAGE_HANDLER(GpuMsg_Initialize, OnInitialize)
125 IPC_MESSAGE_HANDLER(GpuMsg_CollectGraphicsInfo, OnCollectGraphicsInfo)
126 IPC_MESSAGE_HANDLER(GpuMsg_GetVideoMemoryUsageStats,
127 OnGetVideoMemoryUsageStats)
128 IPC_MESSAGE_HANDLER(GpuMsg_Clean, OnClean)
129 IPC_MESSAGE_HANDLER(GpuMsg_Crash, OnCrash)
130 IPC_MESSAGE_HANDLER(GpuMsg_Hang, OnHang)
131 IPC_MESSAGE_HANDLER(GpuMsg_DisableWatchdog, OnDisableWatchdog)
132 IPC_MESSAGE_HANDLER(GpuMsg_GpuSwitched, OnGpuSwitched)
133 IPC_MESSAGE_UNHANDLED(handled = false)
134 IPC_END_MESSAGE_MAP()
136 if (handled)
137 return true;
139 #if defined(USE_OZONE)
140 if (ui::OzonePlatform::GetInstance()
141 ->GetGpuPlatformSupport()
142 ->OnMessageReceived(msg))
143 return true;
144 #endif
146 return gpu_channel_manager_.get() &&
147 gpu_channel_manager_->OnMessageReceived(msg);
150 void GpuChildThread::OnInitialize() {
151 // Record initialization only after collecting the GPU info because that can
152 // take a significant amount of time.
153 gpu_info_.initialization_time = base::Time::Now() - process_start_time_;
154 Send(new GpuHostMsg_Initialized(!dead_on_arrival_, gpu_info_));
155 while (!deferred_messages_.empty()) {
156 Send(deferred_messages_.front());
157 deferred_messages_.pop();
160 if (dead_on_arrival_) {
161 LOG(ERROR) << "Exiting GPU process due to errors during initialization";
162 base::MessageLoop::current()->Quit();
163 return;
166 #if defined(OS_ANDROID)
167 base::PlatformThread::SetThreadPriority(
168 base::PlatformThread::CurrentHandle(),
169 base::kThreadPriority_Display);
170 #endif
172 // We don't need to pipe log messages if we are running the GPU thread in
173 // the browser process.
174 if (!in_browser_process_)
175 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
177 // Defer creation of the render thread. This is to prevent it from handling
178 // IPC messages before the sandbox has been enabled and all other necessary
179 // initialization has succeeded.
180 gpu_channel_manager_.reset(
181 new GpuChannelManager(GetRouter(),
182 watchdog_thread_.get(),
183 ChildProcess::current()->io_message_loop_proxy(),
184 ChildProcess::current()->GetShutDownEvent(),
185 channel()));
187 #if defined(USE_OZONE)
188 ui::OzonePlatform::GetInstance()
189 ->GetGpuPlatformSupport()
190 ->OnChannelEstablished(this);
191 #endif
194 void GpuChildThread::StopWatchdog() {
195 if (watchdog_thread_.get()) {
196 watchdog_thread_->Stop();
200 void GpuChildThread::OnCollectGraphicsInfo() {
201 #if defined(OS_WIN)
202 // GPU full info collection should only happen on un-sandboxed GPU process
203 // or single process/in-process gpu mode on Windows.
204 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
205 DCHECK(command_line->HasSwitch(switches::kDisableGpuSandbox) ||
206 in_browser_process_);
207 #endif // OS_WIN
209 gpu::CollectInfoResult result =
210 gpu::CollectContextGraphicsInfo(&gpu_info_);
211 switch (result) {
212 case gpu::kCollectInfoFatalFailure:
213 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
214 // TODO(piman): can we signal overall failure?
215 break;
216 case gpu::kCollectInfoNonFatalFailure:
217 DVLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
218 break;
219 case gpu::kCollectInfoNone:
220 NOTREACHED();
221 break;
222 case gpu::kCollectInfoSuccess:
223 break;
225 GetContentClient()->SetGpuInfo(gpu_info_);
227 #if defined(OS_WIN)
228 // This is slow, but it's the only thing the unsandboxed GPU process does,
229 // and GpuDataManager prevents us from sending multiple collecting requests,
230 // so it's OK to be blocking.
231 gpu::GetDxDiagnostics(&gpu_info_.dx_diagnostics);
232 gpu_info_.dx_diagnostics_info_state = gpu::kCollectInfoSuccess;
233 #endif // OS_WIN
235 Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_));
237 #if defined(OS_WIN)
238 if (!in_browser_process_) {
239 // The unsandboxed GPU process fulfilled its duty. Rest in peace.
240 base::MessageLoop::current()->Quit();
242 #endif // OS_WIN
245 void GpuChildThread::OnGetVideoMemoryUsageStats() {
246 GPUVideoMemoryUsageStats video_memory_usage_stats;
247 if (gpu_channel_manager_)
248 gpu_channel_manager_->gpu_memory_manager()->GetVideoMemoryUsageStats(
249 &video_memory_usage_stats);
250 Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats));
253 void GpuChildThread::OnClean() {
254 DVLOG(1) << "GPU: Removing all contexts";
255 if (gpu_channel_manager_)
256 gpu_channel_manager_->LoseAllContexts();
259 void GpuChildThread::OnCrash() {
260 DVLOG(1) << "GPU: Simulating GPU crash";
261 // Good bye, cruel world.
262 volatile int* it_s_the_end_of_the_world_as_we_know_it = NULL;
263 *it_s_the_end_of_the_world_as_we_know_it = 0xdead;
266 void GpuChildThread::OnHang() {
267 DVLOG(1) << "GPU: Simulating GPU hang";
268 for (;;) {
269 // Do not sleep here. The GPU watchdog timer tracks the amount of user
270 // time this thread is using and it doesn't use much while calling Sleep.
274 void GpuChildThread::OnDisableWatchdog() {
275 DVLOG(1) << "GPU: Disabling watchdog thread";
276 if (watchdog_thread_.get()) {
277 // Disarm the watchdog before shutting down the message loop. This prevents
278 // the future posting of tasks to the message loop.
279 if (watchdog_thread_->message_loop())
280 watchdog_thread_->PostAcknowledge();
281 // Prevent rearming.
282 watchdog_thread_->Stop();
286 void GpuChildThread::OnGpuSwitched() {
287 DVLOG(1) << "GPU: GPU has switched";
288 // Notify observers in the GPU process.
289 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched();
292 } // namespace content