summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-10 16:42:24 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-10 16:42:24 +0100
commitd6be68c0ad24b9c4bc7ec6cf8deab104f9bc48bc (patch)
treedd24d39dd9e6cdab8411064c8d0ad9bb62ee9c1f
parent88c7b19267e968760fdd350eea77b3199eafe28b (diff)
obtaining a egl context
-rw-r--r--src/graphics.cc70
-rw-r--r--src/graphics.h12
-rw-r--r--src/wayclock.cc7
3 files changed, 86 insertions, 3 deletions
diff --git a/src/graphics.cc b/src/graphics.cc
index d9db4c8..5618824 100644
--- a/src/graphics.cc
+++ b/src/graphics.cc
@@ -1,10 +1,45 @@
#include "graphics.h"
#include <EGL/egl.h>
+#include <EGL/eglplatform.h>
#include "utils.h"
#include "wlstate.h"
+/* clang-format off */
+#define WAYC_OPENGL_MAJOR 4
+#define WAYC_OPENGL_MINOR 6
+#define WAYC_COLOR_BITS 8
+
+static EGLint WAYC_EGL_ATTRS[] = {
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+ EGL_RED_SIZE, WAYC_COLOR_BITS,
+ EGL_GREEN_SIZE, WAYC_COLOR_BITS,
+ EGL_BLUE_SIZE, WAYC_COLOR_BITS,
+ EGL_ALPHA_SIZE, WAYC_COLOR_BITS,
+ EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
+ EGL_NONE
+};
+
+static EGLint WAYC_OGL_ATTRS[] = {
+ EGL_CONTEXT_MAJOR_VERSION, WAYC_OPENGL_MAJOR,
+ EGL_CONTEXT_MINOR_VERSION, WAYC_OPENGL_MINOR,
+ EGL_NONE
+};
+
+/* clang-format on */
+
+static bool wayc_graphics_config(EGLDisplay display, EGLConfig* config) {
+ wayc_notnull(display);
+ wayc_notnull(config);
+
+ EGLint nconfs = 0;
+ if (!eglChooseConfig(display, WAYC_EGL_ATTRS, config, 1, &nconfs))
+ return false;
+
+ return nconfs > 0;
+}
+
enum graphics_error_e wayc_graphics_init(struct graphics_s* graphics,
struct wlstate_s* state) {
wayc_notnull(graphics);
@@ -13,16 +48,45 @@ enum graphics_error_e wayc_graphics_init(struct graphics_s* graphics,
EGLDisplay display = eglGetDisplay(state->display);
if (display == EGL_NO_DISPLAY) return GRAPHICS_ERROR_DISPLAY;
+ if (!eglInitialize(display, nullptr, nullptr)) {
+ eglTerminate(display);
+ return GRAPHICS_ERROR_DISPLAY;
+ }
+
+ if (!eglBindAPI(EGL_OPENGL_API)) {
+ eglTerminate(display);
+ return GRAPHICS_ERROR_BIND;
+ }
+
+ EGLConfig config;
+ if (!wayc_graphics_config(display, &config)) {
+ eglTerminate(display);
+ return GRAPHICS_ERROR_CONFIG;
+ }
+
+ EGLContext context =
+ eglCreateContext(display, config, EGL_NO_CONTEXT, WAYC_OGL_ATTRS);
+
+ if (context == EGL_NO_CONTEXT) {
+ eglDestroyContext(display, context);
+ eglTerminate(display);
+ return GRAPHICS_ERROR_CONTEXT;
+ }
+
graphics->display = display;
+ graphics->config = config;
+ graphics->context = context;
return GRAPHICS_ERROR_NONE;
}
void wayc_graphics_deinit(struct graphics_s* graphics) {
wayc_notnull(graphics);
- if (graphics->display == nullptr) return;
+ if (graphics->display == nullptr || graphics->context == nullptr) return;
- eglMakeCurrent(graphics->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
- EGL_NO_CONTEXT);
+ eglDestroyContext(graphics->display, graphics->context);
eglTerminate(graphics->display);
+
+ graphics->display = nullptr;
+ graphics->context = nullptr;
}
diff --git a/src/graphics.h b/src/graphics.h
index 1ab4651..0419f46 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -3,9 +3,14 @@
#include <EGL/egl.h>
#include <wayland-egl.h>
+#include "utils.h"
+
enum graphics_error_e {
GRAPHICS_ERROR_NONE = 0,
GRAPHICS_ERROR_DISPLAY,
+ GRAPHICS_ERROR_CONFIG,
+ GRAPHICS_ERROR_BIND,
+ GRAPHICS_ERROR_CONTEXT,
};
struct graphics_s {
@@ -17,3 +22,10 @@ struct graphics_s {
enum graphics_error_e wayc_graphics_init(struct graphics_s* graphics,
struct wlstate_s* state);
void wayc_graphics_deinit(struct graphics_s* graphics);
+static inline bool wayc_graphics_use(struct graphics_s* graphics) {
+ wayc_notnull(graphics);
+
+ eglMakeCurrent(graphics->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+ graphics->context);
+ return true;
+}
diff --git a/src/wayclock.cc b/src/wayclock.cc
index 348b122..2c6a98b 100644
--- a/src/wayclock.cc
+++ b/src/wayclock.cc
@@ -3,6 +3,7 @@
#include "events.h"
#include "graphics.h"
+#include "utils.h"
#include "window.h"
#define WAYC_APP_NAME "Wayclock"
@@ -48,6 +49,12 @@ int main() {
if (wayc_graphics_init(&graphics, &loop.state) != GRAPHICS_ERROR_NONE)
wayc_panic("Failed to initialize graphics");
+ if (!wayc_graphics_use(&graphics)) {
+ wayc_graphics_deinit(&graphics);
+ wayc_eventloop_deinit(&loop);
+ wayc_panic("Failed to use graphics");
+ }
+
struct window_s window;
if (wayc_window_init(&window, WAYC_APP_NAME, &loop) != WINDOW_ERROR_NONE) {
wayc_graphics_deinit(&graphics);