Skip to content

Commit

Permalink
GLContext: Wayland support
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 10, 2020
1 parent eab7054 commit 4ce5f7e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 10 additions & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,23 @@ if(USE_EGL)
endif()
endif()

if(SUPPORTS_X11)
if(USE_X11)
target_sources(common PRIVATE
gl/context_glx.cpp
gl/context_glx.h
)
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
Expand Down
11 changes: 10 additions & 1 deletion src/common/gl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,7 +80,9 @@ std::unique_ptr<GL::Context> 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
Expand All @@ -93,6 +97,11 @@ std::unique_ptr<GL::Context> 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;

Expand Down
40 changes: 40 additions & 0 deletions src/common/gl/context_egl_wayland.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions src/common/gl/context_egl_wayland.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4ce5f7e

Please sign in to comment.