From 117c384d4ac65c06481f7a0631051c8ba273c0b5 Mon Sep 17 00:00:00 2001 From: Julian Sikorski Date: Thu, 28 Sep 2023 17:24:59 +0200 Subject: [PATCH] render/drawbgfx.cpp: Added initial support for Wayland on Linux. (#11451) --- makefile | 5 +++ scripts/src/3rdparty.lua | 5 +++ scripts/src/osd/sdl.lua | 15 +++++++++ scripts/src/osd/sdl_cfg.lua | 6 ++++ src/osd/modules/render/drawbgfx.cpp | 48 ++++++++++++++++++++++++++--- src/osd/sdl/osdsdl.cpp | 4 --- 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/makefile b/makefile index bd5a442cb71..f173f8201bc 100644 --- a/makefile +++ b/makefile @@ -37,6 +37,7 @@ # USE_PCAP = 1 # USE_QTDEBUG = 1 # NO_X11 = 1 +# USE_WAYLAND = 1 # NO_USE_XINPUT = 1 # NO_USE_XINPUT_WII_LIGHTGUN_HACK = 1 # FORCE_DRC_C_BACKEND = 1 @@ -792,6 +793,10 @@ ifdef MESA_INSTALL_ROOT PARAMS += --MESA_INSTALL_ROOT='$(MESA_INSTALL_ROOT)' endif +ifdef USE_WAYLAND +PARAMS += --USE_WAYLAND='$(USE_WAYLAND)' +endif + ifdef NO_X11 PARAMS += --NO_X11='$(NO_X11)' endif diff --git a/scripts/src/3rdparty.lua b/scripts/src/3rdparty.lua index 7b80101e973..dacb3a298ac 100755 --- a/scripts/src/3rdparty.lua +++ b/scripts/src/3rdparty.lua @@ -1469,6 +1469,11 @@ end "BGFX_CONFIG_RENDERER_OPENGL=0", } end + if _OPTIONS["USE_WAYLAND"]=="1" then + defines { + "WL_EGL_PLATFORM=1", + } + end end if _OPTIONS["targetos"]=="macosx" and _OPTIONS["gcc"]~=nil then diff --git a/scripts/src/osd/sdl.lua b/scripts/src/osd/sdl.lua index abf4a90fd97..3e749d04017 100644 --- a/scripts/src/osd/sdl.lua +++ b/scripts/src/osd/sdl.lua @@ -37,6 +37,12 @@ function maintargetosdoptions(_target,_subtarget) end end + if _OPTIONS["USE_WAYLAND"]=="1" then + links { + "wayland-egl" + } + end + if _OPTIONS["NO_USE_XINPUT"]~="1" then links { "Xext", @@ -141,6 +147,15 @@ if not _OPTIONS["NO_X11"] then end end +newoption { + trigger = "USE_WAYLAND", + description = "Use Wayland", + allowed = { + { "0", "Do not use Wayland (use XWayland or X11)" }, + { "1", "Use Wayland" }, + }, +} + newoption { trigger = "NO_USE_XINPUT", description = "Disable use of Xinput", diff --git a/scripts/src/osd/sdl_cfg.lua b/scripts/src/osd/sdl_cfg.lua index 6b93c9a831e..17132ad814a 100644 --- a/scripts/src/osd/sdl_cfg.lua +++ b/scripts/src/osd/sdl_cfg.lua @@ -50,6 +50,12 @@ else } end +if _OPTIONS["USE_WAYLAND"]=="1" then + defines { + "SDLMAME_USE_WAYLAND", + } +end + if _OPTIONS["NO_USE_XINPUT"]=="1" then defines { "USE_XINPUT=0", diff --git a/src/osd/modules/render/drawbgfx.cpp b/src/osd/modules/render/drawbgfx.cpp index f1a8d2611f5..19da01898e1 100644 --- a/src/osd/modules/render/drawbgfx.cpp +++ b/src/osd/modules/render/drawbgfx.cpp @@ -56,6 +56,10 @@ extern void *GetOSWindow(void *wincontroller); #endif #endif +#if defined(SDLMAME_USE_WAYLAND) +#include +#endif + #include #include @@ -382,6 +386,36 @@ bool video_bgfx::init_bgfx_library(osd_window &window) } +//============================================================ +// Helper for creating a wayland window +//============================================================ + +#if defined(SDLMAME_USE_WAYLAND) +wl_egl_window *create_wl_egl_window(SDL_Window *window, struct wl_surface *surface) +{ + if (!surface) + { + osd_printf_error("Wayland surface missing, aborting\n"); + return nullptr; + } + wl_egl_window *win_impl = (wl_egl_window *)SDL_GetWindowData(window, "wl_egl_window"); + if (!win_impl) + { + int width, height; + SDL_GetWindowSize(window, &width, &height); + win_impl = wl_egl_window_create(surface, width, height); + if (!win_impl) + { + osd_printf_error("Creating wayland window failed\n"); + return nullptr; + } + SDL_SetWindowData(window, "wl_egl_window", win_impl); + } + return win_impl; +} +#endif + + //============================================================ // Utility for setting up window handle //============================================================ @@ -423,10 +457,16 @@ bool video_bgfx::set_platform_data(bgfx::PlatformData &platform_data, osd_window platform_data.nwh = wmi.info.cocoa.window; break; #endif -#if defined(SDL_VIDEO_DRIVER_WAYLAND) && SDL_VERSION_ATLEAST(2, 0, 16) +#if defined(SDL_VIDEO_DRIVER_WAYLAND) && SDL_VERSION_ATLEAST(2, 0, 16) && defined(SDLMAME_USE_WAYLAND) case SDL_SYSWM_WAYLAND: platform_data.ndt = wmi.info.wl.display; - platform_data.nwh = wmi.info.wl.egl_window; + platform_data.nwh = create_wl_egl_window(dynamic_cast(window).platform_window(), wmi.info.wl.surface); + if (!platform_data.nwh) + { + osd_printf_error("BGFX: Error creating a Wayland window\n"); + return false; + } + platform_data.type = bgfx::NativeWindowHandleType::Wayland; break; #endif #if defined(SDL_VIDEO_DRIVER_ANDROID) @@ -513,9 +553,9 @@ static void *sdlNativeWindowHandle(SDL_Window *window) case SDL_SYSWM_COCOA: return wmi.info.cocoa.window; #endif -#if defined(SDL_VIDEO_DRIVER_WAYLAND) && SDL_VERSION_ATLEAST(2, 0, 16) +#if defined(SDL_VIDEO_DRIVER_WAYLAND) && SDL_VERSION_ATLEAST(2, 0, 16) && defined(SDLMAME_USE_WAYLAND) case SDL_SYSWM_WAYLAND: - return wmi.info.wl.egl_window; + return osd::create_wl_egl_window(window, wmi.info.wl.surface); #endif #if defined(SDL_VIDEO_DRIVER_ANDROID) case SDL_SYSWM_ANDROID: diff --git a/src/osd/sdl/osdsdl.cpp b/src/osd/sdl/osdsdl.cpp index 147244ef0d4..6c5aef9f08c 100644 --- a/src/osd/sdl/osdsdl.cpp +++ b/src/osd/sdl/osdsdl.cpp @@ -271,10 +271,6 @@ void sdl_osd_interface::init(running_machine &machine) exit(-1); } - // bgfx does not work with wayland - if ((strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) && (strcmp(options().video(), "bgfx") == 0)) - fatalerror("Error: BGFX video does not work with wayland videodriver. Please change either of the options."); - osd_sdl_info(); defines_verbose();