#include #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( 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( 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;