diff --git a/CMakeLists.txt b/CMakeLists.txt index c1488cc26..2733fd38f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/") if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") set(LINUX TRUE) set(SUPPORTS_X11 TRUE) + set(SUPPORTS_WAYLAND TRUE) endif() @@ -25,6 +26,9 @@ endif() if(SUPPORTS_X11) option(USE_X11 "Support X11 window system" ON) endif() +if(SUPPORTS_WAYLAND) + option(USE_WAYLAND "Support Wayland window system" OFF) +endif() if(LINUX OR ANDROID) option(USE_EGL "Support EGL OpenGL context creation" ON) endif() @@ -95,6 +99,12 @@ endif() if(USE_X11) find_package(X11 REQUIRED) endif() +if(USE_WAYLAND) + find_package(ECM REQUIRED NO_MODULE) + list(APPEND CMAKE_MODULE_PATH "${ECM_MODULE_PATH}") + find_package(Wayland REQUIRED Egl) + message(STATUS "Wayland support enabled") +endif() # Set _DEBUG macro for Debug builds. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG") diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5ca16a2f2..03e1c12cc 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -149,7 +149,7 @@ if(USE_EGL) endif() endif() -if(SUPPORTS_X11) +if(USE_X11) target_sources(common PRIVATE gl/context_glx.cpp gl/context_glx.h @@ -157,6 +157,15 @@ if(SUPPORTS_X11) target_compile_definitions(common PRIVATE "-DUSE_GLX=1") endif() +if(USE_WAYLAND) + target_sources(common PRIVATE + gl/context_egl_wayland.cpp + gl/context_egl_wayland.h + ) + target_compile_definitions(common PRIVATE "-DUSE_WAYLAND=1") + target_link_libraries(common PRIVATE Wayland::Egl) +endif() + if(APPLE) target_sources(common PRIVATE gl/context_agl.mm diff --git a/src/common/gl/context.cpp b/src/common/gl/context.cpp index 1c6fdbd1a..9298045c6 100644 --- a/src/common/gl/context.cpp +++ b/src/common/gl/context.cpp @@ -18,6 +18,8 @@ Log_SetChannel(GL::Context); #ifdef USE_EGL #if defined(USE_X11) #include "context_egl_x11.h" +#elif defined(USE_WAYLAND) +#include "context_egl_wayland.h" #elif defined(ANDROID) #include "context_egl_android.h" #else @@ -78,7 +80,9 @@ std::unique_ptr Context::Create(const WindowInfo& wi, const Version #ifdef USE_EGL context = ContextEGLAndroid::Create(wi, versions_to_try, num_versions_to_try); #endif -#elif defined(USE_X11) +#endif + +#if defined(USE_X11) if (wi.type == WindowInfo::Type::X11) { #ifdef USE_EGL @@ -93,6 +97,11 @@ std::unique_ptr Context::Create(const WindowInfo& wi, const Version } #endif +#if defined(USE_WAYLAND) + if (wi.type == WindowInfo::Type::Wayland) + context = ContextEGLWayland::Create(wi, versions_to_try, num_versions_to_try); +#endif + if (!context) return nullptr; diff --git a/src/common/gl/context_egl_wayland.cpp b/src/common/gl/context_egl_wayland.cpp new file mode 100644 index 000000000..347ca6f2c --- /dev/null +++ b/src/common/gl/context_egl_wayland.cpp @@ -0,0 +1,40 @@ +#include "context_egl_wayland.h" +#include "../log.h" +#include +Log_SetChannel(GL::ContextEGLWayland); + +namespace GL { +ContextEGLWayland::ContextEGLWayland(const WindowInfo& wi) : ContextEGL(wi) {} +ContextEGLWayland::~ContextEGLWayland() = default; + +std::unique_ptr ContextEGLWayland::Create(const WindowInfo& wi, const Version* versions_to_try, + size_t num_versions_to_try) +{ + std::unique_ptr context = std::make_unique(wi); + if (!context->Initialize(versions_to_try, num_versions_to_try)) + return nullptr; + + return context; +} + +std::unique_ptr ContextEGLWayland::CreateSharedContext(const WindowInfo& wi) +{ + std::unique_ptr context = std::make_unique(wi); + context->m_display = m_display; + + if (!context->CreateContextAndSurface(m_version, m_context, false)) + return nullptr; + + return context; +} + +EGLNativeWindowType ContextEGLWayland::GetNativeWindow(EGLConfig config) +{ + wl_egl_window* window = + wl_egl_window_create(static_cast(m_wi.window_handle), m_wi.surface_width, m_wi.surface_height); + if (!window) + return {}; + + return reinterpret_cast(window); +} +} // namespace GL diff --git a/src/common/gl/context_egl_wayland.h b/src/common/gl/context_egl_wayland.h new file mode 100644 index 000000000..b1d9848ec --- /dev/null +++ b/src/common/gl/context_egl_wayland.h @@ -0,0 +1,21 @@ +#pragma once +#include "context_egl.h" + +namespace GL { + +class ContextEGLWayland final : public ContextEGL +{ +public: + ContextEGLWayland(const WindowInfo& wi); + ~ContextEGLWayland() override; + + static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, + size_t num_versions_to_try); + + std::unique_ptr CreateSharedContext(const WindowInfo& wi) override; + +protected: + EGLNativeWindowType GetNativeWindow(EGLConfig config) override; +}; + +} // namespace GL