geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 1 | // 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 Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 7 | #include <string> |
| 8 | |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 9 | #include "base/command_line.h" |
Corentin Wallez | e660b15 | 2020-07-15 16:07:54 | [diff] [blame] | 10 | #include "base/logging.h" |
Jonathan Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
Jiawei Shao | 04b8bcd | 2020-11-14 01:29:47 | [diff] [blame] | 12 | #include "base/strings/string_split.h" |
Sean Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 13 | #include "build/build_config.h" |
Jonathan Backer | f48f920 | 2021-01-14 18:39:46 | [diff] [blame] | 14 | #include "build/chromeos_buildflags.h" |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 15 | #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
Geoff Lang | 619081b | 2017-08-31 19:23:31 | [diff] [blame] | 16 | #include "gpu/command_buffer/service/context_group.h" |
Geoff Lang | d72f1e9 | 2017-10-12 23:31:38 | [diff] [blame] | 17 | #include "gpu/command_buffer/service/gpu_switches.h" |
Peng Huang | dc3c68f4 | 2019-09-15 02:27:22 | [diff] [blame] | 18 | #include "gpu/config/gpu_finch_features.h" |
Sean Gilhuly | e534232 | 2019-11-08 16:40:53 | [diff] [blame] | 19 | #include "skia/buildflags.h" |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 20 | #include "ui/gl/gl_switches.h" |
Jonah Ryan-Davis | 570ab8b9 | 2019-07-08 20:03:33 | [diff] [blame] | 21 | #include "ui/gl/gl_utils.h" |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 22 | |
Geoff Lang | 619081b | 2017-08-31 19:23:31 | [diff] [blame] | 23 | #if defined(USE_EGL) |
| 24 | #include "ui/gl/gl_surface_egl.h" |
| 25 | #endif // defined(USE_EGL) |
| 26 | |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 27 | namespace gpu { |
| 28 | namespace gles2 { |
| 29 | |
Jonathan Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | bool 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 | |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 43 | gl::GLContextAttribs GenerateGLContextAttribs( |
Antoine Labour | feab239 | 2017-12-21 20:28:39 | [diff] [blame] | 44 | const ContextCreationAttribs& attribs_helper, |
Geoff Lang | 619081b | 2017-08-31 19:23:31 | [diff] [blame] | 45 | const ContextGroup* context_group) { |
Khushal | b2c140b | 2018-07-09 20:21:16 | [diff] [blame] | 46 | return GenerateGLContextAttribs(attribs_helper, |
| 47 | context_group->use_passthrough_cmd_decoder()); |
| 48 | } |
| 49 | |
| 50 | gl::GLContextAttribs GenerateGLContextAttribs( |
| 51 | const ContextCreationAttribs& attribs_helper, |
| 52 | bool use_passthrough_cmd_decoder) { |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 53 | gl::GLContextAttribs attribs; |
| 54 | attribs.gpu_preference = attribs_helper.gpu_preference; |
Khushal | b2c140b | 2018-07-09 20:21:16 | [diff] [blame] | 55 | if (use_passthrough_cmd_decoder) { |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 56 | attribs.bind_generates_resource = attribs_helper.bind_generates_resource; |
| 57 | attribs.webgl_compatibility_context = |
| 58 | IsWebGLContextType(attribs_helper.context_type); |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 59 | |
Jonathan Ross | 6b1ca23 | 2020-07-31 16:43:36 | [diff] [blame] | 60 | // Always use the global texture and semaphore share group for the |
| 61 | // passthrough command decoder |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 62 | attribs.global_texture_share_group = true; |
Jonathan Ross | 6b1ca23 | 2020-07-31 16:43:36 | [diff] [blame] | 63 | attribs.global_semaphore_share_group = true; |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 64 | |
Geoff Lang | 7f82ef4 | 2017-09-23 17:56:02 | [diff] [blame] | 65 | attribs.robust_resource_initialization = true; |
Jiajia Qin | 69aeeb2 | 2017-11-09 05:16:18 | [diff] [blame] | 66 | attribs.robust_buffer_access = true; |
Geoff Lang | 7f82ef4 | 2017-09-23 17:56:02 | [diff] [blame] | 67 | |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 68 | // Request a specific context version instead of always 3.0 |
Geoff Lang | d0c755c | 2021-01-18 21:58:16 | [diff] [blame] | 69 | if (IsWebGL2OrES3ContextType(attribs_helper.context_type)) { |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 70 | 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; |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 80 | } |
geofflang | 3ab039d | 2017-02-22 15:33:58 | [diff] [blame] | 81 | |
| 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 Lang | d0c755c | 2021-01-18 21:58:16 | [diff] [blame] | 89 | 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 | |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 96 | return attribs; |
| 97 | } |
| 98 | |
Geoff Lang | d72f1e9 | 2017-10-12 23:31:38 | [diff] [blame] | 99 | bool UsePassthroughCommandDecoder(const base::CommandLine* command_line) { |
Jonah Ryan-Davis | 570ab8b9 | 2019-07-08 20:03:33 | [diff] [blame] | 100 | return gl::UsePassthroughCommandDecoder(command_line); |
Geoff Lang | d72f1e9 | 2017-10-12 23:31:38 | [diff] [blame] | 101 | } |
| 102 | |
Geoff Lang | 619081b | 2017-08-31 19:23:31 | [diff] [blame] | 103 | bool PassthroughCommandDecoderSupported() { |
Jonah Ryan-Davis | 77b562dc | 2020-07-17 19:11:23 | [diff] [blame] | 104 | return gl::PassthroughCommandDecoderSupported(); |
Jonathan Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | GpuPreferences 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, |
Wez | 4f8b543 | 2019-12-31 06:40:11 | [diff] [blame] | 130 | &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 Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 137 | } |
Sergey Ulanov | 689c31f | 2020-01-24 23:21:16 | [diff] [blame] | 138 | GetUintFromSwitch(command_line, switches::kForceMaxTextureSize, |
| 139 | &gpu_preferences.force_max_texture_size); |
Jonathan Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 140 | 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 Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 150 | 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 Wallez | 5cbffcc | 2020-07-08 10:23:57 | [diff] [blame] | 156 | gpu_preferences.ignore_gpu_blocklist = |
Corentin Wallez | e660b15 | 2020-07-15 16:07:54 | [diff] [blame] | 157 | command_line->HasSwitch(switches::kIgnoreGpuBlocklist); |
Kai Ninomiya | 328e07a | 2018-08-23 01:08:16 | [diff] [blame] | 158 | gpu_preferences.enable_webgpu = |
shrekshao | c653925 | 2021-04-29 19:21:56 | [diff] [blame] | 159 | command_line->HasSwitch(switches::kEnableUnsafeWebGPU) || |
Corentin Wallez | a059a3a2 | 2021-07-30 09:44:54 | [diff] [blame] | 160 | base::FeatureList::IsEnabled(features::kWebGPUService); |
Austin Eng | a442d583 | 2022-01-15 03:00:15 | [diff] [blame] | 161 | gpu_preferences.enable_unsafe_webgpu = |
Corentin Wallez | 721293f | 2021-06-03 22:42:00 | [diff] [blame] | 162 | command_line->HasSwitch(switches::kEnableUnsafeWebGPU); |
Austin Eng | 80eb796 | 2022-02-07 20:35:37 | [diff] [blame] | 163 | gpu_preferences.use_webgpu_adapter = ParseWebGPUAdapterName(command_line); |
Austin Eng | bac59e69 | 2021-05-13 16:46:54 | [diff] [blame] | 164 | 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 Shao | 04b8bcd | 2020-11-14 01:29:47 | [diff] [blame] | 175 | 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 Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 185 | gpu_preferences.gr_context_type = ParseGrContextType(); |
Peng Huang | 07ed1864 | 2020-06-09 00:58:17 | [diff] [blame] | 186 | gpu_preferences.use_vulkan = ParseVulkanImplementationName(command_line); |
Sergey Ulanov | efa2af1 | 2022-06-28 23:17:47 | [diff] [blame^] | 187 | |
| 188 | #if BUILDFLAG(IS_FUCHSIA) |
| 189 | // Vulkan Surface is not used on Fuchsia. |
| 190 | gpu_preferences.disable_vulkan_surface = true; |
| 191 | #else |
Peng Huang | 556da90 | 2019-03-22 17:49:40 | [diff] [blame] | 192 | gpu_preferences.disable_vulkan_surface = |
| 193 | command_line->HasSwitch(switches::kDisableVulkanSurface); |
Sergey Ulanov | efa2af1 | 2022-06-28 23:17:47 | [diff] [blame^] | 194 | #endif |
Vasiliy Telezhnikov | 533c564 | 2019-12-03 15:10:16 | [diff] [blame] | 195 | |
| 196 | gpu_preferences.enable_gpu_blocked_time_metric = |
| 197 | command_line->HasSwitch(switches::kEnableGpuBlockedTime); |
| 198 | |
Jonathan Backer | 7bc4459 | 2018-04-25 19:59:50 | [diff] [blame] | 199 | return gpu_preferences; |
| 200 | } |
Geoff Lang | 619081b | 2017-08-31 19:23:31 | [diff] [blame] | 201 | |
Sean Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 202 | GrContextType ParseGrContextType() { |
| 203 | #if BUILDFLAG(SKIA_USE_DAWN) |
| 204 | if (base::FeatureList::IsEnabled(features::kSkiaDawn)) |
| 205 | return GrContextType::kDawn; |
| 206 | #endif |
Peng Huang | dbde9c7 | 2022-02-18 21:48:44 | [diff] [blame] | 207 | |
Xiaohan Wang | fa22d3e | 2022-01-15 02:02:43 | [diff] [blame] | 208 | #if BUILDFLAG(IS_MAC) |
Peng Huang | dbde9c7 | 2022-02-18 21:48:44 | [diff] [blame] | 209 | if (base::FeatureList::IsEnabled(features::kMetal)) |
| 210 | return GrContextType::kMetal; |
Sean Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 211 | #endif |
Peng Huang | dbde9c7 | 2022-02-18 21:48:44 | [diff] [blame] | 212 | |
| 213 | if (features::IsUsingVulkan()) |
| 214 | return GrContextType::kVulkan; |
| 215 | |
| 216 | return GrContextType::kGL; |
Sean Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | VulkanImplementationName ParseVulkanImplementationName( |
Peng Huang | 07ed1864 | 2020-06-09 00:58:17 | [diff] [blame] | 220 | const base::CommandLine* command_line) { |
Xiaohan Wang | fa22d3e | 2022-01-15 02:02:43 | [diff] [blame] | 221 | #if BUILDFLAG(IS_ANDROID) |
Bo Liu | 693cea5 | 2020-11-05 21:52:44 | [diff] [blame] | 222 | if (command_line->HasSwitch(switches::kWebViewDrawFunctorUsesVulkan) && |
| 223 | base::FeatureList::IsEnabled(features::kWebViewVulkan)) { |
| 224 | return VulkanImplementationName::kForcedNative; |
| 225 | } |
| 226 | #endif |
| 227 | |
Peng Huang | cc65425 | 2020-12-09 03:36:34 | [diff] [blame] | 228 | #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 Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 235 | 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 Huang | 07ed1864 | 2020-06-09 00:58:17 | [diff] [blame] | 243 | |
Jonathan Backer | f48f920 | 2021-01-14 18:39:46 | [diff] [blame] | 244 | 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 Huang | 07ed1864 | 2020-06-09 00:58:17 | [diff] [blame] | 253 | |
Jonathan Backer | f48f920 | 2021-01-14 18:39:46 | [diff] [blame] | 254 | // GrContext is not going to use Vulkan. |
| 255 | return VulkanImplementationName::kNone; |
Peng Huang | cc65425 | 2020-12-09 03:36:34 | [diff] [blame] | 256 | #endif |
Sean Gilhuly | 3e84fc8 | 2020-01-22 22:06:45 | [diff] [blame] | 257 | } |
| 258 | |
Austin Eng | 80eb796 | 2022-02-07 20:35:37 | [diff] [blame] | 259 | WebGPUAdapterName 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 | |
geofflang | df7fff2d4 | 2016-11-11 00:34:03 | [diff] [blame] | 279 | } // namespace gles2 |
| 280 | } // namespace gpu |