Skip to content

Commit 50ede83

Browse files
committed
x/mobile/gl: use EGL to determine the version of OpenGL ES
1 parent 9361995 commit 50ede83

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

gl/egl.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux
6+
// +build linux
7+
8+
package gl
9+
10+
// #include <EGL/egl.h>
11+
// #cgo LDFLAGS: -lEGL
12+
import "C"
13+
14+
func init() {
15+
display := C.eglGetDisplay(C.EGL_DEFAULT_DISPLAY)
16+
17+
C.eglInitialize(display, nil, nil)
18+
19+
attributes := []C.EGLint{
20+
C.EGL_RED_SIZE, 1,
21+
C.EGL_GREEN_SIZE, 1,
22+
C.EGL_BLUE_SIZE, 1,
23+
C.EGL_NONE,
24+
}
25+
26+
var (
27+
config C.EGLConfig
28+
count C.EGLint
29+
)
30+
C.eglChooseConfig(display, &attributes[0], &config, 1, &count)
31+
32+
C.eglBindAPI(C.EGL_OPENGL_ES_API)
33+
34+
attributes = []C.EGLint{
35+
C.EGL_CONTEXT_CLIENT_VERSION, 3,
36+
C.EGL_NONE,
37+
}
38+
39+
context := C.eglCreateContext(display, config, C.EGLContext(C.EGL_NO_CONTEXT), &attributes[0])
40+
if context == nil {
41+
version = "GL_ES_2_0"
42+
43+
return
44+
}
45+
46+
C.eglDestroyContext(display, context)
47+
48+
version = "GL_ES_3_0"
49+
}

gl/work.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,19 @@ func NewContext() (Context, Worker) {
8484
work: make(chan call, workbufLen),
8585
retvalue: make(chan C.uintptr_t),
8686
}
87-
if C.GLES_VERSION == "GL_ES_2_0" {
87+
if version == "GL_ES_2_0" {
8888
return glctx, glctx
8989
}
9090
return context3{glctx}, glctx
9191
}
9292

93+
// version is determined at runtime on platforms that support EGL.
94+
var version = C.GLES_VERSION
95+
9396
// Version returns a GL ES version string, either "GL_ES_2_0" or "GL_ES_3_0".
9497
// Future versions of the gl package may return "GL_ES_3_1".
9598
func Version() string {
96-
return C.GLES_VERSION
99+
return version
97100
}
98101

99102
func (ctx *context) enqueue(c call) uintptr {
@@ -148,11 +151,8 @@ func (ctx *context) DoWork() {
148151
}
149152
}
150153

151-
func init() {
152-
if unsafe.Sizeof(C.GLint(0)) != unsafe.Sizeof(int32(0)) {
153-
panic("GLint is not an int32")
154-
}
155-
}
154+
// GLint should be the same size as int32
155+
var _ [unsafe.Sizeof(C.GLint(0))]struct{} = [unsafe.Sizeof(int32(0))]struct{}{}
156156

157157
// cString creates C string off the Go heap.
158158
// ret is a *char.

0 commit comments

Comments
 (0)