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

Replace GLX with EGL for X11 #389

Merged
merged 10 commits into from
May 30, 2024
84 changes: 84 additions & 0 deletions cmake/Findepoxy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: 2014 Fredrik Höglund <[email protected]>
# SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause

#[=======================================================================[.rst:
Findepoxy
---------

Try to find libepoxy on a Unix system.

This will define the following variables:

``epoxy_FOUND``
True if (the requested version of) libepoxy is available
``epoxy_VERSION``
The version of libepoxy
``epoxy_LIBRARIES``
This should be passed to target_link_libraries() if the target is not
used for linking
``epoxy_INCLUDE_DIRS``
This should be passed to target_include_directories() if the target is not
used for linking
``epoxy_DEFINITIONS``
This should be passed to target_compile_options() if the target is not
used for linking
``epoxy_HAS_GLX``
True if GLX support is available

If ``epoxy_FOUND`` is TRUE, it will also define the following imported target:

``epoxy::epoxy``
The epoxy library

In general we recommend using the imported target, as it is easier to use.
Bear in mind, however, that if the target is in the link interface of an
exported library, it must be made available by the package config file.
#]=======================================================================]

find_package(PkgConfig QUIET)
pkg_check_modules(PKG_epoxy QUIET epoxy)

set(epoxy_VERSION ${PKG_epoxy_VERSION})
set(epoxy_DEFINITIONS ${PKG_epoxy_CFLAGS})

find_path(epoxy_INCLUDE_DIRS
NAMES epoxy/gl.h
HINTS ${PKG_epoxy_INCLUDEDIR} ${PKG_epoxy_INCLUDE_DIRS}
)
find_library(epoxy_LIBRARIES
NAMES epoxy
HINTS ${PKG_epoxy_LIBDIR} ${PKG_epoxy_LIBRARY_DIRS}
)
find_file(epoxy_GLX_HEADER NAMES epoxy/glx.h HINTS ${epoxy_INCLUDE_DIR})

if (epoxy_GLX_HEADER STREQUAL "epoxy_GLX_HEADER-NOTFOUND")
set(epoxy_HAS_GLX FALSE CACHE BOOL "whether glx is available")
else ()
set(epoxy_HAS_GLX TRUE CACHE BOOL "whether glx is available")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(epoxy
FOUND_VAR epoxy_FOUND
REQUIRED_VARS epoxy_LIBRARIES epoxy_INCLUDE_DIRS
VERSION_VAR epoxy_VERSION
)

if (epoxy_FOUND AND NOT TARGET epoxy::epoxy)
add_library(epoxy::epoxy UNKNOWN IMPORTED)
set_target_properties(epoxy::epoxy PROPERTIES
IMPORTED_LOCATION "${epoxy_LIBRARIES}"
INTERFACE_COMPILE_OPTIONS "${epoxy_DEFINITIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${epoxy_INCLUDE_DIRS}"
)
endif()

mark_as_advanced(
epoxy_DEFINITIONS
epoxy_HAS_GLX
epoxy_INCLUDE_DIRS
epoxy_LIBRARIES
epoxy_VERSION
)
2 changes: 2 additions & 0 deletions components/pango_display/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ WindowInterface& CreateWindowAndBind(std::string window_title, int w, int h, con
}

context->MakeCurrent();
#ifdef HAVE_GLEW
glewInit();
#endif

// And finally process pending window events (such as resize) now that we've setup our callbacks.
context->window->ProcessEvents();
Expand Down
28 changes: 17 additions & 11 deletions components/pango_opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PRIVATE

find_package(Eigen3 REQUIRED NO_MODULE)
message(STATUS "Found Eigen: '${EIGEN3_INCLUDE_DIRS}'")
target_compile_definitions(${COMPONENT} PUBLIC HAVE_EIGEN HAVE_GLEW)
target_compile_definitions(${COMPONENT} PUBLIC HAVE_EIGEN)

