GLContext: Wayland support

This commit is contained in:
Connor McLaughlin 2020-07-07 21:40:55 +10:00
parent eab70546c8
commit 4ce5f7e802
5 changed files with 91 additions and 2 deletions

View File

@ -8,6 +8,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(LINUX TRUE) set(LINUX TRUE)
set(SUPPORTS_X11 TRUE) set(SUPPORTS_X11 TRUE)
set(SUPPORTS_WAYLAND TRUE)
endif() endif()
@ -25,6 +26,9 @@ endif()
if(SUPPORTS_X11) if(SUPPORTS_X11)
option(USE_X11 "Support X11 window system" ON) option(USE_X11 "Support X11 window system" ON)
endif() endif()
if(SUPPORTS_WAYLAND)
option(USE_WAYLAND "Support Wayland window system" OFF)
endif()
if(LINUX OR ANDROID) if(LINUX OR ANDROID)
option(USE_EGL "Support EGL OpenGL context creation" ON) option(USE_EGL "Support EGL OpenGL context creation" ON)
endif() endif()
@ -95,6 +99,12 @@ endif()
if(USE_X11) if(USE_X11)
find_package(X11 REQUIRED) find_package(X11 REQUIRED)
endif() 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 _DEBUG macro for Debug builds.
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")

View File

@ -149,7 +149,7 @@ if(USE_EGL)
endif() endif()
endif() endif()
if(SUPPORTS_X11) if(USE_X11)
target_sources(common PRIVATE target_sources(common PRIVATE
gl/context_glx.cpp gl/context_glx.cpp
gl/context_glx.h gl/context_glx.h
@ -157,6 +157,15 @@ if(SUPPORTS_X11)
target_compile_definitions(common PRIVATE "-DUSE_GLX=1") target_compile_definitions(common PRIVATE "-DUSE_GLX=1")
endif() 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) if(APPLE)
target_sources(common PRIVATE target_sources(common PRIVATE
gl/context_agl.mm gl/context_agl.mm

View File

@ -18,6 +18,8 @@ Log_SetChannel(GL::Context);
#ifdef USE_EGL #ifdef USE_EGL
#if defined(USE_X11) #if defined(USE_X11)
#include "context_egl_x11.h" #include "context_egl_x11.h"
#elif defined(USE_WAYLAND)
#include "context_egl_wayland.h"
#elif defined(ANDROID) #elif defined(ANDROID)
#include "context_egl_android.h" #include "context_egl_android.h"
#else #else
@ -78,7 +80,9 @@ std::unique_ptr<GL::Context> Context::Create(const WindowInfo& wi, const Version
#ifdef USE_EGL #ifdef USE_EGL
context = ContextEGLAndroid::Create(wi, versions_to_try, num_versions_to_try); context = ContextEGLAndroid::Create(wi, versions_to_try, num_versions_to_try);
#endif #endif
#elif defined(USE_X11) #endif
#if defined(USE_X11)
if (wi.type == WindowInfo::Type::X11) if (wi.type == WindowInfo::Type::X11)
{ {
#ifdef USE_EGL #ifdef USE_EGL
@ -93,6 +97,11 @@ std::unique_ptr<GL::Context> Context::Create(const WindowInfo& wi, const Version
} }
#endif #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) if (!context)
return nullptr; return nullptr;

View File

@ -0,0 +1,40 @@
#include "context_egl_wayland.h"
#include "../log.h"
#include <wayland-egl.h>
Log_SetChannel(GL::ContextEGLWayland);
namespace GL {
ContextEGLWayland::ContextEGLWayland(const WindowInfo& wi) : ContextEGL(wi) {}
ContextEGLWayland::~ContextEGLWayland() = default;
std::unique_ptr<Context> ContextEGLWayland::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
{
std::unique_ptr<ContextEGLWayland> context = std::make_unique<ContextEGLWayland>(wi);
if (!context->Initialize(versions_to_try, num_versions_to_try))
return nullptr;
return context;
}
std::unique_ptr<Context> ContextEGLWayland::CreateSharedContext(const WindowInfo& wi)
{
std::unique_ptr<ContextEGLWayland> context = std::make_unique<ContextEGLWayland>(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<wl_surface*>(m_wi.window_handle), m_wi.surface_width, m_wi.surface_height);
if (!window)
return {};
return reinterpret_cast<EGLNativeWindowType>(window);
}
} // namespace GL

View File

@ -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<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
std::unique_ptr<Context> CreateSharedContext(const WindowInfo& wi) override;
protected:
EGLNativeWindowType GetNativeWindow(EGLConfig config) override;
};
} // namespace GL