Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/mobile/gl: use EGL to determine the version of OpenGL ES #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions gl/egl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build linux
// +build linux

package gl

// #include <EGL/egl.h>
// #cgo LDFLAGS: -lEGL
import "C"

const (
versionES2 = "GL_ES_2_0"
versionES3 = "GL_ES_3_0"
)

func init() {
display := C.eglGetDisplay(C.EGL_DEFAULT_DISPLAY)

if C.eglInitialize(display, nil, nil) == C.EGL_FALSE {
return
}
defer C.eglTerminate(display)

attributes := []C.EGLint{
C.EGL_RED_SIZE, 1,
C.EGL_GREEN_SIZE, 1,
C.EGL_BLUE_SIZE, 1,
C.EGL_NONE,
}

var (
config C.EGLConfig
count C.EGLint
)
if C.eglChooseConfig(display, &attributes[0], &config, 1, &count) == C.EGL_FALSE {
return
}

C.eglBindAPI(C.EGL_OPENGL_ES_API)

attributes = []C.EGLint{
C.EGL_CONTEXT_CLIENT_VERSION, 3,
C.EGL_NONE,
}

context := C.eglCreateContext(display, config, C.EGLContext(C.EGL_NO_CONTEXT), &attributes[0])
if context == nil {
version = versionES2

return
}

C.eglDestroyContext(display, context)

version = versionES3
}
17 changes: 8 additions & 9 deletions gl/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ func NewContext() (Context, Worker) {
work: make(chan call, workbufLen),
retvalue: make(chan C.uintptr_t),
}
if C.GLES_VERSION == "GL_ES_2_0" {
if version == "GL_ES_2_0" {
return glctx, glctx
}
return context3{glctx}, glctx
}

// Version returns a GL ES version string, either "GL_ES_2_0" or "GL_ES_3_0".
// Future versions of the gl package may return "GL_ES_3_1".
// version is determined at runtime on platforms that support EGL.
var version = C.GLES_VERSION

// Version returns a GL ES Version string such as "GL_ES_2_0" or "GL_ES_3_0".
func Version() string {
return C.GLES_VERSION
return version
}

func (ctx *context) enqueue(c call) uintptr {
Expand Down Expand Up @@ -148,11 +150,8 @@ func (ctx *context) DoWork() {
}
}

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

// cString creates C string off the Go heap.
// ret is a *char.
Expand Down