feat: separate wayland implementation

This commit is contained in:
2026-02-13 13:27:06 +01:00
parent 696d55db02
commit ae9de0a22e
11 changed files with 635 additions and 470 deletions

98
src/renderer.cpp Normal file
View File

@@ -0,0 +1,98 @@
#include <stdio.h>
#include "state/wayland.h"
#include "renderer.h"
Renderer *Renderer::GetInstance() {
if (!s_instance)
s_instance = new Renderer;
return s_instance;
}
const EGLConfig &Renderer::GetConfig() const { return m_config; }
void Renderer::DestroyWindow(const EGLSurface& surface, struct wl_egl_window* window) {
eglMakeCurrent(m_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(m_egl_display, surface);
wl_egl_window_destroy(window);
}
void Renderer::DestroyContext(const EGLContext& context) {
eglDestroyContext(m_egl_display, context);
}
EGLContext Renderer::CreateContext() {
EGLint ctx_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
return eglCreateContext(m_egl_display, m_config, EGL_NO_CONTEXT,
ctx_attribs);
}
EGLSurface Renderer::CreateSurface(struct wl_egl_window *window) {
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurface =
reinterpret_cast<PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC>(
eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"));
return createPlatformWindowSurface(m_egl_display, m_config, window, NULL);
}
void Renderer::SurfaceSetContext(const EGLSurface &surface, const EGLContext &context) {
if (!eglMakeCurrent(m_egl_display, surface, surface, context)) {
fprintf(stderr, "ERROR: could not change current EGL context\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
}
void Renderer::SwapBuffers(const EGLSurface &surface) {
eglSwapBuffers(m_egl_display, surface);
}
Renderer::Renderer() {
PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay =
reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(
eglGetProcAddress("eglGetPlatformDisplayEXT"));
m_egl_display = getPlatformDisplay(
EGL_PLATFORM_WAYLAND_KHR, WaylandState::GetInstance()->m_display, NULL);
if (m_egl_display == NULL) {
fprintf(stderr, "ERROR: could not get platform display\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
if (!eglInitialize(m_egl_display, NULL, NULL)) {
fprintf(stderr, "ERROR: could not initialize EGL\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
EGLint config_attribs[] = {EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_NONE};
EGLint num_configs;
if (!eglChooseConfig(m_egl_display, config_attribs, &m_config, 1,
&num_configs)) {
fprintf(stderr, "ERROR: could not choose EGL config\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
}
Renderer::~Renderer() {
// After all windows are destroyed:
eglTerminate(m_egl_display);
}
Renderer *Renderer::s_instance = nullptr;