Path: blob/master/platform/linuxbsd/x11/gl_manager_x11.cpp
10278 views
/**************************************************************************/1/* gl_manager_x11.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */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/**************************************************************************/2930#include "gl_manager_x11.h"3132#if defined(X11_ENABLED) && defined(GLES3_ENABLED)3334#include "thirdparty/glad/glad/glx.h"3536#include <unistd.h>37#include <cstdio>38#include <cstdlib>3940#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x209141#define GLX_CONTEXT_MINOR_VERSION_ARB 0x20924243typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);4445// To prevent shadowing warnings46#undef glXCreateContextAttribsARB4748struct GLManager_X11_Private {49::GLXContext glx_context;50};5152GLManager_X11::GLDisplay::~GLDisplay() {53if (context) {54//release_current();55glXDestroyContext(x11_display, context->glx_context);56memdelete(context);57context = nullptr;58}59}6061static bool ctxErrorOccurred = false;62static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {63ctxErrorOccurred = true;64return 0;65}6667int GLManager_X11::_find_or_create_display(Display *p_x11_display) {68for (unsigned int n = 0; n < _displays.size(); n++) {69const GLDisplay &d = _displays[n];70if (d.x11_display == p_x11_display) {71return n;72}73}7475// create76GLDisplay d_temp;77d_temp.x11_display = p_x11_display;78_displays.push_back(d_temp);79int new_display_id = _displays.size() - 1;8081// create context82GLDisplay &d = _displays[new_display_id];8384d.context = memnew(GLManager_X11_Private);85d.context->glx_context = nullptr;8687Error err = _create_context(d);8889if (err != OK) {90_displays.remove_at(new_display_id);91return -1;92}9394return new_display_id;95}9697Error GLManager_X11::_create_context(GLDisplay &gl_display) {98// some aliases99::Display *x11_display = gl_display.x11_display;100101//const char *extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));102103GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");104105ERR_FAIL_NULL_V(glXCreateContextAttribsARB, ERR_UNCONFIGURED);106107static int visual_attribs[] = {108GLX_RENDER_TYPE, GLX_RGBA_BIT,109GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,110GLX_DOUBLEBUFFER, true,111GLX_RED_SIZE, 1,112GLX_GREEN_SIZE, 1,113GLX_BLUE_SIZE, 1,114GLX_DEPTH_SIZE, 24,115None116};117118static int visual_attribs_layered[] = {119GLX_RENDER_TYPE, GLX_RGBA_BIT,120GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,121GLX_DOUBLEBUFFER, true,122GLX_RED_SIZE, 8,123GLX_GREEN_SIZE, 8,124GLX_BLUE_SIZE, 8,125GLX_ALPHA_SIZE, 8,126GLX_DEPTH_SIZE, 24,127None128};129130int fbcount;131GLXFBConfig fbconfig = nullptr;132XVisualInfo *vi = nullptr;133134if (OS::get_singleton()->is_layered_allowed()) {135GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs_layered, &fbcount);136ERR_FAIL_NULL_V(fbc, ERR_UNCONFIGURED);137138for (int i = 0; i < fbcount; i++) {139if (vi) {140XFree(vi);141vi = nullptr;142}143vi = (XVisualInfo *)glXGetVisualFromFBConfig(x11_display, fbc[i]);144if (!vi) {145continue;146}147148XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi->visual);149if (!pict_format) {150XFree(vi);151vi = nullptr;152continue;153}154155fbconfig = fbc[i];156if (pict_format->direct.alphaMask > 0) {157break;158}159}160XFree(fbc);161162ERR_FAIL_NULL_V(fbconfig, ERR_UNCONFIGURED);163} else {164GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);165ERR_FAIL_NULL_V(fbc, ERR_UNCONFIGURED);166167vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);168169fbconfig = fbc[0];170XFree(fbc);171}172173int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&ctxErrorHandler);174175switch (context_type) {176case GLES_3_0_COMPATIBLE: {177static int context_attribs[] = {178GLX_CONTEXT_MAJOR_VERSION_ARB, 3,179GLX_CONTEXT_MINOR_VERSION_ARB, 3,180GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,181GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*|GLX_CONTEXT_DEBUG_BIT_ARB*/,182None183};184185gl_display.context->glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);186ERR_FAIL_COND_V(ctxErrorOccurred || !gl_display.context->glx_context, ERR_UNCONFIGURED);187} break;188}189190XSync(x11_display, False);191XSetErrorHandler(oldHandler);192193// make our own copy of the vi data194// for later creating windows using this display195if (vi) {196gl_display.x_vi = *vi;197}198199XFree(vi);200201return OK;202}203204XVisualInfo GLManager_X11::get_vi(Display *p_display, Error &r_error) {205int display_id = _find_or_create_display(p_display);206if (display_id < 0) {207r_error = FAILED;208return XVisualInfo();209}210r_error = OK;211return _displays[display_id].x_vi;212}213214Error GLManager_X11::open_display(Display *p_display) {215int gldisplay_id = _find_or_create_display(p_display);216if (gldisplay_id < 0) {217return ERR_CANT_CREATE;218} else {219return OK;220}221}222223Error GLManager_X11::window_create(DisplayServer::WindowID p_window_id, ::Window p_window, Display *p_display, int p_width, int p_height) {224// make sure vector is big enough...225// we can mirror the external vector, it is simpler226// to keep the IDs identical for fast lookup227if (p_window_id >= (int)_windows.size()) {228_windows.resize(p_window_id + 1);229}230231GLWindow &win = _windows[p_window_id];232win.in_use = true;233win.window_id = p_window_id;234win.width = p_width;235win.height = p_height;236win.x11_window = p_window;237win.gldisplay_id = _find_or_create_display(p_display);238239if (win.gldisplay_id == -1) {240return FAILED;241}242243// the display could be invalid .. check NYI244GLDisplay &gl_display = _displays[win.gldisplay_id];245::Display *x11_display = gl_display.x11_display;246::Window &x11_window = win.x11_window;247248if (!glXMakeCurrent(x11_display, x11_window, gl_display.context->glx_context)) {249ERR_PRINT("glXMakeCurrent failed");250}251252_internal_set_current_window(&win);253254return OK;255}256257void GLManager_X11::_internal_set_current_window(GLWindow *p_win) {258_current_window = p_win;259260// quick access to x info261_x_windisp.x11_window = _current_window->x11_window;262const GLDisplay &disp = get_current_display();263_x_windisp.x11_display = disp.x11_display;264}265266void GLManager_X11::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {267get_window(p_window_id).width = p_width;268get_window(p_window_id).height = p_height;269}270271void GLManager_X11::window_destroy(DisplayServer::WindowID p_window_id) {272GLWindow &win = get_window(p_window_id);273win.in_use = false;274275if (_current_window == &win) {276_current_window = nullptr;277_x_windisp.x11_display = nullptr;278_x_windisp.x11_window = -1;279}280}281282void GLManager_X11::release_current() {283if (!_current_window) {284return;285}286287if (!glXMakeCurrent(_x_windisp.x11_display, None, nullptr)) {288ERR_PRINT("glXMakeCurrent failed");289}290_current_window = nullptr;291}292293void GLManager_X11::window_make_current(DisplayServer::WindowID p_window_id) {294if (p_window_id == -1) {295return;296}297298GLWindow &win = _windows[p_window_id];299if (!win.in_use) {300return;301}302303// noop304if (&win == _current_window) {305return;306}307308const GLDisplay &disp = get_display(win.gldisplay_id);309310if (!glXMakeCurrent(disp.x11_display, win.x11_window, disp.context->glx_context)) {311ERR_PRINT("glXMakeCurrent failed");312}313314_internal_set_current_window(&win);315}316317void GLManager_X11::swap_buffers() {318if (!_current_window) {319return;320}321if (!_current_window->in_use) {322WARN_PRINT("current window not in use!");323return;324}325326// On X11, when enabled, transparency is always active, so clear alpha manually.327if (OS::get_singleton()->is_layered_allowed()) {328if (!DisplayServer::get_singleton()->window_get_flag(DisplayServer::WINDOW_FLAG_TRANSPARENT, _current_window->window_id)) {329glColorMask(false, false, false, true);330glClearColor(0, 0, 0, 1);331glClear(GL_COLOR_BUFFER_BIT);332glColorMask(true, true, true, true);333}334}335336glXSwapBuffers(_x_windisp.x11_display, _x_windisp.x11_window);337}338339Error GLManager_X11::initialize(Display *p_display) {340if (!gladLoaderLoadGLX(p_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(p_display)))) {341return ERR_CANT_CREATE;342}343344return OK;345}346347void GLManager_X11::set_use_vsync(bool p_use) {348// we need an active window to get a display to set the vsync349if (!_current_window) {350return;351}352const GLDisplay &disp = get_current_display();353354int val = p_use ? 1 : 0;355if (GLAD_GLX_MESA_swap_control) {356glXSwapIntervalMESA(val);357} else if (GLAD_GLX_SGI_swap_control) {358glXSwapIntervalSGI(val);359} else if (GLAD_GLX_EXT_swap_control) {360GLXDrawable drawable = glXGetCurrentDrawable();361glXSwapIntervalEXT(disp.x11_display, drawable, val);362} else {363WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver.");364return;365}366use_vsync = p_use;367}368369bool GLManager_X11::is_using_vsync() const {370return use_vsync;371}372373void *GLManager_X11::get_glx_context(DisplayServer::WindowID p_window_id) {374if (p_window_id == -1) {375return nullptr;376}377378const GLWindow &win = _windows[p_window_id];379const GLDisplay &disp = get_display(win.gldisplay_id);380381return (void *)disp.context->glx_context;382}383384GLManager_X11::GLManager_X11(const Vector2i &p_size, ContextType p_context_type) {385context_type = p_context_type;386387double_buffer = false;388direct_render = false;389glx_minor = glx_major = 0;390use_vsync = false;391_current_window = nullptr;392}393394GLManager_X11::~GLManager_X11() {395release_current();396}397398#endif // X11_ENABLED && GLES3_ENABLED399400401