Files
RetroArch/gfx/drivers_context/android_ctx.c
LibretroAdmin ae907bff65 libretro: OpenGL contexts for a core's own threads
RETRO_ENVIRONMENT_GET_HW_SHARED_CONTEXT_INTERFACE (104, experimental)
gives an OpenGL or OpenGL ES hardware core contexts in its own
context's share group, for threads of its own: create and destroy on
the core's thread, make_current on the thread that uses one. A core
can then render or compile shaders off its main thread with nothing of
the window system in the core. A frontend that cannot make such
contexts answers false, and the core keeps its GL work on the thread
that calls retro_run.

RetroArch answers it with the context drivers' shared contexts, which
GLX, Wayland and X11 EGL already had for OpenXR; WGL, KMS/DRM and
Android get them here. The WGL one shares with the context current on
the calling thread (the core's), made with the window context's
attributes, and binds on the window's device context.

samples/gfx/gl_shared_context is a core whose second thread clears a
texture through such a context that retro_run then shows; the EGL
hardware core workflow runs it on GLX and EGL, threaded and not, with
a compatibility and a 3.3 core context.
2026-11-12 04:32:16 +01:00

504 lines
14 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* 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/>.
*/
#include <stdint.h>
#include <sys/system_properties.h>
#include <formats/image.h>
#include <string/stdstring.h>
#include <compat/strl.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#ifdef HAVE_EGL
#include "../common/egl_common.h"
#endif
#include "../common/android_windows.h"
#include "../../frontend/frontend_driver.h"
#include "../../frontend/drivers/platform_unix.h"
#include "../../verbosity.h"
#include "../../configuration.h"
#ifdef HAVE_OPENGLES
#ifndef EGL_OPENGL_ES3_BIT_KHR
#define EGL_OPENGL_ES3_BIT_KHR 0x0040
#endif
#endif
typedef struct
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
EGLint format; /* EGL_NATIVE_VISUAL_ID */
#endif
} android_ctx_data_t;
/* TODO/FIXME - static globals */
static enum gfx_ctx_api android_api = GFX_CTX_NONE;
#ifdef HAVE_OPENGLES
static bool g_es3 = false;
#endif
/* FORWARD DECLARATION */
bool android_display_get_metrics(void *data,
enum display_metric_types type, float *value);
bool android_display_has_focus(void *data);
#ifdef HAVE_EGL
/* The second display's EGL surface, made on first use after its
* surface arrives, on the main context */
static bool android_gfx_ctx_window_bind_gl(void *owner,
video_window_native_t *window)
{
android_ctx_data_t *and = (android_ctx_data_t*)owner;
android_window_t *state;
bool made = false;
if (!window)
return egl_window_surface_bind(&and->egl, EGL_NO_SURFACE, false);
state = (android_window_t*)window->state;
if (!state || !window->configured || !window->surface)
return false;
if (state->gl_surface && state->gl_window != window->surface)
{
egl_window_surface_free(&and->egl, (EGLSurface)state->gl_surface);
state->gl_surface = NULL;
}
if (!state->gl_surface)
{
EGLSurface surf;
struct android_app *android_app = (struct android_app*)g_android;
/* Java took the surface back since the last poll */
if ( android_app && state->node
&& state->node->generation == retro_atomic_load_acquire_int(
&android_app->second_display.surface_gone))
return false;
ANativeWindow_setBuffersGeometry(
(ANativeWindow*)window->surface, 0, 0, and->format);
if ((surf = egl_window_surface_new(&and->egl, window->surface))
== EGL_NO_SURFACE)
return false;
state->gl_surface = surf;
state->gl_window = window->surface;
made = true;
}
/* The interval is the surface's own, so once is enough */
return egl_window_surface_bind(&and->egl,
(EGLSurface)state->gl_surface, made);
}
/* A window whose surface Java took back loses its EGL surface now,
* not when the window goes */
static unsigned android_gfx_ctx_window_poll(void *owner,
video_window_event_t *events, unsigned capacity)
{
unsigned i;
android_ctx_data_t *and = (android_ctx_data_t*)owner;
unsigned count = android_windows_poll(owner, events, capacity);
for (i = 1; i < RETRO_VIDEO_VIEWS_MAX; i++)
{
android_window_t *state = android_windows_find(owner, i);
if ( state && state->gl_surface
&& state->gl_window != state->common.native->surface)
{
egl_window_surface_free(&and->egl, (EGLSurface)state->gl_surface);
state->gl_surface = NULL;
state->gl_window = NULL;
}
}
return count;
}
static bool android_gfx_ctx_window_present_gl(void *owner,
video_window_native_t *window)
{
android_ctx_data_t *and = (android_ctx_data_t*)owner;
android_window_t *state = window ? (android_window_t*)window->state : NULL;
if (!state || !state->gl_surface || !window->presentable)
return false;
return egl_window_surface_swap(&and->egl, (EGLSurface)state->gl_surface);
}
static void android_gfx_ctx_window_destroy(void *owner,
video_window_native_t *window)
{
android_ctx_data_t *and = (android_ctx_data_t*)owner;
android_window_t *state = window ? (android_window_t*)window->state : NULL;
if (state && state->common.owner == owner && state->gl_surface)
{
egl_window_surface_free(&and->egl, (EGLSurface)state->gl_surface);
state->gl_surface = NULL;
}
android_windows_destroy(owner, window);
}
/* The EGL surface dies with this context's EGLDisplay; the next driver
* makes its own */
static void android_gfx_ctx_window_keep(void *owner,
video_window_native_t *window)
{
android_ctx_data_t *and = (android_ctx_data_t*)owner;
android_window_t *state = window ? (android_window_t*)window->state : NULL;
if (state && state->common.owner == owner && state->gl_surface)
{
egl_window_surface_free(&and->egl, (EGLSurface)state->gl_surface);
state->gl_surface = NULL;
state->gl_window = NULL;
}
android_windows_keep(owner, window);
}
static const gfx_ctx_window_driver_t android_gfx_ctx_windows = {
android_windows_create,
android_windows_update,
android_gfx_ctx_window_poll,
android_gfx_ctx_window_bind_gl,
android_gfx_ctx_window_present_gl,
android_gfx_ctx_window_destroy,
android_gfx_ctx_window_keep,
android_windows_take
};
#endif
static void android_gfx_ctx_destroy(void *data)
{
android_ctx_data_t *and = (android_ctx_data_t*)data;
if (!and)
return;
#ifdef HAVE_EGL
android_windows_free(and, android_gfx_ctx_window_destroy);
egl_destroy(&and->egl);
#endif
free(data);
}
static void *android_gfx_ctx_init(void *video_driver)
{
#ifdef HAVE_OPENGLES
EGLint n, major, minor;
EGLint format;
#ifdef DEBUG
struct retro_hw_render_callback *hwr = video_driver_get_hw_context();
bool debug = hwr->debug_context;
#endif
EGLint attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
#endif
struct android_app *android_app = (struct android_app*)g_android;
ANativeWindow *window = NULL;
android_ctx_data_t *and = (android_ctx_data_t*)
calloc(1, sizeof(*and));
/* Separate the two checks so the combined bail doesn't leak
* 'and' when it succeeded but android_app was NULL. In the
* pre-patch form both conditions shared one return without
* a free. Also fixed the return value: the pre-patch
* 'return false' was 'return 0' which is NULL for a void*-
* returning function but sloppy; use NULL explicitly. */
if (!and)
return NULL;
if (!android_app)
{
free(and);
return NULL;
}
#ifdef HAVE_OPENGLES
if (g_es3)
attribs[1] = EGL_OPENGL_ES3_BIT_KHR;
#endif
#ifdef HAVE_EGL
RARCH_LOG("[Android] EGL: GLES version = %d.\n", g_es3 ? 3 : 2);
if (!egl_init_context(&and->egl, EGL_NONE, EGL_DEFAULT_DISPLAY,
&major, &minor, &n, attribs, NULL))
{
egl_report_error();
goto error;
}
if (!egl_get_native_visual_id(&and->egl, &format))
goto error;
and->format = format;
#endif
/* Held across the call, so the app thread cannot hand the window
* back to the framework under it. */
if (!(window = (ANativeWindow*)android_lifecycle_window_acquire(
&android_app->lc)))
{
android_gfx_ctx_destroy(and);
return NULL;
}
ANativeWindow_setBuffersGeometry(window, 0, 0, format);
android_lifecycle_window_release(&android_app->lc);
return and;
error:
android_gfx_ctx_destroy(and);
return NULL;
}
static void android_gfx_ctx_get_video_size(void *data,
unsigned *dims)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
egl_get_video_size(&and->egl, dims);
#endif
}
static void android_gfx_ctx_check_window(void *data, bool *quit,
bool *resize, unsigned *dims)
{
unsigned new_dims = 0;
android_ctx_data_t *and = (android_ctx_data_t*)data;
*quit = false;
#ifdef HAVE_EGL
egl_get_video_size(&and->egl, &new_dims);
#endif
if (new_dims != *dims)
{
RARCH_LOG("[Android] Resizing (%u x %u) -> (%u x %u).\n",
VIDEO_SCALE_W(*dims), VIDEO_SCALE_H(*dims),
VIDEO_SCALE_W(new_dims), VIDEO_SCALE_H(new_dims));
*dims = new_dims;
*resize = true;
}
}
static bool android_gfx_ctx_set_resize(void *data, unsigned dims) { return false; }
static bool android_gfx_ctx_set_video_mode(void *data,
unsigned dims,
bool fullscreen)
{
#if defined(HAVE_OPENGLES)
struct android_app *android_app = (struct android_app*)g_android;
android_ctx_data_t *and = (android_ctx_data_t*)data;
#if defined(HAVE_EGL)
EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, g_es3 ? 3 : 2,
#ifdef DEBUG
EGL_CONTEXT_FLAGS_KHR, debug ? EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR : 0,
#endif
EGL_NONE
};
if (android_api == GFX_CTX_OPENGL_ES_API)
{
if (!egl_create_context(&and->egl, context_attributes))
{
egl_report_error();
return false;
}
if (!egl_create_surface(&and->egl, android_app_window(android_app)))
return false;
}
#endif
#endif
return true;
}
static void android_gfx_ctx_input_driver(void *data,
const char *joypad_name)
{
/* no input driver of this context's own: the frontend starts the
* platform's */
input_driver_video_window(INPUT_WINDOW_PLATFORM, NULL);
}
static enum gfx_ctx_api android_gfx_ctx_get_api(void *data)
{
return android_api;
}
static bool android_gfx_ctx_bind_api(void *data,
enum gfx_ctx_api api, unsigned major, unsigned minor)
{
unsigned version;
android_api = api;
#ifdef HAVE_OPENGLES
version = major * 100 + minor;
if (version >= 300)
g_es3 = true;
if (api == GFX_CTX_OPENGL_ES_API)
return true;
#endif
return false;
}
static bool android_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; }
static void android_gfx_ctx_swap_buffers(void *data)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
egl_swap_buffers(&and->egl);
#endif
}
static void android_gfx_ctx_set_swap_interval(void *data, int swap_interval)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
egl_set_swap_interval(&and->egl, swap_interval);
#endif
}
static void android_gfx_ctx_bind_hw_render(void *data, bool enable)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
egl_bind_hw_render(&and->egl, enable);
#endif
}
static void android_gfx_ctx_release_current(void *data)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
if (and)
egl_release_current(&and->egl);
#endif
}
static uint32_t android_gfx_ctx_get_flags(void *data)
{
uint32_t flags = 0;
#ifdef HAVE_GLSL
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_GLSL);
#endif
return flags;
}
static void android_gfx_ctx_set_flags(void *data, uint32_t flags) { }
static bool android_gfx_ctx_create_surface(void *data)
{
#ifdef HAVE_EGL
struct android_app *android_app = (struct android_app*)g_android;
android_ctx_data_t *and = (android_ctx_data_t*)data;
return egl_create_surface(&and->egl, android_app_window(android_app));
#else
return false;
#endif
}
static bool android_gfx_ctx_destroy_surface(void *data)
{
#ifdef HAVE_EGL
android_ctx_data_t *and = (android_ctx_data_t*)data;
return egl_destroy_surface(&and->egl);
#else
return false;
#endif
}
#ifdef HAVE_EGL
/* A context in the share group, of the window's client API: OpenGL ES */
static bool android_gfx_ctx_shared_context_new(void *data,
gfx_ctx_shared_context_t *out)
{
android_ctx_data_t *and = (android_ctx_data_t*)data;
memset(out, 0, sizeof(*out));
if (!and || android_api != GFX_CTX_OPENGL_ES_API)
return false;
return egl_shared_context_new(&and->egl, EGL_OPENGL_ES_API, out);
}
#endif
const gfx_ctx_driver_t gfx_ctx_android = {
android_gfx_ctx_init,
android_gfx_ctx_destroy,
android_gfx_ctx_get_api,
android_gfx_ctx_bind_api,
android_gfx_ctx_set_swap_interval,
android_gfx_ctx_set_video_mode,
android_gfx_ctx_get_video_size,
NULL, /* get_refresh_rate */
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics - handled by display server */
NULL,
NULL, /* update_title */
android_gfx_ctx_check_window,
android_gfx_ctx_set_resize,
android_display_has_focus,
android_gfx_ctx_suppress_screensaver,
false, /* has_windowed */
android_gfx_ctx_swap_buffers,
android_gfx_ctx_input_driver,
#ifdef HAVE_EGL
egl_get_proc_address,
#else
NULL,
#endif
NULL,
NULL,
NULL,
"egl_android",
android_gfx_ctx_get_flags,
android_gfx_ctx_set_flags,
android_gfx_ctx_bind_hw_render,
NULL,
NULL,
android_gfx_ctx_create_surface,
android_gfx_ctx_destroy_surface,
NULL, /* presentable */
NULL, /* last_present_time */
android_gfx_ctx_release_current,
#ifdef HAVE_EGL
android_gfx_ctx_shared_context_new,
egl_shared_context_bind,
egl_shared_context_free,
#else
NULL, /* shared_context_new */
NULL, /* shared_context_bind */
NULL, /* shared_context_free */
#endif
#ifdef HAVE_EGL
&android_gfx_ctx_windows
#else
NULL /* windows */
#endif
};