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