context_gl_x11.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. ERR_FAIL_COND_V(!fbconfig, ERR_UNCONFIGURED);
  121. swa.background_pixmap = None;
  122. swa.background_pixel = 0;
  123. swa.border_pixmap = None;
  124. valuemask |= CWBackPixel;
  125. } else {
  126. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  127. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  128. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  129. fbconfig = fbc[0];
  130. }
  131. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);
  132. switch (context_type) {
  133. case OLDSTYLE: {
  134. p->glx_context = glXCreateContext(x11_display, vi, 0, GL_TRUE);
  135. ERR_FAIL_COND_V(!p->glx_context, ERR_UNCONFIGURED);
  136. } break;
  137. case GLES_2_0_COMPATIBLE: {
  138. p->glx_context = glXCreateNewContext(x11_display, fbconfig, GLX_RGBA_TYPE, 0, true);
  139. ERR_FAIL_COND_V(!p->glx_context, ERR_UNCONFIGURED);
  140. } break;
  141. case GLES_3_0_COMPATIBLE: {
  142. static int context_attribs[] = {
  143. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  144. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  145. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  146. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
  147. None
  148. };
  149. p->glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, NULL, true, context_attribs);
  150. ERR_FAIL_COND_V(ctxErrorOccurred || !p->glx_context, ERR_UNCONFIGURED);
  151. } break;
  152. }
  153. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  154. 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);
  155. ERR_FAIL_COND_V(!x11_window, ERR_UNCONFIGURED);
  156. set_class_hint(x11_display, x11_window);
  157. XMapWindow(x11_display, x11_window);
  158. XSync(x11_display, False);
  159. XSetErrorHandler(oldHandler);
  160. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  161. XFree(vi);
  162. return OK;
  163. }
  164. int ContextGL_X11::get_window_width() {
  165. XWindowAttributes xwa;
  166. XGetWindowAttributes(x11_display, x11_window, &xwa);
  167. return xwa.width;
  168. }
  169. int ContextGL_X11::get_window_height() {
  170. XWindowAttributes xwa;
  171. XGetWindowAttributes(x11_display, x11_window, &xwa);
  172. return xwa.height;
  173. }
  174. void ContextGL_X11::set_use_vsync(bool p_use) {
  175. static bool setup = false;
  176. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = NULL;
  177. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = NULL;
  178. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = NULL;
  179. if (!setup) {
  180. setup = true;
  181. String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  182. if (extensions.find("GLX_EXT_swap_control") != -1)
  183. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  184. if (extensions.find("GLX_MESA_swap_control") != -1)
  185. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  186. if (extensions.find("GLX_SGI_swap_control") != -1)
  187. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  188. }
  189. int val = p_use ? 1 : 0;
  190. if (glXSwapIntervalMESA) {
  191. glXSwapIntervalMESA(val);
  192. } else if (glXSwapIntervalSGI) {
  193. glXSwapIntervalSGI(val);
  194. } else if (glXSwapIntervalEXT) {
  195. GLXDrawable drawable = glXGetCurrentDrawable();
  196. glXSwapIntervalEXT(x11_display, drawable, val);
  197. } else
  198. return;
  199. use_vsync = p_use;
  200. }
  201. bool ContextGL_X11::is_using_vsync() const {
  202. return use_vsync;
  203. }
  204. ContextGL_X11::ContextGL_X11(::Display *p_x11_display, ::Window &p_x11_window, const OS::VideoMode &p_default_video_mode, ContextType p_context_type) :
  205. x11_window(p_x11_window) {
  206. default_video_mode = p_default_video_mode;
  207. x11_display = p_x11_display;
  208. context_type = p_context_type;
  209. double_buffer = false;
  210. direct_render = false;
  211. glx_minor = glx_major = 0;
  212. p = memnew(ContextGL_X11_Private);
  213. p->glx_context = 0;
  214. use_vsync = false;
  215. }
  216. ContextGL_X11::~ContextGL_X11() {
  217. release_current();
  218. glXDestroyContext(x11_display, p->glx_context);
  219. memdelete(p);
  220. }
  221. #endif
  222. #endif