context_gl_x11.cpp 9.3 KB

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