613 lines
14 KiB
C
Raw Normal View History

2012-09-15 15:17:34 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2012-09-15 15:17:34 +02:00
2015-11-19 13:16:43 +01:00
/* X/EGL context. Mostly used for testing GLES code paths. */
2012-09-15 15:17:34 +02:00
2015-09-16 05:53:34 +02:00
#include <stdint.h>
2016-09-11 16:24:02 +02:00
#include <stdlib.h>
2015-09-16 05:53:34 +02:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include "../../frontend/frontend_driver.h"
2015-11-19 13:16:43 +01:00
#include "../common/egl_common.h"
2015-11-17 08:01:33 +01:00
#include "../common/gl_common.h"
2015-04-09 05:02:57 +02:00
#include "../common/x11_common.h"
2017-04-26 15:29:21 +02:00
#ifdef HAVE_XINERAMA
#include "../common/xinerama_common.h"
2017-04-26 15:29:21 +02:00
#endif
2012-09-15 15:17:34 +02:00
#ifndef EGL_OPENGL_ES3_BIT_KHR
#define EGL_OPENGL_ES3_BIT_KHR 0x0040
#endif
#ifndef EGL_PLATFORM_X11_KHR
#define EGL_PLATFORM_X11_KHR 0x31D5
#endif
2016-03-01 06:49:05 +01:00
typedef struct
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
2016-03-01 06:49:05 +01:00
#endif
bool should_reset_mode;
} xegl_ctx_data_t;
2012-09-15 15:17:34 +02:00
2016-10-08 14:37:26 +02:00
static enum gfx_ctx_api x_api = GFX_CTX_NONE;
static int x_nul_handler(Display *dpy, XErrorEvent *event)
{
(void)dpy;
(void)event;
return 0;
}
static void gfx_ctx_xegl_destroy(void *data)
{
xegl_ctx_data_t *xegl = (xegl_ctx_data_t*)data;
x11_input_ctx_destroy();
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
egl_destroy(&xegl->egl);
#endif
if (g_x11_win)
{
2017-03-23 19:46:06 +01:00
#ifdef HAVE_XINERAMA
/* Save last used monitor for later. */
2017-03-23 19:46:06 +01:00
xinerama_save_last_used_monitor(RootWindow(
2016-02-14 16:39:27 +01:00
g_x11_dpy, DefaultScreen(g_x11_dpy)));
2017-03-23 19:46:06 +01:00
#endif
x11_window_destroy(false);
}
x11_colormap_destroy();
if (xegl->should_reset_mode)
{
x11_exit_fullscreen(g_x11_dpy);
xegl->should_reset_mode = false;
}
free(data);
/* Do not close g_x11_dpy. We'll keep one for the entire application
* lifecycle to work-around nVidia EGL limitations.
*/
}
2012-09-25 01:26:22 +02:00
2016-10-08 14:37:26 +02:00
#define XEGL_ATTRIBS_BASE \
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \
EGL_RED_SIZE, 1, \
EGL_GREEN_SIZE, 1, \
EGL_BLUE_SIZE, 1, \
EGL_ALPHA_SIZE, 0, \
EGL_DEPTH_SIZE, 0
static void *gfx_ctx_xegl_init(video_frame_info_t *video_info, void *video_driver)
2014-10-27 14:35:23 +01:00
{
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
static const EGLint egl_attribs_gl[] = {
2014-10-27 14:35:23 +01:00
XEGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE,
};
static const EGLint egl_attribs_gles[] = {
2014-10-27 14:35:23 +01:00
XEGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE,
};
#ifdef EGL_KHR_create_context
static const EGLint egl_attribs_gles3[] = {
2014-10-27 14:35:23 +01:00
XEGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
EGL_NONE,
};
#endif
2016-03-01 06:56:41 +01:00
#ifdef HAVE_VG
static const EGLint egl_attribs_vg[] = {
2014-10-27 14:35:23 +01:00
XEGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
EGL_NONE,
};
2016-03-01 06:56:41 +01:00
#endif
const EGLint *attrib_ptr = NULL;
EGLint major, minor;
EGLint n;
2016-03-01 06:49:05 +01:00
#endif
xegl_ctx_data_t *xegl;
2015-11-20 13:27:31 +01:00
if (g_egl_inited)
return NULL;
XInitThreads();
xegl = (xegl_ctx_data_t*)calloc(1, sizeof(xegl_ctx_data_t));
if (!xegl)
return NULL;
switch (x_api)
{
case GFX_CTX_OPENGL_API:
attrib_ptr = egl_attribs_gl;
break;
case GFX_CTX_OPENGL_ES_API:
#ifdef EGL_KHR_create_context
if (xegl->egl.major >= 3)
attrib_ptr = egl_attribs_gles3;
else
#endif
attrib_ptr = egl_attribs_gles;
break;
case GFX_CTX_OPENVG_API:
2016-03-01 06:56:41 +01:00
#ifdef HAVE_VG
attrib_ptr = egl_attribs_vg;
2016-03-01 06:56:41 +01:00
#endif
break;
default:
2016-03-01 06:56:41 +01:00
break;
}
2015-11-19 11:07:52 +01:00
if (!x11_connect())
goto error;
2012-09-15 15:17:34 +02:00
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
if (!egl_init_context(&xegl->egl, EGL_PLATFORM_X11_KHR,
(EGLNativeDisplayType)g_x11_dpy, &major, &minor, &n, attrib_ptr))
{
egl_report_error();
2012-09-15 15:17:34 +02:00
goto error;
}
2012-09-15 15:17:34 +02:00
2016-03-01 06:49:05 +01:00
if (n == 0 || !egl_has_config(&xegl->egl))
goto error;
2016-03-01 06:49:05 +01:00
#endif
2012-09-15 15:17:34 +02:00
return xegl;
2012-09-15 15:17:34 +02:00
error:
gfx_ctx_xegl_destroy(xegl);
return NULL;
2012-09-15 15:17:34 +02:00
}
static EGLint *xegl_fill_attribs(xegl_ctx_data_t *xegl, EGLint *attr)
{
switch (x_api)
{
#ifdef EGL_KHR_create_context
case GFX_CTX_OPENGL_API:
{
unsigned version = xegl->egl.major * 1000 + xegl->egl.minor;
bool core = version >= 3001;
#ifdef GL_DEBUG
2016-05-12 02:58:09 +02:00
bool debug = true;
2015-09-29 17:35:28 +02:00
#else
2016-05-12 02:58:09 +02:00
struct retro_hw_render_callback *hwr = video_driver_get_hw_context();
bool debug = hwr->debug_context;
#endif
if (core)
{
*attr++ = EGL_CONTEXT_MAJOR_VERSION_KHR;
*attr++ = xegl->egl.major;
*attr++ = EGL_CONTEXT_MINOR_VERSION_KHR;
*attr++ = xegl->egl.minor;
/* Technically, we don't have core/compat until 3.2.
* Version 3.1 is either compat or not depending
* on GL_ARB_compatibility.
*/
if (version >= 3002)
{
*attr++ = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
*attr++ = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
}
}
2014-10-27 14:35:23 +01:00
if (debug)
{
*attr++ = EGL_CONTEXT_FLAGS_KHR;
*attr++ = EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
}
break;
}
#endif
case GFX_CTX_OPENGL_ES_API:
/* Same as EGL_CONTEXT_MAJOR_VERSION. */
2014-10-27 14:35:23 +01:00
*attr++ = EGL_CONTEXT_CLIENT_VERSION;
*attr++ = xegl->egl.major ? (EGLint)xegl->egl.major : 2;
2014-05-03 15:21:14 +02:00
#ifdef EGL_KHR_create_context
if (xegl->egl.minor > 0)
2014-05-03 15:21:14 +02:00
{
*attr++ = EGL_CONTEXT_MINOR_VERSION_KHR;
*attr++ = xegl->egl.minor;
2014-05-03 15:21:14 +02:00
}
#endif
break;
default:
break;
}
*attr = EGL_NONE;
return attr;
}
2016-03-01 06:49:05 +01:00
/* forward declaration */
static void gfx_ctx_xegl_set_swap_interval(void *data, unsigned swap_interval);
static bool gfx_ctx_xegl_set_video_mode(void *data,
2017-01-18 17:41:27 +01:00
video_frame_info_t *video_info,
unsigned width, unsigned height,
bool fullscreen)
2012-09-15 15:17:34 +02:00
{
2015-11-19 11:04:17 +01:00
XEvent event;
EGLint egl_attribs[16];
EGLint vid, num_visuals;
2016-11-06 18:41:59 +01:00
EGLint *attr = NULL;
bool true_full = false;
int x_off = 0;
int y_off = 0;
XVisualInfo temp = {0};
XSetWindowAttributes swa = {0};
2016-11-06 18:41:59 +01:00
XVisualInfo *vi = NULL;
xegl_ctx_data_t *xegl = (xegl_ctx_data_t*)data;
2015-06-26 17:46:13 +02:00
int (*old_handler)(Display*, XErrorEvent*) = NULL;
frontend_driver_install_signal_handler();
attr = egl_attribs;
attr = xegl_fill_attribs(xegl, attr);
2014-02-06 22:49:14 +01:00
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
if (!egl_get_native_visual_id(&xegl->egl, &vid))
2012-09-15 15:17:34 +02:00
goto error;
2016-03-01 06:49:05 +01:00
#endif
2012-09-15 15:17:34 +02:00
temp.visualid = vid;
2012-09-15 15:17:34 +02:00
vi = XGetVisualInfo(g_x11_dpy, VisualIDMask, &temp, &num_visuals);
2012-09-15 15:17:34 +02:00
if (!vi)
goto error;
2016-02-14 16:39:27 +01:00
swa.colormap = g_x11_cmap = XCreateColormap(
g_x11_dpy, RootWindow(g_x11_dpy, vi->screen),
2012-09-15 15:17:34 +02:00
vi->visual, AllocNone);
2016-02-14 16:39:27 +01:00
swa.event_mask = StructureNotifyMask | KeyPressMask |
ButtonPressMask | ButtonReleaseMask | KeyReleaseMask;
2012-09-29 10:47:55 +02:00
swa.override_redirect = fullscreen ? True : False;
2017-01-18 17:41:27 +01:00
if (fullscreen && !video_info->windowed_fullscreen)
2012-09-29 10:47:55 +02:00
{
if (x11_enter_fullscreen(video_info, g_x11_dpy, width, height))
2012-10-05 14:15:54 +02:00
{
xegl->should_reset_mode = true;
2012-10-05 14:15:54 +02:00
true_full = true;
}
else
RARCH_ERR("[X/EGL]: Entering true fullscreen failed. Will attempt windowed mode.\n");
2012-09-29 10:47:55 +02:00
}
2012-09-15 15:17:34 +02:00
2017-01-18 17:41:27 +01:00
if (video_info->monitor_index)
g_x11_screen = video_info->monitor_index - 1;
2012-10-12 21:15:58 +02:00
2012-10-12 19:05:29 +02:00
#ifdef HAVE_XINERAMA
2015-12-01 08:42:33 +01:00
if (fullscreen || g_x11_screen != 0)
2012-10-12 19:05:29 +02:00
{
unsigned new_width = width;
unsigned new_height = height;
2017-03-23 19:46:06 +01:00
if (xinerama_get_coord(g_x11_dpy, g_x11_screen,
2016-02-14 16:39:27 +01:00
&x_off, &y_off, &new_width, &new_height))
2015-12-01 08:42:33 +01:00
RARCH_LOG("[X/EGL]: Using Xinerama on screen #%u.\n", g_x11_screen);
2012-10-12 19:05:29 +02:00
else
RARCH_LOG("[X/EGL]: Xinerama is not active on screen.\n");
if (fullscreen)
{
width = new_width;
height = new_height;
}
}
#endif
RARCH_LOG("[X/EGL]: X = %d, Y = %d, W = %u, H = %u.\n",
x_off, y_off, width, height);
g_x11_win = XCreateWindow(g_x11_dpy, RootWindow(g_x11_dpy, vi->screen),
2012-10-12 19:05:29 +02:00
x_off, y_off, width, height, 0,
2012-09-15 15:17:34 +02:00
vi->depth, InputOutput, vi->visual,
2016-02-14 16:39:27 +01:00
CWBorderPixel | CWColormap | CWEventMask |
(true_full ? CWOverrideRedirect : 0), &swa);
XSetWindowBackground(g_x11_dpy, g_x11_win, 0);
2012-09-15 15:17:34 +02:00
2016-03-01 06:49:05 +01:00
if (!egl_create_context(&xegl->egl, (attr != egl_attribs) ? egl_attribs : NULL))
{
egl_report_error();
goto error;
}
2016-09-08 06:11:02 +02:00
if (!egl_create_surface(&xegl->egl, (void*)g_x11_win))
2012-09-15 15:17:34 +02:00
goto error;
x11_set_window_attr(g_x11_dpy, g_x11_win);
2017-01-18 17:41:27 +01:00
x11_update_title(NULL, video_info);
2012-10-12 19:05:29 +02:00
if (fullscreen)
x11_show_mouse(g_x11_dpy, g_x11_win, false);
2012-09-29 11:59:08 +02:00
2012-10-05 14:15:54 +02:00
if (true_full)
2012-09-29 11:59:08 +02:00
{
2013-02-23 14:49:49 +01:00
RARCH_LOG("[X/EGL]: Using true fullscreen.\n");
XMapRaised(g_x11_dpy, g_x11_win);
2012-09-29 11:59:08 +02:00
}
2014-10-27 14:35:23 +01:00
else if (fullscreen)
{
2014-10-27 14:35:23 +01:00
/* We attempted true fullscreen, but failed.
* Attempt using windowed fullscreen. */
XMapRaised(g_x11_dpy, g_x11_win);
2012-10-12 19:05:29 +02:00
RARCH_LOG("[X/EGL]: Using windowed fullscreen.\n");
2014-10-27 14:35:23 +01:00
/* We have to move the window to the screen we
* want to go fullscreen on first.
* x_off and y_off usually get ignored in XCreateWindow().
*/
x11_move_window(g_x11_dpy, g_x11_win, x_off, y_off, width, height);
x11_windowed_fullscreen(g_x11_dpy, g_x11_win);
}
2012-09-29 11:59:08 +02:00
else
2012-10-12 19:05:29 +02:00
{
XMapWindow(g_x11_dpy, g_x11_win);
2014-10-27 14:35:23 +01:00
/* If we want to map the window on a different screen,
* we'll have to do it by force.
*
* Otherwise, we should try to let the window manager sort it out.
* x_off and y_off usually get ignored in XCreateWindow().
*/
2015-12-01 08:42:33 +01:00
if (g_x11_screen)
x11_move_window(g_x11_dpy, g_x11_win, x_off, y_off, width, height);
2012-10-12 19:05:29 +02:00
}
2012-09-15 15:17:34 +02:00
2015-11-19 15:05:17 +01:00
x11_event_queue_check(&event);
2015-11-19 12:18:35 +01:00
x11_install_quit_atom();
2012-09-15 15:17:34 +02:00
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
gfx_ctx_xegl_set_swap_interval(&xegl->egl, xegl->egl.interval);
#endif
2014-10-27 14:35:23 +01:00
/* This can blow up on some drivers. It's not fatal,
* so override errors for this call.
*/
old_handler = XSetErrorHandler(x_nul_handler);
XSetInputFocus(g_x11_dpy, g_x11_win, RevertToNone, CurrentTime);
XSync(g_x11_dpy, False);
XSetErrorHandler(old_handler);
2012-09-15 15:17:34 +02:00
XFree(vi);
g_egl_inited = true;
2012-09-15 15:17:34 +02:00
2015-11-19 12:05:32 +01:00
if (!x11_input_ctx_new(true_full))
2013-12-08 14:42:03 +01:00
goto error;
2012-09-15 15:17:34 +02:00
return true;
error:
if (vi)
XFree(vi);
gfx_ctx_xegl_destroy(data);
2012-09-15 15:17:34 +02:00
return false;
}
static void gfx_ctx_xegl_input_driver(void *data,
const char *joypad_name,
const input_driver_t **input, void **input_data)
2012-09-15 15:17:34 +02:00
{
void *xinput = input_x.init(joypad_name);
2012-09-15 15:17:34 +02:00
*input = xinput ? &input_x : NULL;
*input_data = xinput;
}
2015-01-18 22:32:14 +01:00
static bool gfx_ctx_xegl_suppress_screensaver(void *data, bool enable)
{
(void)data;
if (video_driver_display_type_get() != RARCH_DISPLAY_X11)
2015-01-18 22:32:14 +01:00
return false;
x11_suspend_screensaver(video_driver_window_get(), enable);
2015-01-18 22:32:14 +01:00
return true;
}
static bool gfx_ctx_xegl_has_windowed(void *data)
{
(void)data;
/* TODO - verify if this has windowed mode or not. */
return true;
}
static bool gfx_ctx_xegl_bind_api(void *video_driver,
enum gfx_ctx_api api, unsigned major, unsigned minor)
2012-09-25 01:26:22 +02:00
{
g_egl_major = major;
g_egl_minor = minor;
x_api = api;
2012-09-25 01:26:22 +02:00
switch (api)
{
case GFX_CTX_OPENGL_API:
#ifndef EGL_KHR_create_context
if ((major * 1000 + minor) >= 3001)
2016-02-14 16:39:27 +01:00
break;
#endif
2012-09-25 01:26:22 +02:00
return eglBindAPI(EGL_OPENGL_API);
case GFX_CTX_OPENGL_ES_API:
#ifndef EGL_KHR_create_context
if (major >= 3)
2016-02-14 16:39:27 +01:00
break;
#endif
2012-09-25 01:26:22 +02:00
return eglBindAPI(EGL_OPENGL_ES_API);
case GFX_CTX_OPENVG_API:
2016-03-01 06:56:41 +01:00
#ifdef HAVE_VG
2012-09-25 01:26:22 +02:00
return eglBindAPI(EGL_OPENVG_API);
2016-03-01 06:56:41 +01:00
#endif
2012-09-25 01:26:22 +02:00
default:
2015-04-16 23:03:55 +02:00
break;
2012-09-25 01:26:22 +02:00
}
2015-04-16 23:03:55 +02:00
return false;
2012-09-25 01:26:22 +02:00
}
static void gfx_ctx_xegl_show_mouse(void *data, bool state)
2013-03-29 13:46:11 +01:00
{
(void)data;
x11_show_mouse(g_x11_dpy, g_x11_win, state);
2013-03-29 13:46:11 +01:00
}
2017-05-19 03:34:53 +02:00
static void gfx_ctx_xegl_swap_buffers(void *data, void *data2)
2016-03-01 06:49:05 +01:00
{
xegl_ctx_data_t *xegl = (xegl_ctx_data_t*)data;
switch (x_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_EGL
egl_swap_buffers(&xegl->egl);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static void gfx_ctx_xegl_bind_hw_render(void *data, bool enable)
{
xegl_ctx_data_t *xegl = (xegl_ctx_data_t*)data;
switch (x_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
egl_bind_hw_render(&xegl->egl, enable);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static void gfx_ctx_xegl_set_swap_interval(void *data, unsigned swap_interval)
{
xegl_ctx_data_t *xegl = (xegl_ctx_data_t*)data;
switch (x_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_EGL
egl_set_swap_interval(&xegl->egl, swap_interval);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static gfx_ctx_proc_t gfx_ctx_xegl_get_proc_address(const char *symbol)
{
switch (x_api)
{
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
return egl_get_proc_address(symbol);
#else
break;
#endif
2016-10-08 14:37:26 +02:00
case GFX_CTX_OPENGL_API:
break;
2016-03-01 06:49:05 +01:00
case GFX_CTX_NONE:
default:
break;
}
return NULL;
}
static uint32_t gfx_ctx_xegl_get_flags(void *data)
{
2016-05-05 17:35:28 +02:00
uint32_t flags = 0;
BIT32_SET(flags, GFX_CTX_FLAGS_NONE);
return flags;
}
2016-05-05 17:35:28 +02:00
static void gfx_ctx_xegl_set_flags(void *data, uint32_t flags)
{
(void)data;
}
const gfx_ctx_driver_t gfx_ctx_x_egl =
{
gfx_ctx_xegl_init,
gfx_ctx_xegl_destroy,
gfx_ctx_xegl_bind_api,
2016-03-01 06:49:05 +01:00
gfx_ctx_xegl_set_swap_interval,
gfx_ctx_xegl_set_video_mode,
x11_get_video_size,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
2015-11-19 10:25:07 +01:00
x11_get_metrics,
NULL,
2017-01-18 17:41:27 +01:00
x11_update_title,
x11_check_window,
NULL, /* set_resize */
2016-10-08 14:37:26 +02:00
x11_has_focus,
2015-01-18 22:32:14 +01:00
gfx_ctx_xegl_suppress_screensaver,
gfx_ctx_xegl_has_windowed,
2016-03-01 06:49:05 +01:00
gfx_ctx_xegl_swap_buffers,
gfx_ctx_xegl_input_driver,
2016-03-01 06:49:05 +01:00
gfx_ctx_xegl_get_proc_address,
NULL,
NULL,
gfx_ctx_xegl_show_mouse,
2012-09-25 01:26:22 +02:00
"x-egl",
gfx_ctx_xegl_get_flags,
2016-05-05 17:35:28 +02:00
gfx_ctx_xegl_set_flags,
2016-08-31 15:02:07 +02:00
gfx_ctx_xegl_bind_hw_render,
NULL,
NULL
2012-09-25 01:26:22 +02:00
};