[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / gpu / compositor_util.cc
blobf2dc9a35c31b001890d3141de7e406d2aa6a5059
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"
19 namespace content {
21 namespace {
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 {
40 std::string name;
41 bool blocked;
42 bool disabled;
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[] = {
54 "2d_canvas",
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.",
62 true
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.",
71 true
74 kWebGLFeatureName,
75 manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL),
76 command_line.HasSwitch(switches::kDisableExperimentalWebGL),
77 "WebGL has been disabled via the command line.",
78 false
81 "flash_3d",
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"
85 " command line.",
86 true
89 "flash_stage3d",
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"
93 " command line.",
94 true
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.",
104 true
107 "video_decode",
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"
112 " or command line.",
113 true
115 #if defined(ENABLE_WEBRTC)
117 "video_encode",
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"
122 " or command line.",
123 true
125 #endif
126 #if defined(OS_CHROMEOS)
128 "panel_fitting",
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"
132 " line.",
133 false
135 #endif
137 kRasterizationFeatureName,
138 IsGpuRasterizationBlacklisted() &&
139 !IsGpuRasterizationEnabled() && !IsForceGpuRasterizationEnabled(),
140 !IsGpuRasterizationEnabled() && !IsForceGpuRasterizationEnabled() &&
141 !IsGpuRasterizationBlacklisted(),
142 "Accelerated rasterization has been disabled, either via about:flags"
143 " or command line.",
144 true
147 kMultipleRasterThreadsFeatureName,
148 false,
149 NumberOfRendererRasterThreads() == 1,
150 "Raster is using a single thread.",
151 false
154 DCHECK(index < arraysize(kGpuFeatureInfo));
155 *eof = (index == arraysize(kGpuFeatureInfo) - 1);
156 return kGpuFeatureInfo[index];
159 } // namespace
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.
174 enabled = true;
175 #endif
177 // Flags override.
178 enabled |= command_line.HasSwitch(switches::kEnableDelegatedRenderer);
179 enabled &= !command_line.HasSwitch(switches::kDisableDelegatedRenderer);
180 return enabled;
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);
190 #endif
192 int num_raster_threads = num_processors / 2;
194 #if defined(OS_ANDROID)
195 // Limit the number of raster threads to 1 on Android.
196 // TODO(reveman): Remove this when we have a better mechanims to prevent
197 // pre-paint raster work from slowing down non-raster work. crbug.com/504515
198 num_raster_threads = 1;
199 #endif
201 const base::CommandLine& command_line =
202 *base::CommandLine::ForCurrentProcess();
204 if (command_line.HasSwitch(switches::kNumRasterThreads)) {
205 std::string string_value = command_line.GetSwitchValueASCII(
206 switches::kNumRasterThreads);
207 if (!base::StringToInt(string_value, &num_raster_threads)) {
208 DLOG(WARNING) << "Failed to parse switch " <<
209 switches::kNumRasterThreads << ": " << string_value;
213 return cc::MathUtil::ClampToRange(num_raster_threads, kMinRasterThreads,
214 kMaxRasterThreads);
217 bool IsZeroCopyUploadEnabled() {
218 const base::CommandLine& command_line =
219 *base::CommandLine::ForCurrentProcess();
220 return command_line.HasSwitch(switches::kEnableZeroCopy);
223 bool IsPersistentGpuMemoryBufferEnabled() {
224 // Zero copy currently doesn't take advantage of persistent buffers.
225 if (IsZeroCopyUploadEnabled())
226 return false;
227 const auto& command_line = *base::CommandLine::ForCurrentProcess();
228 return command_line.HasSwitch(switches::kEnablePersistentGpuMemoryBuffer);
231 bool IsGpuRasterizationEnabled() {
232 const base::CommandLine& command_line =
233 *base::CommandLine::ForCurrentProcess();
235 if (command_line.HasSwitch(switches::kDisableGpuRasterization))
236 return false;
237 else if (command_line.HasSwitch(switches::kEnableGpuRasterization))
238 return true;
240 if (IsGpuRasterizationBlacklisted()) {
241 return false;
244 #if defined(OS_ANDROID)
245 return true;
246 #endif
248 // explicitly disable GPU rasterization on all non-android devices until we
249 // have full test coverage.
250 return false;
253 bool IsForceGpuRasterizationEnabled() {
254 const base::CommandLine& command_line =
255 *base::CommandLine::ForCurrentProcess();
256 return command_line.HasSwitch(switches::kForceGpuRasterization);
259 bool UseSurfacesEnabled() {
260 #if defined(OS_ANDROID)
261 return true;
262 #endif
263 bool enabled = false;
264 #if defined(USE_AURA) || defined(OS_MACOSX)
265 enabled = true;
266 #endif
268 const base::CommandLine& command_line =
269 *base::CommandLine::ForCurrentProcess();
271 // Flags override.
272 enabled |= command_line.HasSwitch(switches::kUseSurfaces);
273 enabled &= !command_line.HasSwitch(switches::kDisableSurfaces);
274 return enabled;
277 int GpuRasterizationMSAASampleCount() {
278 const base::CommandLine& command_line =
279 *base::CommandLine::ForCurrentProcess();
281 if (!command_line.HasSwitch(switches::kGpuRasterizationMSAASampleCount))
282 #if defined(OS_ANDROID)
283 return 4;
284 #else
285 // Desktop platforms will compute this automatically based on DPI.
286 return -1;
287 #endif
288 std::string string_value = command_line.GetSwitchValueASCII(
289 switches::kGpuRasterizationMSAASampleCount);
290 int msaa_sample_count = 0;
291 if (base::StringToInt(string_value, &msaa_sample_count) &&
292 msaa_sample_count >= kMinMSAASampleCount) {
293 return msaa_sample_count;
294 } else {
295 DLOG(WARNING) << "Failed to parse switch "
296 << switches::kGpuRasterizationMSAASampleCount << ": "
297 << string_value;
298 return 0;
302 base::DictionaryValue* GetFeatureStatus() {
303 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
304 std::string gpu_access_blocked_reason;
305 bool gpu_access_blocked =
306 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
308 base::DictionaryValue* feature_status_dict = new base::DictionaryValue();
310 bool eof = false;
311 for (size_t i = 0; !eof; ++i) {
312 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
313 std::string status;
314 if (gpu_feature_info.disabled) {
315 status = "disabled";
316 if (gpu_feature_info.fallback_to_software)
317 status += "_software";
318 else
319 status += "_off";
320 } else if (gpu_feature_info.blocked ||
321 gpu_access_blocked) {
322 status = "unavailable";
323 if (gpu_feature_info.fallback_to_software)
324 status += "_software";
325 else
326 status += "_off";
327 } else {
328 status = "enabled";
329 if (gpu_feature_info.name == kWebGLFeatureName &&
330 manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING))
331 status += "_readback";
332 if (gpu_feature_info.name == kRasterizationFeatureName) {
333 if (IsForceGpuRasterizationEnabled())
334 status += "_force";
336 if (gpu_feature_info.name == kMultipleRasterThreadsFeatureName) {
337 const base::CommandLine& command_line =
338 *base::CommandLine::ForCurrentProcess();
339 if (command_line.HasSwitch(switches::kNumRasterThreads))
340 status += "_force";
342 if (gpu_feature_info.name == kMultipleRasterThreadsFeatureName)
343 status += "_on";
345 if (gpu_feature_info.name == kWebGLFeatureName &&
346 (gpu_feature_info.blocked || gpu_access_blocked) &&
347 manager->ShouldUseSwiftShader()) {
348 status = "unavailable_software";
351 feature_status_dict->SetString(
352 gpu_feature_info.name.c_str(), status.c_str());
354 return feature_status_dict;
357 base::Value* GetProblems() {
358 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
359 std::string gpu_access_blocked_reason;
360 bool gpu_access_blocked =
361 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
363 base::ListValue* problem_list = new base::ListValue();
364 manager->GetBlacklistReasons(problem_list);
366 if (gpu_access_blocked) {
367 base::DictionaryValue* problem = new base::DictionaryValue();
368 problem->SetString("description",
369 "GPU process was unable to boot: " + gpu_access_blocked_reason);
370 problem->Set("crBugs", new base::ListValue());
371 problem->Set("webkitBugs", new base::ListValue());
372 base::ListValue* disabled_features = new base::ListValue();
373 disabled_features->AppendString("all");
374 problem->Set("affectedGpuSettings", disabled_features);
375 problem->SetString("tag", "disabledFeatures");
376 problem_list->Insert(0, problem);
379 bool eof = false;
380 for (size_t i = 0; !eof; ++i) {
381 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
382 if (gpu_feature_info.disabled) {
383 base::DictionaryValue* problem = new base::DictionaryValue();
384 problem->SetString(
385 "description", gpu_feature_info.disabled_description);
386 problem->Set("crBugs", new base::ListValue());
387 problem->Set("webkitBugs", new base::ListValue());
388 base::ListValue* disabled_features = new base::ListValue();
389 disabled_features->AppendString(gpu_feature_info.name);
390 problem->Set("affectedGpuSettings", disabled_features);
391 problem->SetString("tag", "disabledFeatures");
392 problem_list->Append(problem);
395 return problem_list;
398 std::vector<std::string> GetDriverBugWorkarounds() {
399 return GpuDataManagerImpl::GetInstance()->GetDriverBugWorkarounds();
402 } // namespace content