glx.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "opengl/opengl.hpp"
  2. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  3. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  4. auto VideoGLX_X11ErrorHandler(Display*, XErrorEvent*) -> int {
  5. return 0; //suppress errors
  6. }
  7. struct VideoGLX : VideoDriver, OpenGL {
  8. VideoGLX& self = *this;
  9. VideoGLX(Video& super) : VideoDriver(super) { construct(); }
  10. ~VideoGLX() { destruct(); }
  11. auto create() -> bool override {
  12. VideoDriver::exclusive = true;
  13. VideoDriver::format = "ARGB24";
  14. return initialize();
  15. }
  16. auto driver() -> string override { return "OpenGL 3.2"; }
  17. auto ready() -> bool override { return _ready; }
  18. auto hasFullScreen() -> bool override { return true; }
  19. auto hasMonitor() -> bool override { return true; }
  20. auto hasContext() -> bool override { return true; }
  21. auto hasBlocking() -> bool override { return true; }
  22. auto hasFlush() -> bool override { return true; }
  23. auto hasShader() -> bool override { return true; }
  24. auto hasFormats() -> vector<string> override {
  25. if(_depth == 30) return {"ARGB30", "ARGB24"};
  26. if(_depth == 24) return {"ARGB24"};
  27. return {"ARGB24"}; //fallback
  28. }
  29. auto setFullScreen(bool fullScreen) -> bool override {
  30. return initialize();
  31. }
  32. auto setMonitor(string monitor) -> bool override {
  33. return initialize();
  34. }
  35. auto setContext(uintptr context) -> bool override {
  36. return initialize();
  37. }
  38. auto setBlocking(bool blocking) -> bool override {
  39. if(glXSwapInterval) glXSwapInterval(blocking);
  40. return true;
  41. }
  42. auto setFlush(bool flush) -> bool override {
  43. return true;
  44. }
  45. auto setFormat(string format) -> bool override {
  46. if(format == "ARGB24") {
  47. OpenGL::inputFormat = GL_RGBA8;
  48. return initialize();
  49. }
  50. if(format == "ARGB30") {
  51. OpenGL::inputFormat = GL_RGB10_A2;
  52. return initialize();
  53. }
  54. return false;
  55. }
  56. auto setShader(string shader) -> bool override {
  57. OpenGL::setShader(shader);
  58. return true;
  59. }
  60. auto focused() -> bool override {
  61. return true;
  62. }
  63. auto clear() -> void override {
  64. OpenGL::clear();
  65. if(_doubleBuffer) glXSwapBuffers(_display, _glXWindow);
  66. }
  67. auto size(uint& width, uint& height) -> void override {
  68. if(self.fullScreen) {
  69. width = _monitorWidth;
  70. height = _monitorHeight;
  71. } else {
  72. XWindowAttributes parent;
  73. XGetWindowAttributes(_display, _parent, &parent);
  74. width = parent.width;
  75. height = parent.height;
  76. }
  77. }
  78. auto acquire(uint32_t*& data, uint& pitch, uint width, uint height) -> bool override {
  79. OpenGL::size(width, height);
  80. return OpenGL::lock(data, pitch);
  81. }
  82. auto release() -> void override {
  83. }
  84. auto output(uint width, uint height) -> void override {
  85. XWindowAttributes window;
  86. XGetWindowAttributes(_display, _window, &window);
  87. XWindowAttributes parent;
  88. XGetWindowAttributes(_display, _parent, &parent);
  89. if(window.width != parent.width || window.height != parent.height) {
  90. XResizeWindow(_display, _window, parent.width, parent.height);
  91. }
  92. //convert (0,0) from top-left to bottom-left coordinates
  93. auto _height = height ? height : _monitorHeight;
  94. auto _monitorY = parent.height - (this->_monitorY + _height) - (_monitorHeight - _height);
  95. OpenGL::absoluteWidth = width;
  96. OpenGL::absoluteHeight = height;
  97. OpenGL::outputX = self.fullScreen ? _monitorX : 0;
  98. OpenGL::outputY = self.fullScreen ? _monitorY : 0;
  99. OpenGL::outputWidth = self.fullScreen ? _monitorWidth : parent.width;
  100. OpenGL::outputHeight = self.fullScreen ? _monitorHeight : parent.height;
  101. OpenGL::output();
  102. if(_doubleBuffer) glXSwapBuffers(_display, _glXWindow);
  103. if(self.flush) glFinish();
  104. }
  105. auto poll() -> void override {
  106. while(XPending(_display)) {
  107. XEvent event;
  108. XNextEvent(_display, &event);
  109. if(event.type == Expose) {
  110. XWindowAttributes attributes;
  111. XGetWindowAttributes(_display, _window, &attributes);
  112. super.doUpdate(attributes.width, attributes.height);
  113. }
  114. }
  115. }
  116. private:
  117. auto construct() -> void {
  118. _display = XOpenDisplay(nullptr);
  119. _screen = DefaultScreen(_display);
  120. XWindowAttributes attributes{};
  121. XGetWindowAttributes(_display, RootWindow(_display, _screen), &attributes);
  122. _depth = attributes.depth;
  123. }
  124. auto destruct() -> void {
  125. terminate();
  126. XCloseDisplay(_display);
  127. }
  128. auto initialize() -> bool {
  129. terminate();
  130. if(!self.fullScreen && !self.context) return false;
  131. //require GLX 1.2+ API
  132. glXQueryVersion(_display, &_versionMajor, &_versionMinor);
  133. if(_versionMajor < 1 || (_versionMajor == 1 && _versionMinor < 2)) return false;
  134. int redDepth = VideoDriver::format == "RGB30" ? 10 : 8;
  135. int greenDepth = VideoDriver::format == "RGB30" ? 10 : 8;
  136. int blueDepth = VideoDriver::format == "RGB30" ? 10 : 8;
  137. //let GLX determine the best Visual to use for GL output; provide a few hints
  138. //note: some video drivers will override double buffering attribute
  139. int attributeList[] = {
  140. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  141. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  142. GLX_DOUBLEBUFFER, True,
  143. GLX_RED_SIZE, redDepth,
  144. GLX_GREEN_SIZE, greenDepth,
  145. GLX_BLUE_SIZE, blueDepth,
  146. None
  147. };
  148. int fbCount = 0;
  149. GLXFBConfig* fbConfig = glXChooseFBConfig(_display, _screen, attributeList, &fbCount);
  150. if(fbCount == 0) return false;
  151. auto visual = glXGetVisualFromFBConfig(_display, fbConfig[0]);
  152. _parent = self.fullScreen ? RootWindow(_display, visual->screen) : (Window)self.context;
  153. XWindowAttributes windowAttributes;
  154. XGetWindowAttributes(_display, _parent, &windowAttributes);
  155. auto monitor = Video::monitor(self.monitor);
  156. _monitorX = monitor.x;
  157. _monitorY = monitor.y;
  158. _monitorWidth = monitor.width;
  159. _monitorHeight = monitor.height;
  160. //(Window)self.context has already been realized, most likely with DefaultVisual.
  161. //GLX requires that the GL output window has the same Visual as the GLX context.
  162. //it is not possible to change the Visual of an already realized (created) window.
  163. //therefore a new child window, using the same GLX Visual, must be created and binded to it.
  164. _colormap = XCreateColormap(_display, RootWindow(_display, visual->screen), visual->visual, AllocNone);
  165. XSetWindowAttributes attributes{};
  166. attributes.border_pixel = 0;
  167. attributes.colormap = _colormap;
  168. attributes.override_redirect = self.fullScreen;
  169. _window = XCreateWindow(_display, _parent,
  170. 0, 0, windowAttributes.width, windowAttributes.height,
  171. 0, visual->depth, InputOutput, visual->visual,
  172. CWBorderPixel | CWColormap | CWOverrideRedirect, &attributes);
  173. XSelectInput(_display, _window, ExposureMask);
  174. XSetWindowBackground(_display, _window, 0);
  175. XMapWindow(_display, _window);
  176. XFlush(_display);
  177. //window must be realized (appear onscreen) before we make the context current
  178. while(XPending(_display)) {
  179. XEvent event;
  180. XNextEvent(_display, &event);
  181. }
  182. _glXContext = glXCreateContext(_display, visual, 0, GL_TRUE);
  183. glXMakeCurrent(_display, _glXWindow = _window, _glXContext);
  184. //glXSwapInterval is used to toggle Vsync
  185. //note that the ordering is very important! MESA declares SGI, but the SGI function does nothing
  186. if(!glXSwapInterval) glXSwapInterval = (int (*)(int))glGetProcAddress("glXSwapIntervalMESA");
  187. if(!glXSwapInterval) glXSwapInterval = (int (*)(int))glGetProcAddress("glXSwapIntervalSGI");
  188. if(auto glXCreateContextAttribs = (auto (*)(Display*, GLXFBConfig, GLXContext, int, const int*) -> GLXContext)glGetProcAddress("glXCreateContextAttribsARB")) {
  189. int attributes[] = {
  190. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  191. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  192. None
  193. };
  194. //glXCreateContextAttribs tends to throw BadRequest errors instead of simply failing gracefully
  195. auto originalHandler = XSetErrorHandler(VideoGLX_X11ErrorHandler);
  196. auto context = glXCreateContextAttribs(_display, fbConfig[0], nullptr, true, attributes);
  197. XSync(_display, False);
  198. XSetErrorHandler(originalHandler);
  199. if(context) {
  200. glXMakeCurrent(_display, 0, nullptr);
  201. glXDestroyContext(_display, _glXContext);
  202. glXMakeCurrent(_display, _glXWindow, _glXContext = context);
  203. } else {
  204. //OpenGL 3.2+ not supported (most likely OpenGL 2.x)
  205. return false;
  206. }
  207. } else {
  208. //missing required glXCreateContextAtribs function
  209. return false;
  210. }
  211. if(glXSwapInterval) glXSwapInterval(self.blocking);
  212. //read attributes of frame buffer for later use, as requested attributes from above are not always granted
  213. int value = 0;
  214. glXGetConfig(_display, visual, GLX_DOUBLEBUFFER, &value);
  215. _doubleBuffer = value;
  216. _isDirect = glXIsDirect(_display, _glXContext);
  217. return _ready = OpenGL::initialize(self.shader);
  218. }
  219. auto terminate() -> void {
  220. _ready = false;
  221. OpenGL::terminate();
  222. if(_glXContext) {
  223. glXDestroyContext(_display, _glXContext);
  224. _glXContext = nullptr;
  225. }
  226. if(_window) {
  227. XUnmapWindow(_display, _window);
  228. _window = 0;
  229. }
  230. if(_colormap) {
  231. XFreeColormap(_display, _colormap);
  232. _colormap = 0;
  233. }
  234. }
  235. bool _ready = false;
  236. auto (*glXSwapInterval)(int) -> int = nullptr;
  237. Display* _display = nullptr;
  238. uint _monitorX = 0;
  239. uint _monitorY = 0;
  240. uint _monitorWidth = 0;
  241. uint _monitorHeight = 0;
  242. int _screen = 0;
  243. uint _depth = 24; //depth of the default root window
  244. Window _parent = 0;
  245. Window _window = 0;
  246. Colormap _colormap = 0;
  247. GLXContext _glXContext = nullptr;
  248. GLXWindow _glXWindow = 0;
  249. int _versionMajor = 0;
  250. int _versionMinor = 0;
  251. bool _doubleBuffer = false;
  252. bool _isDirect = false;
  253. };