/
display.go
276 lines (235 loc) · 6.8 KB
/
display.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
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
package egl
/*
#cgo pkg-config: egl
#include <EGL/egl.h>
// These variables are necessary because EGL_DEFAULT_DISPLAY and EGL_NO_DISPLAY
// are pointer constants, and cgo doesn't translate them correctly.
const EGLNativeDisplayType kDefaultDisplay = EGL_DEFAULT_DISPLAY;
const EGLDisplay kNoDisplay = EGL_NO_DISPLAY;
*/
import "C"
import (
"bytes"
"errors"
"fmt"
//"runtime"
)
var defaultDisplay C.EGLNativeDisplayType = C.kDefaultDisplay
var noDisplay C.EGLDisplay = C.kNoDisplay
func OpenDisplay() (*Display, error) {
display := new(Display)
display.eglDisplay = C.eglGetDisplay(defaultDisplay)
return display, nil
}
func (display *Display) Initialize() error {
if display.eglDisplay == noDisplay {
return getError()
}
var major, minor C.EGLint
success := C.eglInitialize(display.eglDisplay, &major, &minor)
//fmt.Printf("display == %v, version == %d.%d, success == %d\n", display.eglDisplay, major, minor, success)
if success == C.EGL_FALSE {
return getError()
}
display.majorVersion = int(major)
display.minorVersion = int(minor)
return nil
}
func (display *Display) GetVersion() (major, minor int) {
return display.majorVersion, display.minorVersion
}
func (display *Display) QueryString(name int) (string, error) {
cString := C.eglQueryString(display.eglDisplay, C.EGLint(name))
if cString == nil {
return "", getError()
}
return C.GoString(cString), nil
}
func (display *Display) GetConfigs() ([]Config, error) {
var configCount C.EGLint
success := C.eglGetConfigs(display.eglDisplay, nil, 0, &configCount)
if success == C.EGL_FALSE {
return nil, getError()
}
if configCount <= 0 {
configCount = 10
}
configurations := make([]Config, configCount)
success = C.eglGetConfigs(
display.eglDisplay,
(*C.EGLConfig)(&(configurations[0])),
configCount,
&configCount)
if success == C.EGL_FALSE {
return nil, getError()
}
if configCount <= 0 {
return nil, errors.New("eglGetConfigs() returned zero configs")
}
return configurations[:configCount], nil
}
func (display *Display) ChooseConfig(attribList []Attrib) ([]Config, error) {
var eglAttribs *C.EGLint
if attribList != nil {
eglAttribs = (*C.EGLint)(&(attribList[0]))
}
var configCount C.EGLint
success := C.eglChooseConfig(display.eglDisplay, eglAttribs, nil, 0, &configCount)
if success == C.EGL_FALSE {
return nil, getError()
}
if configCount <= 0 {
configCount = 10
}
configurations := make([]Config, configCount)
success = C.eglChooseConfig(
display.eglDisplay,
eglAttribs,
(*C.EGLConfig)(&(configurations[0])),
configCount,
&configCount)
//fmt.Printf("success == %d, configCount == %d\n", success, configCount)
if success == C.EGL_FALSE {
return nil, getError()
}
if configCount <= 0 {
return nil, errors.New("eglGetConfigs() returned zero configs")
}
return configurations[:configCount], nil
}
func (display *Display) GetConfigAttrib(config Config, name Attrib) (Attrib, error) {
var value Attrib
success := C.eglGetConfigAttrib(display.eglDisplay, C.EGLConfig(config), C.EGLint(name), (*C.EGLint)(&value))
if success == C.EGL_FALSE {
return None, getError()
}
return value, nil
}
func (display *Display) PrintAllConfigs() {
configs, configErr := display.GetConfigs()
if configErr != nil {
fmt.Printf("could not get configs: %v\n", configErr)
}
fmt.Printf("got %d configs:\n", len(configs))
for _, name := range(AllConfigAttribNames) {
fmt.Printf("\t%v:\n\t", name)
for _, config := range(configs) {
value, err := display.GetConfigAttrib(config, name)
if err != nil {
fmt.Printf("? ")
continue
}
fmt.Printf("%d ", value)
}
fmt.Printf("\n")
}
}
func (display *Display) PrintConfigs(configs []Config) {
fmt.Printf("got %d configs:\n", len(configs))
for _, name := range(AllConfigAttribNames) {
fmt.Printf("\t%v:\n\t", name)
for _, config := range(configs) {
value, err := display.GetConfigAttrib(config, name)
if err != nil {
fmt.Print("? ")
continue
}
fmt.Printf("%d ", value)
}
fmt.Printf("\n")
}
}
func (display *Display) PrintConfig(config Config) {
for _, name := range(AllConfigAttribNames) {
fmt.Printf("%v: ", name)
value, err := display.GetConfigAttrib(config, name)
if err != nil {
fmt.Print("?\n")
continue
}
fmt.Printf("%d\n", value)
}
fmt.Printf("\n")
}
func (display *Display) CreatePbufferSurface(config Config, attribList []Attrib) (*Surface, error) {
var eglAttribs *C.EGLint
if attribList != nil {
eglAttribs = (*C.EGLint)(&(attribList[0]))
}
eglSurface := C.eglCreatePbufferSurface(display.eglDisplay, C.EGLConfig(config), eglAttribs)
if eglSurface == noSurface {
return nil, getError()
}
surface := new(Surface)
//runtime.SetFinalizer(surface, destroySurface)
surface.Display = display
surface.eglSurface = eglSurface
return surface, nil
}
func (display *Display) CreateContext(config Config, shareContext *Context, attribList []Attrib) (*Context, error) {
var eglShareContext C.EGLContext
if shareContext != nil {
eglShareContext = shareContext.eglContext
}
var eglAttribs *C.EGLint
if attribList != nil {
eglAttribs = (*C.EGLint)(&(attribList[0]))
}
eglContext := C.eglCreateContext(display.eglDisplay, C.EGLConfig(config), eglShareContext, eglAttribs)
if eglContext == noContext {
return nil, getError()
}
context := new(Context)
//runtime.SetFinalizer(context, destroyContext)
context.eglContext = eglContext
context.Display = display
return context, nil
}
func (display *Display) String() string {
var buffer bytes.Buffer
vendor, vendorErr := display.QueryString(Vendor)
var vendorStr string
if vendorErr != nil {
vendorStr = "couldn't query vendor\n"
} else {
vendorStr = fmt.Sprintf("vendor is:\n\t%v\n", vendor)
}
buffer.WriteString(vendorStr)
version, versionErr := display.QueryString(Version)
var versionStr string
if versionErr != nil {
versionStr = "couldn't query version\n"
} else {
versionStr = fmt.Sprintf("version is:\n\t%v\n", version)
}
buffer.WriteString(versionStr)
extensions, extensionsErr := display.QueryString(Extensions)
var extensionsStr string
if extensionsErr != nil {
extensionsStr = "couldn't query extensions\n"
} else {
extensionsStr = fmt.Sprintf("extensions are:\n\t%v\n", extensions)
}
buffer.WriteString(extensionsStr)
apis, apisErr := display.QueryString(ClientAPIs)
var apisStr string
if apisErr != nil {
apisStr = "couldn't query APIs\n"
} else {
apisStr = fmt.Sprintf("client APIs are:\n\t%v\n", apis)
}
buffer.WriteString(apisStr)
return buffer.String()
}
/*
* WARNING: this method does not work with Mesa as of
* version 8.x. Their implementation violates the EGL
* specification and does not allow you to release a context.
*/
func (display *Display) ReleaseCurrentContext() error {
success := C.eglMakeCurrent(display.eglDisplay, noSurface, noSurface, noContext)
if success == C.EGL_FALSE {
return getError()
}
return nil
}