/
procaddr_others.go
179 lines (158 loc) · 4.51 KB
/
procaddr_others.go
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
// 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 void* getProcAddressGL(void* libGL, 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(void* libGLES, const char* name) {
// return dlsym(libGLES, name);
// }
import "C"
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"unsafe"
)
var (
libGL unsafe.Pointer
libGLES unsafe.Pointer
)
// 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).
// For example, libGL.so.1 might exist instead of libGL.so.
for _, dir := range libraryPaths {
libs, err := listLibsInDirectory(dir, libName)
if err != nil {
return nil, err
}
if len(libs) == 0 {
continue
}
// The file names are sorted in the alphabetical order.
// TODO: What is the best version to use?
sort.Strings(libs)
libNames = append(libNames, libs...)
}
return libNames, nil
}
// 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)
if err != nil {
return nil, err
}
var files []string
for _, ent := range ents {
if ent.IsDir() {
continue
}
if ent.Name() == prefix {
files = append(files, filepath.Join(dir, ent.Name()))
continue
}
if strings.HasPrefix(ent.Name(), prefix+".") {
files = append(files, filepath.Join(dir, ent.Name()))
continue
}
}
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 {
names, err := listLibs(libraryPaths, "libGL.so")
if err != nil {
return err
}
for _, name := range names {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))
if lib != nil {
libGL = lib
return nil
}
}
}
// Try OpenGL ES.
names, err := listLibs(libraryPaths, "libGLESv2.so")
if err != nil {
return err
}
for _, name := range names {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))
if lib != nil {
libGLES = lib
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(libGL, 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(libGLES, cname)
}