wayland: opengl support

This commit is contained in:
IoIxD
2025-12-30 22:26:19 -07:00
parent a03065ae52
commit 6946c1eaf3
4 changed files with 229 additions and 29 deletions

View File

@@ -170,8 +170,7 @@ struct _MwLLWayland {
struct wl_keyboard* keyboard;
MwU32 keyboard_serial;
MwU64 events_pending;
MwBool test;
MwU64 events_pending;
MwU32 mod_state;
@@ -186,8 +185,7 @@ struct _MwLLWayland {
MwBool force_render;
MwBool break_dispatch;
MwBool break_pending;
MwBool hmmmm;
struct _MwLLWaylandShmBuffer framebuffer;
struct _MwLLWaylandShmBuffer cursor;

View File

@@ -32,6 +32,10 @@ if (grep(/^wayland$/, @backends)) {
add_cflags(`pkg-config --cflags cairo wayland-client xkbcommon`);
add_libs(`pkg-config --libs cairo wayland-client xkbcommon`);
if (param_get("opengl")) {
add_libs(`pkg-config --libs egl wayland-egl`);
}
scan_wayland_protocol("stable", "xdg-shell", "");
scan_wayland_protocol("stable", "tablet", "-v2");
scan_wayland_protocol("staging", "xdg-toplevel-icon", "-v1");
@@ -39,7 +43,7 @@ if (grep(/^wayland$/, @backends)) {
scan_wayland_protocol("unstable", "xdg-decoration", "-unstable-v1");
scan_wayland_protocol("unstable", "primary-selection", "-unstable-v1");
$gl_libs = "-lEGL -lwayland-egl lGL -lGLU";
$gl_libs = "-lGL -lGLU";
}
if (param_get("stb-image")) {
@@ -48,6 +52,11 @@ if (param_get("stb-image")) {
if (param_get("stb-truetype")) {
add_cflags("-DUSE_STB_TRUETYPE");
}
if (param_get("opengl")) {
add_cflags("-DHAS_OPENGL");
}
if (param_get("freetype2")) {
add_cflags("-DUSE_FREETYPE2");
if ($cross) {

View File

@@ -8,7 +8,6 @@
#include <sys/mman.h>
#include <wayland-util.h>
#include <unistd.h>
#include <pthread.h>
/* TODO:
* - MwLLGrabPointerImpl
@@ -46,7 +45,7 @@ static void new_protocol(void* data, struct wl_registry* registry,
wayland_protocol_callback_table_t* cb = shget(wayland->wl_protocol_setup_map, interface);
if(cb != NULL) {
char* inter = malloc(strlen(interface));
char* inter = malloc(strlen(interface) + 1);
strcpy(inter, interface);
shput(wayland->wl_protocol_map, inter, cb->setup(name, data));
} else {
@@ -360,6 +359,7 @@ static void pointer_motion(void* data, struct wl_pointer* wl_pointer, MwU32 time
p.point = self->wayland.cur_mouse_pos;
MwLLDispatch(self, move, &p);
self->wayland.events_pending += 1;
/*timed_redraw(self, time, 50, &self->wayland.cooldown_timer);*/
@@ -396,8 +396,10 @@ static void pointer_button(void* data, struct wl_pointer* wl_pointer, MwU32 seri
}
}
MwLLDispatch(self, draw, NULL);
self->wayland.events_pending += 1;
if(!self->wayland.always_render) {
MwLLDispatch(self, draw, NULL);
self->wayland.events_pending += 1;
}
};
/* `wl_pointer.axis` callback */
@@ -433,8 +435,6 @@ static void keyboard_keymap(void* data,
close(fd);
struct xkb_state* xkb_state = xkb_state_new(xkb_keymap);
xkb_keymap_unref(wayland->xkb_keymap);
xkb_state_unref(wayland->xkb_state);
wayland->xkb_keymap = xkb_keymap;
wayland->xkb_state = xkb_state;
@@ -556,8 +556,11 @@ static void keyboard_key(void* data,
}
}
}
MwLLDispatch(self, draw, NULL);
self->wayland.events_pending += 1;
if(!self->wayland.always_render) {
MwLLDispatch(self, draw, NULL);
self->wayland.events_pending += 1;
}
};
/* `wl_keyboard.modifiers` callback */
@@ -783,6 +786,8 @@ static void xdg_toplevel_configure(void* data,
MwLLDispatch(self, resize, NULL);
MwLLDispatch(self, draw, NULL);
MwLLForceRender(self);
/*if(!self->wayland.egl_setup) {
self->wayland.egl_setup = egl_setup(self, self->wayland.x, self->wayland.y, width, height);
} else {
@@ -891,6 +896,7 @@ static void framebuffer_setup(struct _MwLLWayland* wayland) {
memset(wayland->framebuffer.buf, 255, wayland->framebuffer.buf_size);
update_buffer(&wayland->framebuffer);
wayland->events_pending += 1;
};
static void framebuffer_destroy(struct _MwLLWayland* wayland) {
@@ -1287,9 +1293,6 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLLForceRender(r);
pthread_mutex_init(&r->wayland.dispatch_mutex, NULL);
pthread_mutex_init(&r->wayland.pending_mutex, NULL);
return r;
}
@@ -1398,6 +1401,7 @@ static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count, MwLL
}
cairo_close_path(handle->wayland.cairo);
cairo_fill(handle->wayland.cairo);
handle->wayland.events_pending += 1;
}
@@ -1416,6 +1420,7 @@ static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
}
cairo_close_path(handle->wayland.cairo);
cairo_stroke(handle->wayland.cairo);
handle->wayland.events_pending += 1;
}
@@ -1424,6 +1429,7 @@ static void MwLLBeginDrawImpl(MwLL handle) {
static void MwLLEndDrawImpl(MwLL handle) {
update_buffer(&handle->wayland.framebuffer);
handle->wayland.events_pending += 1;
}
@@ -1451,20 +1457,25 @@ static int MwLLPendingImpl(MwLL handle) {
MwBool pending = MwFALSE;
struct timespec timeout;
timeout.tv_nsec = 100;
timeout.tv_sec = 0;
while(pending == 0 && !handle->wayland.break_dispatch) {
pending = wl_display_dispatch_timeout(handle->wayland.display, &timeout);
if(handle->wayland.always_render) {
return event_loop(handle);
}
return handle->wayland.force_render || handle->wayland.events_pending || pending;
timeout.tv_nsec = 10;
timeout.tv_sec = 0;
return handle->wayland.force_render || handle->wayland.events_pending || wl_display_dispatch_timeout(handle->wayland.display, &timeout);
}
static void MwLLNextEventImpl(MwLL handle) {
event_loop(handle);
if(handle->wayland.events_pending) {
handle->wayland.events_pending = 0;
if(!handle->wayland.always_render) {
event_loop(handle);
if(handle->wayland.events_pending) {
handle->wayland.events_pending = 0;
}
if(handle->wayland.force_render) {
handle->wayland.force_render = 0;
}
}
}
@@ -1575,6 +1586,8 @@ static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) {
static void MwLLForceRenderImpl(MwLL handle) {
wl_surface_damage(handle->wayland.framebuffer.surface, 0, 0, handle->wayland.ww, handle->wayland.wh);
handle->wayland.force_render = MwTRUE;
/*
if(handle->wayland.egl_setup) {
timed_redraw_by_epoch(handle, 25);

View File

@@ -1,6 +1,7 @@
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
#ifdef HAS_OPENGL
#ifdef USE_GDI
typedef HGLRC(WINAPI* MWwglCreateContext)(HDC);
typedef BOOL(WINAPI* MWwglMakeCurrent)(HDC, HGLRC);
@@ -42,6 +43,19 @@ typedef struct x11opengl {
} x11opengl_t;
#endif
#ifdef USE_WAYLAND
#include <EGL/egl.h>
#include <wayland-egl-core.h>
typedef struct waylandopengl {
EGLNativeWindowType egl_window_native;
EGLDisplay egl_display;
EGLContext egl_context;
EGLSurface egl_surface;
EGLConfig egl_config;
} waylandopengl_t;
#endif
static int create(MwWidget handle) {
void* r = NULL;
#ifdef USE_GDI
@@ -105,7 +119,89 @@ static int create(MwWidget handle) {
#endif
#ifdef USE_WAYLAND
if(handle->lowlevel->common.type == MwLLBackendWayland) {
/* todo */
int err;
EGLint numConfigs;
EGLint majorVersion;
EGLint minorVersion;
EGLContext context;
EGLSurface surface;
EGLint fbAttribs[] =
{
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_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE};
EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 1,
EGL_CONTEXT_MAJOR_VERSION, 1,
EGL_CONTEXT_MINOR_VERSION, 1,
EGL_NONE};
EGLDisplay display;
waylandopengl_t* o = r = malloc(sizeof(*o));
MwLL topmost_parent = handle->lowlevel->wayland.parent;
topmost_parent->wayland.always_render = MwTRUE;
while(topmost_parent->wayland.parent != NULL) {
topmost_parent = topmost_parent->wayland.parent;
topmost_parent->wayland.always_render = MwTRUE;
}
display = eglGetDisplay((EGLNativeDisplayType)handle->lowlevel->wayland.display);
if(display == EGL_NO_DISPLAY) {
printf("ERROR: eglGetDisplay, %0X\n", eglGetError());
return MwFALSE;
}
/* Initialize EGL */
if(!eglInitialize(display, &majorVersion, &minorVersion)) {
printf("ERROR: eglInitialize, %0X\n", eglGetError());
return MwFALSE;
}
/* Get configs */
if((eglGetConfigs(display, NULL, 0, &numConfigs) != EGL_TRUE) || (numConfigs == 0)) {
printf("ERROR: eglGetConfigs, %0X\n", eglGetError());
return MwFALSE;
}
/* Choose config */
if((eglChooseConfig(display, fbAttribs, &o->egl_config, 1, &numConfigs) != EGL_TRUE) || (numConfigs != 1)) {
printf("ERROR: eglChooseConfig, %0X\n", eglGetError());
return MwFALSE;
}
o->egl_window_native =
(EGLNativeWindowType)wl_egl_window_create(handle->lowlevel->wayland.framebuffer.surface, handle->lowlevel->wayland.ww, handle->lowlevel->wayland.wh);
if(o->egl_window_native == EGL_NO_SURFACE) {
printf("ERROR: wl_egl_window_create, EGL_NO_SURFACE\n");
return MwFALSE;
}
/* Create a surface */
surface = eglCreateWindowSurface(display, o->egl_config, o->egl_window_native, NULL);
if(surface == EGL_NO_SURFACE) {
printf("ERROR: eglCreateWindowSurface, %0X\n", eglGetError());
return MwFALSE;
}
eglBindAPI(EGL_OPENGL_API);
/* Create a GL context */
context = eglCreateContext(display, o->egl_config, EGL_NO_CONTEXT, contextAttribs);
if(context == EGL_NO_CONTEXT) {
printf("ERROR: eglCreateContext, %0X\n", eglGetError());
return MwFALSE;
}
if(!eglMakeCurrent(display, surface, surface, context)) {
printf("ERROR: eglMakeCurrent (setup): %0X\n", eglGetError());
}
o->egl_display = display;
o->egl_surface = surface;
o->egl_context = context;
}
#endif
@@ -165,7 +261,11 @@ static void mwOpenGLMakeCurrentImpl(MwWidget handle) {
#endif
#ifdef USE_WAYLAND
if(handle->lowlevel->common.type == MwLLBackendWayland) {
/* todo */
waylandopengl_t* o = handle->internal;
if(!eglMakeCurrent(o->egl_display, o->egl_surface, o->egl_surface, o->egl_context)) {
printf("ERROR: eglMakeCurrent, %0X\n", eglGetError());
}
}
#endif
}
@@ -187,7 +287,13 @@ static void mwOpenGLSwapBufferImpl(MwWidget handle) {
#endif
#ifdef USE_WAYLAND
if(handle->lowlevel->common.type == MwLLBackendWayland) {
/* todo */
waylandopengl_t* o = handle->internal;
eglSwapInterval(o->egl_display, 0);
if(!eglSwapBuffers(o->egl_display, o->egl_surface)) {
printf("ERROR: eglSwapBuffers, %0X\n", eglGetError());
};
wl_egl_window_resize((struct wl_egl_window*)o->egl_window_native, handle->lowlevel->wayland.ww, handle->lowlevel->wayland.wh, 0, 0);
MwLLForceRender(handle->lowlevel);
}
#endif
}
@@ -209,11 +315,85 @@ static void* mwOpenGLGetProcAddressImpl(MwWidget handle, const char* name) {
#endif
#ifdef USE_WAYLAND
if(handle->lowlevel->common.type == MwLLBackendWayland) {
/* todo */
return eglGetProcAddress(name);
}
#endif
return NULL;
}
#else
#ifdef USE_GDI
typedef HGLRC(WINAPI* MWwglCreateContext)(HDC);
typedef BOOL(WINAPI* MWwglMakeCurrent)(HDC, HGLRC);
typedef PROC(WINAPI* MWwglGetProcAddress)(LPCSTR);
typedef BOOL(WINAPI* MWwglDeleteContext)(HGLRC);
typedef struct gdiopengl {
HDC dc;
HGLRC gl;
void* lib;
MWwglCreateContext wglCreateContext;
MWwglMakeCurrent wglMakeCurrent;
MWwglDeleteContext wglDeleteContext;
MWwglGetProcAddress wglGetProcAddress;
} gdiopengl_t;
#endif
#ifdef USE_X11
typedef XVisualInfo* (*MWglXChooseVisual)(Display* dpy, int screen, int* attribList);
typedef GLXContext (*MWglXCreateContext)(Display* dpy, XVisualInfo* vis, GLXContext shareList, Bool direct);
typedef void (*MWglXDestroyContext)(Display* dpy, GLXContext ctx);
typedef Bool (*MWglXMakeCurrent)(Display* dpy, GLXDrawable drawable, GLXContext ctx);
typedef void (*MWglXSwapBuffers)(Display* dpy, GLXDrawable drawable);
typedef void* (*MWglXGetProcAddress)(const GLubyte* procname);
typedef struct x11opengl {
XVisualInfo* visual;
GLXContext gl;
void* lib;
MWglXChooseVisual glXChooseVisual;
MWglXCreateContext glXCreateContext;
MWglXDestroyContext glXDestroyContext;
MWglXMakeCurrent glXMakeCurrent;
MWglXSwapBuffers glXSwapBuffers;
MWglXGetProcAddress glXGetProcAddress;
} x11opengl_t;
#endif
#ifdef USE_WAYLAND
#include <EGL/egl.h>
#include <wayland-egl.h>
typedef struct waylandopengl {
struct wl_egl_window* egl_window_native;
EGLDisplay egl_display;
EGLContext egl_context;
EGLSurface egl_surface;
EGLConfig egl_config;
} waylandopengl_t;
#endif
static int create(MwWidget handle) {
printf("Milsko compiled without OpenGL support! OpenGL widgets will not work properly.\n");
return 1;
}
static void destroy(MwWidget handle) {
}
static void mwOpenGLMakeCurrentImpl(MwWidget handle) {
}
static void mwOpenGLSwapBufferImpl(MwWidget handle) {
}
static void* mwOpenGLGetProcAddressImpl(MwWidget handle, const char* name) {
return NULL;
}
#endif
static void func_handler(MwWidget handle, const char* name, void* out, va_list va) {
if(strcmp(name, "mwOpenGLMakeCurrent") == 0) {