Intial Wayland EGL integration.

This commit is contained in:
James Grogan 2022-11-11 09:14:41 +00:00
parent a4d3019f04
commit 6af296409a
20 changed files with 278 additions and 44 deletions

View file

@ -51,7 +51,7 @@ In `xdg-shell-protocol.cpp` - we need access to `xdg_wm_base_interface` so expor
#### 3D Rendering
```bash
sudo apt-get install libgl-dev
sudo apt-get install libgl-dev libegl1-mesa-dev
```
#### fonts

View file

@ -84,13 +84,13 @@ void GuiApplication::Run()
#ifdef __linux__
mDesktopManager->SetKeyboard(XcbKeyboard::Create());
bool useOpenGl = false;
bool useHardwareRendering = false;
XcbInterface window_interface;
window_interface.SetUseOpenGl(useOpenGl);
window_interface.setUseHardwareRendering(useHardwareRendering);
window_interface.initialize(mDesktopManager.get());
window_interface.addWindow(mainWindow, mDesktopManager.get());
window_interface.showWindow(mainWindow);
if (useOpenGl)
if (useHardwareRendering)
{
window_interface.CreateOpenGlDrawable(mainWindow);
}

View file

@ -18,6 +18,7 @@ list(APPEND graphics_HEADERS
INativeDrawingContext.h
)
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL QUIET)
if (OpenGL_FOUND)
list(APPEND platform_LIBS OpenGL::GL)

View file

@ -40,8 +40,16 @@ if(UNIX)
ui_interfaces/wayland/WaylandPointerInterface.cpp
ui_interfaces/wayland/WaylandSeatInterface.cpp
ui_interfaces/wayland/WaylandKeyboard.cpp
ui_interfaces/wayland/WaylandEglInterface.cpp
ui_interfaces/wayland/WaylandEglWindowInterface.cpp
${WAYLAND_EXTENSIONS_INCLUDE_DIR}/xdg-shell-protocol.cpp
)
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL QUIET)
if (OpenGL_FOUND)
list(APPEND platform_LIBS OpenGL::EGL)
endif()
set(_HAS_WAYLAND ON)
#else()
#message(STATUS "Wayland Extensions Header not found - not building Wayland support")
@ -87,7 +95,7 @@ target_include_directories(windows PUBLIC
${X11_INCLUDE_DIRS}
)
target_link_libraries(windows PUBLIC ${platform_LIBS} core geometry graphics ui_elements ${WAYLAND_CLIENT_LIBRARY})
target_link_libraries(windows PUBLIC ${platform_LIBS} core geometry graphics ui_elements ${WAYLAND_CLIENT_LIBRARY} wayland-egl)
target_compile_options(windows PRIVATE -Wno-attributes) # From xdg shell autogen code
set_property(TARGET windows PROPERTY FOLDER src)

View file

@ -20,4 +20,12 @@ public:
virtual void showWindow(mt::Window* window) = 0;
virtual void addWindow(mt::Window* window, DesktopManager* desktopManager) = 0;
void setUseHardwareRendering(bool useHardware)
{
mUseHardwareRendering = useHardware;
}
protected:
bool mUseHardwareRendering{false};
};

View file

