ebiten/internal/graphicsdriver/opengl/gl/procaddr_others.go
Hajime Hoshi df7b7b731e internal/graphicsdriver/opengl/gl: bug fix: libGL.so might not exist in Steam
Instead, libGL.so.1 might exist. Use LD_LIBRARY_PATH and list the
candidates.

Closes #2523
2023-01-07 00:03:07 +09:00

189 lines
4.3 KiB
Go

// Copyright 2022 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !darwin && !js && !nintendosdk && !windows
package gl
// #cgo LDFLAGS: -ldl
//
// #include <dlfcn.h>
// #include <stdlib.h>
//
// static const char* libGLName;
// static const char* libGLESName;
//
// static void setLibGLName(const char* name) {
// libGLName = name;
// }
//
// static void setLibGLESName(const char* name) {
// libGLESName = name;
// }
//
// static void* libGL() {
// static void* so;
// if (!so) {
// so = dlopen(libGLName, RTLD_LAZY | RTLD_GLOBAL);
// }
// return so;
// }
//
// static void* libGLES() {
// static void* so;
// if (!so) {
// so = dlopen(libGLESName, RTLD_LAZY | RTLD_GLOBAL);
// }
// return so;
// }
//
// static void* getProcAddressGL(const char* name) {
// static void*(*glXGetProcAddress)(const char*);
// if (!glXGetProcAddress) {
// glXGetProcAddress = dlsym(libGL(), "glXGetProcAddress");
// if (!glXGetProcAddress) {
// glXGetProcAddress = dlsym(libGL(), "glXGetProcAddressARB");
// }
// }
// return glXGetProcAddress(name);
// }
//
// static void* getProcAddressGLES(const char* name) {
// return dlsym(libGLES(), name);
// }
import "C"
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"unsafe"
)
func findLib(libraryPaths []string, libName string) (string, error) {
// 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 _, dir := range libraryPaths {
libs, err := listLibs(dir, libName)
if err != nil {
return "", err
}
if len(libs) == 0 {
continue
}
// The file names are sorted in the alphabetical order. Use the first item.
// TODO: What is the best version to use?
return filepath.Join(dir, libs[0]), nil
}
// LD_LIBRARY_PATH might be empty. Use the original name.
return libName, nil
}
func listLibs(dir string, prefix string) ([]string, error) {
ents, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var files []string
for _, ent := range ents {
if ent.IsDir() {
continue
}
if ent.Name() == prefix {
files = append(files, ent.Name())
continue
}
if strings.HasPrefix(ent.Name(), prefix+".") {
files = append(files, ent.Name())
continue
}
}
sort.Strings(files)
return files, nil
}
func (c *defaultContext) init() error {
var preferES bool
if runtime.GOOS == "android" {
preferES = true
}
if !preferES {
for _, t := range strings.Split(os.Getenv("EBITENGINE_OPENGL"), ",") {
switch strings.TrimSpace(t) {
case "es":
preferES = true
break
}
}
}
libraryPaths := filepath.SplitList(os.Getenv("LD_LIBRARY_PATH"))
// Try OpenGL first. OpenGL is preferrable as this doesn't cause context losts.
if !preferES {
libGLName, err := findLib(libraryPaths, "libGL.so")
if err != nil {
return err
}
// This string is never released.
C.setLibGLName(C.CString(libGLName))
if C.libGL() != nil {
return nil
}
}
// Try OpenGL ES.
libGLESName, err := findLib(libraryPaths, "libGLESv2.so")
if err != nil {
return err
}
// This string is never released.
C.setLibGLESName(C.CString(libGLESName))
if C.libGLES() != nil {
c.isES = true
return nil
}
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so")
}
func (c *defaultContext) getProcAddress(name string) unsafe.Pointer {
if c.isES {
return getProcAddressGLES(name)
}
return getProcAddressGL(name)
}
func getProcAddressGL(name string) unsafe.Pointer {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return C.getProcAddressGL(cname)
}
func getProcAddressGLES(name string) unsafe.Pointer {
name = strings.TrimSuffix(name, "EXT")
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return C.getProcAddressGLES(cname)
}