gl_manager_x11.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*************************************************************************/
  2. /* gl_manager_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "gl_manager_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 GLManager_X11_Private {
  43. ::GLXContext glx_context;
  44. };
  45. GLManager_X11::GLDisplay::~GLDisplay() {
  46. if (context) {
  47. //release_current();
  48. glXDestroyContext(x11_display, context->glx_context);
  49. memdelete(context);
  50. context = nullptr;
  51. }
  52. }
  53. static bool ctxErrorOccurred = false;
  54. static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
  55. ctxErrorOccurred = true;
  56. return 0;
  57. }
  58. int GLManager_X11::_find_or_create_display(Display *p_x11_display) {
  59. for (unsigned int n = 0; n < _displays.size(); n++) {
  60. const GLDisplay &d = _displays[n];
  61. if (d.x11_display == p_x11_display)
  62. return n;
  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. ;
  73. d.context->glx_context = 0;
  74. //Error err = _create_context(d);
  75. _create_context(d);
  76. return new_display_id;
  77. }
  78. Error GLManager_X11::_create_context(GLDisplay &gl_display) {
  79. // some aliases
  80. ::Display *x11_display = gl_display.x11_display;
  81. //const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  82. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  83. ERR_FAIL_COND_V(!glXCreateContextAttribsARB, ERR_UNCONFIGURED);
  84. static int visual_attribs[] = {
  85. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  86. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  87. GLX_DOUBLEBUFFER, true,
  88. GLX_RED_SIZE, 1,
  89. GLX_GREEN_SIZE, 1,
  90. GLX_BLUE_SIZE, 1,
  91. GLX_DEPTH_SIZE, 24,
  92. None
  93. };
  94. static int visual_attribs_layered[] = {
  95. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  96. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  97. GLX_DOUBLEBUFFER, true,
  98. GLX_RED_SIZE, 8,
  99. GLX_GREEN_SIZE, 8,
  100. GLX_BLUE_SIZE, 8,
  101. GLX_ALPHA_SIZE, 8,
  102. GLX_DEPTH_SIZE, 24,
  103. None
  104. };
  105. int fbcount;
  106. GLXFBConfig fbconfig = 0;
  107. XVisualInfo *vi = nullptr;
  108. gl_display.x_swa.event_mask = StructureNotifyMask;
  109. gl_display.x_swa.border_pixel = 0;
  110. gl_display.x_valuemask = CWBorderPixel | CWColormap | CWEventMask;
  111. if (OS::get_singleton()->is_layered_allowed()) {
  112. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs_layered, &fbcount);
  113. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  114. for (int i = 0; i < fbcount; i++) {
  115. vi = (XVisualInfo *)glXGetVisualFromFBConfig(x11_display, fbc[i]);
  116. if (!vi)
  117. continue;
  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_COND_V(!fbconfig, ERR_UNCONFIGURED);
  131. gl_display.x_swa.background_pixmap = None;
  132. gl_display.x_swa.background_pixel = 0;
  133. gl_display.x_swa.border_pixmap = None;
  134. gl_display.x_valuemask |= CWBackPixel;
  135. } else {
  136. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  137. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  138. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  139. fbconfig = fbc[0];
  140. XFree(fbc);
  141. }
  142. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);
  143. switch (context_type) {
  144. case GLES_3_0_COMPATIBLE: {
  145. // FIXME: Use `GLX_CONTEXT_CORE_PROFILE_BIT_ARB` instead of compatibility profile
  146. // once deprecated API usages are fixed.
  147. static int context_attribs[] = {
  148. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  149. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  150. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  151. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
  152. None
  153. };
  154. gl_display.context->glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  155. ERR_FAIL_COND_V(ctxErrorOccurred || !gl_display.context->glx_context, ERR_UNCONFIGURED);
  156. } break;
  157. }
  158. gl_display.x_swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  159. XSync(x11_display, False);
  160. XSetErrorHandler(oldHandler);
  161. // make our own copy of the vi data
  162. // for later creating windows using this display
  163. if (vi) {
  164. gl_display.x_vi = *vi;
  165. }
  166. XFree(vi);
  167. return OK;
  168. }
  169. Error GLManager_X11::window_create(DisplayServer::WindowID p_window_id, ::Window p_window, Display *p_display, int p_width, int p_height) {
  170. print_line("window_create window id " + itos(p_window_id));
  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. // the display could be invalid .. check NYI
  185. GLDisplay &gl_display = _displays[win.gldisplay_id];
  186. //const XVisualInfo &vi = gl_display.x_vi;
  187. //XSetWindowAttributes &swa = gl_display.x_swa;
  188. ::Display *x11_display = gl_display.x11_display;
  189. ::Window &x11_window = win.x11_window;
  190. if (!glXMakeCurrent(x11_display, x11_window, gl_display.context->glx_context)) {
  191. ERR_PRINT("glXMakeCurrent failed");
  192. }
  193. _internal_set_current_window(&win);
  194. return OK;
  195. }
  196. void GLManager_X11::_internal_set_current_window(GLWindow *p_win) {
  197. _current_window = p_win;
  198. // quick access to x info
  199. _x_windisp.x11_window = _current_window->x11_window;
  200. const GLDisplay &disp = get_current_display();
  201. _x_windisp.x11_display = disp.x11_display;
  202. }
  203. void GLManager_X11::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  204. get_window(p_window_id).width = p_width;
  205. get_window(p_window_id).height = p_height;
  206. }
  207. void GLManager_X11::window_destroy(DisplayServer::WindowID p_window_id) {
  208. GLWindow &win = get_window(p_window_id);
  209. win.in_use = false;
  210. if (_current_window == &win) {
  211. _current_window = nullptr;
  212. _x_windisp.x11_display = nullptr;
  213. _x_windisp.x11_window = -1;
  214. }
  215. }
  216. void GLManager_X11::release_current() {
  217. if (!_current_window)
  218. return;
  219. glXMakeCurrent(_x_windisp.x11_display, None, nullptr);
  220. }
  221. void GLManager_X11::window_make_current(DisplayServer::WindowID p_window_id) {
  222. if (p_window_id == -1)
  223. return;
  224. GLWindow &win = _windows[p_window_id];
  225. if (!win.in_use)
  226. return;
  227. // noop
  228. if (&win == _current_window)
  229. return;
  230. const GLDisplay &disp = get_display(win.gldisplay_id);
  231. glXMakeCurrent(disp.x11_display, win.x11_window, disp.context->glx_context);
  232. _internal_set_current_window(&win);
  233. }
  234. void GLManager_X11::make_current() {
  235. if (!_current_window)
  236. return;
  237. if (!_current_window->in_use) {
  238. WARN_PRINT("current window not in use!");
  239. return;
  240. }
  241. const GLDisplay &disp = get_current_display();
  242. glXMakeCurrent(_x_windisp.x11_display, _x_windisp.x11_window, disp.context->glx_context);
  243. }
  244. void GLManager_X11::swap_buffers() {
  245. // NO NEED TO CALL SWAP BUFFERS for each window...
  246. // see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXSwapBuffers.xml
  247. if (!_current_window)
  248. return;
  249. if (!_current_window->in_use) {
  250. WARN_PRINT("current window not in use!");
  251. return;
  252. }
  253. // print_line("\tswap_buffers");
  254. // only for debugging without drawing anything
  255. // glClearColor(Math::randf(), 0, 1, 1);
  256. //glClear(GL_COLOR_BUFFER_BIT);
  257. //const GLDisplay &disp = get_current_display();
  258. glXSwapBuffers(_x_windisp.x11_display, _x_windisp.x11_window);
  259. }
  260. Error GLManager_X11::initialize() {
  261. return OK;
  262. }
  263. void GLManager_X11::set_use_vsync(bool p_use) {
  264. static bool setup = false;
  265. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
  266. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
  267. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
  268. // force vsync in the editor for now, as a safety measure
  269. bool is_editor = Engine::get_singleton()->is_editor_hint();
  270. if (is_editor) {
  271. p_use = true;
  272. }
  273. // we need an active window to get a display to set the vsync
  274. if (!_current_window)
  275. return;
  276. const GLDisplay &disp = get_current_display();
  277. if (!setup) {
  278. setup = true;
  279. String extensions = glXQueryExtensionsString(disp.x11_display, DefaultScreen(disp.x11_display));
  280. if (extensions.find("GLX_EXT_swap_control") != -1)
  281. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  282. if (extensions.find("GLX_MESA_swap_control") != -1)
  283. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  284. if (extensions.find("GLX_SGI_swap_control") != -1)
  285. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  286. }
  287. int val = p_use ? 1 : 0;
  288. if (glXSwapIntervalMESA) {
  289. glXSwapIntervalMESA(val);
  290. } else if (glXSwapIntervalSGI) {
  291. glXSwapIntervalSGI(val);
  292. } else if (glXSwapIntervalEXT) {
  293. GLXDrawable drawable = glXGetCurrentDrawable();
  294. glXSwapIntervalEXT(disp.x11_display, drawable, val);
  295. } else
  296. return;
  297. use_vsync = p_use;
  298. }
  299. bool GLManager_X11::is_using_vsync() const {
  300. return use_vsync;
  301. }
  302. GLManager_X11::GLManager_X11(const Vector2i &p_size, ContextType p_context_type) {
  303. context_type = p_context_type;
  304. double_buffer = false;
  305. direct_render = false;
  306. glx_minor = glx_major = 0;
  307. use_vsync = false;
  308. _current_window = nullptr;
  309. }
  310. GLManager_X11::~GLManager_X11() {
  311. release_current();
  312. }
  313. #endif
  314. #endif