Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/linuxbsd/x11/gl_manager_x11.cpp
10278 views
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 <unistd.h>
38
#include <cstdio>
39
#include <cstdlib>
40
41
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
42
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
43
44
typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
45
46
// To prevent shadowing warnings
47
#undef glXCreateContextAttribsARB
48
49
struct GLManager_X11_Private {
50
::GLXContext glx_context;
51
};
52
53
GLManager_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
62
static bool ctxErrorOccurred = false;
63
static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
64
ctxErrorOccurred = true;
65
return 0;
66
}
67
68
int 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
98
Error 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
if (vi) {
141
XFree(vi);
142
vi = nullptr;
143
}
144
vi = (XVisualInfo *)glXGetVisualFromFBConfig(x11_display, fbc[i]);
145
if (!vi) {
146
continue;
147
}
148
149
XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi->visual);
150
if (!pict_format) {
151
XFree(vi);
152
vi = nullptr;
153
continue;
154
}
155
156
fbconfig = fbc[i];
157
if (pict_format->direct.alphaMask > 0) {
158
break;
159
}
160
}
161
XFree(fbc);
162
163
ERR_FAIL_NULL_V(fbconfig, ERR_UNCONFIGURED);
164
} else {
165
GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
166
ERR_FAIL_NULL_V(fbc, ERR_UNCONFIGURED);
167
168
vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
169
170
fbconfig = fbc[0];
171
XFree(fbc);
172
}
173
174
int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);
175
176
switch (context_type) {
177
case GLES_3_0_COMPATIBLE: {
178
static int context_attribs[] = {
179
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
180
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
181
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
182
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
183
None
184
};
185
186
gl_display.context->glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
187
ERR_FAIL_COND_V(ctxErrorOccurred || !gl_display.context->glx_context, ERR_UNCONFIGURED);
188
} break;
189
}
190
191
XSync(x11_display, False);
192
XSetErrorHandler(oldHandler);
193
194
// make our own copy of the vi data
195
// for later creating windows using this display
196
if (vi) {
197
gl_display.x_vi = *vi;
198
}
199
200
XFree(vi);
201
202
return OK;
203
}
204
205
XVisualInfo GLManager_X11::get_vi(Display *p_display, Error &r_error) {
206
int display_id = _find_or_create_display(p_display);
207
if (display_id < 0) {
208
r_error = FAILED;
209
return XVisualInfo();
210
}
211
r_error = OK;
212
return _displays[display_id].x_vi;
213
}
214
215
Error GLManager_X11::open_display(Display *p_display) {
216
int gldisplay_id = _find_or_create_display(p_display);
217
if (gldisplay_id < 0) {
218
return ERR_CANT_CREATE;
219
} else {
220
return OK;
221
}
222
}
223
224
Error GLManager_X11::window_create(DisplayServer::WindowID p_window_id, ::Window p_window, Display *p_display, int p_width, int p_height) {
225
// make sure vector is big enough...
226
// we can mirror the external vector, it is simpler
227
// to keep the IDs identical for fast lookup
228
if (p_window_id >= (int)_windows.size()) {
229
_windows.resize(p_window_id + 1);
230
}
231
232
GLWindow &win = _windows[p_window_id];
233
win.in_use = true;
234
win.window_id = p_window_id;
235
win.width = p_width;
236
win.height = p_height;
237
win.x11_window = p_window;
238
win.gldisplay_id = _find_or_create_display(p_display);
239
240
if (win.gldisplay_id == -1) {
241
return FAILED;
242
}
243
244
// the display could be invalid .. check NYI
245
GLDisplay &gl_display = _displays[win.gldisplay_id];
246
::Display *x11_display = gl_display.x11_display;
247
::Window &x11_window = win.x11_window;
248
249
if (!glXMakeCurrent(x11_display, x11_window, gl_display.context->glx_context)) {
250
ERR_PRINT("glXMakeCurrent failed");
251
}
252
253
_internal_set_current_window(&win);
254
255
return OK;
256
}
257
258
void GLManager_X11::_internal_set_current_window(GLWindow *p_win) {
259
_current_window = p_win;
260
261
// quick access to x info
262
_x_windisp.x11_window = _current_window->x11_window;
263
const GLDisplay &disp = get_current_display();
264
_x_windisp.x11_display = disp.x11_display;
265
}
266
267
void GLManager_X11::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
268
get_window(p_window_id).width = p_width;
269
get_window(p_window_id).height = p_height;
270
}
271
272
void GLManager_X11::window_destroy(DisplayServer::WindowID p_window_id) {
273
GLWindow &win = get_window(p_window_id);
274
win.in_use = false;
275
276
if (_current_window == &win) {
277
_current_window = nullptr;
278
_x_windisp.x11_display = nullptr;
279
_x_windisp.x11_window = -1;
280
}
281
}
282
283
void GLManager_X11::release_current() {
284
if (!_current_window) {
285
return;
286
}
287
288
if (!glXMakeCurrent(_x_windisp.x11_display, None, nullptr)) {
289
ERR_PRINT("glXMakeCurrent failed");
290
}
291
_current_window = nullptr;
292
}
293
294
void GLManager_X11::window_make_current(DisplayServer::WindowID p_window_id) {
295
if (p_window_id == -1) {
296
return;
297
}
298
299
GLWindow &win = _windows[p_window_id];
300
if (!win.in_use) {
301
return;
302
}
303
304
// noop
305
if (&win == _current_window) {
306
return;
307
}
308
309
const GLDisplay &disp = get_display(win.gldisplay_id);
310
311
if (!glXMakeCurrent(disp.x11_display, win.x11_window, disp.context->glx_context)) {
312
ERR_PRINT("glXMakeCurrent failed");
313
}
314
315
_internal_set_current_window(&win);
316
}
317
318
void GLManager_X11::swap_buffers() {
319
if (!_current_window) {
320
return;
321
}
322
if (!_current_window->in_use) {
323
WARN_PRINT("current window not in use!");
324
return;
325
}
326
327
// On X11, when enabled, transparency is always active, so clear alpha manually.
328
if (OS::get_singleton()->is_layered_allowed()) {
329
if (!DisplayServer::get_singleton()->window_get_flag(DisplayServer::WINDOW_FLAG_TRANSPARENT, _current_window->window_id)) {
330
glColorMask(false, false, false, true);
331
glClearColor(0, 0, 0, 1);
332
glClear(GL_COLOR_BUFFER_BIT);
333
glColorMask(true, true, true, true);
334
}
335
}
336
337
glXSwapBuffers(_x_windisp.x11_display, _x_windisp.x11_window);
338
}
339
340
Error GLManager_X11::initialize(Display *p_display) {
341
if (!gladLoaderLoadGLX(p_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(p_display)))) {
342
return ERR_CANT_CREATE;
343
}
344
345
return OK;
346
}
347
348
void GLManager_X11::set_use_vsync(bool p_use) {
349
// we need an active window to get a display to set the vsync
350
if (!_current_window) {
351
return;
352
}
353
const GLDisplay &disp = get_current_display();
354
355
int val = p_use ? 1 : 0;
356
if (GLAD_GLX_MESA_swap_control) {
357
glXSwapIntervalMESA(val);
358
} else if (GLAD_GLX_SGI_swap_control) {
359
glXSwapIntervalSGI(val);
360
} else if (GLAD_GLX_EXT_swap_control) {
361
GLXDrawable drawable = glXGetCurrentDrawable();
362
glXSwapIntervalEXT(disp.x11_display, drawable, val);
363
} else {
364
WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver.");
365
return;
366
}
367
use_vsync = p_use;
368
}
369
370
bool GLManager_X11::is_using_vsync() const {
371
return use_vsync;
372
}
373
374
void *GLManager_X11::get_glx_context(DisplayServer::WindowID p_window_id) {
375
if (p_window_id == -1) {
376
return nullptr;
377
}
378
379
const GLWindow &win = _windows[p_window_id];
380
const GLDisplay &disp = get_display(win.gldisplay_id);
381
382
return (void *)disp.context->glx_context;
383
}
384
385
GLManager_X11::GLManager_X11(const Vector2i &p_size, ContextType p_context_type) {
386
context_type = p_context_type;
387
388
double_buffer = false;
389
direct_render = false;
390
glx_minor = glx_major = 0;
391
use_vsync = false;
392
_current_window = nullptr;
393
}
394
395
GLManager_X11::~GLManager_X11() {
396
release_current();
397
}
398
399
#endif // X11_ENABLED && GLES3_ENABLED
400
401