ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / gpu / compositor_util.cc
blob18eefc766f4c1314bb2e5debcd4158ab8cfd7830
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/switches.h"
14 #include "content/browser/gpu/gpu_data_manager_impl.h"
15 #include "content/public/common/content_switches.h"
16 #include "gpu/config/gpu_feature_type.h"
18 namespace content {
20 namespace {
22 static bool IsGpuRasterizationBlacklisted() {
23 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
24 return manager->IsFeatureBlacklisted(
25 gpu::GPU_FEATURE_TYPE_GPU_RASTERIZATION);
28 const char* kGpuCompositingFeatureName = "gpu_compositing";
29 const char* kWebGLFeatureName = "webgl";
30 const char* kRasterizationFeatureName = "rasterization";
31 const char* kThreadedRasterizationFeatureName = "threaded_rasterization";
32 const char* kMultipleRasterThreadsFeatureName = "multiple_raster_threads";
34 const int kMinRasterThreads = 1;
35 const int kMaxRasterThreads = 64;
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, either via about:flags or 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 kThreadedRasterizationFeatureName,
148 false,
149 !IsImplSidePaintingEnabled(),
150 "Threaded rasterization has not been enabled or"
151 " is not supported by the current system.",
152 false
155 kMultipleRasterThreadsFeatureName,
156 false,
157 NumberOfRendererRasterThreads() == 1,
158 "Raster is using a single thread.",
159 false
162 DCHECK(index < arraysize(kGpuFeatureInfo));
163 *eof = (index == arraysize(kGpuFeatureInfo) - 1);
164 return kGpuFeatureInfo[index];
167 } // namespace
169 bool IsPinchVirtualViewportEnabled() {
170 const base::CommandLine& command_line =
171 *base::CommandLine::ForCurrentProcess();
173 // Command line switches take precedence over platform default.
174 if (command_line.HasSwitch(cc::switches::kDisablePinchVirtualViewport))
175 return false;
176 if (command_line.HasSwitch(cc::switches::kEnablePinchVirtualViewport))
177 return true;
179 return true;
182 bool IsPropertyTreeVerificationEnabled() {
183 const base::CommandLine& command_line =
184 *base::CommandLine::ForCurrentProcess();
185 return command_line.HasSwitch(cc::switches::kEnablePropertyTreeVerification);
188 bool IsDelegatedRendererEnabled() {
189 const base::CommandLine& command_line =
190 *base::CommandLine::ForCurrentProcess();
191 bool enabled = false;
193 #if defined(USE_AURA) || defined(OS_MACOSX)
194 // Enable on Aura and Mac.
195 enabled = true;
196 #endif
198 // Flags override.
199 enabled |= command_line.HasSwitch(switches::kEnableDelegatedRenderer);
200 enabled &= !command_line.HasSwitch(switches::kDisableDelegatedRenderer);
201 return enabled;
204 bool IsImplSidePaintingEnabled() {
205 const base::CommandLine& command_line =
206 *base::CommandLine::ForCurrentProcess();
207 if (command_line.HasSwitch(switches::kDisableImplSidePainting))
208 return false;
209 return true;
212 int NumberOfRendererRasterThreads() {
213 int num_raster_threads = 1;
215 // Async uploads uses its own thread, so allow an extra thread when async
216 // uploads is not in use.
217 bool allow_extra_thread =
218 IsZeroCopyUploadEnabled() || IsOneCopyUploadEnabled();
219 if (base::SysInfo::NumberOfProcessors() >= 4 && allow_extra_thread)
220 num_raster_threads = 2;
222 int force_num_raster_threads = ForceNumberOfRendererRasterThreads();
223 if (force_num_raster_threads)
224 num_raster_threads = force_num_raster_threads;
226 return num_raster_threads;
229 bool IsOneCopyUploadEnabled() {
230 if (IsZeroCopyUploadEnabled())
231 return false;
233 const base::CommandLine& command_line =
234 *base::CommandLine::ForCurrentProcess();
235 if (command_line.HasSwitch(switches::kEnableOneCopy))
236 return true;
237 if (command_line.HasSwitch(switches::kDisableOneCopy))
238 return false;
240 #if defined(OS_ANDROID)
241 return false;
242 #endif
243 return true;
246 bool IsZeroCopyUploadEnabled() {
247 const base::CommandLine& command_line =
248 *base::CommandLine::ForCurrentProcess();
249 return command_line.HasSwitch(switches::kEnableZeroCopy);
252 int ForceNumberOfRendererRasterThreads() {
253 const base::CommandLine& command_line =
254 *base::CommandLine::ForCurrentProcess();
256 if (!command_line.HasSwitch(switches::kNumRasterThreads))
257 return 0;
258 std::string string_value =
259 command_line.GetSwitchValueASCII(switches::kNumRasterThreads);
260 int force_num_raster_threads = 0;
261 if (base::StringToInt(string_value, &force_num_raster_threads) &&
262 force_num_raster_threads >= kMinRasterThreads &&
263 force_num_raster_threads <= kMaxRasterThreads) {
264 return force_num_raster_threads;
265 } else {
266 DLOG(WARNING) << "Failed to parse switch " <<
267 switches::kNumRasterThreads << ": " << string_value;
268 return 0;
272 bool IsGpuRasterizationEnabled() {
273 const base::CommandLine& command_line =
274 *base::CommandLine::ForCurrentProcess();
276 if (!IsImplSidePaintingEnabled())
277 return false;
279 if (command_line.HasSwitch(switches::kDisableGpuRasterization))
280 return false;
281 else if (command_line.HasSwitch(switches::kEnableGpuRasterization))
282 return true;
284 if (IsGpuRasterizationBlacklisted()) {
285 return false;
288 return true;
291 bool IsForceGpuRasterizationEnabled() {
292 const base::CommandLine& command_line =
293 *base::CommandLine::ForCurrentProcess();
295 if (!IsImplSidePaintingEnabled())
296 return false;
298 return command_line.HasSwitch(switches::kForceGpuRasterization);
301 bool IsThreadedGpuRasterizationEnabled() {
302 if (!IsImplSidePaintingEnabled())
303 return false;
304 if (!IsGpuRasterizationEnabled() && !IsForceGpuRasterizationEnabled())
305 return false;
307 const base::CommandLine& command_line =
308 *base::CommandLine::ForCurrentProcess();
310 if (command_line.HasSwitch(switches::kDisableThreadedGpuRasterization))
311 return false;
313 return true;
316 bool UseSurfacesEnabled() {
317 #if defined(OS_ANDROID)
318 return false;
319 #endif
320 bool enabled = false;
321 #if (defined(USE_AURA) && !defined(OS_CHROMEOS)) || defined(OS_MACOSX)
322 enabled = true;
323 #endif
325 const base::CommandLine& command_line =
326 *base::CommandLine::ForCurrentProcess();
328 // Flags override.
329 enabled |= command_line.HasSwitch(switches::kUseSurfaces);
330 enabled &= !command_line.HasSwitch(switches::kDisableSurfaces);
331 return enabled;
334 int GpuRasterizationMSAASampleCount() {
335 const base::CommandLine& command_line =
336 *base::CommandLine::ForCurrentProcess();
338 if (!command_line.HasSwitch(switches::kGpuRasterizationMSAASampleCount))
339 return 0;
340 std::string string_value = command_line.GetSwitchValueASCII(
341 switches::kGpuRasterizationMSAASampleCount);
342 int msaa_sample_count = 0;
343 if (base::StringToInt(string_value, &msaa_sample_count) &&
344 msaa_sample_count >= kMinMSAASampleCount) {
345 return msaa_sample_count;
346 } else {
347 DLOG(WARNING) << "Failed to parse switch "
348 << switches::kGpuRasterizationMSAASampleCount << ": "
349 << string_value;
350 return 0;
354 base::DictionaryValue* GetFeatureStatus() {
355 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
356 std::string gpu_access_blocked_reason;
357 bool gpu_access_blocked =
358 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
360 base::DictionaryValue* feature_status_dict = new base::DictionaryValue();
362 bool eof = false;
363 for (size_t i = 0; !eof; ++i) {
364 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
365 std::string status;
366 if (gpu_feature_info.disabled) {
367 status = "disabled";
368 if (gpu_feature_info.fallback_to_software)
369 status += "_software";
370 else
371 status += "_off";
372 if (gpu_feature_info.name == kThreadedRasterizationFeatureName)
373 status += "_ok";
374 } else if (gpu_feature_info.blocked ||
375 gpu_access_blocked) {
376 status = "unavailable";
377 if (gpu_feature_info.fallback_to_software)
378 status += "_software";
379 else
380 status += "_off";
381 } else {
382 status = "enabled";
383 if (gpu_feature_info.name == kWebGLFeatureName &&
384 manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING))
385 status += "_readback";
386 if (gpu_feature_info.name == kRasterizationFeatureName) {
387 if (IsForceGpuRasterizationEnabled())
388 status += "_force";
390 if (gpu_feature_info.name == kMultipleRasterThreadsFeatureName) {
391 if (ForceNumberOfRendererRasterThreads() > 0)
392 status += "_force";
394 if (gpu_feature_info.name == kThreadedRasterizationFeatureName ||
395 gpu_feature_info.name == kMultipleRasterThreadsFeatureName)
396 status += "_on";
398 if (gpu_feature_info.name == kWebGLFeatureName &&
399 (gpu_feature_info.blocked || gpu_access_blocked) &&
400 manager->ShouldUseSwiftShader()) {
401 status = "unavailable_software";
404 feature_status_dict->SetString(
405 gpu_feature_info.name.c_str(), status.c_str());
407 return feature_status_dict;
410 base::Value* GetProblems() {
411 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
412 std::string gpu_access_blocked_reason;
413 bool gpu_access_blocked =
414 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
416 base::ListValue* problem_list = new base::ListValue();
417 manager->GetBlacklistReasons(problem_list);
419 if (gpu_access_blocked) {
420 base::DictionaryValue* problem = new base::DictionaryValue();
421 problem->SetString("description",
422 "GPU process was unable to boot: " + gpu_access_blocked_reason);
423 problem->Set("crBugs", new base::ListValue());
424 problem->Set("webkitBugs", new base::ListValue());
425 base::ListValue* disabled_features = new base::ListValue();
426 disabled_features->AppendString("all");
427 problem->Set("affectedGpuSettings", disabled_features);
428 problem->SetString("tag", "disabledFeatures");
429 problem_list->Insert(0, problem);
432 bool eof = false;
433 for (size_t i = 0; !eof; ++i) {
434 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
435 if (gpu_feature_info.disabled) {
436 base::DictionaryValue* problem = new base::DictionaryValue();
437 problem->SetString(
438 "description", gpu_feature_info.disabled_description);
439 problem->Set("crBugs", new base::ListValue());
440 problem->Set("webkitBugs", new base::ListValue());
441 base::ListValue* disabled_features = new base::ListValue();
442 disabled_features->AppendString(gpu_feature_info.name);
443 problem->Set("affectedGpuSettings", disabled_features);
444 problem->SetString("tag", "disabledFeatures");
445 problem_list->Append(problem);
448 return problem_list;
451 std::vector<std::string> GetDriverBugWorkarounds() {
452 return GpuDataManagerImpl::GetInstance()->GetDriverBugWorkarounds();
455 } // namespace content