blob: 3f32067a171f3e9878fa0c88174a5e69a05b3519 [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"
Jonathan Backer7bc44592018-04-25 19:59:5010#include "base/strings/string_number_conversions.h"
Sean Gilhuly3e84fc82020-01-22 22:06:4511#include "build/build_config.h"
geofflangdf7fff2d42016-11-11 00:34:0312#include "gpu/command_buffer/common/gles2_cmd_utils.h"
Geoff Lang619081b2017-08-31 19:23:3113#include "gpu/command_buffer/service/context_group.h"
Geoff Langd72f1e92017-10-12 23:31:3814#include "gpu/command_buffer/service/gpu_switches.h"
Peng Huangdc3c68f42019-09-15 02:27:2215#include "gpu/config/gpu_finch_features.h"
Sean Gilhulye5342322019-11-08 16:40:5316#include "skia/buildflags.h"
geofflang3ab039d2017-02-22 15:33:5817#include "ui/gl/gl_switches.h"
Jonah Ryan-Davis570ab8b92019-07-08 20:03:3318#include "ui/gl/gl_utils.h"
geofflangdf7fff2d42016-11-11 00:34:0319
Geoff Lang619081b2017-08-31 19:23:3120#if defined(USE_EGL)
21#include "ui/gl/gl_surface_egl.h"
22#endif // defined(USE_EGL)
23
geofflangdf7fff2d42016-11-11 00:34:0324namespace gpu {
25namespace gles2 {
26
Jonathan Backer7bc44592018-04-25 19:59:5027namespace {
28
29bool GetUintFromSwitch(const base::CommandLine* command_line,
30 const base::StringPiece& switch_string,
31 uint32_t* value) {
32 if (!command_line->HasSwitch(switch_string))
33 return false;
34 std::string switch_value(command_line->GetSwitchValueASCII(switch_string));
35 return base::StringToUint(switch_value, value);
36}
37
38} // namespace
39
geofflangdf7fff2d42016-11-11 00:34:0340gl::GLContextAttribs GenerateGLContextAttribs(
Antoine Labourfeab2392017-12-21 20:28:3941 const ContextCreationAttribs& attribs_helper,
Geoff Lang619081b2017-08-31 19:23:3142 const ContextGroup* context_group) {
Khushalb2c140b2018-07-09 20:21:1643 return GenerateGLContextAttribs(attribs_helper,
44 context_group->use_passthrough_cmd_decoder());
45}
46
47gl::GLContextAttribs GenerateGLContextAttribs(
48 const ContextCreationAttribs& attribs_helper,
49 bool use_passthrough_cmd_decoder) {
geofflangdf7fff2d42016-11-11 00:34:0350 gl::GLContextAttribs attribs;
51 attribs.gpu_preference = attribs_helper.gpu_preference;
Khushalb2c140b2018-07-09 20:21:1652 if (use_passthrough_cmd_decoder) {
geofflangdf7fff2d42016-11-11 00:34:0353 attribs.bind_generates_resource = attribs_helper.bind_generates_resource;
54 attribs.webgl_compatibility_context =
55 IsWebGLContextType(attribs_helper.context_type);
geofflang3ab039d2017-02-22 15:33:5856
57 // Always use the global texture share group for the passthrough command
58 // decoder
59 attribs.global_texture_share_group = true;
60
Geoff Lang7f82ef42017-09-23 17:56:0261 attribs.robust_resource_initialization = true;
Jiajia Qin69aeeb22017-11-09 05:16:1862 attribs.robust_buffer_access = true;
Geoff Lang7f82ef42017-09-23 17:56:0263
geofflang3ab039d2017-02-22 15:33:5864 // Request a specific context version instead of always 3.0
Jiajia Qin252a8132018-07-19 01:03:2265 if (IsWebGL2ComputeContextType(attribs_helper.context_type)) {
66 attribs.client_major_es_version = 3;
67 attribs.client_minor_es_version = 1;
68 } else if (IsWebGL2OrES3ContextType(attribs_helper.context_type)) {
geofflang3ab039d2017-02-22 15:33:5869 attribs.client_major_es_version = 3;
70 attribs.client_minor_es_version = 0;
71 } else {
72 DCHECK(IsWebGL1OrES2ContextType(attribs_helper.context_type));
73 attribs.client_major_es_version = 2;
74 attribs.client_minor_es_version = 0;
75 }
76 } else {
77 attribs.client_major_es_version = 3;
78 attribs.client_minor_es_version = 0;
geofflangdf7fff2d42016-11-11 00:34:0379 }
geofflang3ab039d2017-02-22 15:33:5880
81 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
82 switches::kDisableES3GLContext)) {
83 // Forcefully disable ES3 contexts
84 attribs.client_major_es_version = 2;
85 attribs.client_minor_es_version = 0;
86 }
87
geofflangdf7fff2d42016-11-11 00:34:0388 return attribs;
89}
90
Geoff Langd72f1e92017-10-12 23:31:3891bool UsePassthroughCommandDecoder(const base::CommandLine* command_line) {
Jonah Ryan-Davis570ab8b92019-07-08 20:03:3392 return gl::UsePassthroughCommandDecoder(command_line);
Geoff Langd72f1e92017-10-12 23:31:3893}
94
Geoff Lang619081b2017-08-31 19:23:3195bool PassthroughCommandDecoderSupported() {
96#if defined(USE_EGL)
97 // Using the passthrough command buffer requires that specific ANGLE
98 // extensions are exposed
99 return gl::GLSurfaceEGL::IsCreateContextBindGeneratesResourceSupported() &&
100 gl::GLSurfaceEGL::IsCreateContextWebGLCompatabilitySupported() &&
Geoff Langd72f1e92017-10-12 23:31:38101 gl::GLSurfaceEGL::IsRobustResourceInitSupported() &&
Geoff Lang619081b2017-08-31 19:23:31102 gl::GLSurfaceEGL::IsDisplayTextureShareGroupSupported() &&
103 gl::GLSurfaceEGL::IsCreateContextClientArraysSupported();
104#else
105 // The passthrough command buffer is only supported on top of ANGLE/EGL
106 return false;
107#endif // defined(USE_EGL)
Jonathan Backer7bc44592018-04-25 19:59:50108}
109
110GpuPreferences ParseGpuPreferences(const base::CommandLine* command_line) {
111 GpuPreferences gpu_preferences;
112 gpu_preferences.compile_shader_always_succeeds =
113 command_line->HasSwitch(switches::kCompileShaderAlwaysSucceeds);
114 gpu_preferences.disable_gl_error_limit =
115 command_line->HasSwitch(switches::kDisableGLErrorLimit);
116 gpu_preferences.disable_glsl_translator =
117 command_line->HasSwitch(switches::kDisableGLSLTranslator);
118 gpu_preferences.disable_shader_name_hashing =
119 command_line->HasSwitch(switches::kDisableShaderNameHashing);
120 gpu_preferences.enable_gpu_command_logging =
121 command_line->HasSwitch(switches::kEnableGPUCommandLogging);
122 gpu_preferences.enable_gpu_debugging =
123 command_line->HasSwitch(switches::kEnableGPUDebugging);
124 gpu_preferences.enable_gpu_service_logging_gpu =
125 command_line->HasSwitch(switches::kEnableGPUServiceLoggingGPU);
126 gpu_preferences.enable_gpu_driver_debug_logging =
127 command_line->HasSwitch(switches::kEnableGPUDriverDebugLogging);
128 gpu_preferences.disable_gpu_program_cache =
129 command_line->HasSwitch(switches::kDisableGpuProgramCache);
130 gpu_preferences.enforce_gl_minimums =
131 command_line->HasSwitch(switches::kEnforceGLMinimums);
132 if (GetUintFromSwitch(command_line, switches::kForceGpuMemAvailableMb,
Wez4f8b5432019-12-31 06:40:11133 &gpu_preferences.force_gpu_mem_available_bytes)) {
134 gpu_preferences.force_gpu_mem_available_bytes *= 1024 * 1024;
135 }
136 if (GetUintFromSwitch(
137 command_line, switches::kForceGpuMemDiscardableLimitMb,
138 &gpu_preferences.force_gpu_mem_discardable_limit_bytes)) {
139 gpu_preferences.force_gpu_mem_discardable_limit_bytes *= 1024 * 1024;
Jonathan Backer7bc44592018-04-25 19:59:50140 }
141 if (GetUintFromSwitch(command_line, switches::kGpuProgramCacheSizeKb,
142 &gpu_preferences.gpu_program_cache_size)) {
143 gpu_preferences.gpu_program_cache_size *= 1024;
144 }
145 gpu_preferences.disable_gpu_shader_disk_cache =
146 command_line->HasSwitch(switches::kDisableGpuShaderDiskCache);
147 gpu_preferences.enable_threaded_texture_mailboxes =
148 command_line->HasSwitch(switches::kEnableThreadedTextureMailboxes);
149 gpu_preferences.gl_shader_interm_output =
150 command_line->HasSwitch(switches::kGLShaderIntermOutput);
151 gpu_preferences.emulate_shader_precision =
152 command_line->HasSwitch(switches::kEmulateShaderPrecision);
Jonathan Backer7bc44592018-04-25 19:59:50153 gpu_preferences.enable_gpu_service_logging =
154 command_line->HasSwitch(switches::kEnableGPUServiceLogging);
155 gpu_preferences.enable_gpu_service_tracing =
156 command_line->HasSwitch(switches::kEnableGPUServiceTracing);
157 gpu_preferences.use_passthrough_cmd_decoder =
158 gpu::gles2::UsePassthroughCommandDecoder(command_line);
Jonathan Backer7bc44592018-04-25 19:59:50159 gpu_preferences.ignore_gpu_blacklist =
160 command_line->HasSwitch(switches::kIgnoreGpuBlacklist);
Kai Ninomiya328e07a2018-08-23 01:08:16161 gpu_preferences.enable_webgpu =
162 command_line->HasSwitch(switches::kEnableUnsafeWebGPU);
Sean Gilhuly3e84fc82020-01-22 22:06:45163 gpu_preferences.gr_context_type = ParseGrContextType();
164 gpu_preferences.use_vulkan = ParseVulkanImplementationName(
165 command_line, gpu_preferences.gr_context_type);
Peng Huang556da902019-03-22 17:49:40166 gpu_preferences.disable_vulkan_surface =
167 command_line->HasSwitch(switches::kDisableVulkanSurface);
Vasiliy Telezhnikov533c5642019-12-03 15:10:16168
169 gpu_preferences.enable_gpu_blocked_time_metric =
170 command_line->HasSwitch(switches::kEnableGpuBlockedTime);
171
Jonathan Backer7bc44592018-04-25 19:59:50172 return gpu_preferences;
173}
Geoff Lang619081b2017-08-31 19:23:31174
Sean Gilhuly3e84fc82020-01-22 22:06:45175GrContextType ParseGrContextType() {
176#if BUILDFLAG(SKIA_USE_DAWN)
177 if (base::FeatureList::IsEnabled(features::kSkiaDawn))
178 return GrContextType::kDawn;
179#endif
180#if defined(OS_MACOSX)
181 return base::FeatureList::IsEnabled(features::kMetal) ? GrContextType::kMetal
182 : GrContextType::kGL;
183#else
184 return base::FeatureList::IsEnabled(features::kVulkan)
185 ? GrContextType::kVulkan
186 : GrContextType::kGL;
187#endif
188}
189
190VulkanImplementationName ParseVulkanImplementationName(
191 const base::CommandLine* command_line,
192 GrContextType gr_context_type) {
193 if (command_line->HasSwitch(switches::kUseVulkan)) {
194 auto value = command_line->GetSwitchValueASCII(switches::kUseVulkan);
195 if (value.empty() || value == switches::kVulkanImplementationNameNative) {
196 return VulkanImplementationName::kForcedNative;
197 } else if (value == switches::kVulkanImplementationNameSwiftshader) {
198 return VulkanImplementationName::kSwiftshader;
199 }
200 }
201 // If the vulkan implementation is not set from --use-vulkan, the native
202 // vulkan implementation will be used by default.
203 return gr_context_type == GrContextType::kVulkan
204 ? VulkanImplementationName::kNative
205 : VulkanImplementationName::kNone;
206}
207
geofflangdf7fff2d42016-11-11 00:34:03208} // namespace gles2
209} // namespace gpu