context_x11.go 2.5 KB

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