| 1 |
// Copyright 2014 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 && !android |
| 6 |
|
| 7 |
package glutil |
| 8 |
|
| 9 |
/* |
| 10 |
#cgo LDFLAGS: -lEGL |
| 11 |
#include <EGL/egl.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
|
| 15 |
void createContext(EGLDisplay *out_dpy, EGLContext *out_ctx, EGLSurface *out_surf) { |
| 16 |
EGLDisplay e_dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 17 |
if (!e_dpy) { |
| 18 |
fprintf(stderr, "eglGetDisplay failed\n"); |
| 19 |
exit(1); |
| 20 |
} |
| 21 |
EGLint e_major, e_minor; |
| 22 |
if (!eglInitialize(e_dpy, &e_major, &e_minor)) { |
| 23 |
fprintf(stderr, "eglInitialize failed\n"); |
| 24 |
exit(1); |
| 25 |
} |
| 26 |
eglBindAPI(EGL_OPENGL_ES_API); |
| 27 |
static const EGLint config_attribs[] = { |
| 28 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 29 |
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 30 |
EGL_BLUE_SIZE, 8, |
| 31 |
EGL_GREEN_SIZE, 8, |
| 32 |
EGL_RED_SIZE, 8, |
| 33 |
EGL_CONFIG_CAVEAT, EGL_NONE, |
| 34 |
EGL_NONE |
| 35 |
}; |
| 36 |
EGLConfig config; |
| 37 |
EGLint num_configs; |
| 38 |
if (!eglChooseConfig(e_dpy, config_attribs, &config, 1, &num_configs)) { |
| 39 |
fprintf(stderr, "eglChooseConfig failed\n"); |
| 40 |
exit(1); |
| 41 |
} |
| 42 |
static const EGLint ctx_attribs[] = { |
| 43 |
EGL_CONTEXT_CLIENT_VERSION, 2, |
| 44 |
EGL_NONE |
| 45 |
}; |
| 46 |
EGLContext e_ctx = eglCreateContext(e_dpy, config, EGL_NO_CONTEXT, ctx_attribs); |
| 47 |
if (e_ctx == EGL_NO_CONTEXT) { |
| 48 |
fprintf(stderr, "eglCreateContext failed\n"); |
| 49 |
exit(1); |
| 50 |
} |
| 51 |
static const EGLint pbuf_attribs[] = { |
| 52 |
EGL_NONE |
| 53 |
}; |
| 54 |
EGLSurface e_surf = eglCreatePbufferSurface(e_dpy, config, pbuf_attribs); |
| 55 |
if (e_surf == EGL_NO_SURFACE) { |
| 56 |
fprintf(stderr, "eglCreatePbufferSurface failed\n"); |
| 57 |
exit(1); |
| 58 |
} |
| 59 |
if (!eglMakeCurrent(e_dpy, e_surf, e_surf, e_ctx)) { |
| 60 |
fprintf(stderr, "eglMakeCurrent failed\n"); |
| 61 |
exit(1); |
| 62 |
} |
| 63 |
*out_surf = e_surf; |
| 64 |
*out_ctx = e_ctx; |
| 65 |
*out_dpy = e_dpy; |
| 66 |
} |
| 67 |
|
| 68 |
void destroyContext(EGLDisplay e_dpy, EGLContext e_ctx, EGLSurface e_surf) { |
| 69 |
if (!eglMakeCurrent(e_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { |
| 70 |
fprintf(stderr, "eglMakeCurrent failed\n"); |
| 71 |
exit(1); |
| 72 |
} |
| 73 |
if (!eglDestroySurface(e_dpy, e_surf)) { |
| 74 |
fprintf(stderr, "eglDestroySurface failed\n"); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
if (!eglDestroyContext(e_dpy, e_ctx)) { |
| 78 |
fprintf(stderr, "eglDestroyContext failed\n"); |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
} |
| 82 |
*/ |
| 83 |
import "C" |
| 84 |
|
| 85 |
import ( |
| 86 |
"runtime" |
| 87 |
) |
| 88 |
|
| 89 |
type contextGL struct { |
| 90 |
dpy C.EGLDisplay |
| 91 |
ctx C.EGLContext |
| 92 |
surf C.EGLSurface |
| 93 |
} |
| 94 |
|
| 95 |
func createContext() (*contextGL, error) { |
| 96 |
runtime.LockOSThread() |
| 97 |
c := &contextGL{} |
| 98 |
C.createContext(&c.dpy, &c.ctx, &c.surf) |
| 99 |
return c, nil |
| 100 |
} |
| 101 |
|
| 102 |
func (c *contextGL) destroy() { |
| 103 |
C.destroyContext(c.dpy, c.ctx, c.surf) |
| 104 |
runtime.UnlockOSThread() |
| 105 |
} |