gl_manager_x11.cpp 11 KB

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