- Setup EGL context for Wayland. Test window now opens as black rectangle.

This commit is contained in:
2025-12-15 13:20:08 -05:00
parent 5dcb58f53c
commit 1acf00138a
36 changed files with 992 additions and 80 deletions

View File

@@ -46,7 +46,7 @@ void platform::initialize() {
}
void platform::shutdown() {
display.reset();
}
}

View File

@@ -30,6 +30,7 @@
#include <fennec/platform/interface/window.h>
#include <fennec/platform/interface/display_server.h>
#include <fennec/renderers/interface/gfxsurface.h>
namespace fennec
{
@@ -38,4 +39,12 @@ window* window::get_parent() const {
return server->get_window(cfg.parent);
}
void window::begin_frame() {
gfx_surface->make_current();
}
void window::end_frame() {
gfx_surface->swap();
}
}

View File

@@ -0,0 +1,38 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <fennec/platform/linux/wayland/window.h>
#include <fennec/platform/linux/wayland/egl/context.h>
#include <fennec/platform/linux/wayland/egl/surface.h>
namespace fennec
{
wayland_eglcontext::wayland_eglcontext(display_server* display)
: eglcontext(display) {
}
wayland_eglcontext::~wayland_eglcontext() {
}
gfxsurface* wayland_eglcontext::create_surface(window* window) {
return new wayland_eglsurface(static_cast<wayland_window*>(window), this);
}
}

View File

@@ -0,0 +1,46 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <fennec/platform/linux/wayland/window.h>
#include <fennec/platform/linux/wayland/egl/surface.h>
#include <fennec/platform/linux/wayland/lib/wayland.h>
namespace fennec
{
wayland_eglsurface::wayland_eglsurface(wayland_window* win, eglcontext* ctx)
: eglsurface(win, ctx,
reinterpret_cast<EGLNativeWindowType>(
wl_egl_window_create(
static_cast<wl_surface*>(win->get_native_handle()), win->get_size().x, win->get_size().y
)
)
) {
}
wayland_eglsurface::~wayland_eglsurface() {
wl_egl_window_destroy(reinterpret_cast<wl_egl_window*>(_eglwindow));
}
void wayland_eglsurface::resize(const ivec2& size) {
wl_egl_window_resize(reinterpret_cast<wl_egl_window*>(_eglwindow), size.x, size.y, 0, 0);
}
}

View File