@ -0,0 +1,89 @@
#pragma once
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
class WaylandEglInterface
{
public:
void initialize(wl_display* display)
{
mEglDisplay = eglGetDisplay((EGLNativeDisplayType) display);
if (mEglDisplay == EGL_NO_DISPLAY)
{
fprintf(stderr, "Can't create egl display\n");
return;
}
else
{
fprintf(stderr, "Created egl display\n");
}
EGLint major{0};
EGLint minor{0};
if (eglInitialize(mEglDisplay, &major, &minor) != EGL_TRUE)
{
fprintf(stderr, "Can't initialise egl display\n");
return;
}
printf("EGL major: %d, minor %d\n", major, minor);
EGLint count{0};
eglGetConfigs(mEglDisplay, nullptr, 0, &count);
printf("EGL has %d configs\n", count);
EGLConfig* configs;
configs = static_cast<EGLConfig*>(calloc(count, sizeof* configs));
EGLint n{0};
EGLint config_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
eglChooseConfig(mEglDisplay, config_attribs, configs, count, &n);
EGLint size{0};
for (int i = 0; i < n; i++)
{
eglGetConfigAttrib(mEglDisplay, configs[i], EGL_BUFFER_SIZE, &size);
printf("Buffer size for config %d is %d\n", i, size);
eglGetConfigAttrib(mEglDisplay, configs[i], EGL_RED_SIZE, &size);
printf("Red size for config %d is %d\n", i, size);
// just choose the first one
mEglConfig = configs[i];
break;
}
static const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, context_attribs);
}
EGLDisplay getDisplay() const
{
return mEglDisplay;
}
EGLConfig getConfig() const
{
return mEglConfig;
}
EGLContext getContext() const
{
return mEglContext;
}
private:
EGLDisplay mEglDisplay;
EGLConfig mEglConfig;
EGLContext mEglContext;
};

View file

@ -0,0 +1,108 @@
#pragma once
#include "WaylandEglInterface.h"
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
class WaylandEglWindowInterface
{
public:
WaylandEglWindowInterface(WaylandEglInterface* eglInterface)
: mEglInterface(eglInterface)
{
}
void initialize(wl_surface* surface, int width, int height)
{
if (mEglSurface)
{
return;
}
mEglWindow = wl_egl_window_create(surface, width, height);
if (mEglWindow == EGL_NO_SURFACE)
{
fprintf(stderr, "Can't create egl window\n");
return;
}
else
{
fprintf(stderr, "Created egl window\n");
}
mEglSurface = eglCreateWindowSurface(mEglInterface->getDisplay(), mEglInterface->getConfig(), mEglWindow, nullptr);
if (!mEglSurface)
{
switch(eglGetError())
{
//case EGL_NO_SURFACE:
//fprintf(stderr, "EGL_NO_SURFACE\n");
//break;
case EGL_BAD_DISPLAY:
fprintf(stderr, "EGL_BAD_DISPLAY\n");
break;
case EGL_NOT_INITIALIZED:
fprintf(stderr, "EGL_NOT_INITIALIZED\n");
break;
case EGL_BAD_CONFIG:
fprintf(stderr, "EGL_BAD_CONFIG\n");
break;
case EGL_BAD_NATIVE_WINDOW:
fprintf(stderr, "EGL_BAD_NATIVE_WINDOW\n");
break;
case EGL_BAD_ATTRIBUTE:
fprintf(stderr, "EGL_BAD_ATTRIBUTE\n");
break;
case EGL_BAD_ALLOC:
fprintf(stderr, "EGL_BAD_ALLOC\n");
break;
case EGL_BAD_MATCH:
fprintf(stderr, "EGL_BAD_MATCH\n");
break;
default:
fprintf(stderr, "Unknown EGL error\n");
}
fprintf(stderr, "Created surface failed\n");
}
}
void draw()
{
if (!mEglSurface)
{
return;
}
if (eglMakeCurrent(mEglInterface->getDisplay(), mEglSurface, mEglSurface, mEglInterface->getContext()))
{
fprintf(stderr, "Made current\n");
}
else {
fprintf(stderr, "Made current failed\n");
}
glClearColor(1.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
if (eglSwapBuffers(mEglInterface->getDisplay(), mEglSurface))
{
fprintf(stderr, "Swapped buffers\n");
}
else {
fprintf(stderr, "Swapped buffers failed\n");
}
}
private:
wl_egl_window* mEglWindow{nullptr};
EGLSurface mEglSurface;
WaylandEglInterface* mEglInterface;
};

View file

@ -25,7 +25,7 @@ void WaylandKeyboard::keyboardPressedEvent(void *data, struct wl_keyboard *wl_ke
static void wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface)
{
fprintf(stderr, "keyboard leave\n");
}
static void wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay)
@ -46,8 +46,6 @@ void WaylandKeyboard::onKeyboardModifier(uint32_t mods_depressed, uint32_t mods_
void WaylandKeyboard::onEnterEvent(struct wl_array *keys)
{
MLOG_INFO("keyboard enter; keys pressed are:");
void* key;
wl_array_for_each(key, keys)
{
@ -56,9 +54,9 @@ void WaylandKeyboard::onEnterEvent(struct wl_array *keys)
char buf[128];
xkb_keysym_t sym = xkb_state_key_get_one_sym(mXkbState, *key_type + 8);
xkb_keysym_get_name(sym, buf, sizeof(buf));
fprintf(stderr, "sym: %-12s (%d), ", buf, sym);
//fprintf(stderr, "sym: %-12s (%d), ", buf, sym);
xkb_state_key_get_utf8(mXkbState, *key_type + 8, buf, sizeof(buf));
fprintf(stderr, "utf8: '%s'\n", buf);
//fprintf(stderr, "utf8: '%s'\n", buf);
}
}
@ -69,9 +67,9 @@ void WaylandKeyboard::onPressedEvent(uint32_t key, uint32_t state)
xkb_keysym_t sym = xkb_state_key_get_one_sym(mXkbState, keycode);
xkb_keysym_get_name(sym, buf, sizeof(buf));
const char *action = state == WL_KEYBOARD_KEY_STATE_PRESSED ? "press" : "release";
fprintf(stderr, "key %s: sym: %-12s (%d), ", action, buf, sym);
//fprintf(stderr, "key %s: sym: %-12s (%d), ", action, buf, sym);
xkb_state_key_get_utf8(mXkbState, keycode, buf, sizeof(buf));
fprintf(stderr, "utf8: '%s'\n", buf);
//fprintf(stderr, "utf8: '%s'\n", buf);
}
void WaylandKeyboard::onKeymapEvent(struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size)

