1 // Copyright (c) 2013 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_data_manager_impl_private.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/sparse_histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/sys_info.h"
16 #include "base/trace_event/trace_event.h"
17 #include "base/version.h"
18 #include "cc/base/switches.h"
19 #include "content/browser/gpu/gpu_process_host.h"
20 #include "content/common/gpu/gpu_messages.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/gpu_data_manager_observer.h"
23 #include "content/public/common/content_client.h"
24 #include "content/public/common/content_constants.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/common/web_preferences.h"
27 #include "gpu/command_buffer/service/gpu_switches.h"
28 #include "gpu/config/gpu_control_list_jsons.h"
29 #include "gpu/config/gpu_driver_bug_workaround_type.h"
30 #include "gpu/config/gpu_feature_type.h"
31 #include "gpu/config/gpu_info_collector.h"
32 #include "gpu/config/gpu_switches.h"
33 #include "gpu/config/gpu_util.h"
34 #include "ui/base/ui_base_switches.h"
35 #include "ui/gl/gl_implementation.h"
36 #include "ui/gl/gl_switches.h"
37 #include "ui/gl/gpu_switching_manager.h"
39 #if defined(OS_MACOSX)
40 #include <ApplicationServices/ApplicationServices.h>
43 #include "base/win/windows_version.h"
45 #if defined(OS_ANDROID)
46 #include "ui/gfx/android/device_display_info.h"
53 enum GpuFeatureStatus
{
54 kGpuFeatureEnabled
= 0,
55 kGpuFeatureBlacklisted
= 1,
56 kGpuFeatureDisabled
= 2, // disabled by user but not blacklisted
71 int GetGpuBlacklistHistogramValueWin(GpuFeatureStatus status
) {
72 static WinSubVersion sub_version
= kNumWinSubVersions
;
73 if (sub_version
== kNumWinSubVersions
) {
74 sub_version
= kWinOthers
;
75 std::string version_str
= base::SysInfo::OperatingSystemVersion();
76 size_t pos
= version_str
.find_first_not_of("0123456789.");
77 if (pos
!= std::string::npos
)
78 version_str
= version_str
.substr(0, pos
);
79 Version
os_version(version_str
);
80 if (os_version
.IsValid() && os_version
.components().size() >= 2) {
81 const std::vector
<uint32_t>& version_numbers
= os_version
.components();
82 if (version_numbers
[0] == 5)
84 else if (version_numbers
[0] == 6 && version_numbers
[1] == 0)
85 sub_version
= kWinVista
;
86 else if (version_numbers
[0] == 6 && version_numbers
[1] == 1)
88 else if (version_numbers
[0] == 6 && version_numbers
[1] == 2)
92 int entry_index
= static_cast<int>(sub_version
) * kGpuFeatureNumStatus
;
94 case kGpuFeatureEnabled
:
96 case kGpuFeatureBlacklisted
:
99 case kGpuFeatureDisabled
:
107 // Send UMA histograms about the enabled features and GPU properties.
108 void UpdateStats(const gpu::GPUInfo
& gpu_info
,
109 const gpu::GpuBlacklist
* blacklist
,
110 const std::set
<int>& blacklisted_features
) {
111 uint32 max_entry_id
= blacklist
->max_entry_id();
112 if (max_entry_id
== 0) {
113 // GPU Blacklist was not loaded. No need to go further.
117 const base::CommandLine
& command_line
=
118 *base::CommandLine::ForCurrentProcess();
119 bool disabled
= false;
121 // Use entry 0 to capture the total number of times that data
122 // was recorded in this histogram in order to have a convenient
123 // denominator to compute blacklist percentages for the rest of the
125 UMA_HISTOGRAM_ENUMERATION("GPU.BlacklistTestResultsPerEntry",
126 0, max_entry_id
+ 1);
128 if (blacklisted_features
.size() != 0) {
129 std::vector
<uint32
> flag_entries
;
130 blacklist
->GetDecisionEntries(&flag_entries
, disabled
);
131 DCHECK_GT(flag_entries
.size(), 0u);
132 for (size_t i
= 0; i
< flag_entries
.size(); ++i
) {
133 UMA_HISTOGRAM_ENUMERATION("GPU.BlacklistTestResultsPerEntry",
134 flag_entries
[i
], max_entry_id
+ 1);
138 // This counts how many users are affected by a disabled entry - this allows
139 // us to understand the impact of an entry before enable it.
140 std::vector
<uint32
> flag_disabled_entries
;
142 blacklist
->GetDecisionEntries(&flag_disabled_entries
, disabled
);
143 for (uint32 disabled_entry
: flag_disabled_entries
) {
144 UMA_HISTOGRAM_ENUMERATION("GPU.BlacklistTestResultsPerDisabledEntry",
145 disabled_entry
, max_entry_id
+ 1);
148 const gpu::GpuFeatureType kGpuFeatures
[] = {
149 gpu::GPU_FEATURE_TYPE_ACCELERATED_2D_CANVAS
,
150 gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
, gpu::GPU_FEATURE_TYPE_WEBGL
};
151 const std::string kGpuBlacklistFeatureHistogramNames
[] = {
152 "GPU.BlacklistFeatureTestResults.Accelerated2dCanvas",
153 "GPU.BlacklistFeatureTestResults.GpuCompositing",
154 "GPU.BlacklistFeatureTestResults.Webgl", };
155 const bool kGpuFeatureUserFlags
[] = {
156 command_line
.HasSwitch(switches::kDisableAccelerated2dCanvas
),
157 command_line
.HasSwitch(switches::kDisableGpu
),
158 command_line
.HasSwitch(switches::kDisableExperimentalWebGL
), };
160 const std::string kGpuBlacklistFeatureHistogramNamesWin
[] = {
161 "GPU.BlacklistFeatureTestResultsWindows.Accelerated2dCanvas",
162 "GPU.BlacklistFeatureTestResultsWindows.GpuCompositing",
163 "GPU.BlacklistFeatureTestResultsWindows.Webgl", };
165 const size_t kNumFeatures
=
166 sizeof(kGpuFeatures
) / sizeof(gpu::GpuFeatureType
);
167 for (size_t i
= 0; i
< kNumFeatures
; ++i
) {
168 // We can't use UMA_HISTOGRAM_ENUMERATION here because the same name is
169 // expected if the macro is used within a loop.
170 GpuFeatureStatus value
= kGpuFeatureEnabled
;
171 if (blacklisted_features
.count(kGpuFeatures
[i
]))
172 value
= kGpuFeatureBlacklisted
;
173 else if (kGpuFeatureUserFlags
[i
])
174 value
= kGpuFeatureDisabled
;
175 base::HistogramBase
* histogram_pointer
= base::LinearHistogram::FactoryGet(
176 kGpuBlacklistFeatureHistogramNames
[i
],
177 1, kGpuFeatureNumStatus
, kGpuFeatureNumStatus
+ 1,
178 base::HistogramBase::kUmaTargetedHistogramFlag
);
179 histogram_pointer
->Add(value
);
181 histogram_pointer
= base::LinearHistogram::FactoryGet(
182 kGpuBlacklistFeatureHistogramNamesWin
[i
],
183 1, kNumWinSubVersions
* kGpuFeatureNumStatus
,
184 kNumWinSubVersions
* kGpuFeatureNumStatus
+ 1,
185 base::HistogramBase::kUmaTargetedHistogramFlag
);
186 histogram_pointer
->Add(GetGpuBlacklistHistogramValueWin(value
));
190 UMA_HISTOGRAM_SPARSE_SLOWLY("GPU.GLResetNotificationStrategy",
191 gpu_info
.gl_reset_notification_strategy
);
194 // Combine the integers into a string, seperated by ','.
195 std::string
IntSetToString(const std::set
<int>& list
) {
197 for (std::set
<int>::const_iterator it
= list
.begin();
198 it
!= list
.end(); ++it
) {
201 rt
+= base::IntToString(*it
);
206 #if defined(OS_MACOSX)
207 void DisplayReconfigCallback(CGDirectDisplayID display
,
208 CGDisplayChangeSummaryFlags flags
,
209 void* gpu_data_manager
) {
210 if (flags
== kCGDisplayBeginConfigurationFlag
)
211 return; // This call contains no information about the display change
213 GpuDataManagerImpl
* manager
=
214 reinterpret_cast<GpuDataManagerImpl
*>(gpu_data_manager
);
218 bool display_changed
= false;
219 uint32_t displayCount
;
220 CGGetActiveDisplayList(0, NULL
, &displayCount
);
221 if (displayCount
!= manager
->GetDisplayCount()) {
222 manager
->SetDisplayCount(displayCount
);
223 display_changed
= true;
227 bool gpu_changed
= false;
228 if (flags
& kCGDisplayAddFlag
) {
229 uint32 vendor_id
, device_id
;
230 if (gpu::CollectGpuID(&vendor_id
, &device_id
) == gpu::kCollectInfoSuccess
) {
231 gpu_changed
= manager
->UpdateActiveGpu(vendor_id
, device_id
);
235 if (display_changed
|| gpu_changed
)
236 manager
->HandleGpuSwitch();
240 // Block all domains' use of 3D APIs for this many milliseconds if
241 // approaching a threshold where system stability might be compromised.
242 const int64 kBlockAllDomainsMs
= 10000;
243 const int kNumResetsWithinDuration
= 1;
245 // Enums for UMA histograms.
246 enum BlockStatusHistogram
{
247 BLOCK_STATUS_NOT_BLOCKED
,
248 BLOCK_STATUS_SPECIFIC_DOMAIN_BLOCKED
,
249 BLOCK_STATUS_ALL_DOMAINS_BLOCKED
,
253 } // namespace anonymous
255 void GpuDataManagerImplPrivate::InitializeForTesting(
256 const std::string
& gpu_blacklist_json
,
257 const gpu::GPUInfo
& gpu_info
) {
258 // This function is for testing only, so disable histograms.
259 update_histograms_
= false;
261 // Prevent all further initialization.
264 InitializeImpl(gpu_blacklist_json
, std::string(), gpu_info
);
267 bool GpuDataManagerImplPrivate::IsFeatureBlacklisted(int feature
) const {
268 #if defined(OS_CHROMEOS)
269 if (feature
== gpu::GPU_FEATURE_TYPE_PANEL_FITTING
&&
270 base::CommandLine::ForCurrentProcess()->HasSwitch(
271 switches::kDisablePanelFitting
)) {
274 #endif // OS_CHROMEOS
275 if (use_swiftshader_
|| ShouldUseWarp()) {
276 // Skia's software rendering is probably more efficient than going through
277 // software emulation of the GPU, so use that.
278 if (feature
== gpu::GPU_FEATURE_TYPE_ACCELERATED_2D_CANVAS
)
283 return (blacklisted_features_
.count(feature
) == 1);
286 bool GpuDataManagerImplPrivate::IsDriverBugWorkaroundActive(int feature
) const {
287 return (gpu_driver_bugs_
.count(feature
) == 1);
290 size_t GpuDataManagerImplPrivate::GetBlacklistedFeatureCount() const {
291 if (use_swiftshader_
|| ShouldUseWarp())
293 return blacklisted_features_
.size();
296 void GpuDataManagerImplPrivate::SetDisplayCount(unsigned int display_count
) {
297 display_count_
= display_count
;
300 unsigned int GpuDataManagerImplPrivate::GetDisplayCount() const {
301 return display_count_
;
304 gpu::GPUInfo
GpuDataManagerImplPrivate::GetGPUInfo() const {
308 void GpuDataManagerImplPrivate::GetGpuProcessHandles(
309 const GpuDataManager::GetGpuProcessHandlesCallback
& callback
) const {
310 GpuProcessHost::GetProcessHandles(callback
);
313 bool GpuDataManagerImplPrivate::GpuAccessAllowed(
314 std::string
* reason
) const {
315 if (use_swiftshader_
|| ShouldUseWarp())
318 if (!gpu_process_accessible_
) {
320 *reason
= "GPU process launch failed.";
325 if (card_blacklisted_
) {
327 *reason
= "GPU access is disabled ";
328 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
329 if (command_line
->HasSwitch(switches::kDisableGpu
))
330 *reason
+= "through commandline switch --disable-gpu.";
332 *reason
+= "in chrome://settings.";
337 // We only need to block GPU process if more features are disallowed other
338 // than those in the preliminary gpu feature flags because the latter work
339 // through renderer commandline switches.
340 std::set
<int> features
= preliminary_blacklisted_features_
;
341 gpu::MergeFeatureSets(&features
, blacklisted_features_
);
342 if (features
.size() > preliminary_blacklisted_features_
.size()) {
344 *reason
= "Features are disabled upon full but not preliminary GPU info.";
349 if (blacklisted_features_
.size() == gpu::NUMBER_OF_GPU_FEATURE_TYPES
) {
350 // On Linux, we use cached GL strings to make blacklist decsions at browser
351 // startup time. We need to launch the GPU process to validate these
352 // strings even if all features are blacklisted. If all GPU features are
353 // disabled, the GPU process will only initialize GL bindings, create a GL
354 // context, and collect full GPU info.
355 #if !defined(OS_LINUX)
357 *reason
= "All GPU features are blacklisted.";
366 void GpuDataManagerImplPrivate::RequestCompleteGpuInfoIfNeeded() {
367 if (complete_gpu_info_already_requested_
|| IsCompleteGpuInfoAvailable())
369 complete_gpu_info_already_requested_
= true;
371 GpuProcessHost::SendOnIO(
373 GpuProcessHost::GPU_PROCESS_KIND_UNSANDBOXED
,
375 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED
,
377 CAUSE_FOR_GPU_LAUNCH_GPUDATAMANAGER_REQUESTCOMPLETEGPUINFOIFNEEDED
,
378 new GpuMsg_CollectGraphicsInfo());
381 bool GpuDataManagerImplPrivate::IsEssentialGpuInfoAvailable() const {
382 if (gpu_info_
.basic_info_state
== gpu::kCollectInfoNone
||
383 gpu_info_
.context_info_state
== gpu::kCollectInfoNone
) {
389 bool GpuDataManagerImplPrivate::IsCompleteGpuInfoAvailable() const {
391 if (gpu_info_
.dx_diagnostics_info_state
== gpu::kCollectInfoNone
)
394 return IsEssentialGpuInfoAvailable();
397 void GpuDataManagerImplPrivate::RequestVideoMemoryUsageStatsUpdate() const {
398 GpuProcessHost::SendOnIO(
399 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED
,
400 CAUSE_FOR_GPU_LAUNCH_NO_LAUNCH
,
401 new GpuMsg_GetVideoMemoryUsageStats());
404 bool GpuDataManagerImplPrivate::ShouldUseSwiftShader() const {
405 return use_swiftshader_
;
408 void GpuDataManagerImplPrivate::RegisterSwiftShaderPath(
409 const base::FilePath
& path
) {
410 swiftshader_path_
= path
;
411 EnableSwiftShaderIfNecessary();
414 bool GpuDataManagerImplPrivate::ShouldUseWarp() const {
416 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp
);
419 void GpuDataManagerImplPrivate::AddObserver(GpuDataManagerObserver
* observer
) {
420 GpuDataManagerImpl::UnlockedSession
session(owner_
);
421 observer_list_
->AddObserver(observer
);
424 void GpuDataManagerImplPrivate::RemoveObserver(
425 GpuDataManagerObserver
* observer
) {
426 GpuDataManagerImpl::UnlockedSession
session(owner_
);
427 observer_list_
->RemoveObserver(observer
);
430 void GpuDataManagerImplPrivate::UnblockDomainFrom3DAPIs(const GURL
& url
) {
431 // This method must do two things:
433 // 1. If the specific domain is blocked, then unblock it.
435 // 2. Reset our notion of how many GPU resets have occurred recently.
436 // This is necessary even if the specific domain was blocked.
437 // Otherwise, if we call Are3DAPIsBlocked with the same domain right
438 // after unblocking it, it will probably still be blocked because of
439 // the recent GPU reset caused by that domain.
441 // These policies could be refined, but at a certain point the behavior
442 // will become difficult to explain.
443 std::string domain
= GetDomainFromURL(url
);
445 blocked_domains_
.erase(domain
);
446 timestamps_of_gpu_resets_
.clear();
449 void GpuDataManagerImplPrivate::DisableGpuWatchdog() {
450 GpuProcessHost::SendOnIO(
451 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED
,
452 CAUSE_FOR_GPU_LAUNCH_NO_LAUNCH
,
453 new GpuMsg_DisableWatchdog
);
456 void GpuDataManagerImplPrivate::SetGLStrings(const std::string
& gl_vendor
,
457 const std::string
& gl_renderer
,
458 const std::string
& gl_version
) {
459 if (gl_vendor
.empty() && gl_renderer
.empty() && gl_version
.empty())
462 // If GPUInfo already got GL strings, do nothing. This is for the rare
463 // situation where GPU process collected GL strings before this call.
464 if (!gpu_info_
.gl_vendor
.empty() ||
465 !gpu_info_
.gl_renderer
.empty() ||
466 !gpu_info_
.gl_version
.empty())
469 gpu::GPUInfo gpu_info
= gpu_info_
;
471 gpu_info
.gl_vendor
= gl_vendor
;
472 gpu_info
.gl_renderer
= gl_renderer
;
473 gpu_info
.gl_version
= gl_version
;
475 gpu::CollectDriverInfoGL(&gpu_info
);
477 UpdateGpuInfo(gpu_info
);
478 UpdateGpuSwitchingManager(gpu_info
);
479 UpdatePreliminaryBlacklistedFeatures();
482 void GpuDataManagerImplPrivate::GetGLStrings(std::string
* gl_vendor
,
483 std::string
* gl_renderer
,
484 std::string
* gl_version
) {
485 DCHECK(gl_vendor
&& gl_renderer
&& gl_version
);
487 *gl_vendor
= gpu_info_
.gl_vendor
;
488 *gl_renderer
= gpu_info_
.gl_renderer
;
489 *gl_version
= gpu_info_
.gl_version
;
492 void GpuDataManagerImplPrivate::Initialize() {
493 TRACE_EVENT0("startup", "GpuDataManagerImpl::Initialize");
495 DVLOG(0) << "GpuDataManagerImpl marked as finalized; skipping Initialize";
499 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
500 if (command_line
->HasSwitch(switches::kSkipGpuDataLoading
))
503 gpu::GPUInfo gpu_info
;
504 if (command_line
->GetSwitchValueASCII(
505 switches::kUseGL
) == gfx::kGLImplementationOSMesaName
) {
506 // If using the OSMesa GL implementation, use fake vendor and device ids to
507 // make sure it never gets blacklisted. This is better than simply
508 // cancelling GPUInfo gathering as it allows us to proceed with loading the
509 // blacklist below which may have non-device specific entries we want to
510 // apply anyways (e.g., OS version blacklisting).
511 gpu_info
.gpu
.vendor_id
= 0xffff;
512 gpu_info
.gpu
.device_id
= 0xffff;
514 // Also declare the driver_vendor to be osmesa to be able to specify
515 // exceptions based on driver_vendor==osmesa for some blacklist rules.
516 gpu_info
.driver_vendor
= gfx::kGLImplementationOSMesaName
;
518 TRACE_EVENT0("startup",
519 "GpuDataManagerImpl::Initialize:CollectBasicGraphicsInfo");
520 gpu::CollectBasicGraphicsInfo(&gpu_info
);
522 #if defined(ARCH_CPU_X86_FAMILY)
523 if (!gpu_info
.gpu
.vendor_id
|| !gpu_info
.gpu
.device_id
) {
524 gpu_info
.context_info_state
= gpu::kCollectInfoNonFatalFailure
;
526 gpu_info
.dx_diagnostics_info_state
= gpu::kCollectInfoNonFatalFailure
;
529 #endif // ARCH_CPU_X86_FAMILY
531 std::string gpu_blacklist_string
;
532 std::string gpu_driver_bug_list_string
;
533 if (!command_line
->HasSwitch(switches::kIgnoreGpuBlacklist
) &&
534 !command_line
->HasSwitch(switches::kUseGpuInTests
)) {
535 gpu_blacklist_string
= gpu::kSoftwareRenderingListJson
;
537 if (!command_line
->HasSwitch(switches::kDisableGpuDriverBugWorkarounds
)) {
538 gpu_driver_bug_list_string
= gpu::kGpuDriverBugListJson
;
540 InitializeImpl(gpu_blacklist_string
,
541 gpu_driver_bug_list_string
,
544 if (command_line
->HasSwitch(switches::kSingleProcess
) ||
545 command_line
->HasSwitch(switches::kInProcessGPU
)) {
546 command_line
->AppendSwitch(switches::kDisableGpuWatchdog
);
547 AppendGpuCommandLine(command_line
);
551 void GpuDataManagerImplPrivate::UpdateGpuInfoHelper() {
552 GetContentClient()->SetGpuInfo(gpu_info_
);
554 if (gpu_blacklist_
) {
555 std::set
<int> features
= gpu_blacklist_
->MakeDecision(
556 gpu::GpuControlList::kOsAny
, std::string(), gpu_info_
);
557 if (update_histograms_
)
558 UpdateStats(gpu_info_
, gpu_blacklist_
.get(), features
);
560 UpdateBlacklistedFeatures(features
);
562 if (gpu_driver_bug_list_
) {
563 gpu_driver_bugs_
= gpu_driver_bug_list_
->MakeDecision(
564 gpu::GpuControlList::kOsAny
, std::string(), gpu_info_
);
566 disabled_extensions_
=
567 JoinString(gpu_driver_bug_list_
->GetDisabledExtensions(), ' ');
569 gpu::GpuDriverBugList::AppendWorkaroundsFromCommandLine(
570 &gpu_driver_bugs_
, *base::CommandLine::ForCurrentProcess());
572 // We have to update GpuFeatureType before notify all the observers.
573 NotifyGpuInfoUpdate();
576 void GpuDataManagerImplPrivate::UpdateGpuInfo(const gpu::GPUInfo
& gpu_info
) {
577 // No further update of gpu_info if falling back to SwiftShader.
578 if (use_swiftshader_
|| ShouldUseWarp())
581 bool was_info_available
= IsCompleteGpuInfoAvailable();
582 gpu::MergeGPUInfo(&gpu_info_
, gpu_info
);
583 if (IsCompleteGpuInfoAvailable()) {
584 complete_gpu_info_already_requested_
= true;
585 } else if (was_info_available
) {
586 // Allow future requests to go through properly.
587 complete_gpu_info_already_requested_
= false;
590 UpdateGpuInfoHelper();
593 void GpuDataManagerImplPrivate::UpdateVideoMemoryUsageStats(
594 const GPUVideoMemoryUsageStats
& video_memory_usage_stats
) {
595 GpuDataManagerImpl::UnlockedSession
session(owner_
);
596 observer_list_
->Notify(FROM_HERE
,
597 &GpuDataManagerObserver::OnVideoMemoryUsageStatsUpdate
,
598 video_memory_usage_stats
);
601 void GpuDataManagerImplPrivate::AppendRendererCommandLine(
602 base::CommandLine
* command_line
) const {
603 DCHECK(command_line
);
605 if (ShouldDisableAcceleratedVideoDecode(command_line
))
606 command_line
->AppendSwitch(switches::kDisableAcceleratedVideoDecode
);
607 #if defined(ENABLE_WEBRTC)
608 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_ENCODE
) &&
609 !command_line
->HasSwitch(switches::kDisableWebRtcHWEncoding
))
610 command_line
->AppendSwitch(switches::kDisableWebRtcHWEncoding
);
613 #if defined(USE_AURA)
614 if (!CanUseGpuBrowserCompositor())
615 command_line
->AppendSwitch(switches::kDisableGpuCompositing
);
619 void GpuDataManagerImplPrivate::AppendGpuCommandLine(
620 base::CommandLine
* command_line
) const {
621 DCHECK(command_line
);
624 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
626 base::FilePath swiftshader_path
=
627 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
628 switches::kSwiftShaderPath
);
629 if (gpu_driver_bugs_
.find(gpu::DISABLE_D3D11
) != gpu_driver_bugs_
.end())
630 command_line
->AppendSwitch(switches::kDisableD3D11
);
631 if (use_swiftshader_
) {
632 command_line
->AppendSwitchASCII(switches::kUseGL
, "swiftshader");
633 if (swiftshader_path
.empty())
634 swiftshader_path
= swiftshader_path_
;
635 } else if ((IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
) ||
636 IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
) ||
637 IsFeatureBlacklisted(
638 gpu::GPU_FEATURE_TYPE_ACCELERATED_2D_CANVAS
)) &&
640 command_line
->AppendSwitchASCII(
641 switches::kUseGL
, gfx::kGLImplementationOSMesaName
);
642 } else if (!use_gl
.empty()) {
643 command_line
->AppendSwitchASCII(switches::kUseGL
, use_gl
);
645 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus())
646 command_line
->AppendSwitchASCII(switches::kSupportsDualGpus
, "true");
648 command_line
->AppendSwitchASCII(switches::kSupportsDualGpus
, "false");
650 if (!swiftshader_path
.empty()) {
651 command_line
->AppendSwitchPath(switches::kSwiftShaderPath
,
655 if (!gpu_driver_bugs_
.empty()) {
656 command_line
->AppendSwitchASCII(switches::kGpuDriverBugWorkarounds
,
657 IntSetToString(gpu_driver_bugs_
));
660 if (ShouldDisableAcceleratedVideoDecode(command_line
)) {
661 command_line
->AppendSwitch(switches::kDisableAcceleratedVideoDecode
);
663 #if defined(ENABLE_WEBRTC)
664 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_ENCODE
) &&
665 !command_line
->HasSwitch(switches::kDisableWebRtcHWEncoding
)) {
666 command_line
->AppendSwitch(switches::kDisableWebRtcHWEncoding
);
670 // Pass GPU and driver information to GPU process. We try to avoid full GPU
671 // info collection at GPU process startup, but we need gpu vendor_id,
672 // device_id, driver_vendor, driver_version for deciding whether we need to
673 // collect full info (on Linux) and for crash reporting purpose.
674 command_line
->AppendSwitchASCII(switches::kGpuVendorID
,
675 base::StringPrintf("0x%04x", gpu_info_
.gpu
.vendor_id
));
676 command_line
->AppendSwitchASCII(switches::kGpuDeviceID
,
677 base::StringPrintf("0x%04x", gpu_info_
.gpu
.device_id
));
678 command_line
->AppendSwitchASCII(switches::kGpuDriverVendor
,
679 gpu_info_
.driver_vendor
);
680 command_line
->AppendSwitchASCII(switches::kGpuDriverVersion
,
681 gpu_info_
.driver_version
);
684 command_line
->AppendSwitch(switches::kUseWarp
);
687 void GpuDataManagerImplPrivate::AppendPluginCommandLine(
688 base::CommandLine
* command_line
) const {
689 DCHECK(command_line
);
691 #if defined(OS_MACOSX)
692 // TODO(jbauman): Add proper blacklist support for core animation plugins so
693 // special-casing this video card won't be necessary. See
694 // http://crbug.com/134015
695 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
)) {
696 if (!command_line
->HasSwitch(
697 switches::kDisableCoreAnimationPlugins
))
698 command_line
->AppendSwitch(
699 switches::kDisableCoreAnimationPlugins
);
704 void GpuDataManagerImplPrivate::UpdateRendererWebPrefs(
705 WebPreferences
* prefs
) const {
708 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
)) {
709 prefs
->experimental_webgl_enabled
= false;
710 prefs
->pepper_3d_enabled
= false;
712 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH3D
))
713 prefs
->flash_3d_enabled
= false;
714 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH_STAGE3D
)) {
715 prefs
->flash_stage3d_enabled
= false;
716 prefs
->flash_stage3d_baseline_enabled
= false;
718 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH_STAGE3D_BASELINE
))
719 prefs
->flash_stage3d_baseline_enabled
= false;
720 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_ACCELERATED_2D_CANVAS
))
721 prefs
->accelerated_2d_canvas_enabled
= false;
722 // TODO(senorblanco): The renderer shouldn't have an extra setting
723 // for this, but should rely on extension availability.
724 // Note that |gl_multisampling_enabled| only affects the decoder's
725 // default framebuffer allocation, which does not support
726 // multisampled_render_to_texture, only msaa with explicit resolve.
727 if (IsDriverBugWorkaroundActive(
728 gpu::DISABLE_CHROMIUM_FRAMEBUFFER_MULTISAMPLE
) ||
729 (IsDriverBugWorkaroundActive(gpu::DISABLE_MULTIMONITOR_MULTISAMPLING
) &&
731 prefs
->gl_multisampling_enabled
= false;
733 #if defined(USE_AURA)
734 if (!CanUseGpuBrowserCompositor()) {
735 prefs
->accelerated_2d_canvas_enabled
= false;
736 prefs
->pepper_3d_enabled
= false;
740 const base::CommandLine
* command_line
=
741 base::CommandLine::ForCurrentProcess();
742 if (!ShouldDisableAcceleratedVideoDecode(command_line
) &&
743 !command_line
->HasSwitch(switches::kDisableAcceleratedVideoDecode
)) {
744 prefs
->pepper_accelerated_video_decode_enabled
= true;
748 void GpuDataManagerImplPrivate::DisableHardwareAcceleration() {
749 card_blacklisted_
= true;
751 for (int i
= 0; i
< gpu::NUMBER_OF_GPU_FEATURE_TYPES
; ++i
)
752 blacklisted_features_
.insert(i
);
754 EnableWarpIfNecessary();
755 EnableSwiftShaderIfNecessary();
756 NotifyGpuInfoUpdate();
759 std::string
GpuDataManagerImplPrivate::GetBlacklistVersion() const {
761 return gpu_blacklist_
->version();
765 std::string
GpuDataManagerImplPrivate::GetDriverBugListVersion() const {
766 if (gpu_driver_bug_list_
)
767 return gpu_driver_bug_list_
->version();
771 void GpuDataManagerImplPrivate::GetBlacklistReasons(
772 base::ListValue
* reasons
) const {
774 gpu_blacklist_
->GetReasons(reasons
, "disabledFeatures");
775 if (gpu_driver_bug_list_
)
776 gpu_driver_bug_list_
->GetReasons(reasons
, "workarounds");
779 std::vector
<std::string
>
780 GpuDataManagerImplPrivate::GetDriverBugWorkarounds() const {
781 std::vector
<std::string
> workarounds
;
782 for (std::set
<int>::const_iterator it
= gpu_driver_bugs_
.begin();
783 it
!= gpu_driver_bugs_
.end(); ++it
) {
784 workarounds
.push_back(
785 gpu::GpuDriverBugWorkaroundTypeToString(
786 static_cast<gpu::GpuDriverBugWorkaroundType
>(*it
)));
791 void GpuDataManagerImplPrivate::AddLogMessage(
792 int level
, const std::string
& header
, const std::string
& message
) {
793 log_messages_
.push_back(LogMessage(level
, header
, message
));
796 void GpuDataManagerImplPrivate::ProcessCrashed(
797 base::TerminationStatus exit_code
) {
798 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
799 // Unretained is ok, because it's posted to UI thread, the thread
800 // where the singleton GpuDataManagerImpl lives until the end.
801 BrowserThread::PostTask(
804 base::Bind(&GpuDataManagerImpl::ProcessCrashed
,
805 base::Unretained(owner_
),
810 gpu_info_
.process_crash_count
= GpuProcessHost::gpu_crash_count();
811 GpuDataManagerImpl::UnlockedSession
session(owner_
);
812 observer_list_
->Notify(
813 FROM_HERE
, &GpuDataManagerObserver::OnGpuProcessCrashed
, exit_code
);
817 base::ListValue
* GpuDataManagerImplPrivate::GetLogMessages() const {
818 base::ListValue
* value
= new base::ListValue
;
819 for (size_t ii
= 0; ii
< log_messages_
.size(); ++ii
) {
820 base::DictionaryValue
* dict
= new base::DictionaryValue();
821 dict
->SetInteger("level", log_messages_
[ii
].level
);
822 dict
->SetString("header", log_messages_
[ii
].header
);
823 dict
->SetString("message", log_messages_
[ii
].message
);
829 void GpuDataManagerImplPrivate::HandleGpuSwitch() {
830 GpuDataManagerImpl::UnlockedSession
session(owner_
);
831 // Notify observers in the browser process.
832 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched();
833 // Pass the notification to the GPU process to notify observers there.
834 GpuProcessHost::SendOnIO(
835 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED
,
836 CAUSE_FOR_GPU_LAUNCH_NO_LAUNCH
,
837 new GpuMsg_GpuSwitched
);
840 bool GpuDataManagerImplPrivate::UpdateActiveGpu(
841 uint32 vendor_id
, uint32 device_id
) {
842 if (gpu_info_
.gpu
.vendor_id
== vendor_id
&&
843 gpu_info_
.gpu
.device_id
== device_id
) {
844 // The primary GPU is active.
845 if (gpu_info_
.gpu
.active
)
847 gpu_info_
.gpu
.active
= true;
848 for (size_t ii
= 0; ii
< gpu_info_
.secondary_gpus
.size(); ++ii
)
849 gpu_info_
.secondary_gpus
[ii
].active
= false;
851 // A secondary GPU is active.
852 for (size_t ii
= 0; ii
< gpu_info_
.secondary_gpus
.size(); ++ii
) {
853 if (gpu_info_
.secondary_gpus
[ii
].vendor_id
== vendor_id
&&
854 gpu_info_
.secondary_gpus
[ii
].device_id
== device_id
) {
855 if (gpu_info_
.secondary_gpus
[ii
].active
)
857 gpu_info_
.secondary_gpus
[ii
].active
= true;
859 gpu_info_
.secondary_gpus
[ii
].active
= false;
862 gpu_info_
.gpu
.active
= false;
864 UpdateGpuInfoHelper();
868 bool GpuDataManagerImplPrivate::CanUseGpuBrowserCompositor() const {
869 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
870 switches::kDisableGpuCompositing
))
874 if (ShouldUseSwiftShader())
876 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
))
882 bool GpuDataManagerImplPrivate::ShouldDisableAcceleratedVideoDecode(
883 const base::CommandLine
* command_line
) const {
884 // Make sure that we initialize the experiment first to make sure that
885 // statistics are bucket correctly in all cases.
886 // This experiment is temporary and will be removed once enough data
887 // to resolve crbug/442039 has been collected.
888 const std::string group_name
= base::FieldTrialList::FindFullName(
889 "DisableAcceleratedVideoDecode");
890 if (command_line
->HasSwitch(switches::kDisableAcceleratedVideoDecode
)) {
891 // It was already disabled on the command line.
894 if (IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_DECODE
))
896 if (group_name
== "Disabled")
901 void GpuDataManagerImplPrivate::GetDisabledExtensions(
902 std::string
* disabled_extensions
) const {
903 DCHECK(disabled_extensions
);
905 *disabled_extensions
= disabled_extensions_
;
908 void GpuDataManagerImplPrivate::BlockDomainFrom3DAPIs(
909 const GURL
& url
, GpuDataManagerImpl::DomainGuilt guilt
) {
910 BlockDomainFrom3DAPIsAtTime(url
, guilt
, base::Time::Now());
913 bool GpuDataManagerImplPrivate::Are3DAPIsBlocked(const GURL
& url
,
914 int render_process_id
,
916 ThreeDAPIType requester
) {
917 bool blocked
= Are3DAPIsBlockedAtTime(url
, base::Time::Now()) !=
918 GpuDataManagerImpl::DOMAIN_BLOCK_STATUS_NOT_BLOCKED
;
920 // Unretained is ok, because it's posted to UI thread, the thread
921 // where the singleton GpuDataManagerImpl lives until the end.
922 BrowserThread::PostTask(
923 BrowserThread::UI
, FROM_HERE
,
924 base::Bind(&GpuDataManagerImpl::Notify3DAPIBlocked
,
925 base::Unretained(owner_
), url
, render_process_id
,
926 render_view_id
, requester
));
932 void GpuDataManagerImplPrivate::DisableDomainBlockingFor3DAPIsForTesting() {
933 domain_blocking_enabled_
= false;
937 GpuDataManagerImplPrivate
* GpuDataManagerImplPrivate::Create(
938 GpuDataManagerImpl
* owner
) {
939 return new GpuDataManagerImplPrivate(owner
);
942 GpuDataManagerImplPrivate::GpuDataManagerImplPrivate(
943 GpuDataManagerImpl
* owner
)
944 : complete_gpu_info_already_requested_(false),
945 observer_list_(new GpuDataManagerObserverList
),
946 use_swiftshader_(false),
948 card_blacklisted_(false),
949 update_histograms_(true),
951 domain_blocking_enabled_(true),
954 gpu_process_accessible_(true),
957 const base::CommandLine
* command_line
=
958 base::CommandLine::ForCurrentProcess();
959 if (command_line
->HasSwitch(switches::kDisableGpu
))
960 DisableHardwareAcceleration();
962 #if defined(OS_MACOSX)
963 CGGetActiveDisplayList (0, NULL
, &display_count_
);
964 CGDisplayRegisterReconfigurationCallback(DisplayReconfigCallback
, owner_
);
968 if (command_line
->HasSwitch(switches::kDisableDomainBlockingFor3DAPIs
)) {
969 domain_blocking_enabled_
= false;
973 GpuDataManagerImplPrivate::~GpuDataManagerImplPrivate() {
974 #if defined(OS_MACOSX)
975 CGDisplayRemoveReconfigurationCallback(DisplayReconfigCallback
, owner_
);
979 void GpuDataManagerImplPrivate::InitializeImpl(
980 const std::string
& gpu_blacklist_json
,
981 const std::string
& gpu_driver_bug_list_json
,
982 const gpu::GPUInfo
& gpu_info
) {
983 const bool log_gpu_control_list_decisions
=
984 base::CommandLine::ForCurrentProcess()->HasSwitch(
985 switches::kLogGpuControlListDecisions
);
987 if (!gpu_blacklist_json
.empty()) {
988 gpu_blacklist_
.reset(gpu::GpuBlacklist::Create());
989 if (log_gpu_control_list_decisions
)
990 gpu_blacklist_
->enable_control_list_logging("gpu_blacklist");
991 bool success
= gpu_blacklist_
->LoadList(
992 gpu_blacklist_json
, gpu::GpuControlList::kCurrentOsOnly
);
995 if (!gpu_driver_bug_list_json
.empty()) {
996 gpu_driver_bug_list_
.reset(gpu::GpuDriverBugList::Create());
997 if (log_gpu_control_list_decisions
)
998 gpu_driver_bug_list_
->enable_control_list_logging("gpu_driver_bug_list");
999 bool success
= gpu_driver_bug_list_
->LoadList(
1000 gpu_driver_bug_list_json
, gpu::GpuControlList::kCurrentOsOnly
);
1004 gpu_info_
= gpu_info
;
1005 UpdateGpuInfo(gpu_info
);
1006 UpdateGpuSwitchingManager(gpu_info
);
1007 UpdatePreliminaryBlacklistedFeatures();
1010 void GpuDataManagerImplPrivate::UpdateBlacklistedFeatures(
1011 const std::set
<int>& features
) {
1012 blacklisted_features_
= features
;
1014 // Force disable using the GPU for these features, even if they would
1015 // otherwise be allowed.
1016 if (card_blacklisted_
) {
1017 blacklisted_features_
.insert(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
);
1018 blacklisted_features_
.insert(gpu::GPU_FEATURE_TYPE_WEBGL
);
1021 EnableWarpIfNecessary();
1022 EnableSwiftShaderIfNecessary();
1025 void GpuDataManagerImplPrivate::UpdatePreliminaryBlacklistedFeatures() {
1026 preliminary_blacklisted_features_
= blacklisted_features_
;
1029 void GpuDataManagerImplPrivate::UpdateGpuSwitchingManager(
1030 const gpu::GPUInfo
& gpu_info
) {
1031 ui::GpuSwitchingManager::GetInstance()->SetGpuCount(
1032 gpu_info
.secondary_gpus
.size() + 1);
1034 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) {
1035 if (gpu_driver_bugs_
.count(gpu::FORCE_DISCRETE_GPU
) == 1)
1036 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
1037 else if (gpu_driver_bugs_
.count(gpu::FORCE_INTEGRATED_GPU
) == 1)
1038 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
1042 void GpuDataManagerImplPrivate::NotifyGpuInfoUpdate() {
1043 observer_list_
->Notify(FROM_HERE
, &GpuDataManagerObserver::OnGpuInfoUpdate
);
1046 void GpuDataManagerImplPrivate::EnableSwiftShaderIfNecessary() {
1047 if (ShouldUseWarp())
1050 if (!GpuAccessAllowed(NULL
) ||
1051 blacklisted_features_
.count(gpu::GPU_FEATURE_TYPE_WEBGL
)) {
1052 if (!swiftshader_path_
.empty() &&
1053 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1054 switches::kDisableSoftwareRasterizer
))
1055 use_swiftshader_
= true;
1059 void GpuDataManagerImplPrivate::EnableWarpIfNecessary() {
1063 // We should only use WARP if we are unable to use the regular GPU for
1064 // compositing, and if we in Metro mode.
1065 use_warp_
= base::CommandLine::ForCurrentProcess()->HasSwitch(
1066 switches::kViewerConnect
) &&
1067 !CanUseGpuBrowserCompositor();
1071 void GpuDataManagerImplPrivate::ForceWarpModeForTesting() {
1075 std::string
GpuDataManagerImplPrivate::GetDomainFromURL(
1076 const GURL
& url
) const {
1077 // For the moment, we just use the host, or its IP address, as the
1078 // entry in the set, rather than trying to figure out the top-level
1079 // domain. This does mean that a.foo.com and b.foo.com will be
1080 // treated independently in the blocking of a given domain, but it
1081 // would require a third-party library to reliably figure out the
1082 // top-level domain from a URL.
1083 if (!url
.has_host()) {
1084 return std::string();
1090 void GpuDataManagerImplPrivate::BlockDomainFrom3DAPIsAtTime(
1092 GpuDataManagerImpl::DomainGuilt guilt
,
1093 base::Time at_time
) {
1094 if (!domain_blocking_enabled_
)
1097 std::string domain
= GetDomainFromURL(url
);
1099 DomainBlockEntry
& entry
= blocked_domains_
[domain
];
1100 entry
.last_guilt
= guilt
;
1101 timestamps_of_gpu_resets_
.push_back(at_time
);
1104 GpuDataManagerImpl::DomainBlockStatus
1105 GpuDataManagerImplPrivate::Are3DAPIsBlockedAtTime(
1106 const GURL
& url
, base::Time at_time
) const {
1107 if (!domain_blocking_enabled_
)
1108 return GpuDataManagerImpl::DOMAIN_BLOCK_STATUS_NOT_BLOCKED
;
1110 // Note: adjusting the policies in this code will almost certainly
1111 // require adjusting the associated unit tests.
1112 std::string domain
= GetDomainFromURL(url
);
1114 DomainBlockMap::const_iterator iter
= blocked_domains_
.find(domain
);
1115 if (iter
!= blocked_domains_
.end()) {
1116 // Err on the side of caution, and assume that if a particular
1117 // domain shows up in the block map, it's there for a good
1118 // reason and don't let its presence there automatically expire.
1120 UMA_HISTOGRAM_ENUMERATION("GPU.BlockStatusForClient3DAPIs",
1121 BLOCK_STATUS_SPECIFIC_DOMAIN_BLOCKED
,
1124 return GpuDataManagerImpl::DOMAIN_BLOCK_STATUS_BLOCKED
;
1127 // Look at the timestamps of the recent GPU resets to see if there are
1128 // enough within the threshold which would cause us to blacklist all
1129 // domains. This doesn't need to be overly precise -- if time goes
1130 // backward due to a system clock adjustment, that's fine.
1132 // TODO(kbr): make this pay attention to the TDR thresholds in the
1133 // Windows registry, but make sure it continues to be testable.
1135 std::list
<base::Time
>::iterator iter
= timestamps_of_gpu_resets_
.begin();
1136 int num_resets_within_timeframe
= 0;
1137 while (iter
!= timestamps_of_gpu_resets_
.end()) {
1138 base::Time time
= *iter
;
1139 base::TimeDelta delta_t
= at_time
- time
;
1141 // If this entry has "expired", just remove it.
1142 if (delta_t
.InMilliseconds() > kBlockAllDomainsMs
) {
1143 iter
= timestamps_of_gpu_resets_
.erase(iter
);
1147 ++num_resets_within_timeframe
;
1151 if (num_resets_within_timeframe
>= kNumResetsWithinDuration
) {
1152 UMA_HISTOGRAM_ENUMERATION("GPU.BlockStatusForClient3DAPIs",
1153 BLOCK_STATUS_ALL_DOMAINS_BLOCKED
,
1156 return GpuDataManagerImpl::DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED
;
1160 UMA_HISTOGRAM_ENUMERATION("GPU.BlockStatusForClient3DAPIs",
1161 BLOCK_STATUS_NOT_BLOCKED
,
1164 return GpuDataManagerImpl::DOMAIN_BLOCK_STATUS_NOT_BLOCKED
;
1167 int64
GpuDataManagerImplPrivate::GetBlockAllDomainsDurationInMs() const {
1168 return kBlockAllDomainsMs
;
1171 void GpuDataManagerImplPrivate::Notify3DAPIBlocked(const GURL
& url
,
1172 int render_process_id
,
1174 ThreeDAPIType requester
) {
1175 GpuDataManagerImpl::UnlockedSession
session(owner_
);
1176 observer_list_
->Notify(FROM_HERE
, &GpuDataManagerObserver::DidBlock3DAPIs
,
1177 url
, render_process_id
, render_view_id
, requester
);
1180 void GpuDataManagerImplPrivate::OnGpuProcessInitFailure() {
1181 gpu_process_accessible_
= false;
1182 gpu_info_
.context_info_state
= gpu::kCollectInfoFatalFailure
;
1184 gpu_info_
.dx_diagnostics_info_state
= gpu::kCollectInfoFatalFailure
;
1186 complete_gpu_info_already_requested_
= true;
1187 // Some observers might be waiting.
1188 NotifyGpuInfoUpdate();
1191 } // namespace content