RetroArch/gfx/common/egl_common.c

282 lines
6.7 KiB
C
Raw Normal View History

2015-11-19 12:16:43 +00:00
/* RetroArch - A frontend for libretro.
2016-01-10 03:41:52 +00:00
* Copyright (c) 2011-2016 - Daniel De Matteis
2015-11-19 12:16:43 +00:00
*
* 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/>.
*/
2015-11-19 12:24:51 +00:00
#include <retro_assert.h>
2015-11-23 11:03:38 +00:00
#include "../../verbosity.h"
2015-11-19 12:16:43 +00:00
#include "egl_common.h"
2015-11-19 13:23:14 +00:00
#ifdef HAVE_OPENGL
#include "gl_common.h"
#endif
2015-11-19 12:16:43 +00:00
2015-11-19 14:47:30 +00:00
volatile sig_atomic_t g_egl_quit;
bool g_egl_inited;
2015-11-19 14:47:30 +00:00
2015-11-20 12:27:31 +00:00
enum gfx_ctx_api g_egl_api;
unsigned g_egl_major = 0;
unsigned g_egl_minor = 0;
2015-11-19 12:16:43 +00:00
void egl_report_error(void)
{
EGLint error = eglGetError();
const char *str = NULL;
switch (error)
{
case EGL_SUCCESS:
str = "EGL_SUCCESS";
break;
case EGL_BAD_DISPLAY:
str = "EGL_BAD_DISPLAY";
break;
case EGL_BAD_SURFACE:
str = "EGL_BAD_SURFACE";
break;
case EGL_BAD_CONTEXT:
str = "EGL_BAD_CONTEXT";
break;
default:
str = "Unknown";
break;
}
RARCH_ERR("[EGL]: #0x%x, %s\n", (unsigned)error, str);
}
2015-11-19 12:24:51 +00:00
gfx_ctx_proc_t egl_get_proc_address(const char *symbol)
{
2015-12-10 13:10:42 +00:00
return eglGetProcAddress(symbol);
2015-11-19 12:24:51 +00:00
}
2015-11-19 13:23:14 +00:00
2015-11-19 14:34:40 +00:00
void egl_destroy(void *data)
2015-11-19 13:23:14 +00:00
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
if (egl->dpy)
2015-11-19 13:23:14 +00:00
{
#if defined HAVE_OPENGL
#if !defined(RARCH_MOBILE)
if (egl->ctx != EGL_NO_CONTEXT)
2015-11-19 13:23:14 +00:00
{
glFlush();
glFinish();
}
#endif
2015-11-19 13:23:14 +00:00
#endif
eglMakeCurrent(egl->dpy,
2015-11-19 13:23:14 +00:00
EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl->ctx != EGL_NO_CONTEXT)
eglDestroyContext(egl->dpy, egl->ctx);
2015-11-19 13:23:14 +00:00
if (egl->hw_ctx != EGL_NO_CONTEXT)
eglDestroyContext(egl->dpy, egl->hw_ctx);
2015-11-19 13:23:14 +00:00
if (egl->surf != EGL_NO_SURFACE)
eglDestroySurface(egl->dpy, egl->surf);
eglTerminate(egl->dpy);
2015-11-19 13:23:14 +00:00
}
/* Be as careful as possible in deinit.
* If we screw up, any TTY will not restore.
*/
egl->ctx = EGL_NO_CONTEXT;
egl->hw_ctx = EGL_NO_CONTEXT;
egl->surf = EGL_NO_SURFACE;
egl->dpy = EGL_NO_DISPLAY;
egl->config = 0;
g_egl_quit = 0;
egl->api = GFX_CTX_NONE;
g_egl_inited = false;
2015-11-19 13:23:14 +00:00
}
2015-11-19 13:32:39 +00:00
void egl_bind_hw_render(void *data, bool enable)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
egl->use_hw_ctx = enable;
2015-11-19 13:32:39 +00:00
if (egl->dpy == EGL_NO_DISPLAY)
2015-12-03 22:33:50 +00:00
return;
if (egl->surf == EGL_NO_SURFACE)
2015-11-19 13:32:39 +00:00
return;
eglMakeCurrent(egl->dpy, egl->surf,
egl->surf,
enable ? egl->hw_ctx : egl->ctx);
2015-11-19 13:32:39 +00:00
}
2015-11-19 13:38:55 +00:00
void egl_swap_buffers(void *data)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
if (egl->dpy == EGL_NO_DISPLAY)
return;
if (egl->surf == EGL_NO_SURFACE)
return;
eglSwapBuffers(egl->dpy, egl->surf);
2015-11-19 13:38:55 +00:00
}
2015-11-19 14:16:37 +00:00
void egl_set_swap_interval(void *data, unsigned interval)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
2015-11-19 14:16:37 +00:00
/* Can be called before initialization.
* Some contexts require that swap interval
* is known at startup time.
*/
egl->interval = interval;
2015-11-19 14:16:37 +00:00
if (egl->dpy == EGL_NO_DISPLAY)
2015-11-19 14:16:37 +00:00
return;
if (!(eglGetCurrentContext()))
return;
RARCH_LOG("[EGL]: eglSwapInterval(%u)\n", interval);
if (!eglSwapInterval(egl->dpy, interval))
2015-11-19 14:16:37 +00:00
{
RARCH_ERR("[EGL]: eglSwapInterval() failed.\n");
egl_report_error();
}
}
2015-11-19 14:21:04 +00:00
void egl_get_video_size(void *data, unsigned *width, unsigned *height)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
2015-11-19 14:21:04 +00:00
*width = 0;
*height = 0;
if (egl->dpy != EGL_NO_DISPLAY && egl->surf != EGL_NO_SURFACE)
2015-11-19 14:21:04 +00:00
{
EGLint gl_width, gl_height;
eglQuerySurface(egl->dpy, egl->surf, EGL_WIDTH, &gl_width);
eglQuerySurface(egl->dpy, egl->surf, EGL_HEIGHT, &gl_height);
2015-11-19 14:21:04 +00:00
*width = gl_width;
*height = gl_height;
}
}
2015-11-19 14:55:57 +00:00
static void egl_sighandler(int sig)
{
(void)sig;
g_egl_quit = 1;
}
void egl_install_sighandlers(void)
{
2015-11-19 16:52:13 +00:00
struct sigaction sa;
2015-11-19 14:55:57 +00:00
2015-11-19 16:52:13 +00:00
sa.sa_sigaction = NULL;
sa.sa_handler = egl_sighandler;
sa.sa_flags = SA_RESTART;
2015-11-19 14:55:57 +00:00
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
2015-11-19 16:52:13 +00:00
bool egl_init_context(void *data, NativeDisplayType display,
2015-11-19 16:52:13 +00:00
EGLint *major, EGLint *minor,
EGLint *n, const EGLint *attrib_ptr)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
egl->dpy = eglGetDisplay(display);
if (!egl->dpy)
2015-11-19 16:52:13 +00:00
{
2015-11-19 16:55:49 +00:00
RARCH_ERR("[EGL]: Couldn't get EGL display.\n");
2015-11-19 16:52:13 +00:00
return false;
}
if (!eglInitialize(egl->dpy, major, minor))
2015-11-19 16:52:13 +00:00
return false;
2015-11-19 16:55:49 +00:00
RARCH_LOG("[EGL]: EGL version: %d.%d\n", *major, *minor);
if (!eglChooseConfig(egl->dpy, attrib_ptr, &egl->config, 1, n) || *n != 1)
2015-11-19 16:52:13 +00:00
return false;
egl->api = g_egl_api;
egl->major = g_egl_major;
egl->minor = g_egl_minor;
2015-11-19 16:52:13 +00:00
return true;
}
2015-12-08 22:34:26 +00:00
bool egl_create_context(void *data, const EGLint *egl_attribs)
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
egl->ctx = eglCreateContext(egl->dpy, egl->config, EGL_NO_CONTEXT,
egl_attribs);
egl->hw_ctx = NULL;
if (egl->ctx == EGL_NO_CONTEXT)
return false;
if (egl->use_hw_ctx)
{
egl->hw_ctx = eglCreateContext(egl->dpy, egl->config, egl->ctx,
egl_attribs);
RARCH_LOG("[EGL]: Created shared context: %p.\n", (void*)egl->hw_ctx);
if (egl->hw_ctx == EGL_NO_CONTEXT)
return false;;
}
return true;
}
2015-11-20 12:36:16 +00:00
bool egl_create_surface(void *data, NativeWindowType native_window)
2015-11-20 12:36:16 +00:00
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
egl->surf = eglCreateWindowSurface(egl->dpy, egl->config, native_window, NULL);
2015-12-03 22:33:50 +00:00
if (egl->surf == EGL_NO_SURFACE)
2015-11-20 12:36:16 +00:00
return false;
/* Connect the context to the surface. */
if (!eglMakeCurrent(egl->dpy, egl->surf, egl->surf, egl->ctx))
2015-11-20 12:36:16 +00:00
return false;
2015-11-20 12:43:05 +00:00
RARCH_LOG("[EGL]: Current context: %p.\n", (void*)eglGetCurrentContext());
2015-11-20 12:36:16 +00:00
return true;
}
2015-12-08 13:38:06 +00:00
bool egl_get_native_visual_id(void *data, EGLint *value)
2015-12-08 13:38:06 +00:00
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
if (!eglGetConfigAttrib(egl->dpy, egl->config,
2015-12-08 13:38:06 +00:00
EGL_NATIVE_VISUAL_ID, value))
{
RARCH_ERR("[EGL]: egl_get_native_visual_id failed.\n");
return false;
}
return true;
}
bool egl_has_config(void *data)
2015-12-08 13:42:02 +00:00
{
egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
if (!egl->config)
2015-12-08 13:42:02 +00:00
{
RARCH_ERR("[EGL]: No EGL configurations available.\n");
return false;
}
return true;
}