File: service_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (407 lines) | stat: -rw-r--r-- 15,990 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/service_utils.h"

#include <string>
#include <string_view>

#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/service/context_group.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "gpu/config/gpu_finch_features.h"
#include "skia/buildflags.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_features.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_egl.h"
#include "ui/gl/gl_switches.h"
#include "ui/gl/gl_utils.h"

namespace gpu {
namespace gles2 {

namespace {

bool GetUintFromSwitch(const base::CommandLine* command_line,
                       std::string_view switch_string,
                       uint32_t* value) {
  if (!command_line->HasSwitch(switch_string)) {
    return false;
  }
  std::string switch_value(command_line->GetSwitchValueASCII(switch_string));
  return base::StringToUint(switch_value, value);
}

// Parse the value of --use-vulkan from the command line. If unspecified and
// features::kVulkan is enabled (GrContext is going to use vulkan), default to
// the native implementation.
VulkanImplementationName ParseVulkanImplementationName(
    const base::CommandLine* command_line) {
#if BUILDFLAG(IS_ANDROID)
  if (command_line->HasSwitch(switches::kWebViewDrawFunctorUsesVulkan)) {
    return VulkanImplementationName::kForcedNative;
  }
#endif

  if (command_line->HasSwitch(switches::kUseVulkan)) {
    auto value = command_line->GetSwitchValueASCII(switches::kUseVulkan);
    if (value.empty() || value == switches::kVulkanImplementationNameNative) {
      return VulkanImplementationName::kForcedNative;
    } else if (value == switches::kVulkanImplementationNameSwiftshader) {
      return VulkanImplementationName::kSwiftshader;
    }
  }

  if (features::IsUsingVulkan()) {
    // If the vulkan feature is enabled from command line, we will force to use
    // vulkan even if it is blocklisted.
    return base::FeatureList::GetInstance()->IsFeatureOverriddenFromCommandLine(
               features::kVulkan.name,
               base::FeatureList::OVERRIDE_ENABLE_FEATURE)
               ? VulkanImplementationName::kForcedNative
               : VulkanImplementationName::kNative;
  }

  // GrContext is not going to use Vulkan.
  return VulkanImplementationName::kNone;
}

WebGPUAdapterName ParseWebGPUAdapterName(
    const base::CommandLine* command_line) {
  if (command_line->HasSwitch(switches::kUseWebGPUAdapter)) {
    auto value = command_line->GetSwitchValueASCII(switches::kUseWebGPUAdapter);

    static const struct {
      const char* name;
      WebGPUAdapterName value;
    } kAdapterNames[] = {
        {"", WebGPUAdapterName::kDefault},
        {"default", WebGPUAdapterName::kDefault},
        {"d3d11", WebGPUAdapterName::kD3D11},
        {"opengles", WebGPUAdapterName::kOpenGLES},
        {"swiftshader", WebGPUAdapterName::kSwiftShader},
    };

    for (const auto& adapter_name : kAdapterNames) {
      if (value == adapter_name.name) {
        return adapter_name.value;
      }
    }

    DLOG(ERROR) << "Invalid switch " << switches::kUseWebGPUAdapter << "="
                << value << ".";
  }
  return WebGPUAdapterName::kDefault;
}

WebGPUPowerPreference ParseWebGPUPowerPreference(
    const base::CommandLine* command_line) {
  if (command_line->HasSwitch(switches::kUseWebGPUPowerPreference)) {
    auto value =
        command_line->GetSwitchValueASCII(switches::kUseWebGPUPowerPreference);
    if (value.empty()) {
      return WebGPUPowerPreference::kNone;
    } else if (value == "none") {
      return WebGPUPowerPreference::kNone;
    } else if (value == "default-low-power") {
      return WebGPUPowerPreference::kDefaultLowPower;
    } else if (value == "default-high-performance") {
      return WebGPUPowerPreference::kDefaultHighPerformance;
    } else if (value == "force-low-power") {
      return WebGPUPowerPreference::kForceLowPower;
    } else if (value == "force-high-performance") {
      return WebGPUPowerPreference::kForceHighPerformance;
    } else {
      DLOG(ERROR) << "Invalid switch " << switches::kUseWebGPUPowerPreference
                  << "=" << value << ".";
    }
  }
  return WebGPUPowerPreference::kNone;
}

}  // namespace

gl::GLContextAttribs GenerateGLContextAttribsForDecoder(
    const ContextCreationAttribs& attribs_helper,
    const ContextGroup* context_group) {
  gl::GLContextAttribs attribs;
  attribs.gpu_preference = attribs_helper.gpu_preference;
  if (context_group->use_passthrough_cmd_decoder()) {
    attribs.bind_generates_resource = attribs_helper.bind_generates_resource;
    attribs.webgl_compatibility_context =
        IsWebGLContextType(attribs_helper.context_type);

    // Always use the global texture and semaphore share group for the
    // passthrough command decoder
    attribs.global_texture_share_group = true;
    attribs.global_semaphore_share_group = true;

    attribs.robust_resource_initialization = true;
    attribs.robust_buffer_access = true;
    attribs.allow_client_arrays = false;

    // Request a specific context version instead of always 3.0
    if (IsWebGL2OrES3ContextType(attribs_helper.context_type)) {
      attribs.client_major_es_version = 3;
      attribs.client_minor_es_version = 0;
    } else {
      DCHECK(IsWebGL1OrES2ContextType(attribs_helper.context_type));
      attribs.client_major_es_version = 2;
      attribs.client_minor_es_version = 0;
    }
  } else {
    attribs.client_major_es_version = 3;
    attribs.client_minor_es_version = 0;
  }

  if (gl::GetGlWorkarounds().disable_es3gl_context) {
    // Forcefully disable ES3 contexts
    attribs.client_major_es_version = 2;
    attribs.client_minor_es_version = 0;
  }

  if (IsES31ForTestingContextType(attribs_helper.context_type)) {
    // Forcefully disable ES 3.1 contexts. Tests create contexts by initializing
    // the attributes directly.
    attribs.client_major_es_version = 2;
    attribs.client_minor_es_version = 0;
  }

  return attribs;
}

gl::GLContextAttribs GenerateGLContextAttribsForCompositor(
    bool use_passthrough_cmd_decoder) {
  gl::GLContextAttribs attribs;
  if (use_passthrough_cmd_decoder) {
    attribs.bind_generates_resource = false;

    // Always use the global texture and semaphore share group for the
    // passthrough command decoder
    attribs.global_texture_share_group = true;
    attribs.global_semaphore_share_group = true;

    attribs.passthrough_shaders = features::IsANGLEPassthroughShadersAllowed();

    // Disable resource initialization and buffer bounds checks for trusted
    // contexts.
    attribs.robust_resource_initialization = false;
    attribs.robust_buffer_access = false;
    attribs.allow_client_arrays = true;
  }

  bool force_es2_context = gl::GetGlWorkarounds().disable_es3gl_context;
  if (features::UseGles2ForOopR() && use_passthrough_cmd_decoder) {
    force_es2_context = true;
  }

  attribs.client_major_es_version = force_es2_context ? 2 : 3;
  attribs.client_minor_es_version = 0;

  return attribs;
}

bool UsePassthroughCommandDecoder(const base::CommandLine* command_line) {
  return gl::UsePassthroughCommandDecoder(command_line);
}

GpuPreferences ParseGpuPreferences(const base::CommandLine* command_line) {
  GpuPreferences gpu_preferences;
  gpu_preferences.compile_shader_always_succeeds =
      command_line->HasSwitch(switches::kCompileShaderAlwaysSucceeds);
  gpu_preferences.disable_gl_error_limit =
      command_line->HasSwitch(switches::kDisableGLErrorLimit);
  gpu_preferences.disable_glsl_translator =
      command_line->HasSwitch(switches::kDisableGLSLTranslator);
  gpu_preferences.disable_shader_name_hashing =
      command_line->HasSwitch(switches::kDisableShaderNameHashing);
  gpu_preferences.enable_gpu_command_logging =
      command_line->HasSwitch(switches::kEnableGPUCommandLogging);
  gpu_preferences.enable_gpu_debugging =
      command_line->HasSwitch(switches::kEnableGPUDebugging);
  gpu_preferences.enable_gpu_service_logging_gpu =
      command_line->HasSwitch(switches::kEnableGPUServiceLoggingGPU);
  gpu_preferences.enable_gpu_driver_debug_logging =
      command_line->HasSwitch(switches::kEnableGPUDriverDebugLogging);
  gpu_preferences.disable_gpu_program_cache =
      command_line->HasSwitch(switches::kDisableGpuProgramCache);
  gpu_preferences.enforce_gl_minimums =
      command_line->HasSwitch(switches::kEnforceGLMinimums);
  if (GetUintFromSwitch(
          command_line, switches::kForceGpuMemDiscardableLimitMb,
          &gpu_preferences.force_gpu_mem_discardable_limit_bytes)) {
    gpu_preferences.force_gpu_mem_discardable_limit_bytes *= 1024 * 1024;
  }
  GetUintFromSwitch(command_line, switches::kForceMaxTextureSize,
                    &gpu_preferences.force_max_texture_size);
  if (GetUintFromSwitch(command_line, switches::kGpuProgramCacheSizeKb,
                        &gpu_preferences.gpu_program_cache_size)) {
    gpu_preferences.gpu_program_cache_size *= 1024;
  }
  gpu_preferences.disable_gpu_shader_disk_cache =
      command_line->HasSwitch(switches::kDisableGpuShaderDiskCache);
  gpu_preferences.enable_threaded_texture_mailboxes =
      command_line->HasSwitch(switches::kEnableThreadedTextureMailboxes);
  gpu_preferences.gl_shader_interm_output =
      command_line->HasSwitch(switches::kGLShaderIntermOutput);
  gpu_preferences.enable_gpu_service_logging =
      command_line->HasSwitch(switches::kEnableGPUServiceLogging);
  gpu_preferences.enable_gpu_service_tracing =
      command_line->HasSwitch(switches::kEnableGPUServiceTracing);
  gpu_preferences.use_passthrough_cmd_decoder =
      gpu::gles2::UsePassthroughCommandDecoder(command_line);
  gpu_preferences.ignore_gpu_blocklist =
      command_line->HasSwitch(switches::kIgnoreGpuBlocklist);
  gpu_preferences.enable_webgpu =
      command_line->HasSwitch(switches::kEnableUnsafeWebGPU) ||
      base::FeatureList::IsEnabled(features::kWebGPUService);
  gpu_preferences.enable_unsafe_webgpu =
      command_line->HasSwitch(switches::kEnableUnsafeWebGPU);
  gpu_preferences.enable_webgpu_developer_features =
      command_line->HasSwitch(switches::kEnableWebGPUDeveloperFeatures);
  gpu_preferences.use_webgpu_adapter = ParseWebGPUAdapterName(command_line);
  gpu_preferences.use_webgpu_power_preference =
      ParseWebGPUPowerPreference(command_line);
  gpu_preferences.force_webgpu_compat =
      command_line->HasSwitch(switches::kForceWebGPUCompat);
  if (command_line->HasSwitch(switches::kEnableDawnBackendValidation)) {
    auto value = command_line->GetSwitchValueASCII(
        switches::kEnableDawnBackendValidation);
    if (value.empty() || value == "full") {
      gpu_preferences.enable_dawn_backend_validation =
          DawnBackendValidationLevel::kFull;
    } else if (value == "partial") {
      gpu_preferences.enable_dawn_backend_validation =
          DawnBackendValidationLevel::kPartial;
    }
  }
  if (command_line->HasSwitch(switches::kEnableDawnFeatures)) {
    gpu_preferences.enabled_dawn_features_list = base::SplitString(
        command_line->GetSwitchValueASCII(switches::kEnableDawnFeatures), ",",
        base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  }
  if (command_line->HasSwitch(switches::kDisableDawnFeatures)) {
    gpu_preferences.disabled_dawn_features_list = base::SplitString(
        command_line->GetSwitchValueASCII(switches::kDisableDawnFeatures), ",",
        base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  }
  gpu_preferences.gr_context_type = ParseGrContextType(command_line);
  // ParseGrContextType checks Vulkan setting as well, so only parse Vulkan
  // implementation name if gr_context_type is kVulkan.
  gpu_preferences.use_vulkan =
      gpu_preferences.gr_context_type == GrContextType::kVulkan
          ? ParseVulkanImplementationName(command_line)
          : VulkanImplementationName::kNone;

#if BUILDFLAG(IS_FUCHSIA)
  // Vulkan Surface is not used on Fuchsia.
  gpu_preferences.disable_vulkan_surface = true;
#else
  gpu_preferences.disable_vulkan_surface =
      command_line->HasSwitch(switches::kDisableVulkanSurface);
#endif

  gpu_preferences.enable_gpu_blocked_time_metric =
      command_line->HasSwitch(switches::kEnableGpuBlockedTime);

  return gpu_preferences;
}

GrContextType ParseGrContextType(const base::CommandLine* command_line) {
  if (features::IsSkiaGraphiteEnabled(command_line)) {
    [[maybe_unused]] auto value =
        command_line->GetSwitchValueASCII(switches::kSkiaGraphiteBackend);
#if BUILDFLAG(SKIA_USE_DAWN)
    if (value.empty() ||
        base::StartsWith(value, switches::kSkiaGraphiteBackendDawn)) {
      return GrContextType::kGraphiteDawn;
    }
#endif  // BUILDFLAG(SKIA_USE_DAWN)
#if BUILDFLAG(SKIA_USE_METAL)
    if (value == switches::kSkiaGraphiteBackendMetal) {
      return GrContextType::kGraphiteMetal;
    }
#endif  // BUILDFLAG(SKIA_USE_METAL)
    LOG(ERROR) << "Skia Graphite backend = \"" << value
               << "\" not found - falling back to Ganesh!";
  }
  if (features::IsUsingVulkan()) {
    return GrContextType::kVulkan;
  }
  return GrContextType::kGL;
}

bool MSAAIsSlow(const GpuDriverBugWorkarounds& workarounds) {
  // Only query the kEnableMSAAOnNewIntelGPUs feature flag if the host device
  // is affected by the experiment (i.e. is a new Intel GPU).
  // This is to avoid activating the experiment on hosts that are irrelevant
  // to the study in order to boost statistical power.
  bool affected_by_experiment =
      workarounds.msaa_is_slow && !workarounds.msaa_is_slow_2;

  return affected_by_experiment ? !base::FeatureList::IsEnabled(
                                      features::kEnableMSAAOnNewIntelGPUs)
                                : workarounds.msaa_is_slow;
}

}  // namespace gles2

#if BUILDFLAG(IS_MAC)
uint32_t GetTextureTargetForIOSurfaces() {
  // On MacOS, the default texture target for native GpuMemoryBuffers is
  // GL_TEXTURE_RECTANGLE_ARB. This is due to CGL's requirements for creating
  // a GL surface. However, when ANGLE is used on top of SwiftShader or Metal,
  // it's necessary to use GL_TEXTURE_2D instead.
  // TODO(crbug.com/40676774): The proper behavior is to check the config
  // parameter set by the EGL_ANGLE_iosurface_client_buffer extension
  if (gl::GetGLImplementation() == gl::kGLImplementationEGLANGLE &&
      (gl::GetANGLEImplementation() == gl::ANGLEImplementation::kSwiftShader ||
       gl::GetANGLEImplementation() == gl::ANGLEImplementation::kMetal)) {
    return GL_TEXTURE_2D;
  }
  return GL_TEXTURE_RECTANGLE_ANGLE;
}
#endif  // BUILDFLAG(IS_MAC)

size_t UpdateShaderCacheSizeOnMemoryPressure(
    size_t max_cache_size,
    base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
  switch (memory_pressure_level) {
    case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
      return max_cache_size;
    case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
      if (base::FeatureList::IsEnabled(
              ::features::kAggressiveShaderCacheLimits)) {
        // Ignore moderate memory pressure.
      } else {
        max_cache_size /= 4;
      }
      break;
    case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
      if (base::FeatureList::IsEnabled(
              ::features::kAggressiveShaderCacheLimits)) {
#if BUILDFLAG(IS_ANDROID)
        // On Android, critical memory pressure notifications are very common,
        // and not necessarily tied to actual critical memory pressure. Ignore.
        break;
#else
        max_cache_size /= 4;
#endif
      } else {
        max_cache_size = 0;
      }
      break;
  }

  return max_cache_size;
}

}  // namespace gpu