context_gl_x11.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* context_gl_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "context_gl_x11.h"
  31. #ifdef X11_ENABLED
  32. #if defined(OPENGL_ENABLED)
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #define GLX_GLXEXT_PROTOTYPES
  37. #include <GL/glx.h>
  38. #include <GL/glxext.h>
  39. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  40. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  41. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  42. struct ContextGL_X11_Private {
  43. ::GLXContext glx_context;
  44. };
  45. void ContextGL_X11::release_current() {
  46. glXMakeCurrent(x11_display, None, NULL);
  47. }
  48. void ContextGL_X11::make_current() {
  49. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  50. }
  51. void ContextGL_X11::swap_buffers() {
  52. glXSwapBuffers(x11_display, x11_window);
  53. }
  54. static bool ctxErrorOccurred = false;
  55. static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
  56. ctxErrorOccurred = true;
  57. return 0;
  58. }
  59. static void set_class_hint(Display *p_display, Window p_window) {
  60. XClassHint *classHint;
  61. /* set the name and class hints for the window manager to use */
  62. classHint = XAllocClassHint();
  63. if (classHint) {
  64. classHint->res_name = (char *)"Godot_Engine";
  65. classHint->res_class = (char *)"Godot";
  66. }
  67. XSetClassHint(p_display, p_window, classHint);
  68. XFree(classHint);
  69. }
  70. Error ContextGL_X11::initialize() {
  71. //const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  72. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  73. ERR_FAIL_COND_V(!glXCreateContextAttribsARB, ERR_UNCONFIGURED);
  74. static int visual_attribs[] = {
  75. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  76. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  77. GLX_DOUBLEBUFFER, true,
  78. GLX_RED_SIZE, 1,
  79. GLX_GREEN_SIZE, 1,
  80. GLX_BLUE_SIZE, 1,
  81. GLX_DEPTH_SIZE, 24,
  82. None
  83. };
  84. static int visual_attribs_layered[] = {
  85. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  86. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  87. GLX_DOUBLEBUFFER, true,
  88. GLX_RED_SIZE, 8,
  89. GLX_GREEN_SIZE, 8,
  90. GLX_BLUE_SIZE, 8,
  91. GLX_ALPHA_SIZE, 8,
  92. GLX_DEPTH_SIZE, 24,
  93. None
  94. };
  95. int fbcount;
  96. GLXFBConfig fbconfig = 0;
  97. XVisualInfo *vi = NULL;
  98. XSetWindowAttributes swa;
  99. swa.event_mask = StructureNotifyMask;
  100. swa.border_pixel = 0;
  101. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  102. if (OS::get_singleton()->is_layered_allowed()) {
  103. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs_layered, &fbcount);
  104. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  105. for (int i = 0; i < fbcount; i++) {
  106. vi = (XVisualInfo *)glXGetVisualFromFBConfig(x11_display, fbc[i]);
  107. if (!vi)
  108. continue;
  109. XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi->visual);
  110. if (!pict_format) {
  111. XFree(vi);
  112. vi = NULL;
  113. continue;
  114. }
  115. fbconfig = fbc[i];
  116. if (pict_format->direct.alphaMask > 0) {
  117. break;
  118. }
  119. }
  120. XFree(fbc);
  121. ERR_FAIL_COND_V(!fbconfig, ERR_UNCONFIGURED);
  122. swa.background_pixmap = None;
  123. swa.background_pixel = 0;
  124. swa.border_pixmap = None;
  125. valuemask |= CWBackPixel;
  126. } else {
  127. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  128. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  129. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  130. fbconfig = fbc[0];
  131. XFree(fbc);
  132. }
  133. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);
  134. switch (context_type) {
  135. case GLES_2_0_COMPATIBLE: {
  136. p->glx_context = glXCreateNewContext(x11_display, fbconfig, GLX_RGBA_TYPE, 0, true);
  137. ERR_FAIL_COND_V(!p->glx_context, ERR_UNCONFIGURED);
  138. } break;
  139. }
  140. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  141. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  142. XStoreName(x11_display, x11_window, "Godot Engine");
  143. ERR_FAIL_COND_V(!x11_window, ERR_UNCONFIGURED);
  144. set_class_hint(x11_display, x11_window);
  145. XMapWindow(x11_display, x11_window);
  146. XSync(x11_display, False);
  147. XSetErrorHandler(oldHandler);
  148. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  149. XFree(vi);
  150. return OK;
  151. }
  152. int ContextGL_X11::get_window_width() {
  153. XWindowAttributes xwa;
  154. XGetWindowAttributes(x11_display, x11_window, &xwa);
  155. return xwa.width;
  156. }
  157. int ContextGL_X11::get_window_height() {
  158. XWindowAttributes xwa;
  159. XGetWindowAttributes(x11_display, x11_window, &xwa);
  160. return xwa.height;
  161. }
  162. void ContextGL_X11::set_use_vsync(bool p_use) {
  163. static bool setup = false;
  164. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = NULL;
  165. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = NULL;
  166. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = NULL;
  167. if (!setup) {
  168. setup = true;
  169. String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  170. if (extensions.find("GLX_EXT_swap_control") != -1)
  171. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  172. if (extensions.find("GLX_MESA_swap_control") != -1)
  173. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  174. if (extensions.find("GLX_SGI_swap_control") != -1)
  175. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  176. }
  177. int val = p_use ? 1 : 0;
  178. if (glXSwapIntervalMESA) {
  179. glXSwapIntervalMESA(val);
  180. } else if (glXSwapIntervalSGI) {
  181. glXSwapIntervalSGI(val);
  182. } else if (glXSwapIntervalEXT) {
  183. GLXDrawable drawable = glXGetCurrentDrawable();
  184. glXSwapIntervalEXT(x11_display, drawable, val);
  185. } else
  186. return;
  187. use_vsync = p_use;
  188. }
  189. bool ContextGL_X11::is_using_vsync() const {
  190. return use_vsync;
  191. }
  192. ContextGL_X11::ContextGL_X11(::Display *p_x11_display, ::Window &p_x11_window, const OS::VideoMode &p_default_video_mode, ContextType p_context_type) :
  193. x11_window(p_x11_window) {
  194. default_video_mode = p_default_video_mode;
  195. x11_display = p_x11_display;
  196. context_type = p_context_type;
  197. double_buffer = false;
  198. direct_render = false;
  199. glx_minor = glx_major = 0;
  200. p = memnew(ContextGL_X11_Private);
  201. p->glx_context = 0;
  202. use_vsync = false;
  203. }
  204. ContextGL_X11::~ContextGL_X11() {
  205. release_current();
  206. glXDestroyContext(x11_display, p->glx_context);
  207. memdelete(p);
  208. }
  209. #endif
  210. #endif