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
|
// 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 "ui/gl/init/gl_factory.h"
#include <algorithm>
#include <optional>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "ui/gl/gl_features.h"
#include "ui/gl/gl_share_group.h"
#include "ui/gl/gl_surface.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/init/gl_initializer.h"
#include "ui/gl/startup_trace.h"
#if BUILDFLAG(IS_OZONE)
#include "ui/base/ui_base_features.h"
#include "ui/ozone/public/ozone_platform.h"
#endif
namespace gl {
namespace init {
namespace {
GLImplementationParts GetRequestedGLImplementation() {
const base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
std::string requested_implementation_gl_name =
cmd->GetSwitchValueASCII(switches::kUseGL);
// If --use-angle was specified but --use-gl was not, assume --use-gl=angle
if (cmd->HasSwitch(switches::kUseANGLE) &&
!cmd->HasSwitch(switches::kUseGL)) {
requested_implementation_gl_name = kGLImplementationANGLEName;
}
if (requested_implementation_gl_name == kGLImplementationDisabledName) {
return GLImplementationParts(kGLImplementationDisabled);
}
std::vector<GLImplementationParts> allowed_impls =
GetAllowedGLImplementations();
// If the passthrough command decoder is enabled, put ANGLE first if allowed
if (UsePassthroughCommandDecoder(cmd)) {
std::vector<GLImplementationParts> angle_impls = {};
bool software_gl_in_allow_list = false;
auto iter = allowed_impls.begin();
while (iter != allowed_impls.end()) {
if ((*iter) == GetSoftwareGLImplementation()) {
software_gl_in_allow_list = true;
allowed_impls.erase(iter);
} else if (iter->gl == kGLImplementationEGLANGLE) {
angle_impls.emplace_back(*iter);
allowed_impls.erase(iter);
} else {
iter++;
}
}
allowed_impls.insert(allowed_impls.begin(), angle_impls.begin(),
angle_impls.end());
// Insert software implementations at the end, after all other hardware
// implementations. If SwiftShader is not allowed as a fallback, don't
// re-insert it.
if (software_gl_in_allow_list && features::IsSwiftShaderAllowed(cmd)) {
allowed_impls.emplace_back(GetSoftwareGLImplementation());
}
}
if (allowed_impls.empty()) {
LOG(ERROR) << "List of allowed GL implementations is empty.";
return GLImplementationParts(kGLImplementationNone);
}
std::optional<GLImplementationParts> impl_from_cmdline =
GetRequestedGLImplementationFromCommandLine(cmd);
// The default implementation is always the first one in list.
if (!impl_from_cmdline)
return allowed_impls[0];
// Allow software GL if explicitly requested by command line, even if it's not
// in the allowed_impls list.
if (IsSoftwareGLImplementation(*impl_from_cmdline))
return *impl_from_cmdline;
if (impl_from_cmdline->IsAllowed(allowed_impls))
return *impl_from_cmdline;
std::vector<std::string> allowed_impl_strs;
for (const auto& allowed_impl : allowed_impls) {
allowed_impl_strs.push_back(allowed_impl.ToString());
}
LOG(ERROR) << "Requested GL implementation " << impl_from_cmdline->ToString()
<< " not found in allowed implementations: ["
<< base::JoinString(allowed_impl_strs, ",") << "].";
return GLImplementationParts(kGLImplementationNone);
}
GLDisplay* InitializeGLOneOffPlatformHelper(bool init_extensions,
gl::GpuPreference gpu_preference) {
TRACE_EVENT1("gpu,startup", "gl::init::InitializeGLOneOffPlatformHelper",
"init_extensions", init_extensions);
GPU_STARTUP_TRACE_EVENT("gl::init::InitializeGLOneOffPlatformHelper");
const base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
bool disable_gl_drawing = cmd->HasSwitch(switches::kDisableGLDrawingForTests);
return InitializeGLOneOffPlatformImplementation(
disable_gl_drawing, init_extensions, gpu_preference);
}
} // namespace
GLDisplay* InitializeGLOneOff(gl::GpuPreference gpu_preference) {
GPU_STARTUP_TRACE_EVENT("gl::init::InitializeOneOff");
if (!InitializeStaticGLBindingsOneOff())
return nullptr;
if (GetGLImplementation() == kGLImplementationDisabled) {
return GetDefaultDisplayEGL();
}
return InitializeGLOneOffPlatformHelper(true, gpu_preference);
}
GLDisplay* InitializeGLNoExtensionsOneOff(bool init_bindings,
gl::GpuPreference gpu_preference) {
TRACE_EVENT1("gpu,startup", "gl::init::InitializeNoExtensionsOneOff",
"init_bindings", init_bindings);
GPU_STARTUP_TRACE_EVENT("gl::init::InitializeNoExtensionsOneOff");
if (init_bindings) {
if (!InitializeStaticGLBindingsOneOff())
return nullptr;
if (GetGLImplementation() == kGLImplementationDisabled) {
return GetDefaultDisplayEGL();
}
}
return InitializeGLOneOffPlatformHelper(false, gpu_preference);
}
bool InitializeStaticGLBindingsOneOff() {
DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
GPU_STARTUP_TRACE_EVENT("gl::init::InitializeStaticGLBindingsOneOff");
GLImplementationParts impl = GetRequestedGLImplementation();
if (impl.gl == kGLImplementationDisabled) {
SetGLImplementation(kGLImplementationDisabled);
return true;
} else if (impl.gl == kGLImplementationNone) {
return false;
}
return InitializeStaticGLBindingsImplementation(impl);
}
bool InitializeStaticGLBindingsImplementation(GLImplementationParts impl) {
if (!InitializeStaticGLBindings(impl)) {
ShutdownGL(nullptr, /*due_to_fallback*/ false);
return false;
}
return true;
}
GLDisplay* InitializeGLOneOffPlatformImplementation(
bool disable_gl_drawing,
bool init_extensions,
gl::GpuPreference gpu_preference) {
GLDisplay* display = InitializeGLOneOffPlatform(gpu_preference);
bool initialized = !!display;
if (!initialized) {
DVLOG(1) << "Initialization failed. Attempting to initialize default "
"GLDisplayEGL.";
RemoveGpuPreferenceEGL(gpu_preference);
display = InitializeGLOneOffPlatform(gl::GpuPreference::kDefault);
initialized = !!display;
}
if (initialized && init_extensions) {
initialized = InitializeExtensionSettingsOneOffPlatform(display);
}
if (!initialized) {
ShutdownGL(display, false);
return nullptr;
}
DVLOG(1) << "Using " << GetGLImplementationGLName(GetGLImplementationParts())
<< " GL implementation.";
SetNullDrawGLBindings(disable_gl_drawing);
return display;
}
GLDisplay* GetOrInitializeGLOneOffPlatformImplementation(
bool fallback_to_software_gl,
bool disable_gl_drawing,
bool init_extensions,
gl::GpuPreference gpu_preference) {
gl::GLDisplay* display = gl::GetDisplay(gpu_preference);
DCHECK(display);
if (display->IsInitialized()) {
return display;
}
display = gl::init::InitializeGLOneOffPlatformImplementation(
/*disable_gl_drawing=*/false, /*init_extensions=*/true,
/*gpu_preference=*/gpu_preference);
return display;
}
void ShutdownGL(GLDisplay* display, bool due_to_fallback) {
ShutdownGLPlatform(display);
UnloadGLNativeLibraries(due_to_fallback);
SetGLImplementation(kGLImplementationNone);
}
} // namespace init
} // namespace gl
|