View file

@ -63,11 +63,10 @@ void WaylandPointerInterface::onPointerFrame()
{
const auto locx = wl_fixed_to_double(mWorkingPointerEvent.surface_x);
const auto locy = wl_fixed_to_double(mWorkingPointerEvent.surface_y);
MLOG_INFO("Enter at " << locx << " , " << locy);
}
else if (mWorkingPointerEvent.event_mask & POINTER_EVENT_LEAVE)
{
MLOG_INFO("Leave);")
}
else if (mWorkingPointerEvent.event_mask & POINTER_EVENT_MOTION)
{
@ -76,7 +75,6 @@ void WaylandPointerInterface::onPointerFrame()
else if (mWorkingPointerEvent.event_mask & POINTER_EVENT_BUTTON)
{
const bool released = mWorkingPointerEvent.state == WL_POINTER_BUTTON_STATE_RELEASED;
MLOG_INFO("Mouse button" << released ? "released" : "pressed");
}
mWorkingPointerEvent = WaylandPointerEvent();
}

View file

@ -1,5 +1,7 @@
#include "WaylandSurface.h"
#include "WaylandEglWindowInterface.h"
WaylandSurface::WaylandSurface(mt::Window* window)
: mWindow(window)
{
@ -11,9 +13,13 @@ WaylandSurface::~WaylandSurface()
}
void WaylandSurface::initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer)
void WaylandSurface::initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer, WaylandEglInterface* eglInterface)
{
mBuffer = buffer;
if (eglInterface)
{
mEglWindowInterface = std::make_unique<WaylandEglWindowInterface>(eglInterface);
}
mSurface = wl_compositor_create_surface(compositor);
mXdgSurface = xdg_wm_base_get_xdg_surface(xdg_wm_base, mSurface);
@ -31,16 +37,28 @@ void WaylandSurface::initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_b
xdg_toplevel_set_title(mXdgTopLevel, "Example client");
wl_surface_commit(mSurface);
auto region = wl_compositor_create_region(compositor);
wl_region_add(region, 0, 0, mWindow->GetWidth(), mWindow->GetHeight());
wl_surface_set_opaque_region(mSurface, region);
}
void WaylandSurface::onConfigure(xdg_surface *xdg_surface, uint32_t serial)
{
xdg_surface_ack_configure(xdg_surface, serial);
auto buffer = drawFrame();
wl_surface_attach(mSurface, buffer, 0, 0);
//wl_surface_damage(mSurface, 0, 0, UINT32_MAX, UINT32_MAX);
wl_surface_commit(mSurface);
if (mEglWindowInterface)
{
mEglWindowInterface->initialize(mSurface, mWindow->GetWidth(), mWindow->GetHeight());
mEglWindowInterface->draw();
}
else
{
auto buffer = drawFrame();
wl_surface_attach(mSurface, buffer, 0, 0);
//wl_surface_damage(mSurface, 0, 0, UINT32_MAX, UINT32_MAX);
wl_surface_commit(mSurface);
}
}
wl_buffer* WaylandSurface::drawFrame()

