internal/graphicsdriver/opengl/gl: bug fix: an unavailable library name might be chosen

Instead, let's try all the names with dlopen.

Updates #2523
This commit is contained in:
Hajime Hoshi 2023-01-07 03:01:28 +09:00
parent df7b7b731e
commit 983e8abd46

View File

@ -21,31 +21,23 @@ package gl
// #include <dlfcn.h> // #include <dlfcn.h>
// #include <stdlib.h> // #include <stdlib.h>
// //
// static const char* libGLName; // static void* libGLPtr;
// static const char* libGLESName; // static void* libGLESPtr;
// //
// static void setLibGLName(const char* name) { // static void setLibGL(void* lib) {
// libGLName = name; // libGLPtr = lib;
// } // }
// //
// static void setLibGLESName(const char* name) { // static void setLibGLES(void* lib) {
// libGLESName = name; // libGLESPtr = lib;
// } // }
// //
// static void* libGL() { // static void* libGL() {
// static void* so; // return libGLPtr;
// if (!so) {
// so = dlopen(libGLName, RTLD_LAZY | RTLD_GLOBAL);
// }
// return so;
// } // }
// //
// static void* libGLES() { // static void* libGLES() {
// static void* so; // return libGLESPtr;
// if (!so) {
// so = dlopen(libGLESName, RTLD_LAZY | RTLD_GLOBAL);
// }
// return so;
// } // }
// //
// static void* getProcAddressGL(const char* name) { // static void* getProcAddressGL(const char* name) {
@ -74,13 +66,18 @@ import (
"unsafe" "unsafe"
) )
func findLib(libraryPaths []string, libName string) (string, error) { // listLibs returns an appropriate library file paths based on the given library paths and the library name as a prefix.
// Note that the found libraries might not be available e.g. due to architecture mismatches.
func listLibs(libraryPaths []string, libName string) ([]string, error) {
// LD_LIBRARY_PATH might be empty. Use the original name as a candidate.
libNames := []string{libName}
// Look for a library file. In some environments like Steam, a library with the exactly same name might not exist (#2523). // Look for a library file. In some environments like Steam, a library with the exactly same name might not exist (#2523).
// For example, libGL.so.1 might exist instead of libGL.so. // For example, libGL.so.1 might exist instead of libGL.so.
for _, dir := range libraryPaths { for _, dir := range libraryPaths {
libs, err := listLibs(dir, libName) libs, err := listLibsInDirectory(dir, libName)
if err != nil { if err != nil {
return "", err return nil, err
} }
if len(libs) == 0 { if len(libs) == 0 {
continue continue
@ -88,14 +85,17 @@ func findLib(libraryPaths []string, libName string) (string, error) {
// The file names are sorted in the alphabetical order. Use the first item. // The file names are sorted in the alphabetical order. Use the first item.
// TODO: What is the best version to use? // TODO: What is the best version to use?
return filepath.Join(dir, libs[0]), nil sort.Strings(libs)
libNames = append(libNames, libs...)
} }
// LD_LIBRARY_PATH might be empty. Use the original name. return libNames, nil
return libName, nil
} }
func listLibs(dir string, prefix string) ([]string, error) { // listLibsInDirectory returns library file paths with the given prefix in the directory.
// Note that the found libraries might not be available e.g. due to architecture mismatches.
func listLibsInDirectory(dir string, prefix string) ([]string, error) {
ents, err := os.ReadDir(dir) ents, err := os.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
@ -107,16 +107,15 @@ func listLibs(dir string, prefix string) ([]string, error) {
continue continue
} }
if ent.Name() == prefix { if ent.Name() == prefix {
files = append(files, ent.Name()) files = append(files, filepath.Join(dir, ent.Name()))
continue continue
} }
if strings.HasPrefix(ent.Name(), prefix+".") { if strings.HasPrefix(ent.Name(), prefix+".") {
files = append(files, ent.Name()) files = append(files, filepath.Join(dir, ent.Name()))
continue continue
} }
} }
sort.Strings(files)
return files, nil return files, nil
} }
@ -139,32 +138,37 @@ func (c *defaultContext) init() error {
// Try OpenGL first. OpenGL is preferrable as this doesn't cause context losts. // Try OpenGL first. OpenGL is preferrable as this doesn't cause context losts.
if !preferES { if !preferES {
libGLName, err := findLib(libraryPaths, "libGL.so") names, err := listLibs(libraryPaths, "libGL.so")
if err != nil { if err != nil {
return err return err
} }
for _, name := range names {
// This string is never released. cname := C.CString(name)
C.setLibGLName(C.CString(libGLName)) lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
if C.libGL() != nil { C.free(unsafe.Pointer(cname))
return nil if lib != nil {
C.setLibGL(lib)
return nil
}
} }
} }
// Try OpenGL ES. // Try OpenGL ES.
libGLESName, err := findLib(libraryPaths, "libGLESv2.so") names, err := listLibs(libraryPaths, "libGLESv2.so")
if err != nil { if err != nil {
return err return err
} }
for _, name := range names {
// This string is never released. cname := C.CString(name)
C.setLibGLESName(C.CString(libGLESName)) lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
if C.libGLES() != nil { C.free(unsafe.Pointer(cname))
c.isES = true if lib != nil {
return nil C.setLibGLES(lib)
return nil
}
} }
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so") return fmt.Errorf("gl: !?!? failed to load libGL.so and libGLESv2.so")
} }
func (c *defaultContext) getProcAddress(name string) unsafe.Pointer { func (c *defaultContext) getProcAddress(name string) unsafe.Pointer {