RetroArch/gfx/common/egl_common.c

402 lines
10 KiB
C
Raw Normal View History

2015-11-19 12:16:43 +00:00
/* RetroArch - A frontend for libretro.
2017-01-22 12:40:32 +00:00
* Copyright (c) 2011-2017 - 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/>.
*/
2016-04-22 05:56:32 +00:00
#include <stdlib.h>
2016-09-08 06:15:40 +00:00
2015-11-19 12:24:51 +00:00
#include <retro_assert.h>
2015-11-23 11:03:38 +00:00
2016-09-08 06:15:40 +00:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2015-11-19 12:16:43 +00:00
2015-11-19 13:23:14 +00:00
#ifdef HAVE_OPENGL
#include "gl_common.h"
#endif
2015-11-19 12:16:43 +00:00
2016-09-08 06:15:40 +00:00
#include "egl_common.h"
#include "../../verbosity.h"
#include "../../frontend/frontend_driver.h"
2016-12-04 02:45:46 +00:00
bool g_egl_inited = false;
2015-11-19 14:47:30 +00:00
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_ACCESS:
str = "EGL_BAD_ACCESS";
2015-11-19 12:16:43 +00:00
break;
case EGL_BAD_ALLOC:
str = "EGL_BAD_ALLOC";
break;
case EGL_BAD_ATTRIBUTE:
str = "EGL_BAD_ATTRIBUTE";
break;
case EGL_BAD_CONFIG:
str = "EGL_BAD_CONFIG";
2015-11-19 12:16:43 +00:00
break;
case EGL_BAD_CONTEXT:
str = "EGL_BAD_CONTEXT";
break;
case EGL_BAD_CURRENT_SURFACE:
str = "EGL_BAD_CURRENT_SURFACE";
break;
case EGL_BAD_DISPLAY:
str = "EGL_BAD_DISPLAY";
break;
case EGL_BAD_MATCH:
str = "EGL_BAD_MATCH";
break;
case EGL_BAD_NATIVE_PIXMAP:
str = "EGL_BAD_NATIVE_PIXMAP";
break;
case EGL_BAD_NATIVE_WINDOW:
str = "EGL_BAD_NATIVE_WINDOW";
break;
case EGL_BAD_PARAMETER:
str = "EGL_BAD_PARAMETER";
break;
case EGL_BAD_SURFACE:
str = "EGL_BAD_SURFACE";
break;
2015-11-19 12:16:43 +00:00
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
2016-03-01 05:49:05 +00:00
void egl_destroy(egl_ctx_data_t *egl)
2015-11-19 13:23:14 +00:00
{
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_inited = false;
frontend_driver_destroy_signal_handler_state();
2015-11-19 13:23:14 +00:00
}
2015-11-19 13:32:39 +00:00
2016-03-01 05:49:05 +00:00
void egl_bind_hw_render(egl_ctx_data_t *egl, bool enable)
2015-11-19 13:32:39 +00:00
{
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;
2016-03-01 05:49:05 +00:00
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
2016-03-01 05:49:05 +00:00
void egl_set_swap_interval(egl_ctx_data_t *egl, unsigned interval)
2015-11-19 14:16:37 +00:00
{
/* Can be called before initialization.
* Some contexts require that swap interval
2015-11-19 14:16:37 +00:00
* 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
2016-03-01 05:49:05 +00:00
void egl_get_video_size(egl_ctx_data_t *egl, unsigned *width, unsigned *height)
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
bool check_egl_version(int minMajorVersion, int minMinorVersion)
{
int count;
2017-04-16 22:04:08 +00:00
int major, minor;
const char *str = eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
2017-04-16 22:04:08 +00:00
if (!str)
return false;
count = sscanf(str, "%d.%d", &major, &minor);
if (count != 2)
return false;
if (major < minMajorVersion)
return false;
2017-04-16 22:04:08 +00:00
if (major > minMajorVersion)
return true;
2017-04-16 22:04:08 +00:00
if (minor >= minMinorVersion)
return true;
2017-04-16 22:04:08 +00:00
return false;
}
bool check_egl_client_extension(const char *name)
{
size_t nameLen;
2017-04-16 22:04:08 +00:00
const char *str = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2017-04-16 22:04:08 +00:00
/* The EGL implementation doesn't support client extensions at all. */
if (!str)
return false;
nameLen = strlen(name);
while (*str != '\0')
{
2017-04-16 22:04:08 +00:00
/* Use strspn and strcspn to find the start position and length of each
* token in the extension string. Using strtok could also work, but
* that would require allocating a copy of the string. */
size_t len = strcspn(str, " ");
if (len == nameLen && strncmp(str, name, nameLen) == 0)
return true;
str += len;
str += strspn(str, " ");
}
return false;
}
static EGLDisplay get_egl_display(EGLenum platform, void *native)
{
if (platform != EGL_NONE)
{
2017-04-16 22:04:08 +00:00
/* If the client library supports at least EGL 1.5, then we can call
* eglGetPlatformDisplay. Otherwise, see if eglGetPlatformDisplayEXT
* is available. */
#if defined(EGL_VERSION_1_5)
if (check_egl_version(1, 5))
{
typedef EGLDisplay (EGLAPIENTRY * pfn_eglGetPlatformDisplay)
(EGLenum platform, void *native_display, const EGLAttrib *attrib_list);
pfn_eglGetPlatformDisplay ptr_eglGetPlatformDisplay;
RARCH_LOG("[EGL] Found EGL client version >= 1.5, trying eglGetPlatformDisplay\n");
ptr_eglGetPlatformDisplay = (pfn_eglGetPlatformDisplay)
eglGetProcAddress("eglGetPlatformDisplay");
if (ptr_eglGetPlatformDisplay != NULL)
{
EGLDisplay dpy = ptr_eglGetPlatformDisplay(platform, native, NULL);
if (dpy != EGL_NO_DISPLAY)
return dpy;
}
}
2017-04-22 07:37:36 +00:00
#endif /* defined(EGL_VERSION_1_5) */
#if defined(EGL_EXT_platform_base)
if (check_egl_client_extension("EGL_EXT_platform_base"))
{
PFNEGLGETPLATFORMDISPLAYEXTPROC ptr_eglGetPlatformDisplayEXT;
RARCH_LOG("[EGL] Found EGL_EXT_platform_base, trying eglGetPlatformDisplayEXT\n");
ptr_eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
eglGetProcAddress("eglGetPlatformDisplayEXT");
if (ptr_eglGetPlatformDisplayEXT != NULL)
{
EGLDisplay dpy = ptr_eglGetPlatformDisplayEXT(platform, native, NULL);
if (dpy != EGL_NO_DISPLAY)
return dpy;
}
}
2017-04-22 07:37:36 +00:00
#endif /* defined(EGL_EXT_platform_base) */
}
2017-04-16 22:04:08 +00:00
/* Either the caller didn't provide a platform type, or the EGL
* implementation doesn't support eglGetPlatformDisplay. In this case, try
* eglGetDisplay and hope for the best. */
RARCH_LOG("[EGL] Falling back to eglGetDisplay\n");
return eglGetDisplay((EGLNativeDisplayType) native);
}
2016-03-01 05:49:05 +00:00
bool egl_init_context(egl_ctx_data_t *egl,
EGLenum platform,
2016-06-23 05:44:56 +00:00
void *display_data,
2015-11-19 16:52:13 +00:00
EGLint *major, EGLint *minor,
EGLint *n, const EGLint *attrib_ptr)
{
EGLDisplay dpy = get_egl_display(platform, display_data);
if (dpy == EGL_NO_DISPLAY)
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;
}
egl->dpy = dpy;
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->major = g_egl_major;
egl->minor = g_egl_minor;
2015-11-19 16:52:13 +00:00
return true;
}
2016-03-01 05:49:05 +00:00
bool egl_create_context(egl_ctx_data_t *egl, const EGLint *egl_attribs)
{
2017-03-30 00:39:08 +00:00
EGLContext ctx = eglCreateContext(egl->dpy, egl->config, EGL_NO_CONTEXT,
egl_attribs);
2017-03-30 00:39:08 +00:00
if (ctx == EGL_NO_CONTEXT)
return false;
2017-03-30 00:39:08 +00:00
egl->ctx = ctx;
egl->hw_ctx = NULL;
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)
2016-09-03 14:30:21 +00:00
return false;
}
return true;
}
2015-11-20 12:36:16 +00:00
2016-06-23 05:44:56 +00:00
bool egl_create_surface(egl_ctx_data_t *egl, void *native_window)
2015-11-20 12:36:16 +00:00
{
2016-06-23 05:44:56 +00:00
egl->surf = eglCreateWindowSurface(egl->dpy, egl->config, (NativeWindowType)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
2016-03-01 05:49:05 +00:00
bool egl_get_native_visual_id(egl_ctx_data_t *egl, EGLint *value)
2015-12-08 13:38:06 +00:00
{
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;
}
2016-03-01 05:49:05 +00:00
bool egl_has_config(egl_ctx_data_t *egl)
2015-12-08 13:42:02 +00:00
{
if (!egl->config)
2015-12-08 13:42:02 +00:00
{
RARCH_ERR("[EGL]: No EGL configurations available.\n");
return false;
}
return true;
}