blob: 4136e694dacc2900b07cde1aecb4acafa0e92693 [file] [log] [blame]
geofflangdf7fff2d42016-11-11 00:34:031// Copyright (c) 2016 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.
4
5#include "gpu/command_buffer/service/service_utils.h"
6
Jonathan Backer7bc44592018-04-25 19:59:507#include <string>
8
geofflang3ab039d2017-02-22 15:33:589#include "base/command_line.h"
Corentin Walleze660b152020-07-15 16:07:5410#include "base/logging.h"
Jonathan Backer7bc44592018-04-25 19:59:5011#include "base/strings/string_number_conversions.h"
Jiawei Shao04b8bcd2020-11-14 01:29:4712#include "base/strings/string_split.h"
Sean Gilhuly3e84fc82020-01-22 22:06:4513#include "build/build_config.h"
Jonathan Backerf48f9202021-01-14 18:39:4614#include "build/chromeos_buildflags.h"
geofflangdf7fff2d42016-11-11 00:34:0315#include "gpu/command_buffer/common/gles2_cmd_utils.h"
Geoff Lang619081b2017-08-31 19:23:3116#include "gpu/command_buffer/service/context_group.h"
Geoff Langd72f1e92017-10-12 23:31:3817#include "gpu/command_buffer/service/gpu_switches.h"
Peng Huangdc3c68f42019-09-15 02:27:2218#include "gpu/config/gpu_finch_features.h"
Sean Gilhulye5342322019-11-08 16:40:5319#include "skia/buildflags.h"
geofflang3ab039d2017-02-22 15:33:5820#include "ui/gl/gl_switches.h"
Jonah Ryan-Davis570ab8b92019-07-08 20:03:3321#include "ui/gl/gl_utils.h"
geofflangdf7fff2d42016-11-11 00:34:0322
Geoff Lang619081b2017-08-31 19:23:3123#if defined(USE_EGL)
24#include "ui/gl/gl_surface_egl.h"
25#endif // defined(USE_EGL)
26
geofflangdf7fff2d42016-11-11 00:34:0327namespace gpu {
28namespace gles2 {
29
Jonathan Backer7bc44592018-04-25 19:59:5030namespace {
31
32bool GetUintFromSwitch(const base::CommandLine* command_line,
33 const base::StringPiece& switch_string,
34 uint32_t* value) {
35 if (!command_line->HasSwitch(switch_string))
36 return false;
37 std::string switch_value(command_line->GetSwitchValueASCII(switch_string));
38 return base::StringToUint(switch_value, value);
39}
40
41} // namespace
42
geofflangdf7fff2d42016-11-11 00:34:0343gl::GLContextAttribs GenerateGLContextAttribs(
Antoine Labourfeab2392017-12-21 20:28:3944 const ContextCreationAttribs& attribs_helper,
Geoff Lang619081b2017-08-31 19:23:3145 const ContextGroup* context_group) {
Khushalb2c140b2018-07-09 20:21:1646 return GenerateGLContextAttribs(attribs_helper,
47 context_group->use_passthrough_cmd_decoder());
48}
49
50gl::GLContextAttribs GenerateGLContextAttribs(
51 const ContextCreationAttribs& attribs_helper,
52 bool use_passthrough_cmd_decoder) {
geofflangdf7fff2d42016-11-11 00:34:0353 gl::GLContextAttribs attribs;
54 attribs.gpu_preference = attribs_helper.gpu_preference;
Khushalb2c140b2018-07-09 20:21:1655 if (use_passthrough_cmd_decoder) {
geofflangdf7fff2d42016-11-11 00:34:0356 attribs.bind_generates_resource = attribs_helper.bind_generates_resource;
57 attribs.webgl_compatibility_context =
58 IsWebGLContextType(attribs_helper.context_type);
geofflang3ab039d2017-02-22 15:33:5859
Jonathan Ross6b1ca232020-07-31 16:43:3660 // Always use the global texture and semaphore share group for the
61 // passthrough command decoder
geofflang3ab039d2017-02-22 15:33:5862 attribs.global_texture_share_group = true;
Jonathan Ross6b1ca232020-07-31 16:43:3663 attribs.global_semaphore_share_group = true;
geofflang3ab039d2017-02-22 15:33:5864
Geoff Lang7f82ef42017-09-23 17:56:0265 attribs.robust_resource_initialization = true;
Jiajia Qin69aeeb22017-11-09 05:16:1866 attribs.robust_buffer_access = true;
Geoff Lang7f82ef42017-09-23 17:56:0267
geofflang3ab039d2017-02-22 15:33:5868 // Request a specific context version instead of always 3.0
Geoff Langd0c755c2021-01-18 21:58:1669 if (IsWebGL2OrES3ContextType(attribs_helper.context_type)) {
geofflang3ab039d2017-02-22 15:33:5870 attribs.client_major_es_version = 3;
71 attribs.client_minor_es_version = 0;
72 } else {
73 DCHECK(IsWebGL1OrES2ContextType(attribs_helper.context_type));
74 attribs.client_major_es_version = 2;
75 attribs.client_minor_es_version = 0;
76 }
77 } else {
78 attribs.client_major_es_version = 3;
79 attribs.client_minor_es_version = 0;
geofflangdf7fff2d42016-11-11 00:34:0380 }
geofflang3ab039d2017-02-22 15:33:5881
82 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
83 switches::kDisableES3GLContext)) {
84 // Forcefully disable ES3 contexts
85 attribs.client_major_es_version = 2;
86 attribs.client_minor_es_version = 0;
87 }
88
Geoff Langd0c755c2021-01-18 21:58:1689 if (IsES31ForTestingContextType(attribs_helper.context_type)) {
90 // Forcefully disable ES 3.1 contexts. Tests create contexts by initializing
91 // the attributes directly.
92 attribs.client_major_es_version = 2;
93 attribs.client_minor_es_version = 0;
94 }
95
geofflangdf7fff2d42016-11-11 00:34:0396 return attribs;
97}
98
Geoff Langd72f1e92017-10-12 23:31:3899bool UsePassthroughCommandDecoder(const base::CommandLine* command_line) {
Jonah Ryan-Davis570ab8b92019-07-08 20:03:33100 return gl::UsePassthroughCommandDecoder(command_line);
Geoff Langd72f1e92017-10-12 23:31:38101}
102
Geoff Lang619081b2017-08-31 19:23:31103bool PassthroughCommandDecoderSupported() {
Jonah Ryan-Davis77b562dc2020-07-17 19:11:23104 return gl::PassthroughCommandDecoderSupported();
Jonathan Backer7bc44592018-04-25 19:59:50105}
106
107GpuPreferences ParseGpuPreferences(const base::CommandLine* command_line) {
108 GpuPreferences gpu_preferences;
109 gpu_preferences.compile_shader_always_succeeds =
110 command_line->HasSwitch(switches::kCompileShaderAlwaysSucceeds);
111 gpu_preferences.disable_gl_error_limit =
112 command_line->HasSwitch(switches::kDisableGLErrorLimit);
113 gpu_preferences.disable_glsl_translator =
114 command_line->HasSwitch(switches::kDisableGLSLTranslator);
115 gpu_preferences.disable_shader_name_hashing =
116 command_line->HasSwitch(switches::kDisableShaderNameHashing);
117 gpu_preferences.enable_gpu_command_logging =
118 command_line->HasSwitch(switches::kEnableGPUCommandLogging);
119 gpu_preferences.enable_gpu_debugging =
120 command_line->HasSwitch(switches::kEnableGPUDebugging);
121 gpu_preferences.enable_gpu_service_logging_gpu =
122 command_line->HasSwitch(switches::kEnableGPUServiceLoggingGPU);
123 gpu_preferences.enable_gpu_driver_debug_logging =
124 command_line->HasSwitch(switches::kEnableGPUDriverDebugLogging);
125 gpu_preferences.disable_gpu_program_cache =
126 command_line->HasSwitch(switches::kDisableGpuProgramCache);
127 gpu_preferences.enforce_gl_minimums =
128 command_line->HasSwitch(switches::kEnforceGLMinimums);
129 if (GetUintFromSwitch(command_line, switches::kForceGpuMemAvailableMb,
Wez4f8b5432019-12-31 06:40:11130 &gpu_preferences.force_gpu_mem_available_bytes)) {
131 gpu_preferences.force_gpu_mem_available_bytes *= 1024 * 1024;
132 }
133 if (GetUintFromSwitch(
134 command_line, switches::kForceGpuMemDiscardableLimitMb,
135 &gpu_preferences.force_gpu_mem_discardable_limit_bytes)) {
136 gpu_preferences.force_gpu_mem_discardable_limit_bytes *= 1024 * 1024;
Jonathan Backer7bc44592018-04-25 19:59:50137 }
Sergey Ulanov689c31f2020-01-24 23:21:16138 GetUintFromSwitch(command_line, switches::kForceMaxTextureSize,
139 &gpu_preferences.force_max_texture_size);
Jonathan Backer7bc44592018-04-25 19:59:50140 if (GetUintFromSwitch(command_line, switches::kGpuProgramCacheSizeKb,
141 &gpu_preferences.gpu_program_cache_size)) {
142 gpu_preferences.gpu_program_cache_size *= 1024;
143 }
144 gpu_preferences.disable_gpu_shader_disk_cache =
145 command_line->HasSwitch(switches::kDisableGpuShaderDiskCache);
146 gpu_preferences.enable_threaded_texture_mailboxes =
147 command_line->HasSwitch(switches::kEnableThreadedTextureMailboxes);
148 gpu_preferences.gl_shader_interm_output =
149 command_line->HasSwitch(switches::kGLShaderIntermOutput);
Jonathan Backer7bc44592018-04-25 19:59:50150 gpu_preferences.enable_gpu_service_logging =
151 command_line->HasSwitch(switches::kEnableGPUServiceLogging);
152 gpu_preferences.enable_gpu_service_tracing =
153 command_line->HasSwitch(switches::kEnableGPUServiceTracing);
154 gpu_preferences.use_passthrough_cmd_decoder =
155 gpu::gles2::UsePassthroughCommandDecoder(command_line);
Corentin Wallez5cbffcc2020-07-08 10:23:57156 gpu_preferences.ignore_gpu_blocklist =
Corentin Walleze660b152020-07-15 16:07:54157 command_line->HasSwitch(switches::kIgnoreGpuBlocklist);
Kai Ninomiya328e07a2018-08-23 01:08:16158 gpu_preferences.enable_webgpu =
shrekshaoc6539252021-04-29 19:21:56159 command_line->HasSwitch(switches::kEnableUnsafeWebGPU) ||
Corentin Walleza059a3a22021-07-30 09:44:54160 base::FeatureList::IsEnabled(features::kWebGPUService);
Austin Enga442d5832022-01-15 03:00:15161 gpu_preferences.enable_unsafe_webgpu =
Corentin Wallez721293f2021-06-03 22:42:00162 command_line->HasSwitch(switches::kEnableUnsafeWebGPU);
Austin Eng80eb7962022-02-07 20:35:37163 gpu_preferences.use_webgpu_adapter = ParseWebGPUAdapterName(command_line);
Austin Engbac59e692021-05-13 16:46:54164 if (command_line->HasSwitch(switches::kEnableDawnBackendValidation)) {
165 auto value = command_line->GetSwitchValueASCII(
166 switches::kEnableDawnBackendValidation);
167 if (value.empty() || value == "full") {
168 gpu_preferences.enable_dawn_backend_validation =
169 DawnBackendValidationLevel::kFull;
170 } else if (value == "partial") {
171 gpu_preferences.enable_dawn_backend_validation =
172 DawnBackendValidationLevel::kPartial;
173 }
174 }
Jiawei Shao04b8bcd2020-11-14 01:29:47175 if (command_line->HasSwitch(switches::kEnableDawnFeatures)) {
176 gpu_preferences.enabled_dawn_features_list = base::SplitString(
177 command_line->GetSwitchValueASCII(switches::kEnableDawnFeatures), ",",
178 base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
179 }
180 if (command_line->HasSwitch(switches::kDisableDawnFeatures)) {
181 gpu_preferences.disabled_dawn_features_list = base::SplitString(
182 command_line->GetSwitchValueASCII(switches::kDisableDawnFeatures), ",",
183 base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
184 }
Sean Gilhuly3e84fc82020-01-22 22:06:45185 gpu_preferences.gr_context_type = ParseGrContextType();
Peng Huang07ed18642020-06-09 00:58:17186 gpu_preferences.use_vulkan = ParseVulkanImplementationName(command_line);
Sergey Ulanovefa2af12022-06-28 23:17:47187
188#if BUILDFLAG(IS_FUCHSIA)
189 // Vulkan Surface is not used on Fuchsia.
190 gpu_preferences.disable_vulkan_surface = true;
191#else
Peng Huang556da902019-03-22 17:49:40192 gpu_preferences.disable_vulkan_surface =
193 command_line->HasSwitch(switches::kDisableVulkanSurface);
Sergey Ulanovefa2af12022-06-28 23:17:47194#endif
Vasiliy Telezhnikov533c5642019-12-03 15:10:16195
196 gpu_preferences.enable_gpu_blocked_time_metric =
197 command_line->HasSwitch(switches::kEnableGpuBlockedTime);
198
Jonathan Backer7bc44592018-04-25 19:59:50199 return gpu_preferences;
200}
Geoff Lang619081b2017-08-31 19:23:31201
Sean Gilhuly3e84fc82020-01-22 22:06:45202GrContextType ParseGrContextType() {
203#if BUILDFLAG(SKIA_USE_DAWN)
204 if (base::FeatureList::IsEnabled(features::kSkiaDawn))
205 return GrContextType::kDawn;
206#endif
Peng Huangdbde9c72022-02-18 21:48:44207
Xiaohan Wangfa22d3e2022-01-15 02:02:43208#if BUILDFLAG(IS_MAC)
Peng Huangdbde9c72022-02-18 21:48:44209 if (base::FeatureList::IsEnabled(features::kMetal))
210 return GrContextType::kMetal;
Sean Gilhuly3e84fc82020-01-22 22:06:45211#endif
Peng Huangdbde9c72022-02-18 21:48:44212
213 if (features::IsUsingVulkan())
214 return GrContextType::kVulkan;
215
216 return GrContextType::kGL;
Sean Gilhuly3e84fc82020-01-22 22:06:45217}
218
219VulkanImplementationName ParseVulkanImplementationName(
Peng Huang07ed18642020-06-09 00:58:17220 const base::CommandLine* command_line) {
Xiaohan Wangfa22d3e2022-01-15 02:02:43221#if BUILDFLAG(IS_ANDROID)
Bo Liu693cea52020-11-05 21:52:44222 if (command_line->HasSwitch(switches::kWebViewDrawFunctorUsesVulkan) &&
223 base::FeatureList::IsEnabled(features::kWebViewVulkan)) {
224 return VulkanImplementationName::kForcedNative;
225 }
226#endif
227
Peng Huangcc654252020-12-09 03:36:34228#if BUILDFLAG(IS_CHROMEOS_LACROS)
229 // LACROS doesn't support Vulkan right now, to avoid LACROS picking up Linux
230 // finch, kNone is returned for LACROS.
231 // TODO(https://crbug.com/1155622): When LACROS is separated from Linux finch
232 // config.
233 return VulkanImplementationName::kNone;
234#else
Sean Gilhuly3e84fc82020-01-22 22:06:45235 if (command_line->HasSwitch(switches::kUseVulkan)) {
236 auto value = command_line->GetSwitchValueASCII(switches::kUseVulkan);
237 if (value.empty() || value == switches::kVulkanImplementationNameNative) {
238 return VulkanImplementationName::kForcedNative;
239 } else if (value == switches::kVulkanImplementationNameSwiftshader) {
240 return VulkanImplementationName::kSwiftshader;
241 }
242 }
Peng Huang07ed18642020-06-09 00:58:17243
Jonathan Backerf48f9202021-01-14 18:39:46244 if (features::IsUsingVulkan()) {
245 // If the vulkan feature is enabled from command line, we will force to use
246 // vulkan even if it is blocklisted.
247 return base::FeatureList::GetInstance()->IsFeatureOverriddenFromCommandLine(
248 features::kVulkan.name,
249 base::FeatureList::OVERRIDE_ENABLE_FEATURE)
250 ? VulkanImplementationName::kForcedNative
251 : VulkanImplementationName::kNative;
252 }
Peng Huang07ed18642020-06-09 00:58:17253
Jonathan Backerf48f9202021-01-14 18:39:46254 // GrContext is not going to use Vulkan.
255 return VulkanImplementationName::kNone;
Peng Huangcc654252020-12-09 03:36:34256#endif
Sean Gilhuly3e84fc82020-01-22 22:06:45257}
258
Austin Eng80eb7962022-02-07 20:35:37259WebGPUAdapterName ParseWebGPUAdapterName(
260 const base::CommandLine* command_line) {
261 if (command_line->HasSwitch(switches::kUseWebGPUAdapter)) {
262 auto value = command_line->GetSwitchValueASCII(switches::kUseWebGPUAdapter);
263 if (value.empty()) {
264 return WebGPUAdapterName::kDefault;
265 } else if (value == "compat") {
266 return WebGPUAdapterName::kCompat;
267 } else if (value == "swiftshader") {
268 return WebGPUAdapterName::kSwiftShader;
269 } else if (value == "default") {
270 return WebGPUAdapterName::kDefault;
271 } else {
272 DLOG(ERROR) << "Invalid switch " << switches::kUseWebGPUAdapter << "="
273 << value << ".";
274 }
275 }
276 return WebGPUAdapterName::kDefault;
277}
278
geofflangdf7fff2d42016-11-11 00:34:03279} // namespace gles2
280} // namespace gpu