2014-01-11 15:34:15 -08:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2014 by Zachary Lund <admin@computerquip.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
/* Version 2 of the GLX backend...
|
|
|
|
* Difference from version 1 is that we use XCB to help alleviate
|
|
|
|
* pains in a threaded environment that is prone to error.
|
|
|
|
* These errors must be readable and handled for the sake of,
|
|
|
|
* not only the users' sanity, but my own.
|
|
|
|
*
|
|
|
|
* With that said, we have more error checking capabilities...
|
|
|
|
* and not all of them are used to help simplify current code.
|
|
|
|
*
|
|
|
|
* TODO: Implement more complete error checking.
|
|
|
|
* NOTE: GLX loading functions are placed illogically
|
|
|
|
* for the sake of convenience.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 18:43:28 -08:00
|
|
|
#include <X11/Xlib.h>
|
2014-12-06 11:53:04 -08:00
|
|
|
#include <X11/Xlib-xcb.h>
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
2013-12-27 18:43:28 -08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
#include "gl-nix.h"
|
2013-12-27 18:43:28 -08:00
|
|
|
|
2014-04-14 14:28:07 -07:00
|
|
|
#include <glad/glad_glx.h>
|
2014-02-05 20:33:55 -08:00
|
|
|
|
2014-01-25 20:22:35 -08:00
|
|
|
static const int ctx_attribs[] = {
|
2014-01-03 21:14:35 -08:00
|
|
|
#ifdef _DEBUG
|
2019-06-22 22:13:45 -07:00
|
|
|
GLX_CONTEXT_FLAGS_ARB,
|
|
|
|
GLX_CONTEXT_DEBUG_BIT_ARB,
|
2014-01-03 21:14:35 -08:00
|
|
|
#endif
|
2019-06-22 22:13:45 -07:00
|
|
|
GLX_CONTEXT_PROFILE_MASK_ARB,
|
|
|
|
GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
|
|
|
|
GLX_CONTEXT_MAJOR_VERSION_ARB,
|
|
|
|
3,
|
|
|
|
GLX_CONTEXT_MINOR_VERSION_ARB,
|
2019-09-12 21:28:26 -07:00
|
|
|
3,
|
2014-04-12 11:21:47 -07:00
|
|
|
None,
|
2013-12-27 18:43:28 -08:00
|
|
|
};
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static int ctx_pbuffer_attribs[] = {GLX_PBUFFER_WIDTH, 2, GLX_PBUFFER_HEIGHT, 2,
|
|
|
|
None};
|
|
|
|
|
|
|
|
static int ctx_visual_attribs[] = {GLX_STENCIL_SIZE,
|
|
|
|
0,
|
|
|
|
GLX_DEPTH_SIZE,
|
|
|
|
0,
|
|
|
|
GLX_BUFFER_SIZE,
|
|
|
|
32,
|
|
|
|
GLX_ALPHA_SIZE,
|
|
|
|
8,
|
|
|
|
GLX_DOUBLEBUFFER,
|
|
|
|
true,
|
|
|
|
GLX_X_RENDERABLE,
|
|
|
|
true,
|
|
|
|
None};
|
2015-08-01 19:36:43 -07:00
|
|
|
|
2013-12-27 18:43:28 -08:00
|
|
|
struct gl_windowinfo {
|
2014-12-06 11:53:04 -08:00
|
|
|
/* We store this value since we can fetch a lot
|
|
|
|
* of information not only concerning the config
|
|
|
|
* but the visual, and various other settings
|
|
|
|
* for the context.
|
|
|
|
*/
|
|
|
|
GLXFBConfig config;
|
|
|
|
|
|
|
|
/* Windows in X11 are defined with integers (XID).
|
|
|
|
* xcb_window_t is a define for this... they are
|
|
|
|
* compatible with Xlib as well.
|
|
|
|
*/
|
|
|
|
xcb_window_t window;
|
|
|
|
|
|
|
|
/* We can't fetch screen without a request so we cache it. */
|
|
|
|
int screen;
|
2013-12-27 18:43:28 -08:00
|
|
|
};
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2013-12-27 18:43:28 -08:00
|
|
|
struct gl_platform {
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *display;
|
2013-12-27 18:43:28 -08:00
|
|
|
GLXContext context;
|
2015-08-01 19:36:43 -07:00
|
|
|
GLXPbuffer pbuffer;
|
2013-12-27 18:43:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
/*
|
|
|
|
* Since we cannot take advantage of the asynchronous nature of xcb,
|
|
|
|
* all of the helper functions are synchronous but thread-safe.
|
|
|
|
*
|
|
|
|
* They check for errors and will return 0 on problems
|
|
|
|
* with the exception of when 0 is a valid return value... in which case
|
|
|
|
* read the specific function comments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Returns -1 on invalid screen. */
|
|
|
|
static int get_screen_num_from_xcb_screen(xcb_connection_t *xcb_conn,
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_screen_t *screen)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
xcb_screen_iterator_t iter =
|
|
|
|
xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
|
|
|
|
int screen_num = 0;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
for (; iter.rem; xcb_screen_next(&iter), ++screen_num)
|
|
|
|
if (iter.data == screen)
|
|
|
|
return screen_num;
|
|
|
|
|
|
|
|
return -1;
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
static xcb_screen_t *get_screen_from_root(xcb_connection_t *xcb_conn,
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_window_t root)
|
2014-04-12 04:33:47 -07:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_screen_iterator_t iter =
|
|
|
|
xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
while (iter.rem) {
|
|
|
|
if (iter.data->root == root)
|
|
|
|
return iter.data;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_screen_next(&iter);
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
return 0;
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
static inline int get_screen_num_from_root(xcb_connection_t *xcb_conn,
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_window_t root)
|
2014-02-05 20:33:55 -08:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_screen_t *screen = get_screen_from_root(xcb_conn, root);
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!screen)
|
|
|
|
return -1;
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
return get_screen_num_from_xcb_screen(xcb_conn, screen);
|
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static xcb_get_geometry_reply_t *get_window_geometry(xcb_connection_t *xcb_conn,
|
|
|
|
xcb_drawable_t drawable)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
xcb_get_geometry_cookie_t cookie;
|
|
|
|
xcb_generic_error_t *error;
|
|
|
|
xcb_get_geometry_reply_t *reply;
|
|
|
|
|
|
|
|
cookie = xcb_get_geometry(xcb_conn, drawable);
|
|
|
|
reply = xcb_get_geometry_reply(xcb_conn, cookie, &error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
blog(LOG_ERROR, "Failed to fetch parent window geometry!");
|
|
|
|
free(error);
|
|
|
|
free(reply);
|
|
|
|
return 0;
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
free(error);
|
|
|
|
return reply;
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
static bool gl_context_create(struct gl_platform *plat)
|
|
|
|
{
|
|
|
|
Display *display = plat->display;
|
2015-08-01 19:36:43 -07:00
|
|
|
int frame_buf_config_count = 0;
|
|
|
|
GLXFBConfig *config = NULL;
|
2014-12-06 11:53:04 -08:00
|
|
|
GLXContext context;
|
2015-08-01 19:36:43 -07:00
|
|
|
bool success = false;
|
2014-12-06 11:53:04 -08:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
if (!GLAD_GLX_ARB_create_context) {
|
|
|
|
blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
|
2014-12-06 11:53:04 -08:00
|
|
|
return false;
|
2014-01-03 23:08:49 -08:00
|
|
|
}
|
2014-01-24 23:32:20 -08:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
config = glXChooseFBConfig(display, DefaultScreen(display),
|
2019-06-22 22:13:45 -07:00
|
|
|
ctx_visual_attribs, &frame_buf_config_count);
|
2015-08-01 19:36:43 -07:00
|
|
|
if (!config) {
|
|
|
|
blog(LOG_ERROR, "Failed to create OpenGL frame buffer config");
|
2014-12-06 11:53:04 -08:00
|
|
|
return false;
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
context = glXCreateContextAttribsARB(display, config[0], NULL, true,
|
|
|
|
ctx_attribs);
|
2014-12-06 11:53:04 -08:00
|
|
|
if (!context) {
|
|
|
|
blog(LOG_ERROR, "Failed to create OpenGL context.");
|
2015-08-01 19:36:43 -07:00
|
|
|
goto error;
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
plat->context = context;
|
|
|
|
plat->display = display;
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
plat->pbuffer =
|
|
|
|
glXCreatePbuffer(display, config[0], ctx_pbuffer_attribs);
|
2015-08-01 19:36:43 -07:00
|
|
|
if (!plat->pbuffer) {
|
|
|
|
blog(LOG_ERROR, "Failed to create OpenGL pbuffer");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
XFree(config);
|
|
|
|
XSync(display, false);
|
|
|
|
return success;
|
2014-12-06 11:53:04 -08:00
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
static void gl_context_destroy(struct gl_platform *plat)
|
|
|
|
{
|
|
|
|
Display *display = plat->display;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
glXMakeContextCurrent(display, None, None, NULL);
|
2014-12-06 11:53:04 -08:00
|
|
|
glXDestroyContext(display, plat->context);
|
|
|
|
bfree(plat);
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static struct gl_windowinfo *
|
|
|
|
gl_x11_glx_windowinfo_create(const struct gs_init_data *info)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(info);
|
|
|
|
return bmalloc(sizeof(struct gl_windowinfo));
|
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_windowinfo_destroy(struct gl_windowinfo *info)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
bfree(info);
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
static Display *open_windowless_display(void)
|
|
|
|
{
|
|
|
|
Display *display = XOpenDisplay(NULL);
|
|
|
|
xcb_connection_t *xcb_conn;
|
|
|
|
xcb_screen_iterator_t screen_iterator;
|
|
|
|
xcb_screen_t *screen;
|
|
|
|
int screen_num;
|
|
|
|
|
|
|
|
if (!display) {
|
|
|
|
blog(LOG_ERROR, "Unable to open new X connection!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_conn = XGetXCBConnection(display);
|
|
|
|
if (!xcb_conn) {
|
|
|
|
blog(LOG_ERROR, "Unable to get XCB connection to main display");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
|
|
|
|
screen = screen_iterator.data;
|
|
|
|
if (!screen) {
|
|
|
|
blog(LOG_ERROR, "Unable to get screen root");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
screen_num = get_screen_num_from_root(xcb_conn, screen->root);
|
|
|
|
if (screen_num == -1) {
|
|
|
|
blog(LOG_ERROR, "Unable to get screen number from root");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gladLoadGLX(display, screen_num)) {
|
|
|
|
blog(LOG_ERROR, "Unable to load GLX entry functions.");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return display;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (display)
|
|
|
|
XCloseDisplay(display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:41:46 -07:00
|
|
|
static int x_error_handler(Display *display, XErrorEvent *error)
|
|
|
|
{
|
2018-08-02 20:52:47 -07:00
|
|
|
char str1[512];
|
|
|
|
char str2[512];
|
|
|
|
char str3[512];
|
|
|
|
XGetErrorText(display, error->error_code, str1, sizeof(str1));
|
|
|
|
XGetErrorText(display, error->request_code, str2, sizeof(str2));
|
|
|
|
XGetErrorText(display, error->minor_code, str3, sizeof(str3));
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"X Error: %s, Major opcode: %s, "
|
|
|
|
"Minor opcode: %s, Serial: %lu",
|
|
|
|
str1, str2, str3, error->serial);
|
2015-07-30 18:41:46 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static struct gl_platform *gl_x11_glx_platform_create(gs_device_t *device,
|
|
|
|
uint32_t adapter)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
/* There's some trickery here... we're mixing libX11, xcb, and GLX
|
|
|
|
For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
|
|
|
|
Essentially, GLX requires Xlib. Everything else we use xcb. */
|
2019-06-22 22:13:45 -07:00
|
|
|
struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
|
|
|
|
Display *display = open_windowless_display();
|
2013-12-27 18:43:28 -08:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
if (!display) {
|
|
|
|
goto fail_display_open;
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
XSetEventQueueOwner(display, XCBOwnsEventQueue);
|
2015-07-30 18:41:46 -07:00
|
|
|
XSetErrorHandler(x_error_handler);
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2014-04-12 04:33:47 -07:00
|
|
|
/* We assume later that cur_swap is already set. */
|
2014-12-06 11:53:04 -08:00
|
|
|
device->plat = plat;
|
2014-04-03 17:21:24 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
plat->display = display;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
if (!gl_context_create(plat)) {
|
|
|
|
blog(LOG_ERROR, "Failed to create context!");
|
|
|
|
goto fail_context_create;
|
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
if (!glXMakeContextCurrent(plat->display, plat->pbuffer, plat->pbuffer,
|
2019-06-22 22:13:45 -07:00
|
|
|
plat->context)) {
|
2014-12-06 11:53:04 -08:00
|
|
|
blog(LOG_ERROR, "Failed to make context current.");
|
|
|
|
goto fail_make_current;
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
if (!gladLoadGL()) {
|
|
|
|
blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
|
|
|
|
goto fail_load_gl;
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
goto success;
|
|
|
|
|
|
|
|
fail_make_current:
|
|
|
|
gl_context_destroy(plat);
|
|
|
|
fail_context_create:
|
|
|
|
fail_load_gl:
|
|
|
|
XCloseDisplay(display);
|
|
|
|
fail_display_open:
|
2015-09-07 11:55:03 -07:00
|
|
|
bfree(plat);
|
2014-12-06 11:53:04 -08:00
|
|
|
plat = NULL;
|
|
|
|
success:
|
2015-08-01 19:36:43 -07:00
|
|
|
UNUSED_PARAMETER(adapter);
|
2014-12-06 11:53:04 -08:00
|
|
|
return plat;
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_platform_destroy(struct gl_platform *plat)
|
2013-12-27 18:43:28 -08:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
if (!plat) /* In what case would platform be invalid here? */
|
2014-02-05 20:31:03 -08:00
|
|
|
return;
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
gl_context_destroy(plat);
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static bool gl_x11_glx_platform_init_swapchain(struct gs_swap_chain *swap)
|
2014-04-12 04:33:47 -07:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *display = swap->device->plat->display;
|
|
|
|
xcb_connection_t *xcb_conn = XGetXCBConnection(display);
|
|
|
|
xcb_window_t wid = xcb_generate_id(xcb_conn);
|
|
|
|
xcb_window_t parent = swap->info.window.id;
|
|
|
|
xcb_get_geometry_reply_t *geometry =
|
|
|
|
get_window_geometry(xcb_conn, parent);
|
|
|
|
bool status = false;
|
|
|
|
|
|
|
|
int screen_num;
|
|
|
|
int visual;
|
|
|
|
GLXFBConfig *fb_config;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!geometry)
|
|
|
|
goto fail_geometry_request;
|
2014-12-06 11:53:04 -08:00
|
|
|
|
|
|
|
screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
|
|
|
|
if (screen_num == -1) {
|
|
|
|
goto fail_screen;
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
/* ...fetch the best match... */
|
|
|
|
{
|
|
|
|
int num_configs;
|
|
|
|
fb_config = glXChooseFBConfig(display, screen_num,
|
2019-06-22 22:13:45 -07:00
|
|
|
ctx_visual_attribs, &num_configs);
|
2014-12-06 11:53:04 -08:00
|
|
|
|
|
|
|
if (!fb_config || !num_configs) {
|
|
|
|
blog(LOG_ERROR, "Failed to find FBConfig!");
|
|
|
|
goto fail_fb_config;
|
|
|
|
}
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
/* ...then fetch matching visual info for xcb. */
|
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
int error = glXGetFBConfigAttrib(display, fb_config[0],
|
|
|
|
GLX_VISUAL_ID, &visual);
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
if (error) {
|
|
|
|
blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
|
|
|
|
goto fail_visual_id;
|
|
|
|
}
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
|
|
|
|
uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
|
2019-06-22 22:13:45 -07:00
|
|
|
uint32_t mask_values[] = {0, colormap, 0};
|
|
|
|
|
|
|
|
xcb_create_colormap(xcb_conn, XCB_COLORMAP_ALLOC_NONE, colormap, parent,
|
|
|
|
visual);
|
|
|
|
|
|
|
|
xcb_create_window(xcb_conn, 24 /* Hardcoded? */, wid, parent, 0, 0,
|
|
|
|
geometry->width, geometry->height, 0, 0, visual, mask,
|
|
|
|
mask_values);
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
swap->wi->config = fb_config[0];
|
|
|
|
swap->wi->window = wid;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_map_window(xcb_conn, wid);
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
XFree(fb_config);
|
|
|
|
status = true;
|
|
|
|
goto success;
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
fail_visual_id:
|
|
|
|
XFree(fb_config);
|
|
|
|
fail_fb_config:
|
|
|
|
fail_screen:
|
|
|
|
fail_geometry_request:
|
|
|
|
success:
|
|
|
|
free(geometry);
|
|
|
|
return status;
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_platform_cleanup_swapchain(struct gs_swap_chain *swap)
|
2014-04-12 04:33:47 -07:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
UNUSED_PARAMETER(swap);
|
|
|
|
/* Really nothing to clean up? */
|
2014-04-12 04:33:47 -07:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_device_enter_context(gs_device_t *device)
|
2013-12-27 18:43:28 -08:00
|
|
|
{
|
|
|
|
GLXContext context = device->plat->context;
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *display = device->plat->display;
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
if (device->cur_swap) {
|
|
|
|
XID window = device->cur_swap->wi->window;
|
|
|
|
if (!glXMakeContextCurrent(display, window, window, context)) {
|
|
|
|
blog(LOG_ERROR, "Failed to make context current.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GLXPbuffer pbuf = device->plat->pbuffer;
|
|
|
|
if (!glXMakeContextCurrent(display, pbuf, pbuf, context)) {
|
|
|
|
blog(LOG_ERROR, "Failed to make context current.");
|
|
|
|
}
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_device_leave_context(gs_device_t *device)
|
2013-12-27 18:43:28 -08:00
|
|
|
{
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *display = device->plat->display;
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
if (!glXMakeContextCurrent(display, None, None, NULL)) {
|
2013-12-27 18:43:28 -08:00
|
|
|
blog(LOG_ERROR, "Failed to reset current context.");
|
|
|
|
}
|
|
|
|
}
|
2014-01-03 22:48:41 -08:00
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void *gl_x11_glx_device_get_device_obj(gs_device_t *device)
|
2019-08-29 12:43:10 -07:00
|
|
|
{
|
|
|
|
return device->plat->context;
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_getclientsize(const struct gs_swap_chain *swap,
|
|
|
|
uint32_t *width, uint32_t *height)
|
2014-01-03 22:48:41 -08:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_connection_t *xcb_conn =
|
|
|
|
XGetXCBConnection(swap->device->plat->display);
|
2014-12-06 11:53:04 -08:00
|
|
|
xcb_window_t window = swap->wi->window;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_get_geometry_reply_t *geometry =
|
|
|
|
get_window_geometry(xcb_conn, window);
|
2018-08-02 20:55:09 -07:00
|
|
|
if (geometry) {
|
2019-06-22 22:13:45 -07:00
|
|
|
*width = geometry->width;
|
2018-08-02 20:55:09 -07:00
|
|
|
*height = geometry->height;
|
|
|
|
}
|
2014-04-12 04:33:47 -07:00
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
free(geometry);
|
2014-01-03 22:48:41 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_clear_context(gs_device_t *device)
|
libobs-opengl: OpenGL thread-safety on Mac
Attempt to fix threading issues that cause OBS to force-crash when
compiled with latest Xcode. There are two places where the new SDK
introduces a force-crash because operations are not happening on the
main thread: when we modify the context view to switch swap chains, and
when we resize a swap chain.
Instead of using just one context for all rendering, we create an
additional context for each swap chain, set each view once on
initialization, and switch contexts only in present to blit the final
framebuffer. This is an extra copy, but it's pretty hairy to optimize
away, and it's not worth potential regressions just to speed up Mac.
For resizing, we schedule the update code to run on the main thread from
the render thread. Ideally, we wouldn't have to round trip the logic
from main thread to graphics thread and back, but I don't think we want
to hack up the interface for this, especially since OpenGL will give way
to Metal soon enough.
2019-12-25 22:25:38 -08:00
|
|
|
{
|
|
|
|
Display *display = device->plat->display;
|
|
|
|
|
|
|
|
if (!glXMakeContextCurrent(display, None, None, NULL)) {
|
|
|
|
blog(LOG_ERROR, "Failed to reset current context.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_update(gs_device_t *device)
|
2014-12-06 11:53:04 -08:00
|
|
|
{
|
|
|
|
Display *display = device->plat->display;
|
|
|
|
xcb_window_t window = device->cur_swap->wi->window;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
uint32_t values[] = {device->cur_swap->info.cx,
|
|
|
|
device->cur_swap->info.cy};
|
2014-12-06 11:53:04 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
xcb_configure_window(XGetXCBConnection(display), window,
|
|
|
|
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
|
|
|
values);
|
2014-12-06 11:53:04 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_device_load_swapchain(gs_device_t *device,
|
|
|
|
gs_swapchain_t *swap)
|
2013-12-27 18:43:28 -08:00
|
|
|
{
|
2014-04-03 17:21:24 -07:00
|
|
|
if (device->cur_swap == swap)
|
|
|
|
return;
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *dpy = device->plat->display;
|
2014-04-03 17:21:24 -07:00
|
|
|
GLXContext ctx = device->plat->context;
|
|
|
|
|
2013-12-27 18:43:28 -08:00
|
|
|
device->cur_swap = swap;
|
2014-04-03 17:21:24 -07:00
|
|
|
|
2015-08-01 19:36:43 -07:00
|
|
|
if (swap) {
|
|
|
|
XID window = swap->wi->window;
|
|
|
|
if (!glXMakeContextCurrent(dpy, window, window, ctx)) {
|
|
|
|
blog(LOG_ERROR, "Failed to make context current.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GLXPbuffer pbuf = device->plat->pbuffer;
|
|
|
|
if (!glXMakeContextCurrent(dpy, pbuf, pbuf, ctx)) {
|
|
|
|
blog(LOG_ERROR, "Failed to make context current.");
|
|
|
|
}
|
2014-04-03 17:21:24 -07:00
|
|
|
}
|
2013-12-27 18:43:28 -08:00
|
|
|
}
|
|
|
|
|
2016-01-25 10:43:53 -08:00
|
|
|
enum swap_type {
|
|
|
|
SWAP_TYPE_NORMAL,
|
|
|
|
SWAP_TYPE_EXT,
|
|
|
|
SWAP_TYPE_MESA,
|
|
|
|
SWAP_TYPE_SGI,
|
|
|
|
};
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static void gl_x11_glx_device_present(gs_device_t *device)
|
2013-12-27 18:43:28 -08:00
|
|
|
{
|
2016-01-25 10:43:53 -08:00
|
|
|
static bool initialized = false;
|
|
|
|
static enum swap_type swap_type = SWAP_TYPE_NORMAL;
|
|
|
|
|
2014-12-06 11:53:04 -08:00
|
|
|
Display *display = device->plat->display;
|
|
|
|
XID window = device->cur_swap->wi->window;
|
|
|
|
|
2016-01-25 10:43:53 -08:00
|
|
|
if (!initialized) {
|
|
|
|
if (GLAD_GLX_EXT_swap_control)
|
|
|
|
swap_type = SWAP_TYPE_EXT;
|
|
|
|
else if (GLAD_GLX_MESA_swap_control)
|
|
|
|
swap_type = SWAP_TYPE_MESA;
|
|
|
|
else if (GLAD_GLX_SGI_swap_control)
|
|
|
|
swap_type = SWAP_TYPE_SGI;
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2016-10-29 08:31:15 -07:00
|
|
|
xcb_connection_t *xcb_conn = XGetXCBConnection(display);
|
|
|
|
xcb_generic_event_t *xcb_event;
|
2019-06-22 22:13:45 -07:00
|
|
|
while ((xcb_event = xcb_poll_for_event(xcb_conn))) {
|
2016-10-29 08:31:15 -07:00
|
|
|
/* TODO: Handle XCB events. */
|
|
|
|
free(xcb_event);
|
|
|
|
}
|
2014-02-05 20:31:03 -08:00
|
|
|
|
2016-01-25 10:43:53 -08:00
|
|
|
switch (swap_type) {
|
2019-06-22 22:13:45 -07:00
|
|
|
case SWAP_TYPE_EXT:
|
|
|
|
glXSwapIntervalEXT(display, window, 0);
|
|
|
|
break;
|
|
|
|
case SWAP_TYPE_MESA:
|
|
|
|
glXSwapIntervalMESA(0);
|
|
|
|
break;
|
|
|
|
case SWAP_TYPE_SGI:
|
|
|
|
glXSwapIntervalSGI(0);
|
|
|
|
break;
|
2016-01-25 10:43:53 -08:00
|
|
|
case SWAP_TYPE_NORMAL:;
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:43:28 -08:00
|
|
|
glXSwapBuffers(display, window);
|
|
|
|
}
|
2020-03-09 12:46:37 -07:00
|
|
|
|
2020-03-12 19:50:18 -07:00
|
|
|
static struct gs_texture *gl_x11_glx_device_texture_create_from_dmabuf(
|
|
|
|
gs_device_t *device, unsigned int width, unsigned int height,
|
2021-03-25 19:52:29 -07:00
|
|
|
uint32_t drm_format, enum gs_color_format color_format,
|
|
|
|
uint32_t n_planes, const int *fds, const uint32_t *strides,
|
|
|
|
const uint32_t *offsets, const uint64_t *modifiers)
|
2020-03-12 19:50:18 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(device);
|
|
|
|
UNUSED_PARAMETER(width);
|
|
|
|
UNUSED_PARAMETER(height);
|
2021-03-25 19:52:29 -07:00
|
|
|
UNUSED_PARAMETER(drm_format);
|
2020-03-12 19:50:18 -07:00
|
|
|
UNUSED_PARAMETER(color_format);
|
|
|
|
UNUSED_PARAMETER(n_planes);
|
|
|
|
UNUSED_PARAMETER(fds);
|
|
|
|
UNUSED_PARAMETER(strides);
|
|
|
|
UNUSED_PARAMETER(offsets);
|
|
|
|
UNUSED_PARAMETER(modifiers);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:46:37 -07:00
|
|
|
static const struct gl_winsys_vtable glx_winsys_vtable = {
|
|
|
|
.windowinfo_create = gl_x11_glx_windowinfo_create,
|
|
|
|
.windowinfo_destroy = gl_x11_glx_windowinfo_destroy,
|
|
|
|
.platform_create = gl_x11_glx_platform_create,
|
|
|
|
.platform_destroy = gl_x11_glx_platform_destroy,
|
|
|
|
.platform_init_swapchain = gl_x11_glx_platform_init_swapchain,
|
|
|
|
.platform_cleanup_swapchain = gl_x11_glx_platform_cleanup_swapchain,
|
|
|
|
.device_enter_context = gl_x11_glx_device_enter_context,
|
|
|
|
.device_leave_context = gl_x11_glx_device_leave_context,
|
|
|
|
.device_get_device_obj = gl_x11_glx_device_get_device_obj,
|
|
|
|
.getclientsize = gl_x11_glx_getclientsize,
|
|
|
|
.clear_context = gl_x11_glx_clear_context,
|
|
|
|
.update = gl_x11_glx_update,
|
|
|
|
.device_load_swapchain = gl_x11_glx_device_load_swapchain,
|
|
|
|
.device_present = gl_x11_glx_device_present,
|
2020-03-12 19:50:18 -07:00
|
|
|
.device_texture_create_from_dmabuf =
|
|
|
|
gl_x11_glx_device_texture_create_from_dmabuf,
|
2020-03-09 12:46:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct gl_winsys_vtable *gl_x11_glx_get_winsys_vtable(void)
|
|
|
|
{
|
|
|
|
return &glx_winsys_vtable;
|
|
|
|
}
|