cc: Use worker context for one-copy tile initialization.
[chromium-blink-merge.git] / content / browser / gpu / compositor_util.cc
blobe80973bca4a510e3cfe6c196ef1ecd6d035a834e
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, 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 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 // 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;
207 #endif
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,
222 kMaxRasterThreads);
225 bool IsOneCopyUploadEnabled() {
226 if (IsZeroCopyUploadEnabled())
227 return false;
229 const base::CommandLine& command_line =
230 *base::CommandLine::ForCurrentProcess();
231 if (command_line.HasSwitch(switches::kEnableOneCopy))
232 return true;
233 if (command_line.HasSwitch(switches::kDisableOneCopy))
234 return false;
236 #if defined(OS_ANDROID)
237 return false;
238 #endif
239 return true;
242 bool IsZeroCopyUploadEnabled() {
243 const base::CommandLine& command_line =
244 *base::CommandLine::ForCurrentProcess();
245 return command_line.HasSwitch(switches::kEnableZeroCopy);
248 bool IsGpuRasterizationEnabled() {
249 const base::CommandLine& command_line =
250 *base::CommandLine::ForCurrentProcess();
252 if (command_line.HasSwitch(switches::kDisableGpuRasterization))
253 return false;
254 else if (command_line.HasSwitch(switches::kEnableGpuRasterization))
255 return true;
257 if (IsGpuRasterizationBlacklisted()) {
258 return false;
261 #if defined(OS_ANDROID)
262 return true;
263 #endif
265 // explicitly disable GPU rasterization on all non-android devices until we
266 // have full test coverage.
267 return false;
270 bool IsForceGpuRasterizationEnabled() {
271 const base::CommandLine& command_line =
272 *base::CommandLine::ForCurrentProcess();
273 return command_line.HasSwitch(switches::kForceGpuRasterization);
276 bool UseSurfacesEnabled() {
277 #if defined(OS_ANDROID)
278 return true;
279 #endif
280 bool enabled = false;
281 #if defined(USE_AURA) || defined(OS_MACOSX)
282 enabled = true;
283 #endif
285 const base::CommandLine& command_line =
286 *base::CommandLine::ForCurrentProcess();
288 // Flags override.
289 enabled |= command_line.HasSwitch(switches::kUseSurfaces);
290 enabled &= !command_line.HasSwitch(switches::kDisableSurfaces);
291 return enabled;
294 int GpuRasterizationMSAASampleCount() {
295 const base::CommandLine& command_line =
296 *base::CommandLine::ForCurrentProcess();
298 if (!command_line.HasSwitch(switches::kGpuRasterizationMSAASampleCount))
299 #if defined(OS_ANDROID)
300 return 4;
301 #else
302 return 8;
303 #endif
304 std::string string_value = command_line.GetSwitchValueASCII(
305 switches::kGpuRasterizationMSAASampleCount);
306 int msaa_sample_count = 0;
307 if (base::StringToInt(string_value, &msaa_sample_count) &&
308 msaa_sample_count >= kMinMSAASampleCount) {
309 return msaa_sample_count;
310 } else {
311 DLOG(WARNING) << "Failed to parse switch "
312 << switches::kGpuRasterizationMSAASampleCount << ": "
313 << string_value;
314 return 0;
318 base::DictionaryValue* GetFeatureStatus() {
319 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
320 std::string gpu_access_blocked_reason;
321 bool gpu_access_blocked =
322 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
324 base::DictionaryValue* feature_status_dict = new base::DictionaryValue();
326 bool eof = false;
327 for (size_t i = 0; !eof; ++i) {
328 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
329 std::string status;
330 if (gpu_feature_info.disabled) {
331 status = "disabled";
332 if (gpu_feature_info.fallback_to_software)
333 status += "_software";
334 else
335 status += "_off";
336 } else if (gpu_feature_info.blocked ||
337 gpu_access_blocked) {
338 status = "unavailable";
339 if (gpu_feature_info.fallback_to_software)
340 status += "_software";
341 else
342 status += "_off";
343 } else {
344 status = "enabled";
345 if (gpu_feature_info.name == kWebGLFeatureName &&
346 manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_GPU_COMPOSITING))
347 status += "_readback";
348 if (gpu_feature_info.name == kRasterizationFeatureName) {
349 if (IsForceGpuRasterizationEnabled())
350 status += "_force";
352 if (gpu_feature_info.name == kMultipleRasterThreadsFeatureName) {
353 const base::CommandLine& command_line =
354 *base::CommandLine::ForCurrentProcess();
355 if (command_line.HasSwitch(switches::kNumRasterThreads))
356 status += "_force";
358 if (gpu_feature_info.name == kMultipleRasterThreadsFeatureName)
359 status += "_on";
361 if (gpu_feature_info.name == kWebGLFeatureName &&
362 (gpu_feature_info.blocked || gpu_access_blocked) &&
363 manager->ShouldUseSwiftShader()) {
364 status = "unavailable_software";
367 feature_status_dict->SetString(
368 gpu_feature_info.name.c_str(), status.c_str());
370 return feature_status_dict;
373 base::Value* GetProblems() {
374 GpuDataManagerImpl* manager = GpuDataManagerImpl::GetInstance();
375 std::string gpu_access_blocked_reason;
376 bool gpu_access_blocked =
377 !manager->GpuAccessAllowed(&gpu_access_blocked_reason);
379 base::ListValue* problem_list = new base::ListValue();
380 manager->GetBlacklistReasons(problem_list);
382 if (gpu_access_blocked) {
383 base::DictionaryValue* problem = new base::DictionaryValue();
384 problem->SetString("description",
385 "GPU process was unable to boot: " + gpu_access_blocked_reason);
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("all");
390 problem->Set("affectedGpuSettings", disabled_features);
391 problem->SetString("tag", "disabledFeatures");
392 problem_list->Insert(0, problem);
395 bool eof = false;
396 for (size_t i = 0; !eof; ++i) {
397 const GpuFeatureInfo gpu_feature_info = GetGpuFeatureInfo(i, &eof);
398 if (gpu_feature_info.disabled) {
399 base::DictionaryValue* problem = new base::DictionaryValue();
400 problem->SetString(
401 "description", gpu_feature_info.disabled_description);
402 problem->Set("crBugs", new base::ListValue());
403 problem->Set("webkitBugs", new base::ListValue());
404 base::ListValue* disabled_features = new base::ListValue();
405 disabled_features->AppendString(gpu_feature_info.name);
406 problem->Set("affectedGpuSettings", disabled_features);
407 problem->SetString("tag", "disabledFeatures");
408 problem_list->Append(problem);
411 return problem_list;
414 std::vector<std::string> GetDriverBugWorkarounds() {
415 return GpuDataManagerImpl::GetInstance()->GetDriverBugWorkarounds();
418 } // namespace content