Skip to content

Commit a6e372e

Browse files
authored
Dynamically load libwayland-egl.so.1 when dealing with Wayland to remove dependencies at program startup. (#3359)
1 parent 7e5bb54 commit a6e372e

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

examples/common/entry/entry_sdl.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#if ENTRY_CONFIG_USE_SDL
99

1010
#if BX_PLATFORM_LINUX
11-
# if ENTRY_CONFIG_USE_WAYLAND
12-
# include <wayland-egl.h>
13-
# endif
1411
#elif BX_PLATFORM_WINDOWS
1512
# define SDL_MAIN_HANDLED
1613
#endif
@@ -68,16 +65,6 @@ namespace entry
6865
{
6966
if(!_window)
7067
return;
71-
# if BX_PLATFORM_LINUX
72-
# if ENTRY_CONFIG_USE_WAYLAND
73-
wl_egl_window *win_impl = (wl_egl_window*)SDL_GetWindowData(_window, "wl_egl_window");
74-
if(win_impl)
75-
{
76-
SDL_SetWindowData(_window, "wl_egl_window", nullptr);
77-
wl_egl_window_destroy(win_impl);
78-
}
79-
# endif
80-
# endif
8168
SDL_DestroyWindow(_window);
8269
}
8370

scripts/genie.lua

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ newoption {
2222

2323
newoption {
2424
trigger = "with-wayland",
25-
description = "Use Wayland backend.",
25+
description = "Enable Wayland support.",
2626
}
2727

2828
newoption {
@@ -235,13 +235,6 @@ function exampleProjectDefaults()
235235
defines { "ENTRY_CONFIG_USE_SDL=1" }
236236
links { "SDL2" }
237237

238-
configuration { "linux or freebsd" }
239-
if _OPTIONS["with-wayland"] then
240-
links {
241-
"wayland-egl",
242-
}
243-
end
244-
245238
configuration { "osx*" }
246239
libdirs { "$(SDL2_DIR)/lib" }
247240

@@ -253,19 +246,13 @@ function exampleProjectDefaults()
253246
links { "glfw3" }
254247

255248
configuration { "linux or freebsd" }
256-
if _OPTIONS["with-wayland"] then
257-
links {
258-
"wayland-egl",
259-
}
260-
else
261-
links {
262-
"Xrandr",
263-
"Xinerama",
264-
"Xi",
265-
"Xxf86vm",
266-
"Xcursor",
267-
}
268-
end
249+
links {
250+
"Xrandr",
251+
"Xinerama",
252+
"Xi",
253+
"Xxf86vm",
254+
"Xcursor",
255+
}
269256

270257
configuration { "osx*" }
271258
linkoptions {

src/glcontext_egl.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,39 @@ EGL_IMPORT
128128
}
129129
#endif // BGFX_USE_GL_DYNAMIC_LIB
130130

131+
132+
#if defined(WL_EGL_PLATFORM)
133+
#define WL_EGL_IMPORT \
134+
WL_EGL_FUNC(struct wl_egl_window *, wl_egl_window_create, (struct wl_surface *, int, int)) \
135+
WL_EGL_FUNC(void, wl_egl_window_destroy, (struct wl_egl_window *)) \
136+
WL_EGL_FUNC(void, wl_egl_window_resize, (struct wl_egl_window *, int, int, int, int)) \
137+
WL_EGL_FUNC(void, wl_egl_window_get_attached_size, (struct wl_egl_window *, int *, int *)) \
138+
139+
#define WL_EGL_FUNC(rt, fname, params) \
140+
typedef rt(*PFNWLEGL_##fname) params; \
141+
PFNWLEGL_##fname BGFX_WAYLAND_##fname;
142+
WL_EGL_IMPORT
143+
#undef WL_EGL_FUNC
144+
145+
void *waylandEglOpen() {
146+
void *so = bx::dlopen("libwayland-egl.so.1");
147+
BGFX_FATAL(so != NULL, Fatal::UnableToInitialize, "Could not dlopen() libwayland-egl.so.1");
148+
149+
#define WL_EGL_FUNC(rt, fname, params) BGFX_WAYLAND_##fname = (PFNWLEGL_##fname) bx::dlsym(so, #fname);
150+
WL_EGL_IMPORT
151+
#undef WL_EGL_FUNC
152+
153+
return so;
154+
}
155+
156+
void waylandEglClose(void *so) {
157+
bx::dlclose(so);
158+
#define WL_EGL_FUNC(rt, fname, params) BGFX_WAYLAND_##fname = NULL;
159+
WL_EGL_IMPORT
160+
#undef WL_EGL_FUNC
161+
}
162+
#endif
163+
131164
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
132165
# include "glimports.h"
133166

@@ -335,9 +368,11 @@ EGL_IMPORT
335368

336369
# if BX_PLATFORM_LINUX && defined(WL_EGL_PLATFORM)
337370
if (g_platformData.type == NativeWindowHandleType::Wayland) {
371+
m_waylandEglLibrary = waylandEglOpen();
372+
338373
// A wl_surface needs to be first wrapped in a wl_egl_window
339374
// before it can be used to create the EGLSurface.
340-
m_egl_window = wl_egl_window_create((wl_surface*)nwh, _width, _height);
375+
m_egl_window = BGFX_WAYLAND_wl_egl_window_create((wl_surface*)nwh, _width, _height);
341376
nwh = m_egl_window;
342377
}
343378
# endif
@@ -444,7 +479,9 @@ EGL_IMPORT
444479
EGL_CHECK(eglDestroySurface(m_display, m_surface) );
445480
# if BX_PLATFORM_LINUX && defined(WL_EGL_PLATFORM)
446481
if (m_egl_window) {
447-
wl_egl_window_destroy(m_egl_window);
482+
BGFX_WAYLAND_wl_egl_window_destroy(m_egl_window);
483+
waylandEglClose(m_waylandEglLibrary);
484+
m_waylandEglLibrary = NULL;
448485
}
449486
# endif
450487
EGL_CHECK(eglTerminate(m_display) );
@@ -453,6 +490,7 @@ EGL_IMPORT
453490

454491
EGL_CHECK(eglReleaseThread() );
455492
eglClose(m_eglLibrary);
493+
m_eglLibrary = NULL;
456494

457495
# if BX_PLATFORM_RPI
458496
bcm_host_deinit();
@@ -480,7 +518,7 @@ EGL_IMPORT
480518
EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) );
481519
# elif BX_PLATFORM_LINUX && defined(WL_EGL_PLATFORM)
482520
if (NULL != m_egl_window) {
483-
wl_egl_window_resize(m_egl_window, _width, _height, 0, 0);
521+
BGFX_WAYLAND_wl_egl_window_resize(m_egl_window, _width, _height, 0, 0);
484522
}
485523
# else
486524
BX_UNUSED(_width, _height);

src/glcontext_egl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace bgfx { namespace gl
3737
, m_display(NULL)
3838
, m_surface(NULL)
3939
#if BX_PLATFORM_LINUX && defined(WL_EGL_PLATFORM)
40+
, m_waylandEglLibrary(NULL)
4041
, m_egl_window(NULL)
4142
#endif
4243
, m_msaaContext(false)
@@ -67,6 +68,7 @@ namespace bgfx { namespace gl
6768
EGLDisplay m_display;
6869
EGLSurface m_surface;
6970
#if BX_PLATFORM_LINUX && defined(WL_EGL_PLATFORM)
71+
void *m_waylandEglLibrary;
7072
struct wl_egl_window *m_egl_window;
7173
#endif
7274
// true when MSAA is handled by the context instead of using MSAA FBO

0 commit comments

Comments
 (0)