gl_manager_x11.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**************************************************************************/
  2. /* gl_manager_x11.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "gl_manager_x11.h"
  31. #if defined(X11_ENABLED) && defined(GLES3_ENABLED)
  32. #include "thirdparty/glad/glad/glx.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  37. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  38. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  39. // To prevent shadowing warnings
  40. #undef glXCreateContextAttribsARB
  41. struct GLManager_X11_Private {
  42. ::GLXContext glx_context;
  43. };
  44. GLManager_X11::GLDisplay::~GLDisplay() {
  45. if (context) {
  46. //release_current();
  47. glXDestroyContext(x11_display, context->glx_context);
  48. memdelete(context);
  49. context = nullptr;
  50. }
  51. }
  52. static bool ctxErrorOccurred = false;
  53. static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
  54. ctxErrorOccurred = true;
  55. return 0;
  56. }
  57. int GLManager_X11::_find_or_create_display(Display *p_x11_display) {
  58. for (unsigned int n = 0; n < _displays.size(); n++) {
  59. const GLDisplay &d = _displays[n];
  60. if (d.x11_display == p_x11_display) {
  61. return n;
  62. }
  63. }
  64. // create
  65. GLDisplay d_temp;
  66. d_temp.x11_display = p_x11_display;
  67. _displays.push_back(d_temp);
  68. int new_display_id = _displays.size() - 1;
  69. // create context
  70. GLDisplay &d = _displays[new_display_id];
  71. d.context = memnew(GLManager_X11_Private);
  72. d.context->glx_context = nullptr;
  73. Error err = _create_context(d);
  74. if (err != OK) {
  75. _displays.remove_at(new_display_id);
  76. return -1;
  77. }
  78. return new_display_id;
  79. }
  80. Error GLManager_X11::_create_context(GLDisplay &gl_display) {
  81. // some aliases
  82. ::Display *x11_display = gl_display.x11_display;
  83. //const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  84. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  85. ERR_FAIL_NULL_V(glXCreateContextAttribsARB, ERR_UNCONFIGURED);
  86. static int visual_attribs[] = {
  87. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  88. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  89. GLX_DOUBLEBUFFER, true,
  90. GLX_RED_SIZE, 1,
  91. GLX_GREEN_SIZE, 1,
  92. GLX_BLUE_SIZE, 1,
  93. GLX_DEPTH_SIZE, 24,
  94. None
  95. };
  96. static int visual_attribs_layered[] = {
  97. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  98. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  99. GLX_DOUBLEBUFFER, true,
  100. GLX_RED_SIZE, 8,
  101. GLX_GREEN_SIZE, 8,
  102. GLX_BLUE_SIZE, 8,
  103. GLX_ALPHA_SIZE, 8,
  104. GLX_DEPTH_SIZE, 24,
  105. None
  106. };
  107. int fbcount;
  108. GLXFBConfig fbconfig = nullptr;
  109. XVisualInfo *vi = nullptr;
  110. if (OS::get_singleton()->is_layered_allowed()) {
  111. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs_layered, &fbcount);
  112. ERR_FAIL_NULL_V(fbc, ERR_UNCONFIGURED);
  113. for (int i = 0; i < fbcount; i++) {
  114. vi = (XVisualInfo *)glXGetVisualFromFBConfig(x11_display, fbc[i]);
  115. if (!vi) {
  116. continue;
  117. }
  118. XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi->visual);
  119. if (!pict_format) {
  120. XFree(vi);
  121. vi = nullptr;
  122. continue;
  123. }
  124. fbconfig = fbc[i];
  125. if (pict_format->direct.alphaMask > 0) {
  126. break;
  127. }
  128. }
  129. XFree(fbc);
  130. ERR_FAIL_NULL_V(fbconfig, ERR_UNCONFIGURED);
  131. } else {
  132. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  133. ERR_FAIL_NULL_V(fbc, ERR_UNCONFIGURED);
  134. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  135. fbconfig = fbc[0];
  136. XFree(fbc);
  137. }
  138. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);
  139. switch (context_type) {
  140. case GLES_3_0_COMPATIBLE: {
  141. static int context_attribs[] = {
  142. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  143. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  144. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  145. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
  146. None
  147. };
  148. gl_display.context->glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  149. ERR_FAIL_COND_V(ctxErrorOccurred || !gl_display.context->glx_context, ERR_UNCONFIGURED);
  150. } break;
  151. }
  152. XSync(x11_display, False);
  153. XSetErrorHandler(oldHandler);
  154. // make our own copy of the vi data
  155. // for later creating windows using this display
  156. if (vi) {
  157. gl_display.x_vi = *vi;
  158. }
  159. XFree(vi);
  160. return OK;
  161. }
  162. XVisualInfo GLManager_X11::get_vi(Display *p_display, Error &r_error) {
  163. int display_id = _find_or_create_display(p_display);
  164. if (display_id < 0) {
  165. r_error = FAILED;
  166. return XVisualInfo();
  167. }
  168. r_error = OK;
  169. return _displays[display_id].x_vi;
  170. }
  171. Error GLManager_X11::open_display(Display *p_display) {
  172. int gldisplay_id = _find_or_create_display(p_display);
  173. if (gldisplay_id < 0) {
  174. return ERR_CANT_CREATE;
  175. } else {
  176. return OK;
  177. }
  178. }
  179. Error GLManager_X11::window_create(DisplayServer::WindowID p_window_id, ::Window p_window, Display *p_display, int p_width, int p_height) {
  180. // make sure vector is big enough...
  181. // we can mirror the external vector, it is simpler
  182. // to keep the IDs identical for fast lookup
  183. if (p_window_id >= (int)_windows.size()) {
  184. _windows.resize(p_window_id + 1);
  185. }
  186. GLWindow &win = _windows[p_window_id];
  187. win.in_use = true;
  188. win.window_id = p_window_id;
  189. win.width = p_width;
  190. win.height = p_height;
  191. win.x11_window = p_window;
  192. win.gldisplay_id = _find_or_create_display(p_display);
  193. if (win.gldisplay_id == -1) {
  194. return FAILED;
  195. }
  196. // the display could be invalid .. check NYI
  197. GLDisplay &gl_display = _displays[win.gldisplay_id];
  198. ::Display *x11_display = gl_display.x11_display;
  199. ::Window &x11_window = win.x11_window;
  200. if (!glXMakeCurrent(x11_display, x11_window, gl_display.context->glx_context)) {
  201. ERR_PRINT("glXMakeCurrent failed");
  202. }
  203. _internal_set_current_window(&win);
  204. return OK;
  205. }
  206. void GLManager_X11::_internal_set_current_window(GLWindow *p_win) {
  207. _current_window = p_win;
  208. // quick access to x info
  209. _x_windisp.x11_window = _current_window->x11_window;
  210. const GLDisplay &disp = get_current_display();
  211. _x_windisp.x11_display = disp.x11_display;
  212. }
  213. void GLManager_X11::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  214. get_window(p_window_id).width = p_width;
  215. get_window(p_window_id).height = p_height;
  216. }
  217. void GLManager_X11::window_destroy(DisplayServer::WindowID p_window_id) {
  218. GLWindow &win = get_window(p_window_id);
  219. win.in_use = false;
  220. if (_current_window == &win) {
  221. _current_window = nullptr;
  222. _x_windisp.x11_display = nullptr;
  223. _x_windisp.x11_window = -1;
  224. }
  225. }
  226. void GLManager_X11::release_current() {
  227. if (!_current_window) {
  228. return;
  229. }
  230. if (!glXMakeCurrent(_x_windisp.x11_display, None, nullptr)) {
  231. ERR_PRINT("glXMakeCurrent failed");
  232. }
  233. _current_window = nullptr;
  234. }
  235. void GLManager_X11::window_make_current(DisplayServer::WindowID p_window_id) {
  236. if (p_window_id == -1) {
  237. return;
  238. }
  239. GLWindow &win = _windows[p_window_id];
  240. if (!win.in_use) {
  241. return;
  242. }
  243. // noop
  244. if (&win == _current_window) {
  245. return;
  246. }
  247. const GLDisplay &disp = get_display(win.gldisplay_id);
  248. if (!glXMakeCurrent(disp.x11_display, win.x11_window, disp.context->glx_context)) {
  249. ERR_PRINT("glXMakeCurrent failed");
  250. }
  251. _internal_set_current_window(&win);
  252. }
  253. void GLManager_X11::swap_buffers() {
  254. if (!_current_window) {
  255. return;
  256. }
  257. if (!_current_window->in_use) {
  258. WARN_PRINT("current window not in use!");
  259. return;
  260. }
  261. // On X11, when enabled, transparency is always active, so clear alpha manually.
  262. if (OS::get_singleton()->is_layered_allowed()) {
  263. if (!DisplayServer::get_singleton()->window_get_flag(DisplayServer::WINDOW_FLAG_TRANSPARENT, _current_window->window_id)) {
  264. glColorMask(false, false, false, true);
  265. glClearColor(0, 0, 0, 1);
  266. glClear(GL_COLOR_BUFFER_BIT);
  267. glColorMask(true, true, true, true);
  268. }
  269. }
  270. glXSwapBuffers(_x_windisp.x11_display, _x_windisp.x11_window);
  271. }
  272. Error GLManager_X11::initialize(Display *p_display) {
  273. if (!gladLoaderLoadGLX(p_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(p_display)))) {
  274. return ERR_CANT_CREATE;
  275. }
  276. return OK;
  277. }
  278. void GLManager_X11::set_use_vsync(bool p_use) {
  279. // we need an active window to get a display to set the vsync
  280. if (!_current_window) {
  281. return;
  282. }
  283. const GLDisplay &disp = get_current_display();
  284. int val = p_use ? 1 : 0;
  285. if (GLAD_GLX_MESA_swap_control) {
  286. glXSwapIntervalMESA(val);
  287. } else if (GLAD_GLX_SGI_swap_control) {
  288. glXSwapIntervalSGI(val);
  289. } else if (GLAD_GLX_EXT_swap_control) {
  290. GLXDrawable drawable = glXGetCurrentDrawable();
  291. glXSwapIntervalEXT(disp.x11_display, drawable, val);
  292. } else {
  293. WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver.");
  294. return;
  295. }
  296. use_vsync = p_use;
  297. }
  298. bool GLManager_X11::is_using_vsync() const {
  299. return use_vsync;
  300. }
  301. void *GLManager_X11::get_glx_context(DisplayServer::WindowID p_window_id) {
  302. if (p_window_id == -1) {
  303. return nullptr;
  304. }
  305. const GLWindow &win = _windows[p_window_id];
  306. const GLDisplay &disp = get_display(win.gldisplay_id);
  307. return (void *)disp.context->glx_context;
  308. }
  309. GLManager_X11::GLManager_X11(const Vector2i &p_size, ContextType p_context_type) {
  310. context_type = p_context_type;
  311. double_buffer = false;
  312. direct_render = false;
  313. glx_minor = glx_major = 0;
  314. use_vsync = false;
  315. _current_window = nullptr;
  316. }
  317. GLManager_X11::~GLManager_X11() {
  318. release_current();
  319. }
  320. #endif // X11_ENABLED && GLES3_ENABLED