2
0

context_gl_x11.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* context_gl_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "context_gl_x11.h"
  30. #ifdef X11_ENABLED
  31. #if defined(OPENGL_ENABLED) || defined(LEGACYGL_ENABLED)
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <GL/glx.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. struct ContextGL_X11_Private {
  40. ::GLXContext glx_context;
  41. };
  42. void ContextGL_X11::release_current() {
  43. glXMakeCurrent(x11_display, None, NULL);
  44. }
  45. void ContextGL_X11::make_current() {
  46. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  47. }
  48. void ContextGL_X11::swap_buffers() {
  49. glXSwapBuffers(x11_display,x11_window);
  50. }
  51. /*
  52. static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {
  53. //print_line(String()+"getting proc of: "+p_function);
  54. GLWrapperFuncPtr func=(GLWrapperFuncPtr)glXGetProcAddress( (const GLubyte*) p_function);
  55. if (!func) {
  56. print_line("Couldn't find function: "+String(p_function));
  57. }
  58. return func;
  59. }*/
  60. Error ContextGL_X11::initialize() {
  61. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = NULL;
  62. // const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  63. glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
  64. ERR_FAIL_COND_V( !glXCreateContextAttribsARB, ERR_UNCONFIGURED );
  65. static int visual_attribs[] = {
  66. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  67. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  68. GLX_DOUBLEBUFFER, true,
  69. GLX_RED_SIZE, 1,
  70. GLX_GREEN_SIZE, 1,
  71. GLX_BLUE_SIZE, 1,
  72. GLX_DEPTH_SIZE,0,
  73. None
  74. };
  75. int fbcount;
  76. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  77. ERR_FAIL_COND_V(!fbc,ERR_UNCONFIGURED);
  78. XVisualInfo *vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  79. XSetWindowAttributes swa;
  80. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  81. swa.border_pixel = 0;
  82. swa.event_mask = StructureNotifyMask;
  83. /*
  84. char* windowid = getenv("GODOT_WINDOWID");
  85. if (windowid) {
  86. //freopen("/home/punto/stdout", "w", stdout);
  87. //reopen("/home/punto/stderr", "w", stderr);
  88. x11_window = atol(windowid);
  89. } else {
  90. */
  91. 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);
  92. ERR_FAIL_COND_V(!x11_window,ERR_UNCONFIGURED);
  93. XMapWindow(x11_display, x11_window);
  94. while(true) {
  95. // wait for mapnotify (window created)
  96. XEvent e;
  97. XNextEvent(x11_display, &e);
  98. if (e.type == MapNotify)
  99. break;
  100. }
  101. //};
  102. if (!opengl_3_context) {
  103. //oldstyle context:
  104. p->glx_context = glXCreateContext(x11_display, vi, 0, GL_TRUE);
  105. } else {
  106. static int context_attribs[] = {
  107. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  108. GLX_CONTEXT_MINOR_VERSION_ARB, 0,
  109. None
  110. };
  111. p->glx_context = glXCreateContextAttribsARB(x11_display, fbc[0], NULL, true, context_attribs);
  112. ERR_FAIL_COND_V(!p->glx_context,ERR_UNCONFIGURED);
  113. }
  114. glXMakeCurrent(x11_display, x11_window, p->glx_context);
  115. /*
  116. glWrapperInit(wrapper_get_proc_address);
  117. glFlush();
  118. glXSwapBuffers(x11_display,x11_window);
  119. */
  120. //glXMakeCurrent(x11_display, None, NULL);
  121. return OK;
  122. }
  123. int ContextGL_X11::get_window_width() {
  124. XWindowAttributes xwa;
  125. XGetWindowAttributes(x11_display,x11_window,&xwa);
  126. return xwa.width;
  127. }
  128. int ContextGL_X11::get_window_height() {
  129. XWindowAttributes xwa;
  130. XGetWindowAttributes(x11_display,x11_window,&xwa);
  131. return xwa.height;
  132. }
  133. ContextGL_X11::ContextGL_X11(::Display *p_x11_display,::Window &p_x11_window,const OS::VideoMode& p_default_video_mode,bool p_opengl_3_context) : x11_window(p_x11_window) {
  134. default_video_mode=p_default_video_mode;
  135. x11_display=p_x11_display;
  136. opengl_3_context=p_opengl_3_context;
  137. double_buffer=false;
  138. direct_render=false;
  139. glx_minor=glx_major=0;
  140. p = memnew( ContextGL_X11_Private );
  141. p->glx_context=0;
  142. }
  143. ContextGL_X11::~ContextGL_X11() {
  144. memdelete( p );
  145. }
  146. #endif
  147. #endif