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/compositor_util.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/sys_info.h"
12 #include "build/build_config.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/switches.h"
15 #include "content/browser/gpu/gpu_data_manager_impl.h"
16 #include "content/public/common/content_switches.h"
17 #include "gpu/config/gpu_feature_type.h"
23 static bool IsGpuRasterizationBlacklisted() {
24 GpuDataManagerImpl
* manager
= GpuDataManagerImpl::GetInstance();
25 return manager
->IsFeatureBlacklisted(
26 gpu::GPU_FEATURE_TYPE_GPU_RASTERIZATION
);
29 const char* kGpuCompositingFeatureName
= "gpu_compositing";
30 const char* kWebGLFeatureName
= "webgl";
31 const char* kRasterizationFeatureName
= "rasterization";
32 const char* kMultipleRasterThreadsFeatureName
= "multiple_raster_threads";
34 const int kMinRasterThreads
= 1;
35 const int kMaxRasterThreads
= 4;
37 const int kMinMSAASampleCount
= 0;
39 struct GpuFeatureInfo
{
43 std::string disabled_description
;
44 bool fallback_to_software
;
47 const GpuFeatureInfo
GetGpuFeatureInfo(size_t index
, bool* eof
) {
48 const base::CommandLine
& command_line
=
49 *base::CommandLine::ForCurrentProcess();
50 GpuDataManagerImpl
* manager
= GpuDataManagerImpl::GetInstance();
52 const GpuFeatureInfo kGpuFeatureInfo
[] = {
55 manager
->IsFeatureBlacklisted(
56 gpu::GPU_FEATURE_TYPE_ACCELERATED_2D_CANVAS
),
57 command_line
.HasSwitch(switches::kDisableAccelerated2dCanvas
) ||
58 !GpuDataManagerImpl::GetInstance()->
59 GetGPUInfo().SupportsAccelerated2dCanvas(),
60 "Accelerated 2D canvas is unavailable: either disabled at the command"
61 " line or not supported by the current system.",
65 kGpuCompositingFeatureName
,
66 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
),
67 command_line
.HasSwitch(switches::kDisableGpuCompositing
),
68 "Gpu compositing has been disabled, either via about:flags or"
69 " command line. The browser will fall back to software compositing"
70 " and hardware acceleration will be unavailable.",
75 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
),
76 command_line
.HasSwitch(switches::kDisableExperimentalWebGL
),
77 "WebGL has been disabled, either via about:flags or command line.",
82 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH3D
),
83 command_line
.HasSwitch(switches::kDisableFlash3d
),
84 "Using 3d in flash has been disabled, either via about:flags or"
90 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH_STAGE3D
),
91 command_line
.HasSwitch(switches::kDisableFlashStage3d
),
92 "Using Stage3d in Flash has been disabled, either via about:flags or"
97 "flash_stage3d_baseline",
98 manager
->IsFeatureBlacklisted(
99 gpu::GPU_FEATURE_TYPE_FLASH_STAGE3D_BASELINE
) ||
100 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_FLASH_STAGE3D
),
101 command_line
.HasSwitch(switches::kDisableFlashStage3d
),
102 "Using Stage3d Baseline profile in Flash has been disabled, either"
103 " via about:flags or command line.",
108 manager
->IsFeatureBlacklisted(
109 gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_DECODE
),
110 command_line
.HasSwitch(switches::kDisableAcceleratedVideoDecode
),
111 "Accelerated video decode has been disabled, either via about:flags"
115 #if defined(ENABLE_WEBRTC)
118 manager
->IsFeatureBlacklisted(
119 gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_ENCODE
),
120 command_line
.HasSwitch(switches::kDisableWebRtcHWEncoding
),
121 "Accelerated video encode has been disabled, either via about:flags"
126 #if defined(OS_CHROMEOS)
129 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_PANEL_FITTING
),
130 command_line
.HasSwitch(switches::kDisablePanelFitting
),
131 "Panel fitting has been disabled, either via about:flags or command"
137 kRasterizationFeatureName
,
138 IsGpuRasterizationBlacklisted() &&
139 !IsGpuRasterizationEnabled() && !IsForceGpuRasterizationEnabled(),
140 !IsGpuRasterizationEnabled() && !IsForceGpuRasterizationEnabled() &&
141 !IsGpuRasterizationBlacklisted(),
142 "Accelerated rasterization has been disabled, either via about:flags"
147 kMultipleRasterThreadsFeatureName
,
149 NumberOfRendererRasterThreads() == 1,
150 "Raster is using a single thread.",
154 DCHECK(index
< arraysize(kGpuFeatureInfo
));
155 *eof
= (index
== arraysize(kGpuFeatureInfo
) - 1);
156 return kGpuFeatureInfo
[index
];
161 bool IsPropertyTreeVerificationEnabled() {
162 const base::CommandLine
& command_line
=
163 *base::CommandLine::ForCurrentProcess();
164 return command_line
.HasSwitch(cc::switches::kEnablePropertyTreeVerification
);
167 bool IsDelegatedRendererEnabled() {
168 const base::CommandLine
& command_line
=
169 *base::CommandLine::ForCurrentProcess();
170 bool enabled
= false;
172 #if defined(USE_AURA) || defined(OS_MACOSX)
173 // Enable on Aura and Mac.
178 enabled
|= command_line
.HasSwitch(switches::kEnableDelegatedRenderer
);
179 enabled
&= !command_line
.HasSwitch(switches::kDisableDelegatedRenderer
);
183 int NumberOfRendererRasterThreads() {
184 int num_processors
= base::SysInfo::NumberOfProcessors();
186 #if defined(OS_ANDROID)
187 // Android may report 6 to 8 CPUs for big.LITTLE configurations.
188 // Limit the number of raster threads based on maximum of 4 big cores.
189 num_processors
= std::min(num_processors
, 4);
192 int num_raster_threads
= num_processors
/ 2;
194 // Async uploads is used when neither zero-copy nor one-copy is enabled and
195 // it uses its own thread, so reduce the number of raster threads when async
196 // uploads is in use.
197 bool async_uploads_is_used
=
198 !IsZeroCopyUploadEnabled() && !IsOneCopyUploadEnabled();
199 if (async_uploads_is_used
)
200 --num_raster_threads
;
202 #if defined(OS_ANDROID)
203 // Limit the number of raster threads to 1 on Android.
204 // TODO(reveman): Remove this when we have a better mechanims to prevent
205 // pre-paint raster work from slowing down non-raster work. crbug.com/504515
206 num_raster_threads
= 1;
209 const base::CommandLine
& command_line
=
210 *base::CommandLine::ForCurrentProcess();
212 if (command_line
.HasSwitch(switches::kNumRasterThreads
)) {
213 std::string string_value
= command_line
.GetSwitchValueASCII(
214 switches::kNumRasterThreads
);
215 if (!base::StringToInt(string_value
, &num_raster_threads
)) {
216 DLOG(WARNING
) << "Failed to parse switch " <<
217 switches::kNumRasterThreads
<< ": " << string_value
;
221 return cc::MathUtil::ClampToRange(num_raster_threads
, kMinRasterThreads
,
225 bool IsOneCopyUploadEnabled() {
226 if (IsZeroCopyUploadEnabled())
229 const base::CommandLine
& command_line
=
230 *base::CommandLine::ForCurrentProcess();
231 if (command_line
.HasSwitch(switches::kEnableOneCopy
))
233 if (command_line
.HasSwitch(switches::kDisableOneCopy
))
236 #if defined(OS_ANDROID)
242 bool IsZeroCopyUploadEnabled() {
243 const base::CommandLine
& command_line
=
244 *base::CommandLine::ForCurrentProcess();
245 return command_line
.HasSwitch(switches::kEnableZeroCopy
);
248 bool IsPersistentGpuMemoryBufferEnabled() {
249 // Zero copy currently doesn't take advantage of persistent buffers.
250 if (IsZeroCopyUploadEnabled())
252 const auto& command_line
= *base::CommandLine::ForCurrentProcess();
253 return !command_line
.HasSwitch(switches::kDisablePersistentGpuMemoryBuffer
);
256 bool IsGpuRasterizationEnabled() {
257 const base::CommandLine
& command_line
=
258 *base::CommandLine::ForCurrentProcess();
260 if (command_line
.HasSwitch(switches::kDisableGpuRasterization
))
262 else if (command_line
.HasSwitch(switches::kEnableGpuRasterization
))
265 if (IsGpuRasterizationBlacklisted()) {
269 #if defined(OS_ANDROID)
273 // explicitly disable GPU rasterization on all non-android devices until we
274 // have full test coverage.
278 bool IsForceGpuRasterizationEnabled() {
279 const base::CommandLine
& command_line
=
280 *base::CommandLine::ForCurrentProcess();
281 return command_line
.HasSwitch(switches::kForceGpuRasterization
);
284 bool UseSurfacesEnabled() {
285 #if defined(OS_ANDROID)
288 bool enabled
= false;
289 #if defined(USE_AURA) || defined(OS_MACOSX)
293 const base::CommandLine
& command_line
=
294 *base::CommandLine::ForCurrentProcess();
297 enabled
|= command_line
.HasSwitch(switches::kUseSurfaces
);
298 enabled
&= !command_line
.HasSwitch(switches::kDisableSurfaces
);
302 int GpuRasterizationMSAASampleCount() {
303 const base::CommandLine
& command_line
=
304 *base::CommandLine::ForCurrentProcess();
306 if (!command_line
.HasSwitch(switches::kGpuRasterizationMSAASampleCount
))
307 #if defined(OS_ANDROID)
310 // Desktop platforms will compute this automatically based on DPI.
313 std::string string_value
= command_line
.GetSwitchValueASCII(
314 switches::kGpuRasterizationMSAASampleCount
);
315 int msaa_sample_count
= 0;
316 if (base::StringToInt(string_value
, &msaa_sample_count
) &&
317 msaa_sample_count
>= kMinMSAASampleCount
) {
318 return msaa_sample_count
;
320 DLOG(WARNING
) << "Failed to parse switch "
321 << switches::kGpuRasterizationMSAASampleCount
<< ": "
327 base::DictionaryValue
* GetFeatureStatus() {
328 GpuDataManagerImpl
* manager
= GpuDataManagerImpl::GetInstance();
329 std::string gpu_access_blocked_reason
;
330 bool gpu_access_blocked
=
331 !manager
->GpuAccessAllowed(&gpu_access_blocked_reason
);
333 base::DictionaryValue
* feature_status_dict
= new base::DictionaryValue();
336 for (size_t i
= 0; !eof
; ++i
) {
337 const GpuFeatureInfo gpu_feature_info
= GetGpuFeatureInfo(i
, &eof
);
339 if (gpu_feature_info
.disabled
) {
341 if (gpu_feature_info
.fallback_to_software
)
342 status
+= "_software";
345 } else if (gpu_feature_info
.blocked
||
346 gpu_access_blocked
) {
347 status
= "unavailable";
348 if (gpu_feature_info
.fallback_to_software
)
349 status
+= "_software";
354 if (gpu_feature_info
.name
== kWebGLFeatureName
&&
355 manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING
))
356 status
+= "_readback";
357 if (gpu_feature_info
.name
== kRasterizationFeatureName
) {
358 if (IsForceGpuRasterizationEnabled())
361 if (gpu_feature_info
.name
== kMultipleRasterThreadsFeatureName
) {
362 const base::CommandLine
& command_line
=
363 *base::CommandLine::ForCurrentProcess();
364 if (command_line
.HasSwitch(switches::kNumRasterThreads
))
367 if (gpu_feature_info
.name
== kMultipleRasterThreadsFeatureName
)
370 if (gpu_feature_info
.name
== kWebGLFeatureName
&&
371 (gpu_feature_info
.blocked
|| gpu_access_blocked
) &&
372 manager
->ShouldUseSwiftShader()) {
373 status
= "unavailable_software";
376 feature_status_dict
->SetString(
377 gpu_feature_info
.name
.c_str(), status
.c_str());
379 return feature_status_dict
;
382 base::Value
* GetProblems() {
383 GpuDataManagerImpl
* manager
= GpuDataManagerImpl::GetInstance();
384 std::string gpu_access_blocked_reason
;
385 bool gpu_access_blocked
=
386 !manager
->GpuAccessAllowed(&gpu_access_blocked_reason
);
388 base::ListValue
* problem_list
= new base::ListValue();
389 manager
->GetBlacklistReasons(problem_list
);
391 if (gpu_access_blocked
) {
392 base::DictionaryValue
* problem
= new base::DictionaryValue();
393 problem
->SetString("description",
394 "GPU process was unable to boot: " + gpu_access_blocked_reason
);
395 problem
->Set("crBugs", new base::ListValue());
396 problem
->Set("webkitBugs", new base::ListValue());
397 base::ListValue
* disabled_features
= new base::ListValue();
398 disabled_features
->AppendString("all");
399 problem
->Set("affectedGpuSettings", disabled_features
);
400 problem
->SetString("tag", "disabledFeatures");
401 problem_list
->Insert(0, problem
);
405 for (size_t i
= 0; !eof
; ++i
) {
406 const GpuFeatureInfo gpu_feature_info
= GetGpuFeatureInfo(i
, &eof
);
407 if (gpu_feature_info
.disabled
) {
408 base::DictionaryValue
* problem
= new base::DictionaryValue();
410 "description", gpu_feature_info
.disabled_description
);
411 problem
->Set("crBugs", new base::ListValue());
412 problem
->Set("webkitBugs", new base::ListValue());
413 base::ListValue
* disabled_features
= new base::ListValue();
414 disabled_features
->AppendString(gpu_feature_info
.name
);
415 problem
->Set("affectedGpuSettings", disabled_features
);
416 problem
->SetString("tag", "disabledFeatures");
417 problem_list
->Append(problem
);
423 std::vector
<std::string
> GetDriverBugWorkarounds() {
424 return GpuDataManagerImpl::GetInstance()->GetDriverBugWorkarounds();
427 } // namespace content