context_x11.go 2.5 KB

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