set_target_properties(
${COMPONENT} PROPERTIES VERSION ${PANGOLIN_VERSION} SOVERSION ${PANGOLIN_VERSION_MAJOR}
Expand All @@ -40,19 +40,25 @@ install(DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include"
)

if(EMSCRIPTEN)
target_compile_definitions(${COMPONENT} PUBLIC HAVE_GLES HAVE_GLES_2)
target_compile_definitions(${COMPONENT} PUBLIC HAVE_GLES HAVE_GLES_2 HAVE_GLEW)
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/compat/gl2engine.cpp)
else()
if(_LINUX_)
# EGL specific
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL REQUIRED COMPONENTS OpenGL EGL)
find_package(epoxy REQUIRED)
target_link_libraries(${COMPONENT} PUBLIC epoxy::epoxy OpenGL::EGL)
target_include_directories(${COMPONENT} PUBLIC $<BUILD_INTERFACE:${OPENGL_EGL_INCLUDE_DIRS}>)
target_compile_definitions(${COMPONENT} PUBLIC HAVE_EPOXY)
else()
# OpenGL defaults
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
target_include_directories(${COMPONENT} PUBLIC $<BUILD_INTERFACE:${GLEW_INCLUDE_DIR}>)
target_link_libraries(${COMPONENT} PUBLIC ${GLEW_LIBRARY})
target_link_libraries(${COMPONENT} PUBLIC ${OPENGL_LIBRARIES})
target_include_directories(${COMPONENT} PUBLIC $<BUILD_INTERFACE:${OPENGL_INCLUDE_DIR}>)
target_compile_definitions(${COMPONENT} PUBLIC HAVE_GLEW)
endif()
find_package(OpenGL REQUIRED QUIET)
find_package(GLEW REQUIRED QUIET)
target_include_directories( ${COMPONENT} PUBLIC
$<BUILD_INTERFACE:${OPENGL_INCLUDE_DIR}>
$<BUILD_INTERFACE:${GLEW_INCLUDE_DIR}>
)
target_link_libraries( ${COMPONENT} PUBLIC
${GLEW_LIBRARY} ${OPENGL_LIBRARIES}
)
endif()
4 changes: 3 additions & 1 deletion components/pango_opengl/include/pangolin/gl/glplatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
#undef ERROR
#endif

#ifdef HAVE_GLEW
#if defined(HAVE_GLEW)
#include <GL/glew.h>
#elif defined(HAVE_EPOXY)
#include <epoxy/gl.h>
#endif

#ifdef HAVE_GLES
Expand Down
4 changes: 3 additions & 1 deletion components/pango_windowing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ elseif(EMSCRIPTEN)
PangolinRegisterFactory(WindowInterface EmscriptenWindow)
else()
find_package(X11 QUIET)
if(X11_FOUND)
find_package(OpenGL QUIET COMPONENTS EGL)
if(X11_FOUND AND OpenGL_EGL_FOUND)
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_x11.cpp )
target_link_libraries(${COMPONENT} PRIVATE ${X11_LIBRARIES} )
target_include_directories(${COMPONENT} PRIVATE ${X11_INCLUDE_DIR} )
target_link_libraries(${COMPONENT} PRIVATE OpenGL::EGL)
PangolinRegisterFactory(WindowInterface X11Window)
endif()

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <string>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glx.h>
#include <EGL/egl.h>

namespace pangolin
{
Expand All @@ -59,22 +59,23 @@ struct X11Display

struct X11GlContext : public GlContextInterface
{
X11GlContext(std::shared_ptr<X11Display> &d, ::GLXFBConfig chosenFbc, std::shared_ptr<X11GlContext> shared_context = std::shared_ptr<X11GlContext>() );
X11GlContext(std::shared_ptr<X11Display> &d);
~X11GlContext();

std::shared_ptr<X11Display> display;

std::shared_ptr<X11GlContext> shared_context;

// Owns the OpenGl Context
::GLXContext glcontext;
EGLContext egl_context;
EGLDisplay egl_display;
EGLConfig egl_config;
EGLSurface egl_surface;
};

struct X11Window : public WindowInterface
{
X11Window(
const std::string& title, int width, int height,
std::shared_ptr<X11Display>& display, ::GLXFBConfig chosenFbc
std::shared_ptr<X11Display>& display, std::shared_ptr<X11GlContext> newglcontext
);

~X11Window();
Expand All @@ -85,7 +86,7 @@ struct X11Window : public WindowInterface

void Resize(unsigned int w, unsigned int h) override;

void MakeCurrent(GLXContext ctx);
void MakeCurrent(EGLContext ctx);

void MakeCurrent() override;

Expand Down
4 changes: 2 additions & 2 deletions components/pango_windowing/src/display_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,6 @@ static const struct wl_registry_listener wregistry_listener = {
};

WaylandDisplay::WaylandDisplay() {
xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);

wdisplay = wl_display_connect(nullptr);
if (wdisplay == nullptr) {
throw std::runtime_error("Cannot connect to Wayland compositor!");
Expand All @@ -865,6 +863,8 @@ WaylandDisplay::WaylandDisplay() {

wl_display_roundtrip(wdisplay);

xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);

egl_display = eglGetDisplay((EGLNativeDisplayType)wdisplay);
if(!egl_display) {
std::cerr << "Failed to open EGL display" << std::endl;
Expand Down
Loading
Loading