Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for wayland to bgfx renderer #11451

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions scripts/src/3rdparty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions scripts/src/osd/sdl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions scripts/src/osd/sdl_cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 39 additions & 4 deletions src/osd/modules/render/drawbgfx.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really should follow the code style of files you’re modifying. Also, why are you casting data pointers through uintptr_t? That’s only necessary when coercing pointer-to-code to pointer-to-data or vice versa.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the cast. Apologies for the style, I am a C++ newbie at best. The code is more or less a copy-paste from
https://github.com/bkaradzic/bgfx/blob/3101a0d93f024e4278caaab29566647cd3e2bf84/examples/common/entry/entry_sdl.cpp#L53-L64

Do you mind being more specific in terms of what I should fix style-wise?

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ extern void *GetOSWindow(void *wincontroller);
#endif
#endif

#if defined(SDLMAME_USE_WAYLAND)
#include <wayland-egl.h>
#endif

#include <bgfx/bgfx.h>
#include <bgfx/platform.h>

Expand Down Expand Up @@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off in two places – you’ve bumped two levels for some of the if statements. The braces should be at the level of the if statement itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#endif


//============================================================
// Utility for setting up window handle
//============================================================
Expand Down Expand Up @@ -423,10 +457,11 @@ 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<sdl_window_info const &>(window).platform_window(), wmi.info.wl.surface);
platform_data.type = bgfx::NativeWindowHandleType::Wayland;
Copy link
Member

@cuavas cuavas Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because create_wl_egl_window can fail (unlike the other branches of this switch statement), you need to check whether it succeeded here, and return false if it didn’t.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now. Is it also needed for sdlNativeWindowHandle()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now. Is it also needed for sdlNativeWindowHandle()?

It is, but the function would need to be rearranged a bit. I’ll worry about that later.

break;
#endif
#if defined(SDL_VIDEO_DRIVER_ANDROID)
Expand Down Expand Up @@ -513,9 +548,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:
Expand Down
4 changes: 0 additions & 4 deletions src/osd/sdl/osdsdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down