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