View file

@ -5,12 +5,15 @@
#include "Window.h"
#include "SharedMemory.h"
#include <WaylandBuffer.h>
#include "WaylandBuffer.h"
struct wl_surface;
struct xdg_surface;
struct xdg_toplevel;
class WaylandEglInterface;
class WaylandEglWindowInterface;
class WaylandSurface
{
@ -19,7 +22,7 @@ public:
~WaylandSurface();
void initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer);
void initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer, WaylandEglInterface* eglInterface);
void onConfigure(xdg_surface *xdg_surface, uint32_t serial);
@ -37,6 +40,7 @@ private:
xdg_toplevel* mXdgTopLevel{nullptr};
std::shared_ptr<WaylandBuffer> mBuffer;
std::unique_ptr<WaylandEglWindowInterface> mEglWindowInterface{nullptr};
};
using WaylandSurfacePtr = std::unique_ptr<WaylandSurface>;

View file

@ -4,6 +4,7 @@
#include "WaylandSurface.h"
#include "WaylandBuffer.h"
#include "WaylandSeatInterface.h"
#include "WaylandEglInterface.h"
#include <cstring>
#include <sstream>
@ -72,6 +73,12 @@ void WaylandWindowInterface::initialize(DesktopManager* desktopManager)
wl_registry_add_listener(registry, &mRegistryListener, this);
wl_display_roundtrip(mDisplay);
if (mUseHardwareRendering)
{
mEglInterface = std::make_unique<WaylandEglInterface>();
mEglInterface->initialize(mDisplay);
}
}
void WaylandWindowInterface::setSeat(wl_seat* seat)
@ -119,7 +126,7 @@ void WaylandWindowInterface::showWindow(mt::Window* window)
return;
}
mSurfaces[0]->initialize(mCompositor, mXdgBase, mBuffer);
mSurfaces[0]->initialize(mCompositor, mXdgBase, mBuffer, mEglInterface.get());
}
void WaylandWindowInterface::shutDown()

View file

@ -13,6 +13,7 @@
class WaylandSurface;
class WaylandBuffer;
class WaylandSeatInterface;
class WaylandEglInterface;
class WaylandWindowInterface : public AbstractUIInterface
{
@ -56,4 +57,5 @@ private:
std::vector<std::unique_ptr<WaylandSurface> > mSurfaces;
std::shared_ptr<WaylandBuffer> mBuffer;
std::unique_ptr<WaylandSeatInterface> mSeatInterface;
std::unique_ptr<WaylandEglInterface> mEglInterface;
};

View file

