context_gl_x11.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. /*
  55. static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {
  56. //print_line(String()+"getting proc of: "+p_function);
  57. GLWrapperFuncPtr func=(GLWrapperFuncPtr)glXGetProcAddress( (const GLubyte*) p_function);
  58. if (!func) {
  59. print_line("Couldn't find function: "+String(p_function));
  60. }
  61. return func;
  62. }*/
  63. static bool ctxErrorOccurred = false;
  64. static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
  65. ctxErrorOccurred = true;
  66. return 0;
  67. }
  68. static void set_class_hint(Display *p_display, Window p_window) {
  69. XClassHint *classHint;
  70. /* set the name and class hints for the window manager to use */
  71. classHint = XAllocClassHint();
  72. if (classHint) {
  73. classHint->res_name = (char *)"Godot_Engine";
  74. classHint->res_class = (char *)"Godot";
  75. }
  76. XSetClassHint(p_display, p_window, classHint);
  77. XFree(classHint);
  78. }
  79. Error ContextGL_X11::initialize() {
  80. //const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  81. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  82. ERR_FAIL_COND_V(!glXCreateContextAttribsARB, ERR_UNCONFIGURED);
  83. static int visual_attribs[] = {
  84. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  85. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  86. GLX_DOUBLEBUFFER, true,
  87. GLX_RED_SIZE, 1,
  88. GLX_GREEN_SIZE, 1,
  89. GLX_BLUE_SIZE, 1,
  90. GLX_DEPTH_SIZE, 24,
  91. None
  92. };
  93. int fbcount;
  94. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  95. ERR_FAIL_COND_V(!fbc, ERR_UNCONFIGURED);
  96. XVisualInfo *vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  97. XSetWindowAttributes swa;
  98. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  99. swa.border_pixel = 0;
  100. swa.event_mask = StructureNotifyMask;
  101. /*
  102. char* windowid = getenv("GODOT_WINDOWID");
  103. if (windowid) {
  104. //freopen("/home/punto/stdout", "w", stdout);
  105. //reopen("/home/punto/stderr", "w", stderr);
  106. x11_window = atol(windowid);
  107. } else {
  108. */
  109. 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, CWBorderPixel | CWColormap | CWEventMask, &swa);
  110. ERR_FAIL_COND_V(!x11_window, ERR_UNCONFIGURED);
  111. set_class_hint(x11_display, x11_window);
  112. XMapWindow(x11_display, x11_window);
  113. //};
  114. int (*oldHandler)(Display *, XErrorEvent *) =
  115. XSetErrorHandler(&ctxErrorHandler);
  116. if (!opengl_3_context) {
  117. //oldstyle context:
  118. p->glx_context = glXCreateContext(x11_display, vi, 0, GL_TRUE);
  119. } else {
  120. static int context_attribs[] = {
  121. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  122. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  123. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  124. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
  125. None
  126. };
  127. p->glx_context = glXCreateContextAttribsARB(x11_display, fbc[0], NULL, true, context_attribs);
  128. ERR_EXPLAIN("Could not obtain an OpenGL 3.3 context!");
  129. ERR_FAIL_COND_V(ctxErrorOccurred || !p->glx_context, ERR_UNCONFIGURED);
  130. }
  131. XSync(x11_display, False);
  132. XSetErrorHandler(oldHandler);
  133. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  134. /*
  135. glWrapperInit(wrapper_get_proc_address);
  136. glFlush();
  137. glXSwapBuffers(x11_display,x11_window);
  138. */
  139. //glXMakeCurrent(x11_display, None, NULL);
  140. XFree(vi);
  141. XFree(fbc);
  142. return OK;
  143. }
  144. int ContextGL_X11::get_window_width() {
  145. XWindowAttributes xwa;
  146. XGetWindowAttributes(x11_display, x11_window, &xwa);
  147. return xwa.width;
  148. }
  149. int ContextGL_X11::get_window_height() {
  150. XWindowAttributes xwa;
  151. XGetWindowAttributes(x11_display, x11_window, &xwa);
  152. return xwa.height;
  153. }
  154. void ContextGL_X11::set_use_vsync(bool p_use) {
  155. static bool setup = false;
  156. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = NULL;
  157. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = NULL;
  158. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = NULL;
  159. if (!setup) {
  160. setup = true;
  161. String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  162. if (extensions.find("GLX_EXT_swap_control") != -1)
  163. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  164. if (extensions.find("GLX_MESA_swap_control") != -1)
  165. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  166. if (extensions.find("GLX_SGI_swap_control") != -1)
  167. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  168. }
  169. int val = p_use ? 1 : 0;
  170. if (glXSwapIntervalMESA) {
  171. glXSwapIntervalMESA(val);
  172. } else if (glXSwapIntervalSGI) {
  173. glXSwapIntervalSGI(val);
  174. } else if (glXSwapIntervalEXT) {
  175. GLXDrawable drawable = glXGetCurrentDrawable();
  176. glXSwapIntervalEXT(x11_display, drawable, val);
  177. } else
  178. return;
  179. use_vsync = p_use;
  180. }
  181. bool ContextGL_X11::is_using_vsync() const {
  182. return use_vsync;
  183. }
  184. ContextGL_X11::ContextGL_X11(::Display *p_x11_display, ::Window &p_x11_window, const OS::VideoMode &p_default_video_mode, bool p_opengl_3_context) :
  185. x11_window(p_x11_window) {
  186. default_video_mode = p_default_video_mode;
  187. x11_display = p_x11_display;
  188. opengl_3_context = p_opengl_3_context;
  189. double_buffer = false;
  190. direct_render = false;
  191. glx_minor = glx_major = 0;
  192. p = memnew(ContextGL_X11_Private);
  193. p->glx_context = 0;
  194. use_vsync = false;
  195. }
  196. ContextGL_X11::~ContextGL_X11() {
  197. release_current();
  198. glXDestroyContext(x11_display, p->glx_context);
  199. memdelete(p);
  200. }
  201. #endif
  202. #endif