RetroArch/gfx/context/xegl_ctx.c

448 lines
10 KiB
C
Raw Normal View History

2012-09-15 15:17:34 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
* Copyright (C) 2011-2012 - 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/>.
*/
// X/EGL context. Mostly used for testing GLES code paths.
// Should be its own file as it has lots of X11 stuff baked into it as well.
#include "../../driver.h"
#include "../gfx_context.h"
#include "../gl_common.h"
#include "../gfx_common.h"
2012-09-26 15:52:25 +02:00
#include "x11_common.h"
2012-09-15 15:17:34 +02:00
#include <signal.h>
#include <stdint.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
2012-09-15 15:17:34 +02:00
static Display *g_dpy;
static Window g_win;
static Colormap g_cmap;
static Atom g_quit_atom;
static bool g_has_focus;
static EGLContext g_egl_ctx;
static EGLSurface g_egl_surf;
static EGLDisplay g_egl_dpy;
static EGLConfig g_config;
2012-09-29 10:47:55 +02:00
static XF86VidModeModeInfo g_desktop_mode;
static bool g_should_reset_mode;
2012-09-15 15:17:34 +02:00
static volatile sig_atomic_t g_quit;
static bool g_inited;
static unsigned g_interval;
2012-09-25 01:26:22 +02:00
static enum gfx_ctx_api g_api;
2012-09-15 15:17:34 +02:00
static void sighandler(int sig)
{
(void)sig;
g_quit = 1;
}
2012-09-29 10:47:55 +02:00
static Bool egl_wait_notify(Display *d, XEvent *e, char *arg)
{
(void)d;
(void)e;
return e->type == MapNotify && e->xmap.window == g_win;
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_get_video_size(unsigned *width, unsigned *height);
static void gfx_ctx_destroy(void);
static void gfx_ctx_swap_interval(unsigned interval)
2012-09-15 15:17:34 +02:00
{
g_interval = interval;
2012-09-25 01:26:22 +02:00
if (g_egl_dpy)
{
RARCH_LOG("[X/EGL]: eglSwapInterval(%u)\n", g_interval);
if (!eglSwapInterval(g_egl_dpy, g_interval))
RARCH_ERR("[X/EGL]: eglSwapInterval() failed.\n");
}
2012-09-15 15:17:34 +02:00
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_check_window(bool *quit,
2012-09-15 15:17:34 +02:00
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
(void)frame_count;
unsigned new_width = *width, new_height = *height;
gfx_ctx_get_video_size(&new_width, &new_height);
if (new_width != *width || new_height != *height)
{
*resize = true;
*width = new_width;
*height = new_height;
}
XEvent event;
while (XPending(g_dpy))
{
XNextEvent(g_dpy, &event);
switch (event.type)
{
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == g_quit_atom)
g_quit = true;
break;
case DestroyNotify:
g_quit = true;
break;
case MapNotify:
g_has_focus = true;
2012-09-15 15:17:34 +02:00
break;
case UnmapNotify:
g_has_focus = false;
break;
}
}
*quit = g_quit;
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_swap_buffers(void)
2012-09-15 15:17:34 +02:00
{
eglSwapBuffers(g_egl_dpy, g_egl_surf);
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_set_resize(unsigned width, unsigned height)
2012-09-15 15:17:34 +02:00
{
(void)width;
(void)height;
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_update_window_title(bool reset)
2012-09-15 15:17:34 +02:00
{
if (reset)
gfx_window_title_reset();
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
XStoreName(g_dpy, g_win, buf);
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_get_video_size(unsigned *width, unsigned *height)
2012-09-15 15:17:34 +02:00
{
if (!g_dpy || g_win == None)
{
2012-09-16 00:57:39 +02:00
Display *dpy = XOpenDisplay(NULL);
if (dpy)
{
int screen = DefaultScreen(dpy);
*width = DisplayWidth(dpy, screen);
*height = DisplayHeight(dpy, screen);
XCloseDisplay(dpy);
}
else
{
*width = 0;
*height = 0;
}
2012-09-15 15:17:34 +02:00
}
else
{
XWindowAttributes target;
XGetWindowAttributes(g_dpy, g_win, &target);
*width = target.width;
*height = target.height;
}
}
2012-09-25 01:26:22 +02:00
static bool gfx_ctx_init(void)
2012-09-15 15:17:34 +02:00
{
if (g_inited)
return false;
#define EGL_ATTRIBS_BASE \
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \
EGL_RED_SIZE, 8, \
EGL_GREEN_SIZE, 8, \
EGL_BLUE_SIZE, 8, \
EGL_DEPTH_SIZE, 0, \
EGL_STENCIL_SIZE, 0
static const EGLint egl_attribs_gl[] = {
EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE,
};
static const EGLint egl_attribs_gles[] = {
EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE,
};
static const EGLint egl_attribs_vg[] = {
EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
EGL_NONE,
};
const EGLint *attrib_ptr;
switch (g_api)
{
case GFX_CTX_OPENGL_API:
attrib_ptr = egl_attribs_gl;
break;
case GFX_CTX_OPENGL_ES_API:
attrib_ptr = egl_attribs_gles;
break;
case GFX_CTX_OPENVG_API:
attrib_ptr = egl_attribs_vg;
break;
default:
attrib_ptr = NULL;
}
2012-09-15 15:17:34 +02:00
g_quit = 0;
g_dpy = XOpenDisplay(NULL);
if (!g_dpy)
goto error;
g_egl_dpy = eglGetDisplay(g_dpy);
if (!g_egl_dpy)
goto error;
EGLint egl_major, egl_minor;
if (!eglInitialize(g_egl_dpy, &egl_major, &egl_minor))
goto error;
RARCH_LOG("[X/EGL]: EGL version: %d.%d\n", egl_major, egl_minor);
EGLint num_configs;
if (!eglChooseConfig(g_egl_dpy, attrib_ptr, &g_config, 1, &num_configs)
2012-09-15 15:17:34 +02:00
|| num_configs == 0 || !g_config)
goto error;
return true;
error:
gfx_ctx_destroy();
return false;
}
2012-09-25 01:26:22 +02:00
static bool gfx_ctx_set_video_mode(
2012-09-15 15:17:34 +02:00
unsigned width, unsigned height,
unsigned bits, bool fullscreen)
{
(void)bits;
struct sigaction sa = {{0}};
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
XVisualInfo temp = {0};
XSetWindowAttributes swa = {0};
XVisualInfo *vi = NULL;
2012-09-15 15:17:34 +02:00
EGLint vid;
if (!eglGetConfigAttrib(g_egl_dpy, g_config, EGL_NATIVE_VISUAL_ID, &vid))
goto error;
temp.visualid = vid;
2012-09-15 15:17:34 +02:00
EGLint num_visuals;
vi = XGetVisualInfo(g_dpy, VisualIDMask, &temp, &num_visuals);
2012-09-15 15:17:34 +02:00
if (!vi)
goto error;
swa.colormap = g_cmap = XCreateColormap(g_dpy, RootWindow(g_dpy, vi->screen),
vi->visual, AllocNone);
swa.event_mask = StructureNotifyMask;
2012-09-29 10:47:55 +02:00
swa.override_redirect = fullscreen ? True : False;
if (fullscreen)
{
if (x11_enter_fullscreen(g_dpy, width, height, &g_desktop_mode))
g_should_reset_mode = true;
}
2012-09-15 15:17:34 +02:00
g_win = XCreateWindow(g_dpy, RootWindow(g_dpy, vi->screen),
0, 0, width ? width : 200, height ? height : 200, 0,
vi->depth, InputOutput, vi->visual,
2012-09-29 10:47:55 +02:00
CWBorderPixel | CWColormap | CWEventMask | (fullscreen ? CWOverrideRedirect : 0), &swa);
2012-09-15 15:17:34 +02:00
XSetWindowBackground(g_dpy, g_win, 0);
// GLES 2.0. Don't use for any other API.
static const EGLint egl_ctx_gles_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE,
};
g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT,
(g_api == GFX_CTX_OPENGL_ES_API) ? egl_ctx_gles_attribs : NULL);
2012-09-15 15:17:34 +02:00
if (!g_egl_ctx)
goto error;
g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, g_win, NULL);
if (!g_egl_surf)
goto error;
if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx))
goto error;
gfx_ctx_update_window_title(true);
2012-09-26 15:52:25 +02:00
x11_hide_mouse(g_dpy, g_win);
2012-09-15 15:17:34 +02:00
XMapWindow(g_dpy, g_win);
2012-09-29 10:47:55 +02:00
XEvent event;
XIfEvent(g_dpy, &event, egl_wait_notify, NULL);
XSetInputFocus(g_dpy, g_win, RevertToNone, CurrentTime);
2012-09-15 15:17:34 +02:00
g_quit_atom = XInternAtom(g_dpy, "WM_DELETE_WINDOW", False);
if (g_quit_atom)
XSetWMProtocols(g_dpy, g_win, &g_quit_atom, 1);
2012-09-25 01:26:22 +02:00
gfx_ctx_swap_interval(g_interval);
2012-09-15 15:17:34 +02:00
XFree(vi);
g_has_focus = true;
g_inited = true;
2012-09-28 22:38:42 +02:00
driver.display_type = RARCH_DISPLAY_X11;
driver.video_display = (uintptr_t)g_dpy;
driver.video_window = (Window)g_win;
2012-09-15 15:17:34 +02:00
return true;
error:
if (vi)
XFree(vi);
gfx_ctx_destroy();
return false;
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_destroy(void)
2012-09-15 15:17:34 +02:00
{
if (g_egl_dpy)
{
if (g_egl_ctx)
2012-09-15 15:41:12 +02:00
{
eglMakeCurrent(g_egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2012-09-15 15:17:34 +02:00
eglDestroyContext(g_egl_dpy, g_egl_ctx);
2012-09-15 15:41:12 +02:00
}
2012-09-15 15:17:34 +02:00
if (g_egl_surf)
eglDestroySurface(g_egl_dpy, g_egl_surf);
eglTerminate(g_egl_dpy);
}
g_egl_ctx = NULL;
g_egl_surf = NULL;
g_egl_dpy = NULL;
2012-09-15 15:41:12 +02:00
g_config = 0;
2012-09-15 15:17:34 +02:00
if (g_win)
{
2012-09-15 15:41:12 +02:00
XUnmapWindow(g_dpy, g_win);
2012-09-15 15:17:34 +02:00
XDestroyWindow(g_dpy, g_win);
g_win = None;
}
if (g_cmap)
{
XFreeColormap(g_dpy, g_cmap);
g_cmap = None;
}
2012-09-29 10:47:55 +02:00
if (g_should_reset_mode)
{
x11_exit_fullscreen(g_dpy, &g_desktop_mode);
g_should_reset_mode = false;
}
2012-09-15 15:17:34 +02:00
if (g_dpy)
{
XCloseDisplay(g_dpy);
g_dpy = NULL;
}
g_inited = false;
}
2012-09-25 01:26:22 +02:00
static void gfx_ctx_input_driver(const input_driver_t **input, void **input_data)
2012-09-15 15:17:34 +02:00
{
void *xinput = input_x.init();
*input = xinput ? &input_x : NULL;
*input_data = xinput;
}
2012-09-25 01:26:22 +02:00
static bool gfx_ctx_has_focus(void)
2012-09-15 15:17:34 +02:00
{
if (!g_inited)
return false;
Window win;
int rev;
XGetInputFocus(g_dpy, &win, &rev);
return win == g_win && g_has_focus;
}
2012-09-25 01:26:22 +02:00
static gfx_ctx_proc_t gfx_ctx_get_proc_address(const char *symbol)
2012-09-15 15:17:34 +02:00
{
return eglGetProcAddress(symbol);
}
2012-09-25 01:26:22 +02:00
static bool gfx_ctx_bind_api(enum gfx_ctx_api api)
{
g_api = api;
switch (api)
{
case GFX_CTX_OPENGL_API:
return eglBindAPI(EGL_OPENGL_API);
case GFX_CTX_OPENGL_ES_API:
return eglBindAPI(EGL_OPENGL_ES_API);
case GFX_CTX_OPENVG_API:
return eglBindAPI(EGL_OPENVG_API);
default:
return false;
}
}
const gfx_ctx_driver_t gfx_ctx_x_egl = {
gfx_ctx_init,
gfx_ctx_destroy,
gfx_ctx_bind_api,
gfx_ctx_swap_interval,
gfx_ctx_set_video_mode,
gfx_ctx_get_video_size,
2012-09-25 13:51:44 +02:00
NULL,
2012-09-25 01:26:22 +02:00
gfx_ctx_update_window_title,
gfx_ctx_check_window,
gfx_ctx_set_resize,
gfx_ctx_has_focus,
gfx_ctx_swap_buffers,
gfx_ctx_input_driver,
gfx_ctx_get_proc_address,
"x-egl",
};