diff --git a/configure b/configure index a9d158bb944c6..20510ecc082f1 100755 --- a/configure +++ b/configure @@ -881,6 +881,8 @@ enable_video_directfb enable_directfb_shared enable_video_kmsdrm enable_kmsdrm_shared +enable_video_atomic +enable_atomic_shared enable_video_dummy enable_video_opengl enable_video_opengles @@ -1668,6 +1670,8 @@ Optional Features: dynamically load directfb support [default=yes] --enable-video-kmsdrm use KMSDRM video driver [default=yes] --enable-kmsdrm-shared dynamically load kmsdrm support [default=yes] + --enable-video-atomic use ATOMIC video driver [default=yes] + --enable-atomic-shared dynamically load atomic support [default=yes] --enable-video-dummy use dummy video driver [default=yes] --enable-video-opengl include OpenGL support [default=yes] --enable-video-opengles include OpenGL ES support [default=yes] @@ -22264,6 +22268,224 @@ $as_echo "$kmsdrm_shared" >&6; } fi } +CheckATOMIC() +{ + # Check whether --enable-video-atomic was given. +if test "${enable_video_atomic+set}" = set; then : + enableval=$enable_video_atomic; +else + enable_video_atomic=yes +fi + + + if test x$enable_video = xyes && \ + test x$enable_video_atomic = xyes && \ + test x$video_opengl_egl = xyes; then + + video_atomic=no + + +pkg_failed=no +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libdrm >= 1.4.82" >&5 +$as_echo_n "checking for libdrm >= 1.4.82... " >&6; } + +if test -n "$LIBDRM_CFLAGS"; then + pkg_cv_LIBDRM_CFLAGS="$LIBDRM_CFLAGS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libdrm >= 1.4.82\""; } >&5 + ($PKG_CONFIG --exists --print-errors "libdrm >= 1.4.82") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_LIBDRM_CFLAGS=`$PKG_CONFIG --cflags "libdrm >= 1.4.82" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi +if test -n "$LIBDRM_LIBS"; then + pkg_cv_LIBDRM_LIBS="$LIBDRM_LIBS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libdrm >= 1.4.82\""; } >&5 + ($PKG_CONFIG --exists --print-errors "libdrm >= 1.4.82") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_LIBDRM_LIBS=`$PKG_CONFIG --libs "libdrm >= 1.4.82" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi + + + +if test $pkg_failed = yes; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then + _pkg_short_errors_supported=yes +else + _pkg_short_errors_supported=no +fi + if test $_pkg_short_errors_supported = yes; then + LIBDRM_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libdrm >= 1.4.82" 2>&1` + else + LIBDRM_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libdrm >= 1.4.82" 2>&1` + fi + # Put the nasty error message in config.log where it belongs + echo "$LIBDRM_PKG_ERRORS" >&5 + + libdrm_avail=no +elif test $pkg_failed = untried; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + libdrm_avail=no +else + LIBDRM_CFLAGS=$pkg_cv_LIBDRM_CFLAGS + LIBDRM_LIBS=$pkg_cv_LIBDRM_LIBS + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + libdrm_avail=yes +fi + +pkg_failed=no +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gbm >= 11.1.0" >&5 +$as_echo_n "checking for gbm >= 11.1.0... " >&6; } + +if test -n "$LIBGBM_CFLAGS"; then + pkg_cv_LIBGBM_CFLAGS="$LIBGBM_CFLAGS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"gbm >= 11.1.0\""; } >&5 + ($PKG_CONFIG --exists --print-errors "gbm >= 11.1.0") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_LIBGBM_CFLAGS=`$PKG_CONFIG --cflags "gbm >= 11.1.0" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi +if test -n "$LIBGBM_LIBS"; then + pkg_cv_LIBGBM_LIBS="$LIBGBM_LIBS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"gbm >= 11.1.0\""; } >&5 + ($PKG_CONFIG --exists --print-errors "gbm >= 11.1.0") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_LIBGBM_LIBS=`$PKG_CONFIG --libs "gbm >= 11.1.0" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi + + + +if test $pkg_failed = yes; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then + _pkg_short_errors_supported=yes +else + _pkg_short_errors_supported=no +fi + if test $_pkg_short_errors_supported = yes; then + LIBGBM_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "gbm >= 11.1.0" 2>&1` + else + LIBGBM_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "gbm >= 11.1.0" 2>&1` + fi + # Put the nasty error message in config.log where it belongs + echo "$LIBGBM_PKG_ERRORS" >&5 + + libgbm_avail=no +elif test $pkg_failed = untried; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + libgbm_avail=no +else + LIBGBM_CFLAGS=$pkg_cv_LIBGBM_CFLAGS + LIBGBM_LIBS=$pkg_cv_LIBGBM_LIBS + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + libgbm_avail=yes +fi + + if test x$libdrm_avail = xyes -a x$libgbm_avail = xyes; then + video_atomic=yes + fi + + if test x$video_atomic = xyes; then + # Check whether --enable-atomic-shared was given. +if test "${enable_atomic_shared+set}" = set; then : + enableval=$enable_atomic_shared; +else + enable_atomic_shared=yes +fi + + + +$as_echo "#define SDL_VIDEO_DRIVER_ATOMIC 1" >>confdefs.h + + SOURCES="$SOURCES $srcdir/src/video/atomic/*.c" + EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBDRM_CFLAGS $LIBGBM_CFLAGS" + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for atomic video backend dynamic loading support" >&5 +$as_echo_n "checking for atomic video backend dynamic loading support... " >&6; } + atomic_shared=no + drm_lib=`find_lib "libdrm.so.*" "$DRM_LIBS"` + gbm_lib=`find_lib "libgbm.so.*" "$DRM_LIBS"` + if test x$have_loadso != xyes && \ + test x$enable_atomic_shared = xyes; then + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: You must have SDL_LoadObject() support for dynamic atomic loading" >&5 +$as_echo "$as_me: WARNING: You must have SDL_LoadObject() support for dynamic atomic loading" >&2;} + fi + if test x$have_loadso = xyes && \ + test x$enable_atomic_shared = xyes && test x$drm_lib != x && test x$gbm_lib != x; then + atomic_shared=yes + +cat >>confdefs.h <<_ACEOF +#define SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC "$drm_lib" +_ACEOF + + +cat >>confdefs.h <<_ACEOF +#define SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM "$gbm_lib" +_ACEOF + + +cat >>confdefs.h <<_ACEOF +#define HAVE_ATOMIC_SHARED "TRUE" +_ACEOF + + SUMMARY_video="${SUMMARY_video} atomic(dynamic)" + else + EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBDRM_LIBS $LIBGBM_LIBS" + SUMMARY_video="${SUMMARY_video} atomic" + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $atomic_shared" >&5 +$as_echo "$atomic_shared" >&6; } + have_video=yes + fi + fi +} + CheckDummyVideo() { # Check whether --enable-video-dummy was given. @@ -24923,9 +25145,10 @@ $as_echo "#define SDL_VIDEO_DRIVER_ANDROID 1" >>confdefs.h CheckRPI CheckX11 CheckDirectFB - # Need to check for EGL first because KMSDRM and Wayland depends on it. + # Need to check for EGL first because KMSDRM, KMSDRM-ATOMIC and Wayland depends on it. CheckEGL CheckKMSDRM + CheckATOMIC CheckGLX CheckOpenGL CheckOpenGLES diff --git a/configure.ac b/configure.ac index 80ee87bf5459f..a1588921dc2ff 100644 --- a/configure.ac +++ b/configure.ac @@ -2244,6 +2244,60 @@ CheckKMSDRM() fi } +dnl Find ATOMIC +CheckATOMIC() +{ + AC_ARG_ENABLE(video-atomic, +[AS_HELP_STRING([--enable-video-atomic], [use ATOMIC video driver [default=no]])], + , enable_video_atomic=yes) + + if test x$enable_video = xyes && \ + test x$enable_video_atomic = xyes && \ + test x$video_opengl_egl = xyes; then + + video_atomic=no + + PKG_CHECK_MODULES([LIBDRM], [libdrm >= 1.4.82], libdrm_avail=yes, libdrm_avail=no) + PKG_CHECK_MODULES([LIBGBM], [gbm >= 11.1.0], libgbm_avail=yes, libgbm_avail=no) + + if test x$libdrm_avail = xyes -a x$libgbm_avail = xyes; then + video_atomic=yes + fi + + if test x$video_atomic = xyes; then + AC_ARG_ENABLE(atomic-shared, +[AS_HELP_STRING([--enable-atomic-shared], [dynamically load atomic support [default=yes]])], + , enable_atomic_shared=yes) + + AC_DEFINE(SDL_VIDEO_DRIVER_ATOMIC, 1, [ ]) + SOURCES="$SOURCES $srcdir/src/video/atomic/*.c" + EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBDRM_CFLAGS $LIBGBM_CFLAGS" + + AC_MSG_CHECKING(for atomic video backend dynamic loading support) + atomic_shared=no + drm_lib=[`find_lib "libdrm.so.*" "$DRM_LIBS"`] + gbm_lib=[`find_lib "libgbm.so.*" "$DRM_LIBS"`] + if test x$have_loadso != xyes && \ + test x$enable_atomic_shared = xyes; then + AC_MSG_WARN([You must have SDL_LoadObject() support for dynamic atomic loading]) + fi + if test x$have_loadso = xyes && \ + test x$enable_atomic_shared = xyes && test x$drm_lib != x && test x$gbm_lib != x; then + atomic_shared=yes + AC_DEFINE_UNQUOTED(SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC, "$drm_lib", [ ]) + AC_DEFINE_UNQUOTED(SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM, "$gbm_lib", [ ]) + AC_DEFINE_UNQUOTED(HAVE_ATOMIC_SHARED, "TRUE", [ ]) + SUMMARY_video="${SUMMARY_video} atomic(dynamic)" + else + EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBDRM_LIBS $LIBGBM_LIBS" + SUMMARY_video="${SUMMARY_video} atomic" + fi + AC_MSG_RESULT($atomic_shared) + have_video=yes + fi + fi +} + dnl rcg04172001 Set up the Null video driver. CheckDummyVideo() { @@ -3548,9 +3602,10 @@ case "$host" in CheckRPI CheckX11 CheckDirectFB - # Need to check for EGL first because KMSDRM and Wayland depends on it. + # Need to check for EGL first because KMSDRM, KMSDRM-ATOMIC and Wayland depends on it. CheckEGL CheckKMSDRM + CheckATOMIC CheckGLX CheckOpenGL CheckOpenGLES diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index 9ddd932e3779f..fc4d3318895c5 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -370,6 +370,9 @@ #undef SDL_VIDEO_DRIVER_KMSDRM #undef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC #undef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM +#undef SDL_VIDEO_DRIVER_ATOMIC +#undef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC +#undef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM #undef SDL_VIDEO_DRIVER_ANDROID #undef SDL_VIDEO_DRIVER_EMSCRIPTEN #undef SDL_VIDEO_DRIVER_X11_DYNAMIC diff --git a/include/SDL_syswm.h b/include/SDL_syswm.h index 12e86a17cc4c9..2ed5db7d1623d 100644 --- a/include/SDL_syswm.h +++ b/include/SDL_syswm.h @@ -113,7 +113,7 @@ typedef void *EGLSurface; #endif #endif /* SDL_PROTOTYPES_ONLY */ -#if defined(SDL_VIDEO_DRIVER_KMSDRM) +#if defined(SDL_VIDEO_DRIVER_KMSDRM) || defined(SDL_VIDEO_ATOMIC) struct gbm_device; #endif @@ -325,7 +325,7 @@ struct SDL_SysWMinfo } vivante; #endif -#if defined(SDL_VIDEO_DRIVER_KMSDRM) +#if defined(SDL_VIDEO_DRIVER_KMSDRM) || defined(SDL_VIDEO_ATOMIC) struct { int dev_index; /**< Device index (ex: the X in /dev/dri/cardX) */ diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h index 086166d243d8c..8dbebe6ea4077 100644 --- a/src/video/SDL_sysvideo.h +++ b/src/video/SDL_sysvideo.h @@ -437,7 +437,7 @@ extern VideoBootStrap PSP_bootstrap; extern VideoBootStrap VITA_bootstrap; extern VideoBootStrap RPI_bootstrap; extern VideoBootStrap KMSDRM_bootstrap; -extern VideoBootStrap KMSDRM_LEGACY_bootstrap; +extern VideoBootStrap ATOMIC_bootstrap; extern VideoBootStrap DUMMY_bootstrap; extern VideoBootStrap Wayland_bootstrap; extern VideoBootStrap NACL_bootstrap; diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index d029f46731dc8..f3de594abe9a0 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -100,6 +100,9 @@ static VideoBootStrap *bootstrap[] = { #if SDL_VIDEO_DRIVER_KMSDRM &KMSDRM_bootstrap, #endif +#if SDL_VIDEO_DRIVER_ATOMIC + &ATOMIC_bootstrap, +#endif #if SDL_VIDEO_DRIVER_RPI &RPI_bootstrap, #endif diff --git a/src/video/atomic/SDL_atomicdyn.c b/src/video/atomic/SDL_atomicdyn.c new file mode 100644 index 0000000000000..28da078f8509e --- /dev/null +++ b/src/video/atomic/SDL_atomicdyn.c @@ -0,0 +1,169 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +#define DEBUG_DYNAMIC_ATOMIC 0 + +#include "SDL_atomicdyn.h" + + +#ifdef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC + +#include "SDL_name.h" +#include "SDL_loadso.h" + +typedef struct +{ + void *lib; + const char *libname; +} atomicdynlib; + +#ifndef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC +#define SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC NULL +#endif +#ifndef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM +#define SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM NULL +#endif + +static atomicdynlib atomiclibs[] = { + {NULL, SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC_GBM}, + {NULL, SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC} +}; + +static void * +ATOMIC_GetSym(const char *fnname, int *pHasModule) +{ + int i; + void *fn = NULL; + for (i = 0; i < SDL_TABLESIZE(atomiclibs); i++) { + if (atomiclibs[i].lib != NULL) { + fn = SDL_LoadFunction(atomiclibs[i].lib, fnname); + if (fn != NULL) + break; + } + } + +#if DEBUG_DYNAMIC_ATOMIC + if (fn != NULL) + SDL_Log("ATOMIC: Found '%s' in %s (%p)\n", fnname, atomiclibs[i].libname, fn); + else + SDL_Log("ATOMIC: Symbol '%s' NOT FOUND!\n", fnname); +#endif + + if (fn == NULL) + *pHasModule = 0; /* kill this module. */ + + return fn; +} + +#endif /* SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC */ + +/* Define all the function pointers and wrappers... */ +#define SDL_ATOMIC_MODULE(modname) int SDL_ATOMIC_HAVE_##modname = 0; +#define SDL_ATOMIC_SYM(rc,fn,params) SDL_DYNATOMICFN_##fn ATOMIC_##fn = NULL; +#define SDL_ATOMIC_SYM_CONST(type,name) SDL_DYNATOMICCONST_##name ATOMIC_##name = NULL; +#include "SDL_atomicsym.h" + +static int atomic_load_refcount = 0; + +void +SDL_ATOMIC_UnloadSymbols(void) +{ + /* Don't actually unload if more than one module is using the libs... */ + if (atomic_load_refcount > 0) { + if (--atomic_load_refcount == 0) { +#ifdef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC + int i; +#endif + + /* set all the function pointers to NULL. */ +#define SDL_ATOMIC_MODULE(modname) SDL_ATOMIC_HAVE_##modname = 0; +#define SDL_ATOMIC_SYM(rc,fn,params) ATOMIC_##fn = NULL; +#define SDL_ATOMIC_SYM_CONST(type,name) ATOMIC_##name = NULL; +#include "SDL_atomicsym.h" + + +#ifdef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC + for (i = 0; i < SDL_TABLESIZE(atomiclibs); i++) { + if (atomiclibs[i].lib != NULL) { + SDL_UnloadObject(atomiclibs[i].lib); + atomiclibs[i].lib = NULL; + } + } +#endif + } + } +} + +/* returns non-zero if all needed symbols were loaded. */ +int +SDL_ATOMIC_LoadSymbols(void) +{ + int rc = 1; /* always succeed if not using Dynamic ATOMIC stuff. */ + + /* deal with multiple modules needing these symbols... */ + if (atomic_load_refcount++ == 0) { +#ifdef SDL_VIDEO_DRIVER_ATOMIC_DYNAMIC + int i; + int *thismod = NULL; + for (i = 0; i < SDL_TABLESIZE(atomiclibs); i++) { + if (atomiclibs[i].libname != NULL) { + atomiclibs[i].lib = SDL_LoadObject(atomiclibs[i].libname); + } + } + +#define SDL_ATOMIC_MODULE(modname) SDL_ATOMIC_HAVE_##modname = 1; /* default yes */ +#include "SDL_atomicsym.h" + +#define SDL_ATOMIC_MODULE(modname) thismod = &SDL_ATOMIC_HAVE_##modname; +#define SDL_ATOMIC_SYM(rc,fn,params) ATOMIC_##fn = (SDL_DYNATOMICFN_##fn) ATOMIC_GetSym(#fn,thismod); +#define SDL_ATOMIC_SYM_CONST(type,name) ATOMIC_##name = *(SDL_DYNATOMICCONST_##name*) ATOMIC_GetSym(#name,thismod); +#include "SDL_atomicsym.h" + + if ((SDL_ATOMIC_HAVE_LIBDRM) && (SDL_ATOMIC_HAVE_GBM)) { + /* all required symbols loaded. */ + SDL_ClearError(); + } else { + /* in case something got loaded... */ + SDL_ATOMIC_UnloadSymbols(); + rc = 0; + } + +#else /* no dynamic ATOMIC */ + +#define SDL_ATOMIC_MODULE(modname) SDL_ATOMIC_HAVE_##modname = 1; /* default yes */ +#define SDL_ATOMIC_SYM(rc,fn,params) ATOMIC_##fn = fn; +#define SDL_ATOMIC_SYM_CONST(type,name) ATOMIC_##name = name; +#include "SDL_atomicsym.h" + +#endif + } + + return rc; +} + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicdyn.h b/src/video/atomic/SDL_atomicdyn.h new file mode 100644 index 0000000000000..5c500c4dd9372 --- /dev/null +++ b/src/video/atomic/SDL_atomicdyn.h @@ -0,0 +1,54 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL_atomicdyn_h_ +#define SDL_atomicdyn_h_ + +#include "../../SDL_internal.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int SDL_ATOMIC_LoadSymbols(void); +void SDL_ATOMIC_UnloadSymbols(void); + +/* Declare all the function pointers and wrappers... */ +#define SDL_ATOMIC_SYM(rc,fn,params) \ + typedef rc (*SDL_DYNATOMICFN_##fn) params; \ + extern SDL_DYNATOMICFN_##fn ATOMIC_##fn; +#define SDL_ATOMIC_SYM_CONST(type, name) \ + typedef type SDL_DYNATOMICCONST_##name; \ + extern SDL_DYNATOMICCONST_##name ATOMIC_##name; +#include "SDL_atomicsym.h" + +#ifdef __cplusplus +} +#endif + +#endif /* SDL_atomicdyn_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicevents.c b/src/video/atomic/SDL_atomicevents.c new file mode 100644 index 0000000000000..86908c2e4b053 --- /dev/null +++ b/src/video/atomic/SDL_atomicevents.c @@ -0,0 +1,43 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +#include "SDL_atomicvideo.h" +#include "SDL_atomicevents.h" + +#ifdef SDL_INPUT_LINUXEV +#include "../../core/linux/SDL_evdev.h" +#endif + +void ATOMIC_PumpEvents(_THIS) +{ +#ifdef SDL_INPUT_LINUXEV + SDL_EVDEV_Poll(); +#endif + +} + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + diff --git a/src/video/atomic/SDL_atomicevents.h b/src/video/atomic/SDL_atomicevents.h new file mode 100644 index 0000000000000..f3894c7b40584 --- /dev/null +++ b/src/video/atomic/SDL_atomicevents.h @@ -0,0 +1,32 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_atomicevents_h_ +#define SDL_atomicevents_h_ + +extern void ATOMIC_PumpEvents(_THIS); +extern void ATOMIC_EventInit(_THIS); +extern void ATOMIC_EventQuit(_THIS); + +#endif /* SDL_atomicevents_h_ */ diff --git a/src/video/atomic/SDL_atomicgles.c b/src/video/atomic/SDL_atomicgles.c new file mode 100644 index 0000000000000..4bb29e49d17f8 --- /dev/null +++ b/src/video/atomic/SDL_atomicgles.c @@ -0,0 +1,387 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +#include "SDL_atomicvideo.h" +#include "SDL_atomicgles.h" +#include "SDL_atomicdyn.h" +#include "SDL_hints.h" + +#ifndef EGL_PLATFORM_GBM_MESA +#define EGL_PLATFORM_GBM_MESA 0x31D7 +#endif + +#ifndef EGL_SYNC_NATIVE_FENCE_ANDROID +#define EGL_SYNC_NATIVE_FENCE_ANDROID 0x3144 +#endif + +#ifndef EGL_SYNC_NATIVE_FENCE_FD_ANDROID +#define EGL_SYNC_NATIVE_FENCE_FD_ANDROID 0x3145 +#endif + +#ifndef EGL_NO_NATIVE_FENCE_FD_ANDROID +#define EGL_NO_NATIVE_FENCE_FD_ANDROID -1 +#endif + +/* EGL implementation of SDL OpenGL support */ + +void +ATOMIC_GLES_DefaultProfileConfig(_THIS, int *mask, int *major, int *minor) +{ + /* if SDL was _also_ built with the Raspberry Pi driver (so we're + definitely a Pi device), default to GLES2. */ +#if SDL_VIDEO_DRIVER_RPI + *mask = SDL_GL_CONTEXT_PROFILE_ES; + *major = 2; + *minor = 0; +#endif +} + +int +ATOMIC_GLES_LoadLibrary(_THIS, const char *path) { + /* Just pretend you do this here, but don't do it until ATOMIC_CreateWindow(), + where we do the same library load we would normally do here + because this gets called by SDL_CreateWindow() before ATOMIC_CreateWindow(), + so gbm dev isn't yet created when this is called, AND we can't alter the + call order in SDL_CreateWindow(). */ +#if 0 + NativeDisplayType display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev; + return SDL_EGL_LoadLibrary(_this, path, display, EGL_PLATFORM_GBM_MESA); +#endif + return 0; +} + +void +ATOMIC_GLES_UnloadLibrary(_THIS) { + /* As with ATOMIC_GLES_LoadLibrary(), we define our own "dummy" unloading function + so we manually unload the library whenever we want. */ +} + +SDL_EGL_CreateContext_impl(ATOMIC) + +int ATOMIC_GLES_SetSwapInterval(_THIS, int interval) { + if (!_this->egl_data) { + return SDL_SetError("EGL not initialized"); + } + + if (interval == 0 || interval == 1) { + _this->egl_data->egl_swapinterval = interval; + } else { + return SDL_SetError("Only swap intervals of 0 or 1 are supported"); + } + + return 0; +} + +/*********************************/ +/* Atomic functions block */ +/*********************************/ + +#define VOID2U64(x) ((uint64_t)(unsigned long)(x)) + +static EGLSyncKHR create_fence(int fd, _THIS) +{ + EGLint attrib_list[] = { + EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd, + EGL_NONE, + }; + + EGLSyncKHR fence = _this->egl_data->eglCreateSyncKHR + (_this->egl_data->egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list); + + assert(fence); + return fence; +} + +/***********************************************************************************/ +/* Comments about buffer access protection mechanism (=fences) are the ones boxed. */ +/* Also, DON'T remove the asserts: if a fence-related call fails, it's better that */ +/* program exits immediately, or we could leave KMS waiting for a failed/missing */ +/* fence forevever. */ +/***********************************************************************************/ +int +ATOMIC_GLES_SwapWindowFenced(_THIS, SDL_Window * window) +{ + SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; + ATOMIC_FBInfo *fb; + ATOMIC_PlaneInfo info = {0}; + SDL_bool modesetting = SDL_FALSE; + + /******************************************************************/ + /* Create the GPU-side FENCE OBJECT. It will be inserted into the */ + /* GL CMDSTREAM exactly at the end of the gl commands that form a */ + /* frame.(KMS will have to wait on it before doing a pageflip.) */ + /******************************************************************/ + dispdata->gpu_fence = create_fence(EGL_NO_NATIVE_FENCE_FD_ANDROID, _this); + assert(dispdata->gpu_fence); + + /******************************************************************/ + /* eglSwapBuffers flushes the fence down the GL CMDSTREAM, so we */ + /* know for sure it's there now. */ + /* Also it marks, at EGL level, the buffer that we want to become */ + /* the new front buffer. (Remember that won't really happen until */ + /* we request a pageflip at the KMS level and it completes. */ + /******************************************************************/ + if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) { + return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers"); + } + + /******************************************************************/ + /* EXPORT the GPU-side FENCE OBJECT to the fence INPUT FD, so we */ + /* can pass it into the kernel. Atomic ioctl will pass the */ + /* in-fence fd into the kernel, thus telling KMS that it has to */ + /* wait for GPU to finish rendering the frame (remember where we */ + /* put the fence in the GL CMDSTREAM) before doing the changes */ + /* requested in the atomic ioct (the pageflip in this case). */ + /* (We export the GPU-side FENCE OJECT to the fence INPUT FD now, */ + /* not sooner, because now we are sure that the GPU-side fence is */ + /* in the CMDSTREAM to be lifted when the CMDSTREAM to this point */ + /* is completed). */ + /******************************************************************/ + dispdata->kms_in_fence_fd = _this->egl_data->eglDupNativeFenceFDANDROID (_this->egl_data->egl_display, + dispdata->gpu_fence); + + _this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, dispdata->gpu_fence); + assert(dispdata->kms_in_fence_fd != -1); + + /* Lock the buffer that is marked by eglSwapBuffers() to become the + next front buffer (so it can not be chosen by EGL as back buffer + to draw on), and get a handle to it to request the pageflip on it. + REMEMBER that gbm_surface_lock_front_buffer() ALWAYS has to be + called after eglSwapBuffers(). */ + windata->next_bo = ATOMIC_gbm_surface_lock_front_buffer(windata->gs); + if (!windata->next_bo) { + return SDL_SetError("Failed to lock frontbuffer"); + } + fb = ATOMIC_FBFromBO(_this, windata->next_bo); + if (!fb) { + return SDL_SetError("Failed to get a new framebuffer from BO"); + } + + /* Add the pageflip to the request list. */ + info.plane = dispdata->display_plane; + info.crtc_id = dispdata->crtc->crtc->crtc_id; + info.fb_id = fb->fb_id; + + info.src_w = windata->src_w; + info.src_h = windata->src_h; + info.crtc_w = windata->output_w; + info.crtc_h = windata->output_h; + info.crtc_x = windata->output_x; + + drm_atomic_set_plane_props(&info); + + /*****************************************************************/ + /* Tell the display (KMS) that it will have to wait on the fence */ + /* for the GPU-side FENCE. */ + /* */ + /* Since KMS is a kernel thing, we have to pass an FD into */ + /* the kernel, and get another FD out of the kernel. */ + /* */ + /* 1) To pass the GPU-side fence into the kernel, we set the */ + /* INPUT FD as the IN_FENCE_FD prop of the PRIMARY PLANE. */ + /* This FD tells KMS (the kernel) to wait for the GPU-side fence.*/ + /* */ + /* 2) To get the KMS-side fence out of the kernel, we set the */ + /* OUTPUT FD as the OUT_FEWNCE_FD prop of the CRTC. */ + /* This FD will be later imported as a FENCE OBJECT which will be*/ + /* used to tell the GPU to wait for KMS to complete the changes */ + /* requested in atomic_commit (the pageflip in this case). */ + /*****************************************************************/ + if (dispdata->kms_in_fence_fd != -1) + { + add_plane_property(dispdata->atomic_req, dispdata->display_plane, + "IN_FENCE_FD", dispdata->kms_in_fence_fd); + add_crtc_property(dispdata->atomic_req, dispdata->crtc, + "OUT_FENCE_PTR", VOID2U64(&dispdata->kms_out_fence_fd)); + } + + /* Do we have a pending modesetting? If so, set the necessary + props so it's included in the incoming atomic commit. */ + if (dispdata->modeset_pending) { + uint32_t blob_id; + SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; + + add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id); + ATOMIC_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id); + add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id); + add_crtc_property(dispdata->atomic_req, dispdata->crtc, "active", 1); + modesetting = SDL_TRUE; + dispdata->modeset_pending = SDL_FALSE; + } + + /*****************************************************************/ + /* Issue a non-blocking atomic commit: for triple buffering, */ + /* this must not block so the game can start building another */ + /* frame, even if the just-requested pageflip hasnt't completed. */ + /*****************************************************************/ + if (drm_atomic_commit(_this, SDL_FALSE, modesetting)) { + return SDL_SetError("Failed to issue atomic commit on pageflip"); + } + + /* Release the previous front buffer so EGL can chose it as back buffer + and render on it again. */ + if (windata->bo) { + ATOMIC_gbm_surface_release_buffer(windata->gs, windata->bo); + } + + /* Take note of the buffer about to become front buffer, so next + time we come here we can free it like we just did with the previous + front buffer. */ + windata->bo = windata->next_bo; + + /****************************************************************/ + /* Import the KMS-side FENCE OUTPUT FD from the kernel to the */ + /* KMS-side FENCE OBJECT so we can use use it to fence the GPU. */ + /****************************************************************/ + dispdata->kms_fence = create_fence(dispdata->kms_out_fence_fd, _this); + assert(dispdata->kms_fence); + + /****************************************************************/ + /* "Delete" the fence OUTPUT FD, because we already have the */ + /* KMS FENCE OBJECT, the fence itself is away from us, on the */ + /* kernel side. */ + /****************************************************************/ + dispdata->kms_out_fence_fd = -1; + + /*****************************************************************/ + /* Tell the GPU to wait on the fence for the KMS-side FENCE, */ + /* which means waiting until the requested pageflip is completed.*/ + /*****************************************************************/ + _this->egl_data->eglWaitSyncKHR(_this->egl_data->egl_display, dispdata->kms_fence, 0); + + return 0; +} + +int +ATOMIC_GLES_SwapWindowDoubleBuffered(_THIS, SDL_Window * window) +{ + SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; + ATOMIC_FBInfo *fb; + ATOMIC_PlaneInfo info = {0}; + SDL_bool modesetting = SDL_FALSE; + + /**********************************************************************************/ + /* In double-buffer mode, atomic_commit will always be synchronous/blocking (ie: */ + /* won't return until the requested changes are really done). */ + /* Also, there's no need to fence KMS or the GPU, because we won't be entering */ + /* game loop again (hence not building or executing a new cmdstring) until */ + /* pageflip is done, so we don't need to protect the KMS/GPU access to the buffer.*/ + /**********************************************************************************/ + + /* Mark, at EGL level, the buffer that we want to become the new front buffer. + It won't really happen until we request a pageflip at the KMS level and it + completes. */ + if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) { + return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers"); + } + + /* Lock the buffer that is marked by eglSwapBuffers() to become the next front buffer + (so it can not be chosen by EGL as back buffer to draw on), and get a handle to it, + to request the pageflip on it. */ + windata->next_bo = ATOMIC_gbm_surface_lock_front_buffer(windata->gs); + if (!windata->next_bo) { + return SDL_SetError("Failed to lock frontbuffer"); + } + fb = ATOMIC_FBFromBO(_this, windata->next_bo); + if (!fb) { + return SDL_SetError("Failed to get a new framebuffer BO"); + } + + /* Add the pageflip to the request list. */ + info.plane = dispdata->display_plane; + info.crtc_id = dispdata->crtc->crtc->crtc_id; + info.fb_id = fb->fb_id; + + info.src_w = windata->src_w; + info.src_h = windata->src_h; + info.crtc_w = windata->output_w; + info.crtc_h = windata->output_h; + info.crtc_x = windata->output_x; + + drm_atomic_set_plane_props(&info); + + /* Do we have a pending modesetting? If so, set the necessary + props so it's included in the incoming atomic commit. */ + if (dispdata->modeset_pending) { + uint32_t blob_id; + + SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; + + add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id); + ATOMIC_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id); + add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id); + add_crtc_property(dispdata->atomic_req, dispdata->crtc, "active", 1); + modesetting = SDL_TRUE; + dispdata->modeset_pending = SDL_FALSE; + } + + /* Issue the one and only atomic commit where all changes will be requested!. + Blocking for double buffering: won't return until completed. */ + if (drm_atomic_commit(_this, SDL_TRUE, modesetting)) { + return SDL_SetError("Failed to issue atomic commit on pageflip"); + } + + /* Release last front buffer so EGL can chose it as back buffer and render on it again. */ + if (windata->bo) { + ATOMIC_gbm_surface_release_buffer(windata->gs, windata->bo); + } + + /* Take note of current front buffer, so we can free it next time we come here. */ + windata->bo = windata->next_bo; + + return 0; +} + +int +ATOMIC_GLES_SwapWindow(_THIS, SDL_Window * window) +{ + SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); + + if (windata->swap_window == NULL) { + /* We want the fenced version by default, but it needs extensions. */ + if ( (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE)) || + (!SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_ANDROID_native_fence_sync")) ) + { + windata->swap_window = ATOMIC_GLES_SwapWindowDoubleBuffered; + } else { + windata->swap_window = ATOMIC_GLES_SwapWindowFenced; + } + } + + return windata->swap_window(_this, window); +} + +/***************************************/ +/* End of Atomic functions block */ +/***************************************/ + +SDL_EGL_MakeCurrent_impl(ATOMIC) + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicgles.h b/src/video/atomic/SDL_atomicgles.h new file mode 100644 index 0000000000000..d9026629ac38b --- /dev/null +++ b/src/video/atomic/SDL_atomicgles.h @@ -0,0 +1,49 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_atomicgles_h_ +#define SDL_atomicgles_h_ + +#if SDL_VIDEO_DRIVER_ATOMIC + +#include "../SDL_sysvideo.h" +#include "../SDL_egl_c.h" + +/* OpenGLES functions */ +#define ATOMIC_GLES_GetAttribute SDL_EGL_GetAttribute +#define ATOMIC_GLES_GetProcAddress SDL_EGL_GetProcAddress +#define ATOMIC_GLES_DeleteContext SDL_EGL_DeleteContext +#define ATOMIC_GLES_GetSwapInterval SDL_EGL_GetSwapInterval + +extern void ATOMIC_GLES_DefaultProfileConfig(_THIS, int *mask, int *major, int *minor); +extern int ATOMIC_GLES_SetSwapInterval(_THIS, int interval); +extern int ATOMIC_GLES_LoadLibrary(_THIS, const char *path); +extern SDL_GLContext ATOMIC_GLES_CreateContext(_THIS, SDL_Window * window); +extern int ATOMIC_GLES_SwapWindow(_THIS, SDL_Window * window); +extern int ATOMIC_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context); + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +#endif /* SDL_atomicgles_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicinterface.c b/src/video/atomic/SDL_atomicinterface.c new file mode 100644 index 0000000000000..31665482d9f1b --- /dev/null +++ b/src/video/atomic/SDL_atomicinterface.c @@ -0,0 +1,444 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +#include +#include "SDL_atomicdyn.h" +#include "SDL_atomicvideo.h" +#include "SDL_atomicgles.h" + +int add_connector_property(drmModeAtomicReq *req, struct connector *connector, + const char *name, uint64_t value) +{ + unsigned int i; + int prop_id = 0; + + for (i = 0 ; i < connector->props->count_props ; i++) { + if (strcmp(connector->props_info[i]->name, name) == 0) { + prop_id = connector->props_info[i]->prop_id; + break; + } + } + + if (prop_id < 0) { + SDL_SetError("no connector property: %s", name); + return -EINVAL; + } + + return ATOMIC_drmModeAtomicAddProperty(req, connector->connector->connector_id, prop_id, value); +} + +int add_crtc_property(drmModeAtomicReq *req, struct crtc *crtc, + const char *name, uint64_t value) +{ + unsigned int i; + int prop_id = -1; + + for (i = 0 ; i < crtc->props->count_props ; i++) { + if (strcmp(crtc->props_info[i]->name, name) == 0) { + prop_id = crtc->props_info[i]->prop_id; + break; + } + } + + if (prop_id < 0) { + SDL_SetError("no crtc property: %s", name); + return -EINVAL; + } + + return ATOMIC_drmModeAtomicAddProperty(req, crtc->crtc->crtc_id, prop_id, value); +} + +int add_plane_property(drmModeAtomicReq *req, struct plane *plane, + const char *name, uint64_t value) +{ + unsigned int i; + int prop_id = -1; + + for (i = 0 ; i < plane->props->count_props ; i++) { + if (strcmp(plane->props_info[i]->name, name) == 0) { + prop_id = plane->props_info[i]->prop_id; + break; + } + } + + if (prop_id < 0) { + SDL_SetError("no plane property: %s", name); + return -EINVAL; + } + + return ATOMIC_drmModeAtomicAddProperty(req, plane->plane->plane_id, prop_id, value); +} + + +/* Get the plane_id of a plane that is of the specified plane type (primary, + overlay, cursor...) and can use specified CRTC. */ +int get_plane_id(_THIS, unsigned int crtc_id, uint32_t plane_type) +{ + drmModeRes *resources = NULL; + drmModePlaneResPtr plane_resources = NULL; + uint32_t i, j; + unsigned int crtc_index = 0; + int ret = -EINVAL; + int found = 0; + + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + + resources = ATOMIC_drmModeGetResources(viddata->drm_fd); + + /* Get the crtc_index for the current CRTC. + It's needed to find out if a plane supports the CRTC. */ + for (i = 0; i < resources->count_crtcs; i++) { + if (resources->crtcs[i] == crtc_id) { + crtc_index = i; + break; + } + } + + plane_resources = ATOMIC_drmModeGetPlaneResources(viddata->drm_fd); + if (!plane_resources) { + return SDL_SetError("drmModeGetPlaneResources failed."); + } + + /* Iterate on all the available planes. */ + for (i = 0; (i < plane_resources->count_planes) && !found; i++) { + + uint32_t plane_id = plane_resources->planes[i]; + + drmModePlanePtr plane = ATOMIC_drmModeGetPlane(viddata->drm_fd, plane_id); + if (!plane) { + continue; + } + + /* See if the current CRTC is available for this plane. */ + if (plane->possible_crtcs & (1 << crtc_index)) { + + drmModeObjectPropertiesPtr props = ATOMIC_drmModeObjectGetProperties( + viddata->drm_fd, plane_id, DRM_MODE_OBJECT_PLANE); + ret = plane_id; + + /* Iterate on the plane props to find the type of the plane, + to see if it's of the type we want. */ + for (j = 0; j < props->count_props; j++) { + + drmModePropertyPtr p = ATOMIC_drmModeGetProperty(viddata->drm_fd, + props->props[j]); + + if ((strcmp(p->name, "type") == 0) && (props->prop_values[j] == plane_type)) { + /* found our plane, use that: */ + found = 1; + } + + ATOMIC_drmModeFreeProperty(p); + } + + ATOMIC_drmModeFreeObjectProperties(props); + } + + ATOMIC_drmModeFreePlane(plane); + } + + ATOMIC_drmModeFreePlaneResources(plane_resources); + ATOMIC_drmModeFreeResources(resources); + + return ret; +} + +/* Setup a plane and it's props. */ +int +setup_plane(_THIS, struct plane **plane, uint32_t plane_type) +{ + uint32_t plane_id; + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + int ret = 0; + + *plane = SDL_calloc(1, sizeof(**plane)); + if (!(*plane)) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + /* Get plane ID for a given CRTC and plane type. */ + plane_id = get_plane_id(_this, dispdata->crtc->crtc->crtc_id, plane_type); + + if (!plane_id) { + ret = SDL_SetError("Invalid Plane ID"); + goto cleanup; + } + + /* Get the DRM plane itself. */ + (*plane)->plane = ATOMIC_drmModeGetPlane(viddata->drm_fd, plane_id); + + /* Get the DRM plane properties. */ + if ((*plane)->plane) { + unsigned int i; + (*plane)->props = ATOMIC_drmModeObjectGetProperties(viddata->drm_fd, + (*plane)->plane->plane_id, DRM_MODE_OBJECT_PLANE); + (*plane)->props_info = SDL_calloc((*plane)->props->count_props, + sizeof(*(*plane)->props_info)); + + if ( !((*plane)->props_info) ) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + for (i = 0; i < (*plane)->props->count_props; i++) { + (*plane)->props_info[i] = ATOMIC_drmModeGetProperty(viddata->drm_fd, + (*plane)->props->props[i]); + } + } + +cleanup: + + if (ret) { + if (*plane) { + SDL_free(*plane); + *plane = NULL; + } + } + return ret; +} + +/* Free a plane and it's props. */ +void +free_plane(struct plane **plane) +{ + if (*plane) { + if ((*plane)->plane) { + ATOMIC_drmModeFreePlane((*plane)->plane); + (*plane)->plane = NULL; + } + if ((*plane)->props_info) { + SDL_free((*plane)->props_info); + (*plane)->props_info = NULL; + } + SDL_free(*plane); + *plane = NULL; + } +} + +/**********************************************************************************/ +/* The most important ATOMIC fn of the backend. */ +/* A PLANE reads a BUFFER, and a CRTC reads a PLANE and sends it's contents */ +/* over to a CONNECTOR->ENCODER system (several CONNECTORS can be connected */ +/* to the same PLANE). */ +/* Think of a plane as a "frame" sorrounding a picture, where the "picture" */ +/* is the buffer, and we move the "frame" from a picture to another, */ +/* and the one that has the "frame" is the one sent over to the screen */ +/* via the CONNECTOR->ENCODER system. */ +/* Think of a PLANE as being "in the middle", it's the CENTRAL part */ +/* bewteen the CRTC and the BUFFER that is shown on screen. */ +/* What we do here is connect a PLANE to a CRTC and a BUFFER. */ +/* -ALWAYS set the CRTC_ID and FB_ID attribs of a plane at the same time, */ +/* meaning IN THE SAME atomic request. */ +/* -And NEVER destroy a GBM surface whose buffers are being read by a plane: */ +/* first, move the plane away from those buffers and ONLY THEN destroy the */ +/* buffers and/or the GBM surface containig them. */ +/**********************************************************************************/ +void +drm_atomic_set_plane_props(struct ATOMIC_PlaneInfo *info) +{ + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + /* Do we have a set of changes already in the making? If not, allocate a new one. */ + if (!dispdata->atomic_req) + dispdata->atomic_req = ATOMIC_drmModeAtomicAlloc(); + + add_plane_property(dispdata->atomic_req, info->plane, "FB_ID", info->fb_id); + add_plane_property(dispdata->atomic_req, info->plane, "CRTC_ID", info->crtc_id); + add_plane_property(dispdata->atomic_req, info->plane, "SRC_W", info->src_w << 16); + add_plane_property(dispdata->atomic_req, info->plane, "SRC_H", info->src_h << 16); + add_plane_property(dispdata->atomic_req, info->plane, "SRC_X", info->src_x); + add_plane_property(dispdata->atomic_req, info->plane, "SRC_Y", info->src_y); + add_plane_property(dispdata->atomic_req, info->plane, "CRTC_W", info->crtc_w); + add_plane_property(dispdata->atomic_req, info->plane, "CRTC_H", info->crtc_h); + add_plane_property(dispdata->atomic_req, info->plane, "CRTC_X", info->crtc_x); + add_plane_property(dispdata->atomic_req, info->plane, "CRTC_Y", info->crtc_y); +} + +int drm_atomic_commit(_THIS, SDL_bool blocking, SDL_bool allow_modeset) +{ + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + uint32_t atomic_flags = 0; + int ret; + + if (!blocking) { + atomic_flags |= DRM_MODE_ATOMIC_NONBLOCK; + } + + if (allow_modeset) { + atomic_flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; + } + + /* Never issue a new atomic commit if previous has not yet completed, + or it will error. */ + drm_atomic_waitpending(_this); + + ret = ATOMIC_drmModeAtomicCommit(viddata->drm_fd, dispdata->atomic_req, + atomic_flags, NULL); + + if (ret) { + SDL_SetError("Atomic commit failed, returned %d.", ret); + /* Uncomment this for fast-debugging */ +#if 1 + printf("ATOMIC COMMIT FAILED: %s.\n", strerror(errno)); +#endif + goto out; + } + + if (dispdata->kms_in_fence_fd != -1) { + close(dispdata->kms_in_fence_fd); + dispdata->kms_in_fence_fd = -1; + } + +out: + ATOMIC_drmModeAtomicFree(dispdata->atomic_req); + dispdata->atomic_req = NULL; + + return ret; +} + +void +drm_atomic_waitpending(_THIS) +{ + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + /* Will return immediately if we have already destroyed the fence, because we NULL-ify it just after. + Also, will return immediately in double-buffer mode, because kms_fence will alsawys be NULL. */ + if (dispdata->kms_fence) { + EGLint status; + + do { + status = _this->egl_data->eglClientWaitSyncKHR(_this->egl_data->egl_display, + dispdata->kms_fence, 0, EGL_FOREVER_KHR); + } while (status != EGL_CONDITION_SATISFIED_KHR); + + _this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, dispdata->kms_fence); + dispdata->kms_fence = NULL; + } +} + +#if 0 + +void print_plane_info(_THIS, drmModePlanePtr plane) +{ + char *plane_type; + drmModeRes *resources; + uint32_t type = 0; + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + int i; + + drmModeObjectPropertiesPtr props = ATOMIC_drmModeObjectGetProperties(viddata->drm_fd, + plane->plane_id, DRM_MODE_OBJECT_PLANE); + + /* Search the plane props for the plane type. */ + for (i = 0; i < props->count_props; i++) { + drmModePropertyPtr p = ATOMIC_drmModeGetProperty(viddata->drm_fd, props->props[i]); + if ((strcmp(p->name, "type") == 0)) { + type = props->prop_values[i]; + } + + ATOMIC_drmModeFreeProperty(p); + } + + switch (type) { + case DRM_PLANE_TYPE_OVERLAY: + plane_type = "overlay"; + break; + + case DRM_PLANE_TYPE_PRIMARY: + plane_type = "primary"; + break; + + case DRM_PLANE_TYPE_CURSOR: + plane_type = "cursor"; + break; + } + + + /* Remember that to present a plane on screen, it has to be + connected to a CRTC so the CRTC scans it, + scales it, etc... and presents it on screen. */ + + /* Now we look for the CRTCs supported by the plane. */ + resources = ATOMIC_drmModeGetResources(viddata->drm_fd); + if (!resources) + return; + + printf("--PLANE ID: %d\nPLANE TYPE: %s\nCRTC READING THIS PLANE: %d\nCRTCS SUPPORTED BY THIS PLANE: ", plane->plane_id, plane_type, plane->crtc_id); + for (i = 0; i < resources->count_crtcs; i++) { + if (plane->possible_crtcs & (1 << i)) { + uint32_t crtc_id = resources->crtcs[i]; + printf ("%d", crtc_id); + break; + } + } + + printf ("\n\n"); +} + +void get_planes_info(_THIS) +{ + drmModePlaneResPtr plane_resources; + uint32_t i; + + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + plane_resources = ATOMIC_drmModeGetPlaneResources(viddata->drm_fd); + if (!plane_resources) { + printf("drmModeGetPlaneResources failed: %s\n", strerror(errno)); + return; + } + + printf("--Number of planes found: %d-- \n", plane_resources->count_planes); + printf("--Usable CRTC that we have chosen: %d-- \n", dispdata->crtc->crtc->crtc_id); + + /* Iterate on all the available planes. */ + for (i = 0; (i < plane_resources->count_planes); i++) { + + uint32_t plane_id = plane_resources->planes[i]; + + drmModePlanePtr plane = ATOMIC_drmModeGetPlane(viddata->drm_fd, plane_id); + if (!plane) { + printf("drmModeGetPlane(%u) failed: %s\n", plane_id, strerror(errno)); + continue; + } + + /* Print plane info. */ + print_plane_info(_this, plane); + ATOMIC_drmModeFreePlane(plane); + } + + ATOMIC_drmModeFreePlaneResources(plane_resources); +} + +#endif + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicinterface.h b/src/video/atomic/SDL_atomicinterface.h new file mode 100644 index 0000000000000..610f2ecfd76f5 --- /dev/null +++ b/src/video/atomic/SDL_atomicinterface.h @@ -0,0 +1,54 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_atomicinterface_h_ +#define SDL_atomicinterface_h_ + +#if SDL_VIDEO_DRIVER_ATOMIC + +/* ATOMIC interface helper functions */ +extern int add_connector_property(drmModeAtomicReq *req, struct connector *connector, + const char *name, uint64_t value); + +extern int add_crtc_property(drmModeAtomicReq *req, struct crtc *crtc, + const char *name, uint64_t value); + +extern int add_plane_property(drmModeAtomicReq *req, struct plane *plane, + const char *name, uint64_t value); + +extern void print_plane_info(_THIS, drmModePlanePtr plane); + +extern int get_plane_id(_THIS, unsigned int crtc_id, uint32_t plane_type); +extern int setup_plane(_THIS, struct plane **plane, uint32_t plane_type); +extern void free_plane(struct plane **plane); +extern void drm_atomic_set_plane_props(struct ATOMIC_PlaneInfo *info); + +extern int drm_atomic_commit(_THIS, SDL_bool blocking, SDL_bool allow_modeset); + +extern void drm_atomic_waitpending(_THIS); + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +#endif /* SDL_atomicinterface_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicmouse.c b/src/video/atomic/SDL_atomicmouse.c new file mode 100644 index 0000000000000..61205cb3d393b --- /dev/null +++ b/src/video/atomic/SDL_atomicmouse.c @@ -0,0 +1,535 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +#include "SDL_atomicvideo.h" +#include "SDL_atomicmouse.h" +#include "SDL_atomicdyn.h" +#include "SDL_assert.h" + +#include "../../events/SDL_mouse_c.h" +#include "../../events/default_cursor.h" + +static SDL_Cursor *ATOMIC_CreateDefaultCursor(void); +static SDL_Cursor *ATOMIC_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y); +static int ATOMIC_ShowCursor(SDL_Cursor * cursor); +static void ATOMIC_MoveCursor(SDL_Cursor * cursor); +static void ATOMIC_FreeCursor(SDL_Cursor * cursor); +static void ATOMIC_WarpMouse(SDL_Window * window, int x, int y); +static int ATOMIC_WarpMouseGlobal(int x, int y); + +/**************************************************************************************/ +/* BEFORE CODING ANYTHING MOUSE/CURSOR RELATED, REMEMBER THIS. */ +/* How does SDL manage cursors internally? First, mouse =! cursor. The mouse can have */ +/* many cursors in mouse->cursors. */ +/* -SDL tells us to create a cursor with ATOMIC_CreateCursor(). It can create many */ +/* cursosr with this, not only one. */ +/* -SDL stores those cursors in a cursors array, in mouse->cursors. */ +/* -Whenever it wants (or the programmer wants) takes a cursor from that array */ +/* and shows it on screen with ATOMIC_ShowCursor(). */ +/* ATOMIC_ShowCursor() simply shows or hides the cursor it receives: it does NOT */ +/* mind if it's mouse->cur_cursor, etc. */ +/* -If ATOMIC_ShowCursor() returns succesfully, that cursor becomes mouse->cur_cursor */ +/* and mouse->cursor_shown is 1. */ +/**************************************************************************************/ + +/**********************************/ +/* Atomic helper functions block. */ +/**********************************/ + +int +drm_atomic_movecursor(ATOMIC_CursorData *curdata, uint16_t x, uint16_t y) +{ + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + if (!dispdata->cursor_plane) /* We can't move a non-existing cursor, but that's ok. */ + return 0; + + /* Do we have a set of changes already in the making? If not, allocate a new one. */ + if (!dispdata->atomic_req) + dispdata->atomic_req = ATOMIC_drmModeAtomicAlloc(); + + add_plane_property(dispdata->atomic_req, + dispdata->cursor_plane, "CRTC_X", x - curdata->hot_x); + add_plane_property(dispdata->atomic_req, + dispdata->cursor_plane, "CRTC_Y", y - curdata->hot_y); + + return 0; +} + +/***************************************/ +/* Atomic helper functions block ends. */ +/***************************************/ + +/* Converts a pixel from straight-alpha [AA, RR, GG, BB], which the SDL cursor surface has, + to premultiplied-alpha [AA. AA*RR, AA*GG, AA*BB]. + These multiplications have to be done with floats instead of uint32_t's, + and the resulting values have to be converted to be relative to the 0-255 interval, + where 255 is 1.00 and anything between 0 and 255 is 0.xx. */ +void alpha_premultiply_ARGB8888 (uint32_t *pixel) { + + uint32_t A, R, G, B; + + /* Component bytes extraction. */ + A = (*pixel >> (3 << 3)) & 0xFF; + R = (*pixel >> (2 << 3)) & 0xFF; + G = (*pixel >> (1 << 3)) & 0xFF; + B = (*pixel >> (0 << 3)) & 0xFF; + + /* Alpha pre-multiplication of each component. */ + R = (float)A * ((float)R /255); + G = (float)A * ((float)G /255); + B = (float)A * ((float)B /255); + + /* ARGB8888 pixel recomposition. */ + (*pixel) = (((uint32_t)A << 24) | ((uint32_t)R << 16) | ((uint32_t)G << 8)) | ((uint32_t)B << 0); +} + +static SDL_Cursor * +ATOMIC_CreateDefaultCursor(void) +{ + return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY); +} + +/* This simply gets the cursor soft-buffer ready. We don't copy it to a GBO BO + until ShowCursor() because the cusor GBM BO (living in dispata) is destroyed + and recreated when we recreate windows, etc. */ +static SDL_Cursor * +ATOMIC_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y) +{ + ATOMIC_CursorData *curdata; + SDL_Cursor *cursor, *ret; + + curdata = NULL; + ret = NULL; + + /* All code below assumes ARGB8888 format for the cursor surface, + like other backends do. Also, the GBM BO pixels have to be + alpha-premultiplied, but the SDL surface we receive has + straight-alpha pixels, so we always have to convert. */ + SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888); + SDL_assert(surface->pitch == surface->w * 4); + + cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor)); + if (!cursor) { + SDL_OutOfMemory(); + goto cleanup; + } + curdata = (ATOMIC_CursorData *) SDL_calloc(1, sizeof(*curdata)); + if (!curdata) { + SDL_OutOfMemory(); + goto cleanup; + } + + /* hox_x and hot_y are the coordinates of the "tip of the cursor" from it's base. */ + curdata->hot_x = hot_x; + curdata->hot_y = hot_y; + curdata->w = surface->w; + curdata->h = surface->h; + curdata->buffer = NULL; + + /* Configure the cursor buffer info. + This buffer has the original size of the cursor surface we are given. */ + curdata->buffer_pitch = surface->pitch; + curdata->buffer_size = surface->pitch * surface->h; + curdata->buffer = (uint32_t*)SDL_malloc(curdata->buffer_size); + + if (!curdata->buffer) { + SDL_OutOfMemory(); + goto cleanup; + } + + if (SDL_MUSTLOCK(surface)) { + if (SDL_LockSurface(surface) < 0) { + /* Could not lock surface */ + goto cleanup; + } + } + + /* Copy the surface pixels to the cursor buffer, for future use in ShowCursor() */ + SDL_memcpy(curdata->buffer, surface->pixels, curdata->buffer_size); + + if (SDL_MUSTLOCK(surface)) { + SDL_UnlockSurface(surface); + } + + cursor->driverdata = curdata; + + ret = cursor; + +cleanup: + if (ret == NULL) { + if (curdata) { + if (curdata->buffer) { + SDL_free(curdata->buffer); + } + SDL_free(curdata); + } + if (cursor) { + SDL_free(cursor); + } + } + + return ret; +} + +/* When we create a window, we have to test if we have to show the cursor, + and explicily do so if necessary. + This is because when we destroy a window, we take the cursor away from the + cursor plane, and destroy the cusror GBM BO. So we have to re-show it, + so to say. */ +void +ATOMIC_InitCursor() +{ + SDL_Mouse *mouse = NULL; + mouse = SDL_GetMouse(); + + if (!mouse) { + return; + } + if (!(mouse->cur_cursor)) { + return; + } + + if (!(mouse->cursor_shown)) { + return; + } + + ATOMIC_ShowCursor(mouse->cur_cursor); +} + +/* Show the specified cursor, or hide if cursor is NULL or has no focus. */ +static int +ATOMIC_ShowCursor(SDL_Cursor * cursor) +{ + SDL_VideoDevice *video_device = SDL_GetVideoDevice(); + //SDL_VideoData *viddata = ((SDL_VideoData *)video_device->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + SDL_Mouse *mouse; + ATOMIC_CursorData *curdata; + + ATOMIC_FBInfo *fb; + ATOMIC_PlaneInfo info = {0}; + + size_t bo_stride; + size_t bufsize; + uint32_t *ready_buffer = NULL; + uint32_t pixel; + + int i,j; + int ret = 0; + + mouse = SDL_GetMouse(); + if (!mouse) { + return SDL_SetError("No mouse."); + } + + /*********************************************************/ + /* Hide cursor if it's NULL or it has no focus(=winwow). */ + /*********************************************************/ + if (!cursor || !mouse->focus) { + if (dispdata->cursor_plane) { + /* Hide the drm cursor with no more considerations because + SDL_VideoQuit() takes us here after disabling the mouse + so there is no mouse->cur_cursor by now. */ + info.plane = dispdata->cursor_plane; + /* The rest of the members are zeroed, so this takes away the cursor + from the cursor plane. */ + drm_atomic_set_plane_props(&info); + if (drm_atomic_commit(video_device, SDL_TRUE, SDL_FALSE)) { + ret = SDL_SetError("Failed atomic commit in ATOMIC_ShowCursor."); + } + } + return ret; + } + + /************************************************/ + /* If cursor != NULL, DO show cursor on display */ + /************************************************/ + if (!dispdata->cursor_plane) { + return SDL_SetError("Hardware cursor plane not initialized."); + } + + curdata = (ATOMIC_CursorData *) cursor->driverdata; + + if (!curdata || !dispdata->cursor_bo) { + return SDL_SetError("Cursor not initialized properly."); + } + + /* Prepare a buffer we can dump to our GBM BO (different + size, alpha premultiplication...) */ + bo_stride = ATOMIC_gbm_bo_get_stride(dispdata->cursor_bo); + bufsize = bo_stride * curdata->h; + + ready_buffer = (uint32_t*)SDL_malloc(bufsize); + if (!ready_buffer) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + /* Clean the whole buffer we are preparing. */ + SDL_memset(ready_buffer, 0x00, bo_stride * curdata->h); + + /* Copy from the cursor buffer to a buffer that we can dump to the GBM BO, + pre-multiplying by alpha each pixel as we go. */ + for (i = 0; i < curdata->h; i++) { + for (j = 0; j < curdata->w; j++) { + pixel = ((uint32_t*)curdata->buffer)[i * curdata->w + j]; + alpha_premultiply_ARGB8888 (&pixel); + SDL_memcpy(ready_buffer + (i * dispdata->cursor_w) + j, &pixel, 4); + } + } + + /* Dump the cursor buffer to our GBM BO. */ + if (ATOMIC_gbm_bo_write(dispdata->cursor_bo, ready_buffer, bufsize)) { + ret = SDL_SetError("Could not write to GBM cursor BO"); + goto cleanup; + } + + /* Get the fb_id for the GBM BO so we can show it on the cursor plane. */ + fb = ATOMIC_FBFromBO(video_device, dispdata->cursor_bo); + + /* Show the GBM BO buffer on the cursor plane. */ + info.plane = dispdata->cursor_plane; + info.crtc_id = dispdata->crtc->crtc->crtc_id; + info.fb_id = fb->fb_id; + info.src_w = dispdata->cursor_w; + info.src_h = dispdata->cursor_h; + info.crtc_x = mouse->x - curdata->hot_x; + info.crtc_y = mouse->y - curdata->hot_y; + info.crtc_w = curdata->w; + info.crtc_h = curdata->h; + + drm_atomic_set_plane_props(&info); + + if (drm_atomic_commit(video_device, SDL_TRUE, SDL_FALSE)) { + ret = SDL_SetError("Failed atomic commit in ATOMIC_ShowCursor."); + goto cleanup; + } + + ret = 0; + +cleanup: + + if (ready_buffer) { + SDL_free(ready_buffer); + } + return ret; +} + +/* This is only for freeing the SDL_cursor.*/ +static void +ATOMIC_FreeCursor(SDL_Cursor * cursor) +{ + ATOMIC_CursorData *curdata; + + /* Even if the cursor is not ours, free it. */ + if (cursor) { + curdata = (ATOMIC_CursorData *) cursor->driverdata; + /* Free cursor buffer */ + if (curdata->buffer) { + SDL_free(curdata->buffer); + curdata->buffer = NULL; + } + /* Free cursor itself */ + if (cursor->driverdata) { + SDL_free(cursor->driverdata); + } + SDL_free(cursor); + } +} + +/* Warp the mouse to (x,y) */ +static void +ATOMIC_WarpMouse(SDL_Window * window, int x, int y) +{ + /* Only one global/fullscreen window is supported */ + ATOMIC_WarpMouseGlobal(x, y); +} + +/* Warp the mouse to (x,y) */ +static int +ATOMIC_WarpMouseGlobal(int x, int y) +{ + ATOMIC_CursorData *curdata; + SDL_Mouse *mouse = SDL_GetMouse(); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + if (mouse && mouse->cur_cursor && mouse->cur_cursor->driverdata) { + /* Update internal mouse position. */ + SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y); + + /* And now update the cursor graphic position on screen. */ + curdata = (ATOMIC_CursorData *) mouse->cur_cursor->driverdata; + if (dispdata->cursor_bo) { + if (drm_atomic_movecursor(curdata, x, y)) { + return SDL_SetError("drm_atomic_movecursor() failed."); + } + } else { + return SDL_SetError("Cursor not initialized properly."); + } + } else { + return SDL_SetError("No mouse or current cursor."); + } + + return 0; +} + +/* UNDO WHAT WE DID IN ATOMIC_InitMouse(). */ +void +ATOMIC_DeinitMouse(_THIS) +{ + SDL_VideoDevice *video_device = SDL_GetVideoDevice(); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + ATOMIC_PlaneInfo info = {0}; + + /* 1- Destroy the curso GBM BO. */ + if (video_device && dispdata->cursor_bo) { + /* Unsethe the cursor BO from the cursor plane. + (The other members of the plane info are zeroed). */ + info.plane = dispdata->cursor_plane; + drm_atomic_set_plane_props(&info); + /* Wait until the cursor is unset from the cursor plane + before destroying it's BO. */ + if (drm_atomic_commit(video_device, SDL_TRUE, SDL_FALSE)) { + SDL_SetError("Failed atomic commit in ATOMIC_DenitMouse."); + } + /* ..and finally destroy the cursor DRM BO! */ + ATOMIC_gbm_bo_destroy(dispdata->cursor_bo); + dispdata->cursor_bo = NULL; + } + + /* 2- Free the cursor plane, on which the cursor was being shown. */ + if (dispdata->cursor_plane) { + free_plane(&dispdata->cursor_plane); + } +} + +void +ATOMIC_InitMouse(_THIS) +{ + SDL_VideoDevice *dev = SDL_GetVideoDevice(); + SDL_VideoData *viddata = ((SDL_VideoData *)dev->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + SDL_Mouse *mouse = SDL_GetMouse(); + + mouse->CreateCursor = ATOMIC_CreateCursor; + mouse->ShowCursor = ATOMIC_ShowCursor; + mouse->MoveCursor = ATOMIC_MoveCursor; + mouse->FreeCursor = ATOMIC_FreeCursor; + mouse->WarpMouse = ATOMIC_WarpMouse; + mouse->WarpMouseGlobal = ATOMIC_WarpMouseGlobal; + + /***************************************************************************/ + /* REMEMBER TO BE SURE OF UNDOING ALL THESE STEPS PROPERLY BEFORE CALLING */ + /* gbm_device_destroy, OR YOU WON'T BE ABLE TO CREATE A NEW ONE (ERROR -13 */ + /* on gbm_create_device). */ + /***************************************************************************/ + + /* 1- Init cursor plane, if we haven't yet. */ + if (!dispdata->cursor_plane) { + setup_plane(_this, &(dispdata->cursor_plane), DRM_PLANE_TYPE_CURSOR); + } + + /* 2- Create the cursor GBM BO, if we haven't yet. */ + if (!dispdata->cursor_bo) { + + if (!ATOMIC_gbm_device_is_format_supported(viddata->gbm_dev, GBM_FORMAT_ARGB8888, + GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE)) + { + SDL_SetError("Unsupported pixel format for cursor"); + return; + } + + if (ATOMIC_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_WIDTH, &dispdata->cursor_w) || + ATOMIC_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_HEIGHT, &dispdata->cursor_h)) + { + SDL_SetError("Could not get the recommended GBM cursor size"); + goto cleanup; + } + + if (dispdata->cursor_w == 0 || dispdata->cursor_h == 0) { + SDL_SetError("Could not get an usable GBM cursor size"); + goto cleanup; + } + + dispdata->cursor_bo = ATOMIC_gbm_bo_create(viddata->gbm_dev, + dispdata->cursor_w, dispdata->cursor_h, + GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); + + if (!dispdata->cursor_bo) { + SDL_SetError("Could not create GBM cursor BO"); + goto cleanup; + } + } + + /* SDL expects to set the default cursor on screen when we init the mouse, + but since we have moved the ATOMIC_InitMouse() call to ATOMIC_CreateWindow(), + we end up calling ATOMIC_InitMouse() every time we create a window, so we + have to prevent this from being done every time a new window is created. + If we don't, new default cursors would stack up on mouse->cursors and SDL + would have to hide and delete them at quit, not to mention the memory leak... */ + if(dispdata->set_default_cursor_pending) { + SDL_SetDefaultCursor(ATOMIC_CreateDefaultCursor()); + dispdata->set_default_cursor_pending = SDL_FALSE; + } + + return; + +cleanup: + if (dispdata->cursor_bo) { + ATOMIC_gbm_bo_destroy(dispdata->cursor_bo); + dispdata->cursor_bo = NULL; + } +} + +/* This is called when a mouse motion event occurs */ +static void +ATOMIC_MoveCursor(SDL_Cursor * cursor) +{ + SDL_Mouse *mouse = SDL_GetMouse(); + ATOMIC_CursorData *curdata; + + /* We must NOT call SDL_SendMouseMotion() here or we will enter recursivity! + That's why we move the cursor graphic ONLY. */ + if (mouse && mouse->cur_cursor && mouse->cur_cursor->driverdata) { + curdata = (ATOMIC_CursorData *) mouse->cur_cursor->driverdata; + + /* Some programs expect cursor movement even while they don't do SwapWindow() calls, + and since we ride on the atomic_commit() in SwapWindow() for cursor movement, + cursor won't move in these situations. We could do an atomic_commit() here + for each cursor movement request, but it cripples the movement to 30FPS, + so a future solution is needed. SDLPoP "QUIT?" menu is an example of this + situation. */ + + if (drm_atomic_movecursor(curdata, mouse->x, mouse->y)) { + SDL_SetError("drm_atomic_movecursor() failed."); + } + } +} + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicmouse.h b/src/video/atomic/SDL_atomicmouse.h new file mode 100644 index 0000000000000..ad8aff222f66a --- /dev/null +++ b/src/video/atomic/SDL_atomicmouse.h @@ -0,0 +1,54 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_ATOMIC_mouse_h_ +#define SDL_ATOMIC_mouse_h_ + +#include + +#define MAX_CURSOR_W 512 +#define MAX_CURSOR_H 512 + +/* Driverdata with driver-side info about the cursor. */ +typedef struct _ATOMIC_CursorData +{ + uint16_t hot_x, hot_y; + uint16_t w, h; + + /* The buffer where we store the mouse bitmap ready to be used. + We get it ready and filled in CreateCursor(), and copy it to a GBM BO in ShowCursor().*/ + uint32_t *buffer; + size_t buffer_size; + size_t buffer_pitch; + +} ATOMIC_CursorData; + +extern void ATOMIC_InitMouse(_THIS); +extern void ATOMIC_DeinitMouse(_THIS); + +extern void ATOMIC_InitCursor(); + +#endif /* SDL_ATOMIC_mouse_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicsym.h b/src/video/atomic/SDL_atomicsym.h new file mode 100644 index 0000000000000..8becd752fabfa --- /dev/null +++ b/src/video/atomic/SDL_atomicsym.h @@ -0,0 +1,128 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* *INDENT-OFF* */ + +#ifndef SDL_ATOMIC_MODULE +#define SDL_ATOMIC_MODULE(modname) +#endif + +#ifndef SDL_ATOMIC_SYM +#define SDL_ATOMIC_SYM(rc,fn,params) +#endif + +#ifndef SDL_ATOMIC_SYM_CONST +#define SDL_ATOMIC_SYM_CONST(type, name) +#endif + + +SDL_ATOMIC_MODULE(LIBDRM) +SDL_ATOMIC_SYM(void,drmModeFreeResources,(drmModeResPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreeFB,(drmModeFBPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreeCrtc,(drmModeCrtcPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreeConnector,(drmModeConnectorPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreeEncoder,(drmModeEncoderPtr ptr)) +SDL_ATOMIC_SYM(int,drmGetCap,(int fd, uint64_t capability, uint64_t *value)) +SDL_ATOMIC_SYM(int,drmIoctl,(int fd, unsigned long request, void *arg)) +SDL_ATOMIC_SYM(drmModeResPtr,drmModeGetResources,(int fd)) + +SDL_ATOMIC_SYM(int,drmModeAddFB,(int fd, uint32_t width, uint32_t height, uint8_t depth, + uint8_t bpp, uint32_t pitch, uint32_t bo_handle, + uint32_t *buf_id)) + +SDL_ATOMIC_SYM(int,drmModeAddFB2,(int fd, uint32_t width, uint32_t height, + uint32_t pixel_format, const uint32_t bo_handles[4], + const uint32_t pitches[4], const uint32_t offsets[4], + uint32_t *buf_id, uint32_t flags)) + +SDL_ATOMIC_SYM(int,drmModeAddFB2WithModifiers,(int fd, uint32_t width, uint32_t height, + uint32_t pixel_format, const uint32_t bo_handles[4], + const uint32_t pitches[4], const uint32_t offsets[4], + const uint64_t modifier[4], uint32_t *buf_id, + uint32_t flags)) + +SDL_ATOMIC_SYM(int,drmModeRmFB,(int fd, uint32_t bufferId)) +SDL_ATOMIC_SYM(drmModeFBPtr,drmModeGetFB,(int fd, uint32_t buf)) +SDL_ATOMIC_SYM(drmModeCrtcPtr,drmModeGetCrtc,(int fd, uint32_t crtcId)) +SDL_ATOMIC_SYM(drmModeEncoderPtr,drmModeGetEncoder,(int fd, uint32_t encoder_id)) +SDL_ATOMIC_SYM(drmModeConnectorPtr,drmModeGetConnector,(int fd, uint32_t connector_id)) + +/* Atomic functions */ + +SDL_ATOMIC_SYM(int,drmSetClientCap,(int fd, uint64_t capability, uint64_t value)) +SDL_ATOMIC_SYM(drmModePlaneResPtr,drmModeGetPlaneResources,(int fd)) +SDL_ATOMIC_SYM(drmModePlanePtr,drmModeGetPlane,(int fd, uint32_t plane_id)) +SDL_ATOMIC_SYM(drmModeObjectPropertiesPtr,drmModeObjectGetProperties,(int fd,uint32_t object_id,uint32_t object_type)) +SDL_ATOMIC_SYM(drmModePropertyPtr,drmModeGetProperty,(int fd, uint32_t propertyId)) + +SDL_ATOMIC_SYM(void,drmModeFreeProperty,(drmModePropertyPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreeObjectProperties,(drmModeObjectPropertiesPtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreePlane,(drmModePlanePtr ptr)) +SDL_ATOMIC_SYM(void,drmModeFreePlaneResources,(drmModePlaneResPtr ptr)) + +SDL_ATOMIC_SYM(drmModeAtomicReqPtr,drmModeAtomicAlloc,(void)) +SDL_ATOMIC_SYM(void,drmModeAtomicFree,(drmModeAtomicReqPtr req)) +SDL_ATOMIC_SYM(int,drmModeAtomicCommit,(int fd,drmModeAtomicReqPtr req,uint32_t flags,void *user_data)) +SDL_ATOMIC_SYM(int,drmModeAtomicAddProperty,(drmModeAtomicReqPtr req,uint32_t object_id,uint32_t property_id,uint64_t value)) +SDL_ATOMIC_SYM(int,drmModeCreatePropertyBlob,(int fd,const void *data,size_t size,uint32_t *id)) + +/* End of atomic fns */ + +SDL_ATOMIC_MODULE(GBM) +SDL_ATOMIC_SYM(int,gbm_device_get_fd,(struct gbm_device *gbm)) +SDL_ATOMIC_SYM(int,gbm_device_is_format_supported,(struct gbm_device *gbm, + uint32_t format, uint32_t usage)) +SDL_ATOMIC_SYM(void,gbm_device_destroy,(struct gbm_device *gbm)) +SDL_ATOMIC_SYM(struct gbm_device *,gbm_create_device,(int fd)) +SDL_ATOMIC_SYM(unsigned int,gbm_bo_get_width,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(unsigned int,gbm_bo_get_height,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(uint32_t,gbm_bo_get_stride,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(uint32_t,gbm_bo_get_stride_for_plane,(struct gbm_bo *bo,int plane)) +SDL_ATOMIC_SYM(uint32_t,gbm_bo_get_format,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(uint32_t,gbm_bo_get_offset,(struct gbm_bo *bo, int plane)) +SDL_ATOMIC_SYM(int,gbm_bo_get_plane_count,(struct gbm_bo *bo)) + +SDL_ATOMIC_SYM(union gbm_bo_handle,gbm_bo_get_handle,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(int,gbm_bo_write,(struct gbm_bo *bo, const void *buf, size_t count)) +SDL_ATOMIC_SYM(struct gbm_device *,gbm_bo_get_device,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(void,gbm_bo_set_user_data,(struct gbm_bo *bo, void *data, + void (*destroy_user_data)(struct gbm_bo *, void *))) +SDL_ATOMIC_SYM(void *,gbm_bo_get_user_data,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(void,gbm_bo_destroy,(struct gbm_bo *bo)) +SDL_ATOMIC_SYM(struct gbm_bo *,gbm_bo_create,(struct gbm_device *gbm, + uint32_t width, uint32_t height, + uint32_t format, uint32_t usage)) +SDL_ATOMIC_SYM(struct gbm_surface *,gbm_surface_create,(struct gbm_device *gbm, + uint32_t width, uint32_t height, + uint32_t format, uint32_t flags)) +SDL_ATOMIC_SYM(void,gbm_surface_destroy,(struct gbm_surface *surf)) +SDL_ATOMIC_SYM(struct gbm_bo *,gbm_surface_lock_front_buffer,(struct gbm_surface *surf)) +SDL_ATOMIC_SYM(void,gbm_surface_release_buffer,(struct gbm_surface *surf, struct gbm_bo *bo)) + + +#undef SDL_ATOMIC_MODULE +#undef SDL_ATOMIC_SYM +#undef SDL_ATOMIC_SYM_CONST + +/* *INDENT-ON* */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicvideo.c b/src/video/atomic/SDL_atomicvideo.c new file mode 100644 index 0000000000000..67cfc7efb62fd --- /dev/null +++ b/src/video/atomic/SDL_atomicvideo.c @@ -0,0 +1,1426 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_ATOMIC + +/* SDL internals */ +#include "../SDL_sysvideo.h" +#include "SDL_syswm.h" +#include "../../events/SDL_events_c.h" +#include "../../events/SDL_mouse_c.h" +#include "../../events/SDL_keyboard_c.h" + +#ifdef SDL_INPUT_LINUXEV +#include "../../core/linux/SDL_evdev.h" +#endif + +/* KMSDRM ATOMIC declarations */ +#include "SDL_atomicvideo.h" +#include "SDL_atomicevents.h" +#include "SDL_atomicgles.h" +#include "SDL_atomicmouse.h" +#include "SDL_atomicdyn.h" +#include "SDL_atomicvulkan.h" +#include "SDL_atomicinterface.h" +#include +#include +#include +#include + +/* for older ATOMIC headers... */ +#ifndef DRM_FORMAT_MOD_VENDOR_NONE +#define DRM_FORMAT_MOD_VENDOR_NONE 0 +#endif +#ifndef DRM_FORMAT_MOD_LINEAR +#define DRM_FORMAT_MOD_LINEAR fourcc_mod_code(NONE, 0) +#endif + +#define ATOMIC_DRI_PATH "/dev/dri/" + +static int +set_client_caps (int fd) +{ + if (ATOMIC_drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1)) { + return SDL_SetError("no atomic modesetting support."); + } + if (ATOMIC_drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) { + return SDL_SetError("no universal planes support."); + } + return 0; +} + +static int +check_modesetting(int devindex) +{ + SDL_bool available = SDL_FALSE; + char device[512]; + int drm_fd; + + SDL_snprintf(device, sizeof (device), "%scard%d", ATOMIC_DRI_PATH, devindex); + + drm_fd = open(device, O_RDWR | O_CLOEXEC); + if (drm_fd >= 0) { + if (SDL_ATOMIC_LoadSymbols()) { + drmModeRes *resources = ATOMIC_drmModeGetResources(drm_fd); + if (resources) { + + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, + "%scard%d connector, encoder and CRTC counts are: %d %d %d", + ATOMIC_DRI_PATH, devindex, + resources->count_connectors, resources->count_encoders, + resources->count_crtcs); + + if (resources->count_connectors > 0 + && resources->count_encoders > 0 + && resources->count_crtcs > 0) + { + available = SDL_TRUE; + } + ATOMIC_drmModeFreeResources(resources); + } + SDL_ATOMIC_UnloadSymbols(); + } + close(drm_fd); + } + + return available; +} + +static unsigned int +get_dricount(void) +{ + unsigned int devcount = 0; + struct dirent *res; + struct stat sb; + DIR *folder; + + if (!(stat(ATOMIC_DRI_PATH, &sb) == 0 && S_ISDIR(sb.st_mode))) { + SDL_SetError("The path %s cannot be opened or is not available", + ATOMIC_DRI_PATH); + return 0; + } + + if (access(ATOMIC_DRI_PATH, F_OK) == -1) { + SDL_SetError("The path %s cannot be opened", + ATOMIC_DRI_PATH); + return 0; + } + + folder = opendir(ATOMIC_DRI_PATH); + if (folder) { + while ((res = readdir(folder))) { + size_t len = SDL_strlen(res->d_name); + if (len > 4 && SDL_strncmp(res->d_name, "card", 4) == 0) { + devcount++; + } + } + closedir(folder); + } + + return devcount; +} + +static int +get_driindex(void) +{ + const unsigned int devcount = get_dricount(); + unsigned int i; + + for (i = 0; i < devcount; i++) { + if (check_modesetting(i)) { + return i; + } + } + + return -ENOENT; +} + +static int +ATOMIC_Available(void) +{ + int ret = -ENOENT; + + ret = get_driindex(); + if (ret >= 0) + return 1; + + return ret; +} + +static void +ATOMIC_DeleteDevice(SDL_VideoDevice * device) +{ + if (device->driverdata) { + SDL_free(device->driverdata); + device->driverdata = NULL; + } + + SDL_free(device); + + SDL_ATOMIC_UnloadSymbols(); +} + +static int +ATOMIC_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi) +{ + int w, h; + + uint32_t display_mm_width; + uint32_t display_mm_height; + + SDL_DisplayData *dispdata; + + dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); //viddata->devindex); + + + if (!dispdata) { + return SDL_SetError("No available displays"); + } + + display_mm_width = dispdata->connector->connector->mmWidth; + display_mm_height = dispdata->connector->connector->mmHeight; + + w = dispdata->mode.hdisplay; + h = dispdata->mode.vdisplay; + + *hdpi = display_mm_width ? (((float) w) * 25.4f / display_mm_width) : 0.0f; + *vdpi = display_mm_height ? (((float) h) * 25.4f / display_mm_height) : 0.0f; + *ddpi = SDL_ComputeDiagonalDPI(w, h, ((float) display_mm_width) / 25.4f,((float) display_mm_height) / 25.4f); + + return 0; +} + +static SDL_VideoDevice * +ATOMIC_CreateDevice(int devindex) +{ + SDL_VideoDevice *device; + SDL_VideoData *viddata; + + if (!ATOMIC_Available()) { + return NULL; + } + + if (!devindex || (devindex > 99)) { + devindex = get_driindex(); + } + + if (devindex < 0) { + SDL_SetError("devindex (%d) must be between 0 and 99.", devindex); + return NULL; + } + + if (!SDL_ATOMIC_LoadSymbols()) { + return NULL; + } + + device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice)); + if (!device) { + SDL_OutOfMemory(); + return NULL; + } + + viddata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData)); + if (!viddata) { + SDL_OutOfMemory(); + goto cleanup; + } + viddata->devindex = devindex; + viddata->drm_fd = -1; + + viddata->vulkan_mode = SDL_FALSE; + + device->driverdata = viddata; + + /* Setup all functions that can be handled from this backend. */ + device->VideoInit = ATOMIC_VideoInit; + device->VideoQuit = ATOMIC_VideoQuit; + device->GetDisplayModes = ATOMIC_GetDisplayModes; + device->SetDisplayMode = ATOMIC_SetDisplayMode; + device->GetDisplayDPI = ATOMIC_GetDisplayDPI; + device->CreateSDLWindow = ATOMIC_CreateWindow; + device->CreateSDLWindowFrom = ATOMIC_CreateWindowFrom; + device->SetWindowTitle = ATOMIC_SetWindowTitle; + device->SetWindowIcon = ATOMIC_SetWindowIcon; + device->SetWindowPosition = ATOMIC_SetWindowPosition; + device->SetWindowSize = ATOMIC_SetWindowSize; + device->SetWindowFullscreen = ATOMIC_SetWindowFullscreen; + device->ShowWindow = ATOMIC_ShowWindow; + device->HideWindow = ATOMIC_HideWindow; + device->RaiseWindow = ATOMIC_RaiseWindow; + device->MaximizeWindow = ATOMIC_MaximizeWindow; + device->MinimizeWindow = ATOMIC_MinimizeWindow; + device->RestoreWindow = ATOMIC_RestoreWindow; + device->DestroyWindow = ATOMIC_DestroyWindow; + device->GetWindowWMInfo = ATOMIC_GetWindowWMInfo; + + device->GL_DefaultProfileConfig = ATOMIC_GLES_DefaultProfileConfig; + device->GL_GetProcAddress = ATOMIC_GLES_GetProcAddress; + device->GL_CreateContext = ATOMIC_GLES_CreateContext; + device->GL_MakeCurrent = ATOMIC_GLES_MakeCurrent; + device->GL_SetSwapInterval = ATOMIC_GLES_SetSwapInterval; + device->GL_GetSwapInterval = ATOMIC_GLES_GetSwapInterval; + device->GL_SwapWindow = ATOMIC_GLES_SwapWindow; + device->GL_DeleteContext = ATOMIC_GLES_DeleteContext; + /* Those two functions are dummy. We do these things manually. */ + device->GL_LoadLibrary = ATOMIC_GLES_LoadLibrary; + device->GL_UnloadLibrary = ATOMIC_GLES_UnloadLibrary; + +#if SDL_VIDEO_VULKAN + device->Vulkan_LoadLibrary = ATOMIC_Vulkan_LoadLibrary; + device->Vulkan_UnloadLibrary = ATOMIC_Vulkan_UnloadLibrary; + device->Vulkan_GetInstanceExtensions = ATOMIC_Vulkan_GetInstanceExtensions; + device->Vulkan_CreateSurface = ATOMIC_Vulkan_CreateSurface; + device->Vulkan_GetDrawableSize = ATOMIC_Vulkan_GetDrawableSize; +#endif + + device->PumpEvents = ATOMIC_PumpEvents; + device->free = ATOMIC_DeleteDevice; + + return device; + +cleanup: + if (device) + SDL_free(device); + if (viddata) + SDL_free(viddata); + return NULL; +} + +VideoBootStrap ATOMIC_bootstrap = { + "ATOMIC", + "ATOMIC KMS/DRM Video Driver", + ATOMIC_CreateDevice +}; + +static void +ATOMIC_FBDestroyCallback(struct gbm_bo *bo, void *data) +{ + ATOMIC_FBInfo *fb_info = (ATOMIC_FBInfo *)data; + + if (fb_info && fb_info->drm_fd >= 0 && fb_info->fb_id != 0) { + ATOMIC_drmModeRmFB(fb_info->drm_fd, fb_info->fb_id); + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Delete DRM FB %u", fb_info->fb_id); + } + + SDL_free(fb_info); +} + +ATOMIC_FBInfo * +ATOMIC_FBFromBO(_THIS, struct gbm_bo *bo) +{ + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + unsigned width, height; + uint32_t format, strides[4] = {0}, handles[4] = {0}, offsets[4] = {0}; + const int num_planes = ATOMIC_gbm_bo_get_plane_count(bo); + unsigned int i; + int ret; + + /* Check for an existing framebuffer */ + ATOMIC_FBInfo *fb_info = (ATOMIC_FBInfo *)ATOMIC_gbm_bo_get_user_data(bo); + + if (fb_info) { + return fb_info; + } + + /* Create a structure that contains the info about framebuffer + that we need to use it. */ + fb_info = (ATOMIC_FBInfo *)SDL_calloc(1, sizeof(ATOMIC_FBInfo)); + if (!fb_info) { + SDL_OutOfMemory(); + return NULL; + } + + fb_info->drm_fd = viddata->drm_fd; + + width = ATOMIC_gbm_bo_get_width(bo); + height = ATOMIC_gbm_bo_get_height(bo); + format = ATOMIC_gbm_bo_get_format(bo); + + for (i = 0; i < num_planes; i++) { + strides[i] = ATOMIC_gbm_bo_get_stride_for_plane(bo, i); + handles[i] = ATOMIC_gbm_bo_get_handle(bo).u32; + offsets[i] = ATOMIC_gbm_bo_get_offset(bo, i); + } + + /* Create framebuffer object for the buffer. + It's VERY important to note that fb_id is what we use to set the FB_ID prop + of a plane when using the ATOMIC interface, and we get the fb_id here. */ + ret = ATOMIC_drmModeAddFB2(viddata->drm_fd, width, height, format, + handles, strides, offsets, &fb_info->fb_id, 0); + + if (ret) { + SDL_free(fb_info); + return NULL; + } + + /* Set the userdata pointer. This pointer is used to store custom data that we need + to access in the future, so we store the fb_id here for later use, because fb_id is + what we need to set the FB_ID property of a plane when using the ATOMIC interface. */ + ATOMIC_gbm_bo_set_user_data(bo, fb_info, ATOMIC_FBDestroyCallback); + + return fb_info; +} + +/*****************************************************************************/ +/* SDL Video and Display initialization/handling functions. */ +/* _this is an SDL_VideoDevice * */ +/*****************************************************************************/ + +/* Deinitializes the dispdata members needed for ATOMIC operation that are + inoffeensive for VK compatibility. */ +void ATOMIC_DisplayDataDeinit (_THIS, SDL_DisplayData *dispdata) { + /* Free connector */ + if (dispdata && dispdata->connector) { + if (dispdata->connector->connector) { + ATOMIC_drmModeFreeConnector(dispdata->connector->connector); + dispdata->connector->connector = NULL; + } + if (dispdata->connector->props_info) { + SDL_free(dispdata->connector->props_info); + dispdata->connector->props_info = NULL; + } + SDL_free(dispdata->connector); + dispdata->connector = NULL; + } + + /* Free CRTC */ + if (dispdata && dispdata->crtc) { + if (dispdata->crtc->crtc) { + ATOMIC_drmModeFreeCrtc(dispdata->crtc->crtc); + dispdata->crtc->crtc = NULL; + } + if (dispdata->crtc->props_info) { + SDL_free(dispdata->crtc->props_info); + dispdata->crtc->props_info = NULL; + } + SDL_free(dispdata->crtc); + dispdata->crtc = NULL; + } +} + +/* Initializes the dispdata members needed for ATOMIC operation that are + inoffeensive for VK compatibility, except we must leave the drm_fd + closed when we get to the end of this function. + This is to be called early, in VideoInit(), because it gets us + the videomode information, which SDL needs immediately after VideoInit(). */ +int ATOMIC_DisplayDataInit (_THIS, SDL_DisplayData *dispdata) { + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + + drmModeRes *resources = NULL; + drmModeEncoder *encoder = NULL; + drmModeConnector *connector = NULL; + drmModeCrtc *crtc = NULL; + + char devname[32]; + int ret = 0; + unsigned i,j; + + dispdata->atomic_req = NULL; + dispdata->kms_fence = NULL; + dispdata->gpu_fence = NULL; + dispdata->kms_out_fence_fd = -1; + dispdata->modeset_pending = SDL_FALSE; + dispdata->gbm_init = SDL_FALSE; + + dispdata->display_plane = NULL; + dispdata->cursor_plane = NULL; + + dispdata->cursor_bo = NULL; + + /* Open /dev/dri/cardNN */ + SDL_snprintf(viddata->devpath, sizeof(viddata->devpath), "/dev/dri/card%d", viddata->devindex); + + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opening device %s", devname); + viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC); + + if (viddata->drm_fd < 0) { + ret = SDL_SetError("Could not open %s", devname); + goto cleanup; + } + + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", viddata->drm_fd); + + /********************************************/ + /* Block for enabling ATOMIC compatibility. */ + /********************************************/ + + /* Set ATOMIC & UNIVERSAL PLANES compatibility */ + ret = set_client_caps(viddata->drm_fd); + if (ret) { + goto cleanup; + } + + /*******************************************/ + /* Block for getting the ATOMIC resources. */ + /*******************************************/ + + /* Get all of the available connectors / devices / crtcs */ + resources = ATOMIC_drmModeGetResources(viddata->drm_fd); + if (!resources) { + ret = SDL_SetError("drmModeGetResources(%d) failed", viddata->drm_fd); + goto cleanup; + } + + /* Iterate on the available connectors to find a connected connector. */ + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *conn = ATOMIC_drmModeGetConnector(viddata->drm_fd, + resources->connectors[i]); + + if (!conn) { + continue; + } + + if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) { + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.", + conn->connector_id, conn->count_modes); + connector = conn; + + break; + } + + ATOMIC_drmModeFreeConnector(conn); + } + + if (!connector) { + ret = SDL_SetError("No currently active connector found."); + goto cleanup; + } + + /* Try to find the connector's current encoder */ + for (i = 0; i < resources->count_encoders; i++) { + encoder = ATOMIC_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]); + + if (!encoder) { + continue; + } + + if (encoder->encoder_id == connector->encoder_id) { + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id); + break; + } + + ATOMIC_drmModeFreeEncoder(encoder); + encoder = NULL; + } + + if (!encoder) { + /* No encoder was connected, find the first supported one */ + for (i = 0; i < resources->count_encoders; i++) { + encoder = ATOMIC_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]); + + if (!encoder) { + continue; + } + + for (j = 0; j < connector->count_encoders; j++) { + if (connector->encoders[j] == encoder->encoder_id) { + break; + } + } + + if (j != connector->count_encoders) { + break; + } + + ATOMIC_drmModeFreeEncoder(encoder); + encoder = NULL; + } + } + + if (!encoder) { + ret = SDL_SetError("No connected encoder found."); + goto cleanup; + } + + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id); + + /* Try to find a CRTC connected to this encoder */ + crtc = ATOMIC_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id); + + /* If no CRTC was connected to the encoder, find the first CRTC + that is supported by the encoder, and use that. */ + if (!crtc) { + for (i = 0; i < resources->count_crtcs; i++) { + if (encoder->possible_crtcs & (1 << i)) { + encoder->crtc_id = resources->crtcs[i]; + crtc = ATOMIC_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id); + break; + } + } + } + + if (!crtc) { + ret = SDL_SetError("No CRTC found."); + goto cleanup; + } + + /* Figure out the default mode to be set. */ + dispdata->mode = crtc->mode; + + /* Save the original mode for restoration on quit. */ + dispdata->original_mode = dispdata->mode; + + if (dispdata->mode.hdisplay == 0 || dispdata->mode.vdisplay == 0 ) { + ret = SDL_SetError("Couldn't get a valid connector videomode."); + goto cleanup; + } + + /* Get CRTC properties */ + dispdata->crtc->props = ATOMIC_drmModeObjectGetProperties(viddata->drm_fd, + crtc->crtc_id, DRM_MODE_OBJECT_CRTC); + + dispdata->crtc->props_info = SDL_calloc(dispdata->crtc->props->count_props, + sizeof(*dispdata->crtc->props_info)); + + if (!dispdata->crtc->props_info) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + for (i = 0; i < dispdata->crtc->props->count_props; i++) { + dispdata->crtc->props_info[i] = ATOMIC_drmModeGetProperty(viddata->drm_fd, + dispdata->crtc->props->props[i]); + } + + /* Get connector properties */ + dispdata->connector->props = ATOMIC_drmModeObjectGetProperties(viddata->drm_fd, + connector->connector_id, DRM_MODE_OBJECT_CONNECTOR); + + dispdata->connector->props_info = SDL_calloc(dispdata->connector->props->count_props, + sizeof(*dispdata->connector->props_info)); + + if (!dispdata->connector->props_info) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + for (i = 0; i < dispdata->connector->props->count_props; i++) { + dispdata->connector->props_info[i] = ATOMIC_drmModeGetProperty(viddata->drm_fd, + dispdata->connector->props->props[i]); + } + + /* Store the connector and crtc for future use. This is all we keep from this function, + and these are just structs, inoffensive to VK. */ + dispdata->connector->connector = connector; + dispdata->crtc->crtc = crtc; + + /***********************************/ + /* Block for Vulkan compatibility. */ + /***********************************/ + + /* Leave the FD closed, so VULKAN can work. + This will be reopen in CreateWindow, but only if a non-VK window is requested. */ + ATOMIC_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_ATOMIC, 0); + ATOMIC_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 0); + close (viddata->drm_fd); + viddata->drm_fd = -1; + +cleanup: + if (encoder) + ATOMIC_drmModeFreeEncoder(encoder); + if (resources) + ATOMIC_drmModeFreeResources(resources); + if (ret) { + /* Error (complete) cleanup */ + if (dispdata->connector->connector) { + ATOMIC_drmModeFreeConnector(dispdata->connector->connector); + dispdata->connector->connector = NULL; + } + if (dispdata->crtc->props_info) { + SDL_free(dispdata->crtc->props_info); + dispdata->crtc->props_info = NULL; + } + if (dispdata->connector->props_info) { + SDL_free(dispdata->connector->props_info); + dispdata->connector->props_info = NULL; + } + if (dispdata->crtc->crtc) { + ATOMIC_drmModeFreeCrtc(dispdata->crtc->crtc); + dispdata->crtc->crtc = NULL; + } + if (viddata->drm_fd >= 0) { + close(viddata->drm_fd); + viddata->drm_fd = -1; + } + } + + return ret; +} + +/* Init the Vulkan-INCOMPATIBLE stuff: + Reopen FD, create gbm dev, create dumb buffer and setup display plane. + This is to be called late, in WindowCreate(), and ONLY if this is not + a Vulkan window. + We are doing this so late to allow Vulkan to work if we build a VK window. + These things are incompatible with Vulkan, which accesses the same resources + internally so they must be free when trying to build a Vulkan surface. +*/ +int +ATOMIC_GBMInit (_THIS, SDL_DisplayData *dispdata) +{ + SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; + int ret = 0; + + /* Reopen the FD! */ + viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC); + set_client_caps(viddata->drm_fd); + + /* Create the GBM device. */ + viddata->gbm_dev = ATOMIC_gbm_create_device(viddata->drm_fd); + if (!viddata->gbm_dev) { + ret = SDL_SetError("Couldn't create gbm device."); + } + + /* Setup the display plane. ONLY do this after dispdata has the right + crtc and connector, because these are used in this function. */ + ret = setup_plane(_this, &(dispdata->display_plane), DRM_PLANE_TYPE_PRIMARY); + if (ret) { + ret = SDL_SetError("can't find suitable display plane."); + } + + dispdata->gbm_init = SDL_TRUE; + + return ret; +} + +/* Deinit the Vulkan-incompatible ATOMIC stuff. */ +void +ATOMIC_GBMDeinit (_THIS, SDL_DisplayData *dispdata) +{ + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + + /* Free display plane */ + free_plane(&dispdata->display_plane); + + /* Free cursor plane (if still not freed) */ + free_plane(&dispdata->cursor_plane); + + /* Destroy GBM device. GBM surface is destroyed by DestroySurfaces(), + already called when we get here. */ + if (viddata->gbm_dev) { + ATOMIC_gbm_device_destroy(viddata->gbm_dev); + viddata->gbm_dev = NULL; + } + + /* Finally close DRM FD. May be reopen on next non-vulkan window creation. */ + if (viddata->drm_fd >= 0) { + close(viddata->drm_fd); + viddata->drm_fd = -1; + } + + dispdata->gbm_init = SDL_FALSE; +} + +void +ATOMIC_DestroySurfaces(_THIS, SDL_Window *window) +{ + SDL_WindowData *windata = (SDL_WindowData *) window->driverdata; + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + ATOMIC_PlaneInfo plane_info = {0}; + + /* TODO : Continue investigating why this doesn't work. We should do this instead + of making the display plane point to the TTY console, which isn't there + after creating and destroying a Vulkan window. */ +#if 0 + /* Disconnect the connector from the CRTC (remember: several connectors + can read a CRTC), deactivate the CRTC, and set the PRIMARY PLANE props + CRTC_ID and FB_ID to 0. Then we can destroy the GBM buffers and surface. */ + add_connector_property(dispdata->atomic_req, dispdata->connector , "CRTC_ID", 0); + add_crtc_property(dispdata->atomic_req, dispdata->crtc , "MODE_ID", 0); + add_crtc_property(dispdata->atomic_req, dispdata->crtc , "active", 0); + + plane_info.plane = dispdata->display_plane; + plane_info.crtc_id = 0; + plane_info.fb_id = 0; + + drm_atomic_set_plane_props(&plane_info); + + /* Issue atomic commit that is blocking and allows modesetting. */ + if (drm_atomic_commit(_this, SDL_TRUE, SDL_TRUE)) { + SDL_SetError("Failed to issue atomic commit on surfaces destruction."); + } +#endif + +#if 1 + /************************************************************/ + /* Make the display plane point to the original TTY buffer. */ + /* We have to configure it's input and output scaling */ + /* parameters accordingly. */ + /************************************************************/ + + plane_info.plane = dispdata->display_plane; + plane_info.crtc_id = dispdata->crtc->crtc->crtc_id; + plane_info.fb_id = dispdata->crtc->crtc->buffer_id; + plane_info.src_w = dispdata->original_mode.hdisplay; + plane_info.src_h = dispdata->original_mode.vdisplay; + plane_info.crtc_w = dispdata->original_mode.hdisplay; + plane_info.crtc_h = dispdata->original_mode.vdisplay; + + drm_atomic_set_plane_props(&plane_info); + + if (drm_atomic_commit(_this, SDL_TRUE, SDL_FALSE)) { + SDL_SetError("Failed to issue atomic commit on surfaces destruction."); + } +#endif + + /***************************/ + /* Destroy the EGL surface */ + /***************************/ + + SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, EGL_NO_CONTEXT); + + if (windata->egl_surface != EGL_NO_SURFACE) { + SDL_EGL_DestroySurface(_this, windata->egl_surface); + windata->egl_surface = EGL_NO_SURFACE; + } + + /***************************/ + /* Destroy the GBM buffers */ + /***************************/ + + if (windata->bo) { + ATOMIC_gbm_surface_release_buffer(windata->gs, windata->bo); + windata->bo = NULL; + } + + if (windata->next_bo) { + ATOMIC_gbm_surface_release_buffer(windata->gs, windata->next_bo); + windata->next_bo = NULL; + } + + /***************************/ + /* Destroy the GBM surface */ + /***************************/ + + if (windata->gs) { + ATOMIC_gbm_surface_destroy(windata->gs); + windata->gs = NULL; + } +} + +int +ATOMIC_CreateSurfaces(_THIS, SDL_Window * window) +{ + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + SDL_WindowData *windata = (SDL_WindowData *)window->driverdata; + SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; + uint32_t surface_fmt = GBM_FORMAT_ARGB8888; + uint32_t surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING; + uint32_t width, height; + + EGLContext egl_context; + + int ret = 0; + + /* If the current window already has surfaces, destroy them before creating other. + This is mainly for ReconfigureWindow(), where we simply call CreateSurfaces() + for regenerating a window's surfaces. */ + if (windata->gs) { + ATOMIC_DestroySurfaces(_this, window); + } + + if ( ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) + || ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) + { + width = dispdata->mode.hdisplay; + height = dispdata->mode.vdisplay; + } else { + width = window->w; + height = window->h; + } + + if (!ATOMIC_gbm_device_is_format_supported(viddata->gbm_dev, + surface_fmt, surface_flags)) { + SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, + "GBM surface format not supported. Trying anyway."); + } + + windata->gs = ATOMIC_gbm_surface_create(viddata->gbm_dev, + width, height, surface_fmt, surface_flags); + + if (!windata->gs) { + return SDL_SetError("Could not create GBM surface"); + } + + /* We can't get the EGL context yet because SDL_CreateRenderer has not been called, + but we need an EGL surface NOW, or GL won't be able to render into any surface + and we won't see the first frame. */ + SDL_EGL_SetRequiredVisualId(_this, surface_fmt); + windata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windata->gs); + + if (windata->egl_surface == EGL_NO_SURFACE) { + ret = SDL_SetError("Could not create EGL window surface"); + goto cleanup; + } + + /* Current context passing to EGL is now done here. If something fails, + go back to delayed SDL_EGL_MakeCurrent() call in SwapWindow. */ + egl_context = (EGLContext)SDL_GL_GetCurrentContext(); + ret = SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context); + +cleanup: + + if (ret) { + /* Error (complete) cleanup. */ + if (windata->gs) { + ATOMIC_gbm_surface_destroy(windata->gs); + windata->gs = NULL; + } + } + + return ret; +} + +void +ATOMIC_DestroyWindow(_THIS, SDL_Window *window) +{ + SDL_WindowData *windata = (SDL_WindowData *) window->driverdata; + SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; + SDL_VideoData *viddata = windata->viddata; + SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */ + unsigned int i, j; + + if (!windata) { + return; + } + + if ( !is_vulkan && dispdata->gbm_init ) { + + /* Free cursor plane. */ + ATOMIC_DeinitMouse(_this); + + /* Destroy GBM surface and buffers. */ + ATOMIC_DestroySurfaces(_this, window); + + /* Unload EGL library. */ + if (_this->egl_data) { + SDL_EGL_UnloadLibrary(_this); + } + + /* Unload GL library. */ + if (_this->gl_config.driver_loaded) { + SDL_GL_UnloadLibrary(); + } + + /* Free display plane, and destroy GBM device. */ + ATOMIC_GBMDeinit(_this, dispdata); + } + + else { + /* If we were in Vulkan mode, get out of it. */ + if (viddata->vulkan_mode) { + viddata->vulkan_mode = SDL_FALSE; + } + } + + /********************************************/ + /* Remove from the internal SDL window list */ + /********************************************/ + + for (i = 0; i < viddata->num_windows; i++) { + if (viddata->windows[i] == window) { + viddata->num_windows--; + + for (j = i; j < viddata->num_windows; j++) { + viddata->windows[j] = viddata->windows[j + 1]; + } + + break; + } + } + + /*********************************************************************/ + /* Free the window driverdata. Bye bye, surface and buffer pointers! */ + /*********************************************************************/ + window->driverdata = NULL; + SDL_free(windata); +} + +/*****************************************************************************/ +/* Reconfigure the window scaling parameters and re-construct it's surfaces, */ +/* without destroying the window itself. */ +/* To be used by SetWindowSize() and SetWindowFullscreen(). */ +/*****************************************************************************/ +static int +ATOMIC_ReconfigureWindow( _THIS, SDL_Window * window) { + SDL_WindowData *windata = window->driverdata; + SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; + SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */ + float ratio; + + if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) || + ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) { + + windata->src_w = dispdata->mode.hdisplay; + windata->src_h = dispdata->mode.vdisplay; + windata->output_w = dispdata->mode.hdisplay; + windata->output_h = dispdata->mode.vdisplay; + windata->output_x = 0; + + } else { + /* Normal non-fullscreen windows are scaled using the PRIMARY PLANE, so here we store: + input size (ie: the size of the window buffers), + output size (ie: the mode configured on the CRTC), + X position (to compensate for AR correction). + These are used when we set the PRIMARY PLANE props in SwapWindow() */ + ratio = (float)window->w / (float)window->h; + windata->src_w = window->w; + windata->src_h = window->h; + windata->output_w = dispdata->mode.vdisplay * ratio; + windata->output_h = dispdata->mode.vdisplay; + windata->output_x = (dispdata->mode.hdisplay - windata->output_w) / 2; + } + + if (!is_vulkan) { + if (ATOMIC_CreateSurfaces(_this, window)) { + return -1; + } + } + return 0; +} + +int +ATOMIC_VideoInit(_THIS) +{ + int ret = 0; + + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + SDL_DisplayData *dispdata = NULL; + SDL_VideoDisplay display = {0}; + + SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "ATOMIC_VideoInit()"); + + viddata->video_init = SDL_FALSE; + + dispdata = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData)); + if (!dispdata) { + return SDL_OutOfMemory(); + } + + /* Alloc memory for these. */ + dispdata->display_plane = SDL_calloc(1, sizeof(*dispdata->display_plane)); + dispdata->crtc = SDL_calloc(1, sizeof(*dispdata->crtc)); + dispdata->connector = SDL_calloc(1, sizeof(*dispdata->connector)); + if (!(dispdata->display_plane) || !(dispdata->crtc) || !(dispdata->connector)) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + /* Get ATOMIC resources info and store what we need. Getting and storing + this info isn't a problem for VK compatibility. + For VK-incompatible initializations we have ATOMIC_GBMInit(), which is + called on window creation, and only when we know it's not a VK window. */ + if (ATOMIC_DisplayDataInit(_this, dispdata)) { + ret = SDL_SetError("error getting KMS/DRM information"); + goto cleanup; + } + + /* Setup the single display that's available. + There's no problem with it being still incomplete. */ + display.driverdata = dispdata; + display.desktop_mode.w = dispdata->mode.hdisplay; + display.desktop_mode.h = dispdata->mode.vdisplay; + display.desktop_mode.refresh_rate = dispdata->mode.vrefresh; + display.desktop_mode.format = SDL_PIXELFORMAT_ARGB8888; + display.current_mode = display.desktop_mode; + + /* Add the display only when it's ready, */ + SDL_AddVideoDisplay(&display, SDL_FALSE); + + /* Use this if you ever need to see info on all available planes. */ +#if 0 + get_planes_info(_this); +#endif + +#ifdef SDL_INPUT_LINUXEV + SDL_EVDEV_Init(); +#endif + + /* Since we create and show the default cursor on ATOMIC_InitMouse() and + we call ATOMIC_InitMouse() everytime we create a new window, we have + to be sure to create and show the default cursor only the first time. + If we don't, new default cursors would stack up on mouse->cursors and SDL + would have to hide and delete them at quit, not to mention the memory leak... */ + dispdata->set_default_cursor_pending = SDL_TRUE; + + viddata->video_init = SDL_TRUE; + +cleanup: + + if (ret) { + /* Error (complete) cleanup */ + if (dispdata->display_plane) { + SDL_free(dispdata->display_plane); + } + if (dispdata->crtc) { + SDL_free(dispdata->crtc); + } + if (dispdata->connector) { + SDL_free(dispdata->connector); + } + + SDL_free(dispdata); + } + + return ret; +} + +/* The driverdata pointers, like dispdata, viddata, windata, etc... + are freed by SDL internals, so not our job. */ +void +ATOMIC_VideoQuit(_THIS) +{ + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0); + + ATOMIC_DisplayDataDeinit(_this, dispdata); + +#ifdef SDL_INPUT_LINUXEV + SDL_EVDEV_Quit(); +#endif + + /* Clear out the window list */ + SDL_free(viddata->windows); + viddata->windows = NULL; + viddata->max_windows = 0; + viddata->num_windows = 0; + viddata->video_init = SDL_FALSE; +} + +#if 0 +void +ATOMIC_GetDisplayModes(_THIS, SDL_VideoDisplay * display) +{ + /* Only one display mode available: the current one */ + SDL_AddDisplayMode(display, &display->current_mode); +} +#endif + +/* We only change the video mode for FULLSCREEN windows + that are not FULLSCREEN_DESKTOP. + Normal non-fullscreen windows are scaled using the CRTC. */ +void +ATOMIC_GetDisplayModes(_THIS, SDL_VideoDisplay * display) +{ + SDL_DisplayData *dispdata = display->driverdata; + drmModeConnector *conn = dispdata->connector->connector; + SDL_DisplayMode mode; + int i; + + for (i = 0; i < conn->count_modes; i++) { + SDL_DisplayModeData *modedata = SDL_calloc(1, sizeof(SDL_DisplayModeData)); + + if (!modedata) { + SDL_OutOfMemory(); + return; + } + + modedata->mode_index = i; + + mode.w = conn->modes[i].hdisplay; + mode.h = conn->modes[i].vdisplay; + mode.refresh_rate = conn->modes[i].vrefresh; + mode.format = SDL_PIXELFORMAT_ARGB8888; + mode.driverdata = modedata; + + if (!SDL_AddDisplayMode(display, &mode)) { + SDL_free(modedata); + } + } +} + +int +ATOMIC_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode) +{ + /* Set the dispdata->mode to the new mode and leave actual modesetting + pending to be done on SwapWindow(), to be included on next atomic + commit changeset. */ + + SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; + SDL_DisplayData *dispdata = (SDL_DisplayData *)display->driverdata; + SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata; + drmModeConnector *conn = dispdata->connector->connector; + int i; + + /* Don't do anything if we are in Vulkan mode. */ + if (viddata->vulkan_mode) { + return 0; + } + + if (!modedata) { + return SDL_SetError("Mode doesn't have an associated index"); + } + + /* Take note of the new mode to be set. */ + dispdata->mode = conn->modes[modedata->mode_index]; + + /* Take note that we have to change mode in SwapWindow(). We have to do it there + because we need a buffer of the new size so the commit that contains the + mode change can be completed OK. */ + dispdata->modeset_pending = SDL_TRUE; + + for (i = 0; i < viddata->num_windows; i++) { + SDL_Window *window = viddata->windows[i]; + + if (ATOMIC_CreateSurfaces(_this, window)) { + return -1; + } + + /* Tell app about the window resize */ + SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, mode->w, mode->h); + } + + return 0; +} + +int +ATOMIC_CreateWindow(_THIS, SDL_Window * window) +{ + SDL_WindowData *windata = NULL; + SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; + SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); + SDL_DisplayData *dispdata = display->driverdata; + SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */ + SDL_bool vulkan_mode = viddata->vulkan_mode; /* Do we have any Vulkan windows? */ + NativeDisplayType egl_display; + float ratio; + int ret = 0; + + if ( !(dispdata->gbm_init) && !is_vulkan && !vulkan_mode ) { + + /* If this is not a Vulkan Window, then this is a GL window, so at the + end of this function, we must have marked the window as being OPENGL + and we must have loaded the GL library: both things are needed so the + GL_CreateRenderer() and GL_LoadFunctions() calls in SDL_CreateWindow() + succeed without having to re-create the window. + We must load the EGL library too, which can't be loaded until the GBM + device has been created, because SDL_EGL_Library() function uses it. */ + + /* Maybe you didn't ask for an OPENGL window, but that's what you will get. + See previous comment on why. */ + window->flags |= SDL_WINDOW_OPENGL; + + /* Reopen FD, create gbm dev, setup display plane, etc,. + but only when we come here for the first time, + and only if it's not a VK window. */ + if ((ret = ATOMIC_GBMInit(_this, dispdata))) { + goto cleanup; + } + + /* Manually load the GL library. ATOMIC_EGL_LoadLibrary() has already + been called by SDL_CreateWindow() but we don't do anything there, + precisely to be able to load it here. + If we let SDL_CreateWindow() load the lib, it will be loaded + before we call ATOMIC_GBMInit(), causing GLES programs to fail. */ + if (!_this->egl_data) { + egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev; + if (SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA)) { + goto cleanup; + } + + if (SDL_GL_LoadLibrary(NULL) < 0) { + goto cleanup; + } + } + + /* Can't init mouse stuff sooner because cursor plane is not ready, + so we do it here. */ + ATOMIC_InitMouse(_this); + + /* Since we take cursor buffer away from the cursor plane and + destroy the cursor GBM BO when we destroy a window, we must + also manually re-show the cursor on screen, if necessary, + when we create a window. */ + ATOMIC_InitCursor(); + } + + /* Allocate window internal data */ + windata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData)); + if (!windata) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + + if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) || + ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) + { + windata->src_w = dispdata->mode.hdisplay; + windata->src_h = dispdata->mode.vdisplay; + windata->output_w = dispdata->mode.hdisplay; + windata->output_h = dispdata->mode.vdisplay; + windata->output_x = 0; + } else { + /* Normal non-fullscreen windows are scaled using the CRTC, + so get output (CRTC) size and position, for AR correction. */ + ratio = (float)window->w / (float)window->h; + windata->src_w = window->w; + windata->src_h = window->h; + windata->output_w = dispdata->mode.vdisplay * ratio; + windata->output_h = dispdata->mode.vdisplay; + windata->output_x = (dispdata->mode.hdisplay - windata->output_w) / 2; + } + + /* Don't force fullscreen on all windows: it confuses programs that try + to set a window fullscreen after creating it as non-fullscreen (sm64ex) */ + // window->flags |= SDL_WINDOW_FULLSCREEN; + + /* Setup driver data for this window */ + windata->viddata = viddata; + window->driverdata = windata; + + if (!is_vulkan && !vulkan_mode) { + /* Create the window surfaces. Needs the window diverdata in place. */ + if ((ret = ATOMIC_CreateSurfaces(_this, window))) { + goto cleanup; + } + } + + /* Add window to the internal list of tracked windows. Note, while it may + seem odd to support multiple fullscreen windows, some apps create an + extra window as a dummy surface when working with multiple contexts */ + if (viddata->num_windows >= viddata->max_windows) { + unsigned int new_max_windows = viddata->max_windows + 1; + viddata->windows = (SDL_Window **)SDL_realloc(viddata->windows, + new_max_windows * sizeof(SDL_Window *)); + viddata->max_windows = new_max_windows; + + if (!viddata->windows) { + ret = SDL_OutOfMemory(); + goto cleanup; + } + } + + viddata->windows[viddata->num_windows++] = window; + + /* If we have just created a Vulkan window, establish that we are in Vulkan mode now. */ + viddata->vulkan_mode = is_vulkan; + + /* Focus on the newly created window */ + SDL_SetMouseFocus(window); + SDL_SetKeyboardFocus(window); + + /***********************************************************/ + /* Tell SDL that the mouse has entered the window using an */ + /* artificial event: we have no windowing system to tell */ + /* SDL that it has happened. This makes SDL set the */ + /* SDL_WINDOW_MOUSE_FOCUS on this window, thus fixing */ + /* Scummvm sticky-on-sides software cursor. */ + /***********************************************************/ + SDL_SendWindowEvent(window, SDL_WINDOWEVENT_ENTER, 0, 0); + +cleanup: + + if (ret) { + /* Allocated windata will be freed in ATOMIC_DestroyWindow */ + ATOMIC_DestroyWindow(_this, window); + } + return ret; +} + +int +ATOMIC_CreateWindowFrom(_THIS, SDL_Window * window, const void *data) +{ + return -1; +} + +void +ATOMIC_SetWindowTitle(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon) +{ +} +void +ATOMIC_SetWindowPosition(_THIS, SDL_Window * window) +{ +} + +void +ATOMIC_SetWindowSize(_THIS, SDL_Window * window) +{ + if (ATOMIC_ReconfigureWindow(_this, window)) { + SDL_SetError("Can't reconfigure window on SetWindowSize."); + } +} + +void +ATOMIC_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen) +{ + if (ATOMIC_ReconfigureWindow(_this, window)) { + SDL_SetError("Can't reconfigure window on SetWindowFullscreen."); + } +} + +void +ATOMIC_ShowWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_HideWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_RaiseWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_MaximizeWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_MinimizeWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_RestoreWindow(_THIS, SDL_Window * window) +{ +} +void +ATOMIC_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed) +{ + +} + +/*****************************************************************************/ +/* SDL Window Manager function */ +/*****************************************************************************/ +SDL_bool +ATOMIC_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info) +{ + if (info->version.major <= SDL_MAJOR_VERSION) { + return SDL_TRUE; + } else { + SDL_SetError("application not compiled with SDL %d.%d\n", + SDL_MAJOR_VERSION, SDL_MINOR_VERSION); + return SDL_FALSE; + } + + /* Failed to get window manager information */ + return SDL_FALSE; +} + +#endif /* SDL_VIDEO_DRIVER_ATOMIC */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicvideo.h b/src/video/atomic/SDL_atomicvideo.h new file mode 100644 index 0000000000000..61eb6b081e8c1 --- /dev/null +++ b/src/video/atomic/SDL_atomicvideo.h @@ -0,0 +1,229 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef __SDL_ATOMICVIDEO_H__ +#define __SDL_ATOMICVIDEO_H__ + +#include "../SDL_sysvideo.h" + +#include +#include +#include +#include + +#include +#include +#include +#include + +/****************************************************************************************/ +/* Driverdata pointers are void struct* used to store backend-specific variables */ +/* and info that supports the SDL-side structs like SDL Display Devices, SDL_Windows... */ +/* which need to be "supported" with backend-side info and mechanisms to work. */ +/****************************************************************************************/ + +typedef struct SDL_VideoData +{ + int devindex; /* device index that was passed on creation */ + int drm_fd; /* DRM file desc */ + char devpath[32]; /* DRM dev path. */ + + struct gbm_device *gbm_dev; + + SDL_Window **windows; + unsigned int max_windows; + unsigned int num_windows; + + SDL_bool video_init; /* Has VideoInit succeeded? */ + + SDL_bool vulkan_mode; /* Are we in Vulkan mode? One VK window is enough to be. */ + +} SDL_VideoData; + +typedef struct plane { + drmModePlane *plane; + drmModeObjectProperties *props; + drmModePropertyRes **props_info; +} plane; + +typedef struct crtc { + drmModeCrtc *crtc; + drmModeObjectProperties *props; + drmModePropertyRes **props_info; +} crtc; + +typedef struct connector { + drmModeConnector *connector; + drmModeObjectProperties *props; + drmModePropertyRes **props_info; +} connector; + +/* More general driverdata info that gives support and substance to the SDL_Display. */ +typedef struct SDL_DisplayData +{ + drmModeModeInfo mode; + drmModeModeInfo original_mode; + + plane *display_plane; + plane *cursor_plane; + crtc *crtc; + connector *connector; + + /* Central atomic request list, used for the prop + changeset related to pageflip in SwapWindow. */ + drmModeAtomicReq *atomic_req; + + int kms_in_fence_fd; + int kms_out_fence_fd; + + EGLSyncKHR kms_fence; + EGLSyncKHR gpu_fence; + + SDL_bool modeset_pending; + SDL_bool gbm_init; + + /* DRM & GBM cursor stuff lives here, not in an SDL_Cursor's driverdata struct, + because setting/unsetting up these is done on window creation/destruction, + where we may not have an SDL_Cursor at all (so no SDL_Cursor driverdata). + There's only one cursor GBM BO because we only support one cursor. */ + struct gbm_bo *cursor_bo; + uint64_t cursor_w, cursor_h; + + SDL_bool set_default_cursor_pending; + +} SDL_DisplayData; + +/* Driverdata info that gives ATOMIC-side support and substance to the SDL_Window. */ +typedef struct SDL_WindowData +{ + SDL_VideoData *viddata; + /* SDL internals expect EGL surface to be here, and in ATOMIC the GBM surface is + what supports the EGL surface on the driver side, so all these surfaces and buffers + are expected to be here, in the struct pointed by SDL_Window driverdata pointer: + this one. So don't try to move these to dispdata! */ + struct gbm_surface *gs; + struct gbm_bo *bo; + struct gbm_bo *next_bo; + + EGLSurface egl_surface; + + /* For scaling and AR correction. */ + int32_t src_w; + int32_t src_h; + int32_t output_w; + int32_t output_h; + int32_t output_x; + + /* This dictates what approach we'll use for SwapBuffers. */ + int (*swap_window)(_THIS, SDL_Window * window); + +} SDL_WindowData; + +typedef struct SDL_DisplayModeData +{ + int mode_index; +} SDL_DisplayModeData; + +typedef struct ATOMIC_FBInfo +{ + int drm_fd; /* DRM file desc */ + uint32_t fb_id; /* DRM framebuffer ID */ +} ATOMIC_FBInfo; + +typedef struct ATOMIC_PlaneInfo +{ + struct plane *plane; + uint32_t fb_id; + uint32_t crtc_id; + int32_t src_x; + int32_t src_y; + int32_t src_w; + int32_t src_h; + int32_t crtc_x; + int32_t crtc_y; + int32_t crtc_w; + int32_t crtc_h; +} ATOMIC_PlaneInfo; + +/* Helper functions */ +int ATOMIC_CreateEGLSurface(_THIS, SDL_Window * window); +ATOMIC_FBInfo *ATOMIC_FBFromBO(_THIS, struct gbm_bo *bo); + +/* Atomic functions that are used from SDL_atomicgles.c and SDL_atomicmouse.c */ +void drm_atomic_set_plane_props(struct ATOMIC_PlaneInfo *info); + +void drm_atomic_waitpending(_THIS); +int drm_atomic_commit(_THIS, SDL_bool blocking, SDL_bool allow_modeset); +int add_plane_property(drmModeAtomicReq *req, struct plane *plane, + const char *name, uint64_t value); +int add_crtc_property(drmModeAtomicReq *req, struct crtc *crtc, + const char *name, uint64_t value); +int add_connector_property(drmModeAtomicReq *req, struct connector *connector, + const char *name, uint64_t value); +int setup_plane(_THIS, struct plane **plane, uint32_t plane_type); +void free_plane(struct plane **plane); + +/****************************************************************************/ +/* SDL_VideoDevice functions declaration */ +/****************************************************************************/ + +/* Display and window functions */ +int ATOMIC_VideoInit(_THIS); +void ATOMIC_VideoQuit(_THIS); +void ATOMIC_GetDisplayModes(_THIS, SDL_VideoDisplay * display); +int ATOMIC_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode); +int ATOMIC_CreateWindow(_THIS, SDL_Window * window); +int ATOMIC_CreateWindowFrom(_THIS, SDL_Window * window, const void *data); +void ATOMIC_SetWindowTitle(_THIS, SDL_Window * window); +void ATOMIC_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon); +void ATOMIC_SetWindowPosition(_THIS, SDL_Window * window); +void ATOMIC_SetWindowSize(_THIS, SDL_Window * window); +void ATOMIC_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen); +void ATOMIC_ShowWindow(_THIS, SDL_Window * window); +void ATOMIC_HideWindow(_THIS, SDL_Window * window); +void ATOMIC_RaiseWindow(_THIS, SDL_Window * window); +void ATOMIC_MaximizeWindow(_THIS, SDL_Window * window); +void ATOMIC_MinimizeWindow(_THIS, SDL_Window * window); +void ATOMIC_RestoreWindow(_THIS, SDL_Window * window); +void ATOMIC_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed); +void ATOMIC_DestroyWindow(_THIS, SDL_Window * window); + +/* Window manager function */ +SDL_bool ATOMIC_GetWindowWMInfo(_THIS, SDL_Window * window, + struct SDL_SysWMinfo *info); + +/* OpenGL/OpenGL ES functions */ +int ATOMIC_GLES_LoadLibrary(_THIS, const char *path); +void *ATOMIC_GLES_GetProcAddress(_THIS, const char *proc); +void ATOMIC_GLES_UnloadLibrary(_THIS); +SDL_GLContext ATOMIC_GLES_CreateContext(_THIS, SDL_Window * window); +int ATOMIC_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context); +int ATOMIC_GLES_SetSwapInterval(_THIS, int interval); +int ATOMIC_GLES_GetSwapInterval(_THIS); +int ATOMIC_GLES_SwapWindow(_THIS, SDL_Window * window); +void ATOMIC_GLES_DeleteContext(_THIS, SDL_GLContext context); + +#endif /* __SDL_ATOMICVIDEO_H__ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicvulkan.c b/src/video/atomic/SDL_atomicvulkan.c new file mode 100644 index 0000000000000..d9bb9781bc19c --- /dev/null +++ b/src/video/atomic/SDL_atomicvulkan.c @@ -0,0 +1,418 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_ATOMIC + +#include "SDL_atomicvideo.h" +#include "SDL_atomicdyn.h" +#include "SDL_assert.h" + +#include "SDL_loadso.h" +#include "SDL_atomicvulkan.h" +#include "SDL_syswm.h" +#include "sys/ioctl.h" + +#if defined(__OpenBSD__) +#define DEFAULT_VULKAN "libvulkan.so" +#else +#define DEFAULT_VULKAN "libvulkan.so.1" +#endif + +int ATOMIC_Vulkan_LoadLibrary(_THIS, const char *path) +{ + VkExtensionProperties *extensions = NULL; + Uint32 i, extensionCount = 0; + SDL_bool hasSurfaceExtension = SDL_FALSE; + SDL_bool hasDisplayExtension = SDL_FALSE; + PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; + + if(_this->vulkan_config.loader_handle) + return SDL_SetError("Vulkan already loaded"); + + /* Load the Vulkan library */ + if(!path) + path = SDL_getenv("SDL_VULKAN_LIBRARY"); + if(!path) + path = DEFAULT_VULKAN; + + _this->vulkan_config.loader_handle = SDL_LoadObject(path); + + if(!_this->vulkan_config.loader_handle) + return -1; + + SDL_strlcpy(_this->vulkan_config.loader_path, path, + SDL_arraysize(_this->vulkan_config.loader_path)); + + vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( + _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); + + if(!vkGetInstanceProcAddr) + goto fail; + + _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; + _this->vulkan_config.vkEnumerateInstanceExtensionProperties = + (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( + VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); + + if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) + goto fail; + + extensions = SDL_Vulkan_CreateInstanceExtensionsList( + (PFN_vkEnumerateInstanceExtensionProperties) + _this->vulkan_config.vkEnumerateInstanceExtensionProperties, + &extensionCount); + + if(!extensions) + goto fail; + + for(i = 0; i < extensionCount; i++) + { + if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) + hasSurfaceExtension = SDL_TRUE; + else if(SDL_strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extensions[i].extensionName) == 0) + hasDisplayExtension = SDL_TRUE; + } + + SDL_free(extensions); + + if(!hasSurfaceExtension) + { + SDL_SetError("Installed Vulkan doesn't implement the " + VK_KHR_SURFACE_EXTENSION_NAME " extension"); + goto fail; + } + else if(!hasDisplayExtension) + { + SDL_SetError("Installed Vulkan doesn't implement the " + VK_KHR_DISPLAY_EXTENSION_NAME "extension"); + goto fail; + } + + return 0; + +fail: + SDL_UnloadObject(_this->vulkan_config.loader_handle); + _this->vulkan_config.loader_handle = NULL; + return -1; +} + +void ATOMIC_Vulkan_UnloadLibrary(_THIS) +{ + if(_this->vulkan_config.loader_handle) + { + SDL_UnloadObject(_this->vulkan_config.loader_handle); + _this->vulkan_config.loader_handle = NULL; + } +} + +/*********************************************************************/ +/* Here we can put whatever Vulkan extensions we want to be enabled */ +/* at instance creation, which is done in the programs, not in SDL. */ +/* So: programs call SDL_Vulkan_GetInstanceExtensions() and here */ +/* we put the extensions specific to this backend so the programs */ +/* get a list with the extension we want, so they can include that */ +/* list in the ppEnabledExtensionNames and EnabledExtensionCount */ +/* members of the VkInstanceCreateInfo struct passed to */ +/* vkCreateInstance(). */ +/*********************************************************************/ +SDL_bool ATOMIC_Vulkan_GetInstanceExtensions(_THIS, + SDL_Window *window, + unsigned *count, + const char **names) +{ + static const char *const extensionsForATOMIC[] = { + VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_DISPLAY_EXTENSION_NAME + }; + if(!_this->vulkan_config.loader_handle) + { + SDL_SetError("Vulkan is not loaded"); + return SDL_FALSE; + } + return SDL_Vulkan_GetInstanceExtensions_Helper( + count, names, SDL_arraysize(extensionsForATOMIC), + extensionsForATOMIC); +} + +void ATOMIC_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h) +{ + if (w) { + *w = window->w; + } + + if (h) { + *h = window->h; + } +} + +/***********************************************************************/ +/* First thing to know is that we don't call vkCreateInstance() here. */ +/* Instead, programs using SDL and Vulkan create their Vulkan instance */ +/* and we get it here, ready to use. */ +/* Extensions specific for this platform are activated in */ +/* ATOMIC_Vulkan_GetInstanceExtensions(), like we do with */ +/* VK_KHR_DISPLAY_EXTENSION_NAME, which is what we need for x-less VK. */ +/***********************************************************************/ +SDL_bool ATOMIC_Vulkan_CreateSurface(_THIS, + SDL_Window *window, + VkInstance instance, + VkSurfaceKHR *surface) +{ + VkPhysicalDevice gpu; + uint32_t gpu_count; + uint32_t display_count; + uint32_t mode_count; + uint32_t plane_count; + + VkPhysicalDevice *physical_devices = NULL; + VkDisplayPropertiesKHR *displays_props = NULL; + VkDisplayModePropertiesKHR *modes_props = NULL; + VkDisplayPlanePropertiesKHR *planes_props = NULL; + + VkDisplayModeCreateInfoKHR display_mode_create_info; + VkDisplaySurfaceCreateInfoKHR display_plane_surface_create_info; + + VkExtent2D image_size; + VkDisplayModeKHR *display_mode = NULL; + VkDisplayModePropertiesKHR display_mode_props = {0}; + VkDisplayModeParametersKHR new_mode_parameters = { {0, 0}, 0}; + + VkResult result; + SDL_bool ret = SDL_FALSE; + SDL_bool mode_found = SDL_FALSE; + + /* We don't receive a display index in ATOMIC_CreateDevice(), only + a device index, which determines the GPU to use, but not the output. + So we simply use the first connected output (ie, the first connected + video output) for now. + In other words, change this index to select a different output. Easy! */ + int display_index = 0; + + int i; + + SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata); + + /* Get the function pointers for the functions we will use. */ + PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = + (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; + + PFN_vkCreateDisplayPlaneSurfaceKHR vkCreateDisplayPlaneSurfaceKHR = + (PFN_vkCreateDisplayPlaneSurfaceKHR)vkGetInstanceProcAddr( + instance, "vkCreateDisplayPlaneSurfaceKHR"); + + PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices = + (PFN_vkEnumeratePhysicalDevices)vkGetInstanceProcAddr( + instance, "vkEnumeratePhysicalDevices"); + + PFN_vkGetPhysicalDeviceDisplayPropertiesKHR vkGetPhysicalDeviceDisplayPropertiesKHR = + (PFN_vkGetPhysicalDeviceDisplayPropertiesKHR)vkGetInstanceProcAddr( + instance, "vkGetPhysicalDeviceDisplayPropertiesKHR"); + + PFN_vkGetDisplayModePropertiesKHR vkGetDisplayModePropertiesKHR = + (PFN_vkGetDisplayModePropertiesKHR)vkGetInstanceProcAddr( + instance, "vkGetDisplayModePropertiesKHR"); + + PFN_vkGetPhysicalDeviceDisplayPlanePropertiesKHR vkGetPhysicalDeviceDisplayPlanePropertiesKHR = + (PFN_vkGetPhysicalDeviceDisplayPlanePropertiesKHR)vkGetInstanceProcAddr( + instance, "vkGetPhysicalDeviceDisplayPlanePropertiesKHR"); + + /*PFN_vkGetDisplayPlaneSupportedDisplaysKHR vkGetDisplayPlaneSupportedDisplaysKHR = + (PFN_vkGetDisplayPlaneSupportedDisplaysKHR)vkGetInstanceProcAddr( + instance, "vkGetDisplayPlaneSupportedDisplaysKHR"); + + PFN_vkGetDisplayPlaneCapabilitiesKHR vkGetDisplayPlaneCapabilitiesKHR = + (PFN_vkGetDisplayPlaneCapabilitiesKHR)vkGetInstanceProcAddr( + instance, "vkGetDisplayPlaneCapabilitiesKHR"); + */ + + PFN_vkCreateDisplayModeKHR vkCreateDisplayModeKHR = + (PFN_vkCreateDisplayModeKHR)vkGetInstanceProcAddr( + instance, "vkCreateDisplayModeKHR"); + + if(!_this->vulkan_config.loader_handle) + { + SDL_SetError("Vulkan is not loaded"); + goto clean; + } + + /*************************************/ + /* Block for vulkan surface creation */ + /*************************************/ + + /****************************************************************/ + /* If we got vkCreateDisplayPlaneSurfaceKHR() pointer, it means */ + /* that the VK_KHR_Display extension is active on the instance. */ + /* That's the central extension we need for x-less VK! */ + /****************************************************************/ + if(!vkCreateDisplayPlaneSurfaceKHR) + { + SDL_SetError(VK_KHR_DISPLAY_EXTENSION_NAME + " extension is not enabled in the Vulkan instance."); + goto clean; + } + + /* Get the physical device count. */ + vkEnumeratePhysicalDevices(instance, &gpu_count, NULL); + + if (gpu_count == 0) { + SDL_SetError("Vulkan can't find physical devices (gpus)."); + goto clean; + } + + /* Get the physical devices. */ + physical_devices = SDL_malloc(sizeof(VkPhysicalDevice) * gpu_count); + vkEnumeratePhysicalDevices(instance, &gpu_count, physical_devices); + + /* A GPU (or physical_device, in vkcube terms) is a GPU. A machine with more + than one video output doen't need to have more than one GPU, like the Pi4 + which has 1 GPU and 2 video outputs. + We grab the GPU/physical_device with the index we got in KMSDR_CreateDevice(). */ + gpu = physical_devices[viddata->devindex]; + + /* A display is a video output. 1 GPU can have N displays. + Vulkan only counts the connected displays. + Get the display count of the GPU. */ + vkGetPhysicalDeviceDisplayPropertiesKHR(gpu, &display_count, NULL); + if (display_count == 0) { + SDL_SetError("Vulkan can't find any displays."); + goto clean; + } + + /* Get the props of the displays of the physical device. */ + displays_props = (VkDisplayPropertiesKHR *) SDL_malloc(display_count * sizeof(*displays_props)); + vkGetPhysicalDeviceDisplayPropertiesKHR(gpu, + &display_count, + displays_props); + + /* Get the videomode count for the first display. */ + vkGetDisplayModePropertiesKHR(gpu, + displays_props[display_index].display, + &mode_count, NULL); + + if (mode_count == 0) { + SDL_SetError("Vulkan can't find any video modes for display %i (%s)\n", 0, + displays_props[display_index].displayName); + goto clean; + } + + /* Get the props of the videomodes for the first display. */ + modes_props = (VkDisplayModePropertiesKHR *) SDL_malloc(mode_count * sizeof(*modes_props)); + vkGetDisplayModePropertiesKHR(gpu, + displays_props[display_index].display, + &mode_count, modes_props); + + /* Get the planes count of the physical device. */ + vkGetPhysicalDeviceDisplayPlanePropertiesKHR(gpu, &plane_count, NULL); + if (plane_count == 0) { + SDL_SetError("Vulkan can't find any planes."); + goto clean; + } + + /* Get the props of the planes for the physical device. */ + planes_props = SDL_malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count); + vkGetPhysicalDeviceDisplayPlanePropertiesKHR(gpu, &plane_count, planes_props); + + /* Get a video mode equal or smaller than the window size. REMEMBER: + We have to get a small enough videomode for the window size, + because videomode determines how big the scanout region is and we can't + scanout a region bigger than the window (we would be reading past the + buffer, and Vulkan would give us a confusing VK_ERROR_SURFACE_LOST_KHR). */ + for (i = 0; i < mode_count; i++) { + if (modes_props[i].parameters.visibleRegion.width <= window->w && + modes_props[i].parameters.visibleRegion.height <= window->h) + { + display_mode_props = modes_props[i]; + mode_found = SDL_TRUE; + break; + } + } + + if (mode_found && + display_mode_props.parameters.visibleRegion.width > 0 && + display_mode_props.parameters.visibleRegion.height > 0 ) { + /* Found a suitable mode among the predefined ones. Use that. */ + display_mode = &(display_mode_props.displayMode); + } else { + /* Can't find a suitable mode among the predefined ones, so try to create our own. */ + new_mode_parameters.visibleRegion.width = window->w; + new_mode_parameters.visibleRegion.height = window->h; + new_mode_parameters.refreshRate = 60000; /* Always use 60Hz for now. */ + display_mode_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_MODE_CREATE_INFO_KHR; + display_mode_create_info.parameters = new_mode_parameters; + result = vkCreateDisplayModeKHR(gpu, + displays_props[display_index].display, + &display_mode_create_info, + NULL, display_mode); + if (result != VK_SUCCESS) { + SDL_SetError("Vulkan couldn't find a predefined mode for that window size and couldn't create a suitable mode."); + goto clean; + } + } + + /* Just in case we get here without a display_mode. */ + if (!display_mode) { + SDL_SetError("Vulkan couldn't get a display mode."); + goto clean; + } + + /********************************************/ + /* Let's finally create the Vulkan surface! */ + /********************************************/ + + image_size.width = window->w; + image_size.height = window->h; + + display_plane_surface_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR; + display_plane_surface_create_info.displayMode = *display_mode; + /* For now, simply use the first plane. */ + display_plane_surface_create_info.planeIndex = 0; + display_plane_surface_create_info.imageExtent = image_size; + result = vkCreateDisplayPlaneSurfaceKHR(instance, + &display_plane_surface_create_info, + NULL, + surface); + if(result != VK_SUCCESS) + { + SDL_SetError("vkCreateATOMICSurfaceKHR failed: %s", + SDL_Vulkan_GetResultString(result)); + goto clean; + } + + ret = SDL_TRUE; + +clean: + if (physical_devices) + SDL_free (physical_devices); + if (displays_props) + SDL_free (displays_props); + if (planes_props) + SDL_free (planes_props); + if (modes_props) + SDL_free (modes_props); + + return ret; +} + +#endif + +/* vim: set ts=4 sw=4 expandtab: */ diff --git a/src/video/atomic/SDL_atomicvulkan.h b/src/video/atomic/SDL_atomicvulkan.h new file mode 100644 index 0000000000000..67b20f5f47249 --- /dev/null +++ b/src/video/atomic/SDL_atomicvulkan.h @@ -0,0 +1,49 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2021 Sam Lantinga + Atomic KMSDRM backend by Manuel Alfayate Corchete + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_atomicvulkan_h_ +#define SDL_atomicvulkan_h_ + +#include "../SDL_vulkan_internal.h" +#include "../SDL_sysvideo.h" + +#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_ATOMIC + +int ATOMIC_Vulkan_LoadLibrary(_THIS, const char *path); +void ATOMIC_Vulkan_UnloadLibrary(_THIS); +SDL_bool ATOMIC_Vulkan_GetInstanceExtensions(_THIS, + SDL_Window *window, + unsigned *count, + const char **names); +void ATOMIC_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h); +SDL_bool ATOMIC_Vulkan_CreateSurface(_THIS, + SDL_Window *window, + VkInstance instance, + VkSurfaceKHR *surface); + +#endif + +#endif /* SDL_atomicvulkan_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */