#include "graphics.h" #include #include #include "utils.h" #include "wlstate.h" #define WAYC_OPENGL_MAJOR 4 #define WAYC_OPENGL_MINOR 6 #define WAYC_COLOR_BITS 8 /* clang-format off */ 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); wayc_notnull(state); 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 || graphics->context == nullptr) return; eglDestroyContext(graphics->display, graphics->context); eglTerminate(graphics->display); graphics->display = nullptr; graphics->context = nullptr; }