Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / gpu / gpu_process_host.cc
blob54c5a9a6563808a52c25179209eccea14b6ddf22
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/browser/gpu/gpu_process_host.h"
7 #include "base/base64.h"
8 #include "base/base_switches.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/command_line.h"
13 #include "base/debug/trace_event.h"
14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/metrics/histogram.h"
17 #include "base/sha1.h"
18 #include "base/threading/thread.h"
19 #include "content/browser/browser_child_process_host_impl.h"
20 #include "content/browser/gpu/compositor_util.h"
21 #include "content/browser/gpu/gpu_data_manager_impl.h"
22 #include "content/browser/gpu/gpu_process_host_ui_shim.h"
23 #include "content/browser/gpu/shader_disk_cache.h"
24 #include "content/browser/renderer_host/render_widget_host_impl.h"
25 #include "content/browser/renderer_host/render_widget_resize_helper.h"
26 #include "content/common/child_process_host_impl.h"
27 #include "content/common/gpu/gpu_messages.h"
28 #include "content/common/view_messages.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/content_browser_client.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_widget_host_view.h"
33 #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
34 #include "content/public/common/content_client.h"
35 #include "content/public/common/content_switches.h"
36 #include "content/public/common/result_codes.h"
37 #include "content/public/common/sandboxed_process_launcher_delegate.h"
38 #include "gpu/command_buffer/service/gpu_switches.h"
39 #include "ipc/ipc_channel_handle.h"
40 #include "ipc/ipc_switches.h"
41 #include "ipc/message_filter.h"
42 #include "media/base/media_switches.h"
43 #include "ui/base/ui_base_switches.h"
44 #include "ui/events/latency_info.h"
45 #include "ui/gl/gl_switches.h"
47 #if defined(OS_WIN)
48 #include "base/win/windows_version.h"
49 #include "content/common/sandbox_win.h"
50 #include "sandbox/win/src/sandbox_policy.h"
51 #include "ui/gfx/switches.h"
52 #endif
54 #if defined(USE_OZONE)
55 #include "ui/ozone/public/ozone_switches.h"
56 #endif
58 #if defined(USE_X11) && !defined(OS_CHROMEOS)
59 #include "ui/gfx/x/x11_switches.h"
60 #endif
62 namespace content {
64 bool GpuProcessHost::gpu_enabled_ = true;
65 bool GpuProcessHost::hardware_gpu_enabled_ = true;
66 int GpuProcessHost::gpu_crash_count_ = 0;
67 int GpuProcessHost::gpu_recent_crash_count_ = 0;
68 bool GpuProcessHost::crashed_before_ = false;
69 int GpuProcessHost::swiftshader_crash_count_ = 0;
71 namespace {
73 // Command-line switches to propagate to the GPU process.
74 static const char* const kSwitchNames[] = {
75 switches::kDisableAcceleratedVideoDecode,
76 switches::kDisableBreakpad,
77 switches::kDisableGpuSandbox,
78 switches::kDisableGpuWatchdog,
79 switches::kDisableLogging,
80 switches::kDisableSeccompFilterSandbox,
81 #if defined(ENABLE_WEBRTC)
82 switches::kDisableWebRtcHWEncoding,
83 #endif
84 switches::kEnableLogging,
85 switches::kEnableShareGroupAsyncTextureUpload,
86 #if defined(OS_CHROMEOS)
87 switches::kDisableVaapiAcceleratedVideoEncode,
88 #endif
89 switches::kGpuStartupDialog,
90 switches::kGpuSandboxAllowSysVShm,
91 switches::kGpuSandboxFailuresFatal,
92 switches::kGpuSandboxStartEarly,
93 switches::kIgnoreResolutionLimitsForAcceleratedVideoDecode,
94 switches::kLoggingLevel,
95 switches::kLowEndDeviceMode,
96 switches::kNoSandbox,
97 switches::kTestGLLib,
98 switches::kTraceStartup,
99 switches::kTraceToConsole,
100 switches::kV,
101 switches::kVModule,
102 #if defined(OS_MACOSX)
103 switches::kEnableRemoteCoreAnimation,
104 switches::kEnableSandboxLogging,
105 #endif
106 #if defined(USE_AURA)
107 switches::kUIPrioritizeInGpuProcess,
108 #endif
109 #if defined(USE_OZONE)
110 switches::kOzonePlatform,
111 switches::kOzoneUseSurfaceless,
112 #endif
113 #if defined(USE_X11) && !defined(OS_CHROMEOS)
114 switches::kX11Display,
115 #endif
118 enum GPUProcessLifetimeEvent {
119 LAUNCHED,
120 DIED_FIRST_TIME,
121 DIED_SECOND_TIME,
122 DIED_THIRD_TIME,
123 DIED_FOURTH_TIME,
124 GPU_PROCESS_LIFETIME_EVENT_MAX = 100
127 // Indexed by GpuProcessKind. There is one of each kind maximum. This array may
128 // only be accessed from the IO thread.
129 GpuProcessHost* g_gpu_process_hosts[GpuProcessHost::GPU_PROCESS_KIND_COUNT];
132 void SendGpuProcessMessage(GpuProcessHost::GpuProcessKind kind,
133 CauseForGpuLaunch cause,
134 IPC::Message* message) {
135 GpuProcessHost* host = GpuProcessHost::Get(kind, cause);
136 if (host) {
137 host->Send(message);
138 } else {
139 delete message;
143 // NOTE: changes to this class need to be reviewed by the security team.
144 class GpuSandboxedProcessLauncherDelegate
145 : public SandboxedProcessLauncherDelegate {
146 public:
147 GpuSandboxedProcessLauncherDelegate(base::CommandLine* cmd_line,
148 ChildProcessHost* host)
149 #if defined(OS_WIN)
150 : cmd_line_(cmd_line) {}
151 #elif defined(OS_POSIX)
152 : ipc_fd_(host->TakeClientFileDescriptor()) {}
153 #endif
155 virtual ~GpuSandboxedProcessLauncherDelegate() {}
157 #if defined(OS_WIN)
158 virtual bool ShouldSandbox() OVERRIDE {
159 bool sandbox = !cmd_line_->HasSwitch(switches::kDisableGpuSandbox);
160 if(! sandbox) {
161 DVLOG(1) << "GPU sandbox is disabled";
163 return sandbox;
166 virtual void PreSandbox(bool* disable_default_policy,
167 base::FilePath* exposed_dir) OVERRIDE {
168 *disable_default_policy = true;
171 // For the GPU process we gotten as far as USER_LIMITED. The next level
172 // which is USER_RESTRICTED breaks both the DirectX backend and the OpenGL
173 // backend. Note that the GPU process is connected to the interactive
174 // desktop.
175 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy,
176 bool* success) {
177 if (base::win::GetVersion() > base::win::VERSION_XP) {
178 if (cmd_line_->GetSwitchValueASCII(switches::kUseGL) ==
179 gfx::kGLImplementationDesktopName) {
180 // Open GL path.
181 policy->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
182 sandbox::USER_LIMITED);
183 SetJobLevel(*cmd_line_, sandbox::JOB_UNPROTECTED, 0, policy);
184 policy->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
185 } else {
186 policy->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
187 sandbox::USER_LIMITED);
189 // UI restrictions break when we access Windows from outside our job.
190 // However, we don't want a proxy window in this process because it can
191 // introduce deadlocks where the renderer blocks on the gpu, which in
192 // turn blocks on the browser UI thread. So, instead we forgo a window
193 // message pump entirely and just add job restrictions to prevent child
194 // processes.
195 SetJobLevel(*cmd_line_,
196 sandbox::JOB_LIMITED_USER,
197 JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS |
198 JOB_OBJECT_UILIMIT_DESKTOP |
199 JOB_OBJECT_UILIMIT_EXITWINDOWS |
200 JOB_OBJECT_UILIMIT_DISPLAYSETTINGS,
201 policy);
203 policy->SetIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
205 } else {
206 SetJobLevel(*cmd_line_, sandbox::JOB_UNPROTECTED, 0, policy);
207 policy->SetTokenLevel(sandbox::USER_UNPROTECTED,
208 sandbox::USER_LIMITED);
211 // Allow the server side of GPU sockets, which are pipes that have
212 // the "chrome.gpu" namespace and an arbitrary suffix.
213 sandbox::ResultCode result = policy->AddRule(
214 sandbox::TargetPolicy::SUBSYS_NAMED_PIPES,
215 sandbox::TargetPolicy::NAMEDPIPES_ALLOW_ANY,
216 L"\\\\.\\pipe\\chrome.gpu.*");
217 if (result != sandbox::SBOX_ALL_OK) {
218 *success = false;
219 return;
222 // Block this DLL even if it is not loaded by the browser process.
223 policy->AddDllToUnload(L"cmsetac.dll");
225 #ifdef USE_AURA
226 // GPU also needs to add sections to the browser for aura
227 // TODO(jschuh): refactor the GPU channel to remove this. crbug.com/128786
228 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_HANDLES,
229 sandbox::TargetPolicy::HANDLES_DUP_BROKER,
230 L"Section");
231 if (result != sandbox::SBOX_ALL_OK) {
232 *success = false;
233 return;
235 #endif
237 if (cmd_line_->HasSwitch(switches::kEnableLogging)) {
238 base::string16 log_file_path = logging::GetLogFileFullPath();
239 if (!log_file_path.empty()) {
240 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
241 sandbox::TargetPolicy::FILES_ALLOW_ANY,
242 log_file_path.c_str());
243 if (result != sandbox::SBOX_ALL_OK) {
244 *success = false;
245 return;
250 #elif defined(OS_POSIX)
252 virtual int GetIpcFd() OVERRIDE {
253 return ipc_fd_;
255 #endif // OS_WIN
257 private:
258 #if defined(OS_WIN)
259 base::CommandLine* cmd_line_;
260 #elif defined(OS_POSIX)
261 int ipc_fd_;
262 #endif // OS_WIN
265 } // anonymous namespace
267 // static
268 bool GpuProcessHost::ValidateHost(GpuProcessHost* host) {
269 // The Gpu process is invalid if it's not using SwiftShader, the card is
270 // blacklisted, and we can kill it and start over.
271 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
272 switches::kSingleProcess) ||
273 base::CommandLine::ForCurrentProcess()->HasSwitch(
274 switches::kInProcessGPU) ||
275 (host->valid_ &&
276 (host->swiftshader_rendering_ ||
277 !GpuDataManagerImpl::GetInstance()->ShouldUseSwiftShader()))) {
278 return true;
281 host->ForceShutdown();
282 return false;
285 // static
286 GpuProcessHost* GpuProcessHost::Get(GpuProcessKind kind,
287 CauseForGpuLaunch cause) {
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
290 // Don't grant further access to GPU if it is not allowed.
291 GpuDataManagerImpl* gpu_data_manager = GpuDataManagerImpl::GetInstance();
292 DCHECK(gpu_data_manager);
293 if (!gpu_data_manager->GpuAccessAllowed(NULL))
294 return NULL;
296 if (g_gpu_process_hosts[kind] && ValidateHost(g_gpu_process_hosts[kind]))
297 return g_gpu_process_hosts[kind];
299 if (cause == CAUSE_FOR_GPU_LAUNCH_NO_LAUNCH)
300 return NULL;
302 static int last_host_id = 0;
303 int host_id;
304 host_id = ++last_host_id;
306 UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLaunchCause",
307 cause,
308 CAUSE_FOR_GPU_LAUNCH_MAX_ENUM);
310 GpuProcessHost* host = new GpuProcessHost(host_id, kind);
311 if (host->Init())
312 return host;
314 delete host;
315 return NULL;
318 // static
319 void GpuProcessHost::GetProcessHandles(
320 const GpuDataManager::GetGpuProcessHandlesCallback& callback) {
321 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
322 BrowserThread::PostTask(
323 BrowserThread::IO,
324 FROM_HERE,
325 base::Bind(&GpuProcessHost::GetProcessHandles, callback));
326 return;
328 std::list<base::ProcessHandle> handles;
329 for (size_t i = 0; i < arraysize(g_gpu_process_hosts); ++i) {
330 GpuProcessHost* host = g_gpu_process_hosts[i];
331 if (host && ValidateHost(host))
332 handles.push_back(host->process_->GetHandle());
334 BrowserThread::PostTask(
335 BrowserThread::UI,
336 FROM_HERE,
337 base::Bind(callback, handles));
340 // static
341 void GpuProcessHost::SendOnIO(GpuProcessKind kind,
342 CauseForGpuLaunch cause,
343 IPC::Message* message) {
344 if (!BrowserThread::PostTask(
345 BrowserThread::IO, FROM_HERE,
346 base::Bind(
347 &SendGpuProcessMessage, kind, cause, message))) {
348 delete message;
352 GpuMainThreadFactoryFunction g_gpu_main_thread_factory = NULL;
354 void GpuProcessHost::RegisterGpuMainThreadFactory(
355 GpuMainThreadFactoryFunction create) {
356 g_gpu_main_thread_factory = create;
359 // static
360 GpuProcessHost* GpuProcessHost::FromID(int host_id) {
361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
363 for (int i = 0; i < GPU_PROCESS_KIND_COUNT; ++i) {
364 GpuProcessHost* host = g_gpu_process_hosts[i];
365 if (host && host->host_id_ == host_id && ValidateHost(host))
366 return host;
369 return NULL;
372 GpuProcessHost::GpuProcessHost(int host_id, GpuProcessKind kind)
373 : host_id_(host_id),
374 valid_(true),
375 in_process_(false),
376 swiftshader_rendering_(false),
377 kind_(kind),
378 process_launched_(false),
379 initialized_(false),
380 gpu_crash_recorded_(false),
381 uma_memory_stats_received_(false) {
382 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
383 switches::kSingleProcess) ||
384 base::CommandLine::ForCurrentProcess()->HasSwitch(
385 switches::kInProcessGPU)) {
386 in_process_ = true;
389 // If the 'single GPU process' policy ever changes, we still want to maintain
390 // it for 'gpu thread' mode and only create one instance of host and thread.
391 DCHECK(!in_process_ || g_gpu_process_hosts[kind] == NULL);
393 g_gpu_process_hosts[kind] = this;
395 // Post a task to create the corresponding GpuProcessHostUIShim. The
396 // GpuProcessHostUIShim will be destroyed if either the browser exits,
397 // in which case it calls GpuProcessHostUIShim::DestroyAll, or the
398 // GpuProcessHost is destroyed, which happens when the corresponding GPU
399 // process terminates or fails to launch.
400 BrowserThread::PostTask(
401 BrowserThread::UI,
402 FROM_HERE,
403 base::Bind(base::IgnoreResult(&GpuProcessHostUIShim::Create), host_id));
405 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_GPU, this));
408 GpuProcessHost::~GpuProcessHost() {
409 DCHECK(CalledOnValidThread());
411 SendOutstandingReplies();
413 RecordProcessCrash();
415 // In case we never started, clean up.
416 while (!queued_messages_.empty()) {
417 delete queued_messages_.front();
418 queued_messages_.pop();
421 // This is only called on the IO thread so no race against the constructor
422 // for another GpuProcessHost.
423 if (g_gpu_process_hosts[kind_] == this)
424 g_gpu_process_hosts[kind_] = NULL;
426 // If there are any remaining offscreen contexts at the point the
427 // GPU process exits, assume something went wrong, and block their
428 // URLs from accessing client 3D APIs without prompting.
429 BlockLiveOffscreenContexts();
431 UMA_HISTOGRAM_COUNTS_100("GPU.AtExitSurfaceCount",
432 GpuSurfaceTracker::Get()->GetSurfaceCount());
433 UMA_HISTOGRAM_BOOLEAN("GPU.AtExitReceivedMemoryStats",
434 uma_memory_stats_received_);
436 if (uma_memory_stats_received_) {
437 UMA_HISTOGRAM_COUNTS_100("GPU.AtExitManagedMemoryClientCount",
438 uma_memory_stats_.client_count);
439 UMA_HISTOGRAM_COUNTS_100("GPU.AtExitContextGroupCount",
440 uma_memory_stats_.context_group_count);
441 UMA_HISTOGRAM_CUSTOM_COUNTS(
442 "GPU.AtExitMBytesAllocated",
443 uma_memory_stats_.bytes_allocated_current / 1024 / 1024, 1, 2000, 50);
444 UMA_HISTOGRAM_CUSTOM_COUNTS(
445 "GPU.AtExitMBytesAllocatedMax",
446 uma_memory_stats_.bytes_allocated_max / 1024 / 1024, 1, 2000, 50);
447 UMA_HISTOGRAM_CUSTOM_COUNTS(
448 "GPU.AtExitMBytesLimit",
449 uma_memory_stats_.bytes_limit / 1024 / 1024, 1, 2000, 50);
452 std::string message;
453 if (!in_process_) {
454 int exit_code;
455 base::TerminationStatus status = process_->GetTerminationStatus(
456 false /* known_dead */, &exit_code);
457 UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessTerminationStatus",
458 status,
459 base::TERMINATION_STATUS_MAX_ENUM);
461 if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION ||
462 status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
463 UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessExitCode",
464 exit_code,
465 RESULT_CODE_LAST_CODE);
468 switch (status) {
469 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
470 message = "The GPU process exited normally. Everything is okay.";
471 break;
472 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
473 message = base::StringPrintf(
474 "The GPU process exited with code %d.",
475 exit_code);
476 break;
477 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
478 message = "You killed the GPU process! Why?";
479 break;
480 case base::TERMINATION_STATUS_PROCESS_CRASHED:
481 message = "The GPU process crashed!";
482 break;
483 default:
484 break;
488 BrowserThread::PostTask(BrowserThread::UI,
489 FROM_HERE,
490 base::Bind(&GpuProcessHostUIShim::Destroy,
491 host_id_,
492 message));
495 bool GpuProcessHost::Init() {
496 init_start_time_ = base::TimeTicks::Now();
498 TRACE_EVENT_INSTANT0("gpu", "LaunchGpuProcess", TRACE_EVENT_SCOPE_THREAD);
500 std::string channel_id = process_->GetHost()->CreateChannel();
501 if (channel_id.empty())
502 return false;
504 if (in_process_) {
505 DCHECK(g_gpu_main_thread_factory);
506 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
507 command_line->AppendSwitch(switches::kDisableGpuWatchdog);
509 GpuDataManagerImpl* gpu_data_manager = GpuDataManagerImpl::GetInstance();
510 DCHECK(gpu_data_manager);
511 gpu_data_manager->AppendGpuCommandLine(command_line);
513 in_process_gpu_thread_.reset(g_gpu_main_thread_factory(channel_id));
514 in_process_gpu_thread_->Start();
516 OnProcessLaunched(); // Fake a callback that the process is ready.
517 } else if (!LaunchGpuProcess(channel_id)) {
518 return false;
521 if (!Send(new GpuMsg_Initialize()))
522 return false;
524 return true;
527 void GpuProcessHost::RouteOnUIThread(const IPC::Message& message) {
528 BrowserThread::PostTask(
529 BrowserThread::UI,
530 FROM_HERE,
531 base::Bind(&RouteToGpuProcessHostUIShimTask, host_id_, message));
534 bool GpuProcessHost::Send(IPC::Message* msg) {
535 DCHECK(CalledOnValidThread());
536 if (process_->GetHost()->IsChannelOpening()) {
537 queued_messages_.push(msg);
538 return true;
541 bool result = process_->Send(msg);
542 if (!result) {
543 // Channel is hosed, but we may not get destroyed for a while. Send
544 // outstanding channel creation failures now so that the caller can restart
545 // with a new process/channel without waiting.
546 SendOutstandingReplies();
548 return result;
551 void GpuProcessHost::AddFilter(IPC::MessageFilter* filter) {
552 DCHECK(CalledOnValidThread());
553 process_->GetHost()->AddFilter(filter);
556 bool GpuProcessHost::OnMessageReceived(const IPC::Message& message) {
557 DCHECK(CalledOnValidThread());
558 IPC_BEGIN_MESSAGE_MAP(GpuProcessHost, message)
559 IPC_MESSAGE_HANDLER(GpuHostMsg_Initialized, OnInitialized)
560 IPC_MESSAGE_HANDLER(GpuHostMsg_ChannelEstablished, OnChannelEstablished)
561 IPC_MESSAGE_HANDLER(GpuHostMsg_CommandBufferCreated, OnCommandBufferCreated)
562 IPC_MESSAGE_HANDLER(GpuHostMsg_DestroyCommandBuffer, OnDestroyCommandBuffer)
563 IPC_MESSAGE_HANDLER(GpuHostMsg_GpuMemoryBufferCreated,
564 OnGpuMemoryBufferCreated)
565 IPC_MESSAGE_HANDLER(GpuHostMsg_DidCreateOffscreenContext,
566 OnDidCreateOffscreenContext)
567 IPC_MESSAGE_HANDLER(GpuHostMsg_DidLoseContext, OnDidLoseContext)
568 IPC_MESSAGE_HANDLER(GpuHostMsg_DidDestroyOffscreenContext,
569 OnDidDestroyOffscreenContext)
570 IPC_MESSAGE_HANDLER(GpuHostMsg_GpuMemoryUmaStats,
571 OnGpuMemoryUmaStatsReceived)
572 #if defined(OS_MACOSX)
573 IPC_MESSAGE_HANDLER_GENERIC(GpuHostMsg_AcceleratedSurfaceBuffersSwapped,
574 OnAcceleratedSurfaceBuffersSwapped(message))
575 #endif
576 IPC_MESSAGE_HANDLER(GpuHostMsg_DestroyChannel,
577 OnDestroyChannel)
578 IPC_MESSAGE_HANDLER(GpuHostMsg_CacheShader,
579 OnCacheShader)
581 IPC_MESSAGE_UNHANDLED(RouteOnUIThread(message))
582 IPC_END_MESSAGE_MAP()
584 return true;
587 void GpuProcessHost::OnChannelConnected(int32 peer_pid) {
588 TRACE_EVENT0("gpu", "GpuProcessHost::OnChannelConnected");
590 while (!queued_messages_.empty()) {
591 Send(queued_messages_.front());
592 queued_messages_.pop();
596 void GpuProcessHost::EstablishGpuChannel(
597 int client_id,
598 bool share_context,
599 bool allow_future_sync_points,
600 const EstablishChannelCallback& callback) {
601 DCHECK(CalledOnValidThread());
602 TRACE_EVENT0("gpu", "GpuProcessHost::EstablishGpuChannel");
604 // If GPU features are already blacklisted, no need to establish the channel.
605 if (!GpuDataManagerImpl::GetInstance()->GpuAccessAllowed(NULL)) {
606 DVLOG(1) << "GPU blacklisted, refusing to open a GPU channel.";
607 callback.Run(IPC::ChannelHandle(), gpu::GPUInfo());
608 return;
611 if (Send(new GpuMsg_EstablishChannel(
612 client_id, share_context, allow_future_sync_points))) {
613 channel_requests_.push(callback);
614 } else {
615 DVLOG(1) << "Failed to send GpuMsg_EstablishChannel.";
616 callback.Run(IPC::ChannelHandle(), gpu::GPUInfo());
619 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
620 switches::kDisableGpuShaderDiskCache)) {
621 CreateChannelCache(client_id);
625 void GpuProcessHost::CreateViewCommandBuffer(
626 const gfx::GLSurfaceHandle& compositing_surface,
627 int surface_id,
628 int client_id,
629 const GPUCreateCommandBufferConfig& init_params,
630 int route_id,
631 const CreateCommandBufferCallback& callback) {
632 TRACE_EVENT0("gpu", "GpuProcessHost::CreateViewCommandBuffer");
634 DCHECK(CalledOnValidThread());
636 if (!compositing_surface.is_null() &&
637 Send(new GpuMsg_CreateViewCommandBuffer(
638 compositing_surface, surface_id, client_id, init_params, route_id))) {
639 create_command_buffer_requests_.push(callback);
640 surface_refs_.insert(std::make_pair(surface_id,
641 GpuSurfaceTracker::GetInstance()->GetSurfaceRefForSurface(surface_id)));
642 } else {
643 // Could distinguish here between compositing_surface being NULL
644 // and Send failing, if desired.
645 callback.Run(CREATE_COMMAND_BUFFER_FAILED_AND_CHANNEL_LOST);
649 void GpuProcessHost::CreateGpuMemoryBuffer(
650 const gfx::GpuMemoryBufferHandle& handle,
651 const gfx::Size& size,
652 unsigned internalformat,
653 unsigned usage,
654 const CreateGpuMemoryBufferCallback& callback) {
655 TRACE_EVENT0("gpu", "GpuProcessHost::CreateGpuMemoryBuffer");
657 DCHECK(CalledOnValidThread());
659 if (Send(new GpuMsg_CreateGpuMemoryBuffer(
660 handle, size, internalformat, usage))) {
661 create_gpu_memory_buffer_requests_.push(callback);
662 } else {
663 callback.Run(gfx::GpuMemoryBufferHandle());
667 void GpuProcessHost::DestroyGpuMemoryBuffer(
668 const gfx::GpuMemoryBufferHandle& handle,
669 int sync_point) {
670 TRACE_EVENT0("gpu", "GpuProcessHost::DestroyGpuMemoryBuffer");
672 DCHECK(CalledOnValidThread());
674 Send(new GpuMsg_DestroyGpuMemoryBuffer(handle, sync_point));
677 void GpuProcessHost::OnInitialized(bool result, const gpu::GPUInfo& gpu_info) {
678 UMA_HISTOGRAM_BOOLEAN("GPU.GPUProcessInitialized", result);
679 initialized_ = result;
681 if (!initialized_)
682 GpuDataManagerImpl::GetInstance()->OnGpuProcessInitFailure();
683 else if (!in_process_)
684 GpuDataManagerImpl::GetInstance()->UpdateGpuInfo(gpu_info);
687 void GpuProcessHost::OnChannelEstablished(
688 const IPC::ChannelHandle& channel_handle) {
689 TRACE_EVENT0("gpu", "GpuProcessHost::OnChannelEstablished");
691 if (channel_requests_.empty()) {
692 // This happens when GPU process is compromised.
693 RouteOnUIThread(GpuHostMsg_OnLogMessage(
694 logging::LOG_WARNING,
695 "WARNING",
696 "Received a ChannelEstablished message but no requests in queue."));
697 return;
699 EstablishChannelCallback callback = channel_requests_.front();
700 channel_requests_.pop();
702 // Currently if any of the GPU features are blacklisted, we don't establish a
703 // GPU channel.
704 if (!channel_handle.name.empty() &&
705 !GpuDataManagerImpl::GetInstance()->GpuAccessAllowed(NULL)) {
706 Send(new GpuMsg_CloseChannel(channel_handle));
707 callback.Run(IPC::ChannelHandle(), gpu::GPUInfo());
708 RouteOnUIThread(GpuHostMsg_OnLogMessage(
709 logging::LOG_WARNING,
710 "WARNING",
711 "Hardware acceleration is unavailable."));
712 return;
715 callback.Run(channel_handle,
716 GpuDataManagerImpl::GetInstance()->GetGPUInfo());
719 void GpuProcessHost::OnCommandBufferCreated(CreateCommandBufferResult result) {
720 TRACE_EVENT0("gpu", "GpuProcessHost::OnCommandBufferCreated");
722 if (create_command_buffer_requests_.empty())
723 return;
725 CreateCommandBufferCallback callback =
726 create_command_buffer_requests_.front();
727 create_command_buffer_requests_.pop();
728 callback.Run(result);
731 void GpuProcessHost::OnDestroyCommandBuffer(int32 surface_id) {
732 TRACE_EVENT0("gpu", "GpuProcessHost::OnDestroyCommandBuffer");
733 SurfaceRefMap::iterator it = surface_refs_.find(surface_id);
734 if (it != surface_refs_.end()) {
735 surface_refs_.erase(it);
739 void GpuProcessHost::OnGpuMemoryBufferCreated(
740 const gfx::GpuMemoryBufferHandle& handle) {
741 TRACE_EVENT0("gpu", "GpuProcessHost::OnGpuMemoryBufferCreated");
743 if (create_gpu_memory_buffer_requests_.empty())
744 return;
746 CreateGpuMemoryBufferCallback callback =
747 create_gpu_memory_buffer_requests_.front();
748 create_gpu_memory_buffer_requests_.pop();
749 callback.Run(handle);
752 void GpuProcessHost::OnDidCreateOffscreenContext(const GURL& url) {
753 urls_with_live_offscreen_contexts_.insert(url);
756 void GpuProcessHost::OnDidLoseContext(bool offscreen,
757 gpu::error::ContextLostReason reason,
758 const GURL& url) {
759 // TODO(kbr): would be nice to see the "offscreen" flag too.
760 TRACE_EVENT2("gpu", "GpuProcessHost::OnDidLoseContext",
761 "reason", reason,
762 "url",
763 url.possibly_invalid_spec());
765 if (!offscreen || url.is_empty()) {
766 // Assume that the loss of the compositor's or accelerated canvas'
767 // context is a serious event and blame the loss on all live
768 // offscreen contexts. This more robustly handles situations where
769 // the GPU process may not actually detect the context loss in the
770 // offscreen context.
771 BlockLiveOffscreenContexts();
772 return;
775 GpuDataManagerImpl::DomainGuilt guilt;
776 switch (reason) {
777 case gpu::error::kGuilty:
778 guilt = GpuDataManagerImpl::DOMAIN_GUILT_KNOWN;
779 break;
780 case gpu::error::kUnknown:
781 guilt = GpuDataManagerImpl::DOMAIN_GUILT_UNKNOWN;
782 break;
783 case gpu::error::kInnocent:
784 return;
785 default:
786 NOTREACHED();
787 return;
790 GpuDataManagerImpl::GetInstance()->BlockDomainFrom3DAPIs(url, guilt);
793 void GpuProcessHost::OnDidDestroyOffscreenContext(const GURL& url) {
794 urls_with_live_offscreen_contexts_.erase(url);
797 void GpuProcessHost::OnGpuMemoryUmaStatsReceived(
798 const GPUMemoryUmaStats& stats) {
799 TRACE_EVENT0("gpu", "GpuProcessHost::OnGpuMemoryUmaStatsReceived");
800 uma_memory_stats_received_ = true;
801 uma_memory_stats_ = stats;
804 #if defined(OS_MACOSX)
805 void GpuProcessHost::OnAcceleratedSurfaceBuffersSwapped(
806 const IPC::Message& message) {
807 RenderWidgetResizeHelper::Get()->PostGpuProcessMsg(host_id_, message);
809 #endif
811 void GpuProcessHost::OnProcessLaunched() {
812 UMA_HISTOGRAM_TIMES("GPU.GPUProcessLaunchTime",
813 base::TimeTicks::Now() - init_start_time_);
816 void GpuProcessHost::OnProcessCrashed(int exit_code) {
817 SendOutstandingReplies();
818 RecordProcessCrash();
819 GpuDataManagerImpl::GetInstance()->ProcessCrashed(
820 process_->GetTerminationStatus(true /* known_dead */, NULL));
823 GpuProcessHost::GpuProcessKind GpuProcessHost::kind() {
824 return kind_;
827 void GpuProcessHost::ForceShutdown() {
828 // This is only called on the IO thread so no race against the constructor
829 // for another GpuProcessHost.
830 if (g_gpu_process_hosts[kind_] == this)
831 g_gpu_process_hosts[kind_] = NULL;
833 process_->ForceShutdown();
836 void GpuProcessHost::BeginFrameSubscription(
837 int surface_id,
838 base::WeakPtr<RenderWidgetHostViewFrameSubscriber> subscriber) {
839 frame_subscribers_[surface_id] = subscriber;
842 void GpuProcessHost::EndFrameSubscription(int surface_id) {
843 frame_subscribers_.erase(surface_id);
846 bool GpuProcessHost::LaunchGpuProcess(const std::string& channel_id) {
847 if (!(gpu_enabled_ &&
848 GpuDataManagerImpl::GetInstance()->ShouldUseSwiftShader()) &&
849 !hardware_gpu_enabled_) {
850 SendOutstandingReplies();
851 return false;
854 const base::CommandLine& browser_command_line =
855 *base::CommandLine::ForCurrentProcess();
857 base::CommandLine::StringType gpu_launcher =
858 browser_command_line.GetSwitchValueNative(switches::kGpuLauncher);
860 #if defined(OS_LINUX)
861 int child_flags = gpu_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
862 ChildProcessHost::CHILD_NORMAL;
863 #else
864 int child_flags = ChildProcessHost::CHILD_NORMAL;
865 #endif
867 base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
868 if (exe_path.empty())
869 return false;
871 base::CommandLine* cmd_line = new base::CommandLine(exe_path);
872 cmd_line->AppendSwitchASCII(switches::kProcessType, switches::kGpuProcess);
873 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
875 if (kind_ == GPU_PROCESS_KIND_UNSANDBOXED)
876 cmd_line->AppendSwitch(switches::kDisableGpuSandbox);
878 // If you want a browser command-line switch passed to the GPU process
879 // you need to add it to |kSwitchNames| at the beginning of this file.
880 cmd_line->CopySwitchesFrom(browser_command_line, kSwitchNames,
881 arraysize(kSwitchNames));
882 cmd_line->CopySwitchesFrom(
883 browser_command_line, switches::kGpuSwitches, switches::kNumGpuSwitches);
884 cmd_line->CopySwitchesFrom(
885 browser_command_line, switches::kGLSwitchesCopiedFromGpuProcessHost,
886 switches::kGLSwitchesCopiedFromGpuProcessHostNumSwitches);
888 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
889 cmd_line, process_->GetData().id);
891 GpuDataManagerImpl::GetInstance()->AppendGpuCommandLine(cmd_line);
893 if (cmd_line->HasSwitch(switches::kUseGL)) {
894 swiftshader_rendering_ =
895 (cmd_line->GetSwitchValueASCII(switches::kUseGL) == "swiftshader");
898 UMA_HISTOGRAM_BOOLEAN("GPU.GPU.GPUProcessSoftwareRendering",
899 swiftshader_rendering_);
901 // If specified, prepend a launcher program to the command line.
902 if (!gpu_launcher.empty())
903 cmd_line->PrependWrapper(gpu_launcher);
905 process_->Launch(
906 new GpuSandboxedProcessLauncherDelegate(cmd_line,
907 process_->GetHost()),
908 cmd_line);
909 process_launched_ = true;
911 UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLifetimeEvents",
912 LAUNCHED, GPU_PROCESS_LIFETIME_EVENT_MAX);
913 return true;
916 void GpuProcessHost::SendOutstandingReplies() {
917 valid_ = false;
918 // First send empty channel handles for all EstablishChannel requests.
919 while (!channel_requests_.empty()) {
920 EstablishChannelCallback callback = channel_requests_.front();
921 channel_requests_.pop();
922 callback.Run(IPC::ChannelHandle(), gpu::GPUInfo());
925 while (!create_command_buffer_requests_.empty()) {
926 CreateCommandBufferCallback callback =
927 create_command_buffer_requests_.front();
928 create_command_buffer_requests_.pop();
929 callback.Run(CREATE_COMMAND_BUFFER_FAILED_AND_CHANNEL_LOST);
933 void GpuProcessHost::BlockLiveOffscreenContexts() {
934 for (std::multiset<GURL>::iterator iter =
935 urls_with_live_offscreen_contexts_.begin();
936 iter != urls_with_live_offscreen_contexts_.end(); ++iter) {
937 GpuDataManagerImpl::GetInstance()->BlockDomainFrom3DAPIs(
938 *iter, GpuDataManagerImpl::DOMAIN_GUILT_UNKNOWN);
942 void GpuProcessHost::RecordProcessCrash() {
943 // Skip if a GPU process crash was already counted.
944 if (gpu_crash_recorded_)
945 return;
947 // Maximum number of times the GPU process is allowed to crash in a session.
948 // Once this limit is reached, any request to launch the GPU process will
949 // fail.
950 const int kGpuMaxCrashCount = 3;
952 // Last time the GPU process crashed.
953 static base::Time last_gpu_crash_time;
955 bool disable_crash_limit = base::CommandLine::ForCurrentProcess()->HasSwitch(
956 switches::kDisableGpuProcessCrashLimit);
958 // Ending only acts as a failure if the GPU process was actually started and
959 // was intended for actual rendering (and not just checking caps or other
960 // options).
961 if (process_launched_ && kind_ == GPU_PROCESS_KIND_SANDBOXED) {
962 gpu_crash_recorded_ = true;
963 if (swiftshader_rendering_) {
964 UMA_HISTOGRAM_ENUMERATION("GPU.SwiftShaderLifetimeEvents",
965 DIED_FIRST_TIME + swiftshader_crash_count_,
966 GPU_PROCESS_LIFETIME_EVENT_MAX);
968 if (++swiftshader_crash_count_ >= kGpuMaxCrashCount &&
969 !disable_crash_limit) {
970 // SwiftShader is too unstable to use. Disable it for current session.
971 gpu_enabled_ = false;
973 } else {
974 ++gpu_crash_count_;
975 UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLifetimeEvents",
976 std::min(DIED_FIRST_TIME + gpu_crash_count_,
977 GPU_PROCESS_LIFETIME_EVENT_MAX - 1),
978 GPU_PROCESS_LIFETIME_EVENT_MAX);
980 // Allow about 1 GPU crash per hour to be removed from the crash count,
981 // so very occasional crashes won't eventually add up and prevent the
982 // GPU process from launching.
983 ++gpu_recent_crash_count_;
984 base::Time current_time = base::Time::Now();
985 if (crashed_before_) {
986 int hours_different = (current_time - last_gpu_crash_time).InHours();
987 gpu_recent_crash_count_ =
988 std::max(0, gpu_recent_crash_count_ - hours_different);
991 crashed_before_ = true;
992 last_gpu_crash_time = current_time;
994 if ((gpu_recent_crash_count_ >= kGpuMaxCrashCount &&
995 !disable_crash_limit) ||
996 !initialized_) {
997 #if !defined(OS_CHROMEOS)
998 // The GPU process is too unstable to use. Disable it for current
999 // session.
1000 hardware_gpu_enabled_ = false;
1001 GpuDataManagerImpl::GetInstance()->DisableHardwareAcceleration();
1002 #endif
1008 std::string GpuProcessHost::GetShaderPrefixKey() {
1009 if (shader_prefix_key_.empty()) {
1010 gpu::GPUInfo info = GpuDataManagerImpl::GetInstance()->GetGPUInfo();
1012 std::string in_str = GetContentClient()->GetProduct() + "-" +
1013 info.gl_vendor + "-" + info.gl_renderer + "-" +
1014 info.driver_version + "-" + info.driver_vendor;
1016 base::Base64Encode(base::SHA1HashString(in_str), &shader_prefix_key_);
1019 return shader_prefix_key_;
1022 void GpuProcessHost::LoadedShader(const std::string& key,
1023 const std::string& data) {
1024 std::string prefix = GetShaderPrefixKey();
1025 if (!key.compare(0, prefix.length(), prefix))
1026 Send(new GpuMsg_LoadedShader(data));
1029 void GpuProcessHost::CreateChannelCache(int32 client_id) {
1030 TRACE_EVENT0("gpu", "GpuProcessHost::CreateChannelCache");
1032 scoped_refptr<ShaderDiskCache> cache =
1033 ShaderCacheFactory::GetInstance()->Get(client_id);
1034 if (!cache.get())
1035 return;
1037 cache->set_host_id(host_id_);
1039 client_id_to_shader_cache_[client_id] = cache;
1042 void GpuProcessHost::OnDestroyChannel(int32 client_id) {
1043 TRACE_EVENT0("gpu", "GpuProcessHost::OnDestroyChannel");
1044 client_id_to_shader_cache_.erase(client_id);
1047 void GpuProcessHost::OnCacheShader(int32 client_id,
1048 const std::string& key,
1049 const std::string& shader) {
1050 TRACE_EVENT0("gpu", "GpuProcessHost::OnCacheShader");
1051 ClientIdToShaderCacheMap::iterator iter =
1052 client_id_to_shader_cache_.find(client_id);
1053 // If the cache doesn't exist then this is an off the record profile.
1054 if (iter == client_id_to_shader_cache_.end())
1055 return;
1056 iter->second->Cache(GetShaderPrefixKey() + ":" + key, shader);
1059 } // namespace content