@ -21,12 +21,11 @@
XcbInterface::XcbInterface()
: mUseOpenGl(true),
mConnection(nullptr),
mX11Display(),
mGlxInterface(),
mXcbEventInterface(XcbEventInterface::Create()),
mXcbWindowInterface(XcbWindowInterface::Create())
: mConnection(nullptr),
mX11Display(),
mGlxInterface(),
mXcbEventInterface(XcbEventInterface::Create()),
mXcbWindowInterface(XcbWindowInterface::Create())
{
}
@ -36,17 +35,12 @@ XcbInterface::~XcbInterface()
}
void XcbInterface::SetUseOpenGl(bool use)
{
mUseOpenGl = use;
}
void XcbInterface::initialize(DesktopManager* desktopManager)
{
Connect();
UpdateScreen(desktopManager);
CreateGraphicsContext(desktopManager);
if (mUseOpenGl)
if (mUseHardwareRendering)
{
InitializeOpenGl();
}
@ -54,7 +48,7 @@ void XcbInterface::initialize(DesktopManager* desktopManager)
void XcbInterface::Connect()
{
if (mUseOpenGl)
if (mUseHardwareRendering)
{
mX11Display = XOpenDisplay(0);
if (!mX11Display)
@ -84,7 +78,7 @@ void XcbInterface::UpdateScreen(DesktopManager* desktopManager)
return;
}
auto screen_iter = xcb_setup_roots_iterator(xcb_get_setup(mConnection));
if (mUseOpenGl)
if (mUseHardwareRendering)
{
if (!mX11Display)
{
@ -109,7 +103,7 @@ void XcbInterface::InitializeOpenGl()
void XcbInterface::CreateOpenGlDrawable(mt::Window* window)
{
if (!mUseOpenGl or !window or !window->GetPlatformWindow())
if (!mUseHardwareRendering or !window or !window->GetPlatformWindow())
{
return;
}
@ -181,7 +175,7 @@ void XcbInterface::onPaint(DesktopManager* desktopManager)
void XcbInterface::OnExposeEvent(xcb_expose_event_t* event, DesktopManager* desktopManager)
{
// Update the window
if (mUseOpenGl)
if (mUseHardwareRendering)
{
mGlxInterface->Draw();
mGlxInterface->SwapBuffers(mX11Display);
@ -247,7 +241,7 @@ void XcbInterface::loop(DesktopManager* desktopManager)
void XcbInterface::OnLoopCompleted(DesktopManager* desktopManagers)
{
if (mUseOpenGl)
if (mUseHardwareRendering)
{
mGlxInterface->DestroyWindow(mX11Display);
mGlxInterface->DestroyContext(mX11Display);

View file

@ -31,8 +31,6 @@ public:
~XcbInterface();
void SetUseOpenGl(bool use);
void initialize(DesktopManager* desktopManager) override;
void loop(DesktopManager* desktopManager) override;
@ -66,7 +64,6 @@ private:
void OnLoopCompleted(DesktopManager* desktopManagers);
private:
bool mUseOpenGl {false};
xcb_connection_t* mConnection;
_XDisplay* mX11Display;
GlxInterfacePtr mGlxInterface;

View file

@ -38,6 +38,7 @@ list(APPEND TestFiles
)
endif()
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL QUIET)
if (OpenGL_FOUND)
list(APPEND TestFiles

View file

@ -20,7 +20,7 @@ public:
bool useOpenGl = true;
XcbInterface window_interface;
window_interface.SetUseOpenGl(true);
window_interface.setUseHardwareRendering(true);
window_interface.initialize(desktopManager.get());
window_interface.addWindow(mainWindow, desktopManager.get());
window_interface.showWindow(mainWindow);

View file

@ -12,6 +12,7 @@ int main()
auto desktop_manager = std::make_unique<DesktopManager>();
WaylandWindowInterface window_interface;
window_interface.setUseHardwareRendering(true);
window_interface.initialize(desktop_manager.get());
auto window = mt::Window::Create();