@@ -16,30 +16,19 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file wayland_server.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#include <fennec/core/logger.h>
#include <fennec/platform/linux/wayland/server.h>
#include <fennec/platform/linux/wayland/window.h>
#include <fennec/platform/linux/wayland/lib/loader.h>
#include <fennec/platform/linux/wayland/lib/wayland.h>
#include <fennec/platform/linux/wayland/lib/headers/xdg-shell-client-protocols.h>
#include <fennec/renderers/interface/gfxcontext.h>
namespace fennec
{
wayland_server::wayland_server(fennec::platform* p)
: display_server(p)
: display_server_base(p)
, display(nullptr), registry(nullptr), compositor(nullptr), seat(nullptr), fifo(false) {
// load shared lib
@@ -91,6 +80,22 @@ void wayland_server::connect() {
disconnect();
return;
}
auto contexts = ctx_registry::get_type_list();
while (not contexts.empty()) {
const auto& ctx = contexts.front();
gfx_context = ctx.ctor(this);
if (gfx_context->is_valid()) {
break;
} else {
delete gfx_context;
gfx_context = nullptr;
contexts.pop();
}
}
assertf(gfx_context != nullptr, "Failed to create graphics context.");
}
void wayland_server::disconnect() {
@@ -100,6 +105,10 @@ void wayland_server::disconnect() {
return;
}
// delete the gfx context
delete gfx_context;
// check compositor
if (compositor) {
wl_compositor_destroy(compositor);
@@ -115,7 +124,8 @@ void wayland_server::disconnect() {
wl_display_disconnect(display);
// clear vars
display = nullptr;
gfx_context = nullptr;
display = nullptr;
registry = nullptr;
compositor = nullptr;
seat = nullptr;
@@ -126,6 +136,10 @@ bool wayland_server::connected() const {
return display != nullptr;
}
void wayland_server::dispatch() {
wl_display_dispatch_pending(display);
}
window* wayland_server::create_window(const window::config& conf) {
size_t id = windows.insert(new wayland_window(this, windows.next_id(), conf));
return windows[id];

View File

@@ -16,29 +16,18 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file window.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#include <fennec/platform/interface/display_server.h>
#include <fennec/platform/linux/wayland/server.h>
#include <fennec/platform/linux/wayland/window.h>
#include <fennec/platform/linux/wayland/lib/wayland.h>
#include <fennec/platform/linux/wayland/lib/headers/xdg-shell-client-protocols.h>
#include <fennec/renderers/interface/gfxcontext.h>
using namespace fennec;
wayland_window::wayland_window(display_server* server, uint32_t id, const config& cfg)
: window(server, id, cfg)
: window_base(server, id, cfg)
, surface(nullptr)
, xdgsurface(nullptr) {
@@ -99,10 +88,12 @@ void wayland_window::show() {
cfg.flags.set(flag_visible);
cfg.flags.set(flag_running);
gfx_surface = wl_server->get_gfx_context()->create_surface(this);
}
void wayland_window::hide() {
if (not is_visible()) {
if (not is_running()) {
return;
}
@@ -134,9 +125,8 @@ void wayland_window::hide() {
cfg.flags.clear(flag_running);
}
void wayland_window::dispatch() {
wayland_server* wl_server = static_cast<wayland_server*>(server);
wl_display_dispatch(wl_server->display);
void* wayland_window::get_native_handle() {
return surface;
}
bool wayland_window::set_flag(uint8_t, bool) {
@@ -156,5 +146,11 @@ void wayland_window::listen_xdg_surface_configure(void*, xdg_surface* xdg, uint3
void wayland_window::listen_xdg_toplevel_configure(void*, xdg_toplevel*, int32_t, int32_t, wl_array*) {}
void wayland_window::listen_xdg_toplevel_configure_bounds(void*, xdg_toplevel*, int32_t, int32_t) {}
void wayland_window::listen_xdg_toplevel_close(void*, xdg_toplevel*) {}
void wayland_window::listen_xdg_toplevel_close(void* data, xdg_toplevel*) {
wayland_window* window = static_cast<wayland_window*>(data);
window->hide();
}
void wayland_window::listen_xdg_toplevel_capabilities(void*, xdg_toplevel*, wl_array*) {}

View File

@@ -0,0 +1,114 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <fennec/format/format.h>
#include <GL/gl.h>
#include <fennec/platform/opengl/egl/context.h>
namespace fennec
{
static const cstring& egl_translate_type(EGLint type) {
static constexpr cstring opengl = "OpenGL";
static constexpr cstring gles = "GLES";
static constexpr cstring openvg = "OpenVG"; // this should never be used
switch (type) {
default:
case EGL_OPENGL_API: return opengl;
case EGL_OPENGL_ES_API: return gles;
case EGL_OPENVG_API: return openvg;
}
}
eglcontext::eglcontext(display_server* display)
: glcontext(display) {
EGLint config_attrs[] = {
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT, // 4
EGL_NONE
};
EGLint context_attrs[] = {
EGL_NONE
};
// Get the EGL display from the display server
_egldisplay = eglGetDisplay(display->get_native_handle());
assertf(_egldisplay, "Failed to connect to egl display.");
// Initialize EGL
assertf(eglInitialize(_egldisplay, &_eglvmajor, &_eglvminor), "Failed to initialize egl.");
// Attempt to bind to OpenGL or GLES
if (not eglBindAPI(EGL_OPENGL_API)) {
config_attrs[4] = EGL_OPENGL_ES_BIT;
assertf(eglBindAPI(EGL_OPENGL_ES_API), "Failed to find suitable OpenGL API.");
}
// Select a configuration
EGLint n;
assertf(eglChooseConfig(_egldisplay, config_attrs, &_eglconfig, 1, &n), "Failed to find suitable OpenGL configuration.");
// Create a context from the configuration and display
_eglcontext = eglCreateContext(_egldisplay, _eglconfig, EGL_NO_CONTEXT, context_attrs);
assertf(_eglcontext, "Failed to create OpenGL Context.");
// Make the created context the current context to query some info
eglMakeCurrent(_egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, _eglcontext);
// Query version information
eglQueryContext(_egldisplay, _eglcontext, EGL_CONTEXT_CLIENT_TYPE, &_eglctype);
glGetIntegerv(GL_MAJOR_VERSION, &_eglvmajor);
glGetIntegerv(GL_MINOR_VERSION, &_eglvminor);
// Set the version fields
version.major = _eglvmajor;
version.minor = _eglvminor;
version.patch = 0;
version.str = format("{} v{}.{}", egl_translate_type(_eglctype), _eglvmajor, _eglvminor);
}
eglcontext::~eglcontext() {
eglMakeCurrent(_egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(_egldisplay, _eglcontext);
eglTerminate(_egldisplay);
eglReleaseThread();
_egldisplay = nullptr;
_eglconfig = nullptr;
_eglcontext = nullptr;
_eglvmajor = 0;
_eglvminor = 0;
version.major = 0;
version.minor = 0;
version.patch = 0;
version.str = "";
}
bool eglcontext::is_valid() {
return _egldisplay != nullptr;
}
}

View File

@@ -0,0 +1,55 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <fennec/platform/opengl/egl/context.h>
#include <fennec/platform/opengl/egl/surface.h>
namespace fennec {
eglsurface::eglsurface(fennec::window* win, eglcontext* ctx, EGLNativeWindowType eglwindow)
: gfxsurface(win, ctx)
, _eglwindow(eglwindow)
, _eglsurface(nullptr) {
assertf(_eglwindow, "Failed to acquire EGL window!");
_eglsurface = eglCreateWindowSurface(
ctx->_egldisplay,
ctx->_eglconfig,
_eglwindow,
nullptr
);
assertf(_eglsurface, "Failed to create EGL surface!");
}
eglsurface::~eglsurface() {
eglcontext* ctx = static_cast<eglcontext*>(context);
eglDestroySurface(ctx->_egldisplay, _eglsurface);
}
void eglsurface::make_current() {
eglcontext* ctx = static_cast<eglcontext*>(context);
eglMakeCurrent(ctx->_egldisplay, _eglsurface, _eglsurface, ctx->_eglcontext);
}
void eglsurface::swap() {
eglcontext* ctx = static_cast<eglcontext*>(context);
eglSwapBuffers(ctx->_egldisplay, _eglsurface);
}
} // fennec

View File

@@ -16,14 +16,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file log.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#include <fennec/renderers/opengl/glcontext.h>
namespace fennec
{
gfxpass* glcontext::create_pass() {
return nullptr;
}
}