From c058551ff1c55f644eb2af18295fe78a09e9244f Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Thu, 21 Nov 2024 05:59:21 +0100 Subject: [PATCH] opengl: move to CFileDescriptor use CFileDescriptor in m_iGBMFD and eglsync functions, convert related usage aswell. --- src/render/OpenGL.cpp | 33 +++++++++++++-------------------- src/render/OpenGL.hpp | 6 +++--- src/render/Renderer.cpp | 4 ++-- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 9c00daee..011efba5 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -296,11 +296,11 @@ CHyprOpenGLImpl::CHyprOpenGLImpl() { Debug::log(WARN, "EGL: EXT_platform_device or EGL_EXT_device_query not supported, using gbm"); if (EGLEXTENSIONS.contains("KHR_platform_gbm")) { success = true; - m_iGBMFD = openRenderNode(m_iDRMFD); - if (m_iGBMFD < 0) + m_iGBMFD = CFileDescriptor(openRenderNode(m_iDRMFD)); + if (!m_iGBMFD.isValid()) RASSERT(false, "Couldn't open a gbm fd"); - m_pGbmDevice = gbm_create_device(m_iGBMFD); + m_pGbmDevice = gbm_create_device(m_iGBMFD.get()); if (!m_pGbmDevice) RASSERT(false, "Couldn't open a gbm device"); @@ -366,9 +366,6 @@ CHyprOpenGLImpl::~CHyprOpenGLImpl() { if (m_pGbmDevice) gbm_device_destroy(m_pGbmDevice); - - if (m_iGBMFD >= 0) - close(m_iGBMFD); } std::optional> CHyprOpenGLImpl::getModsForFormat(EGLint format) { @@ -3011,11 +3008,11 @@ std::vector CHyprOpenGLImpl::getDRMFormats() { return drmFormats; } -SP CHyprOpenGLImpl::createEGLSync(int fenceFD) { +SP CHyprOpenGLImpl::createEGLSync(CFileDescriptor fenceFD) { std::vector attribs; int dupFd = -1; - if (fenceFD > 0) { - dupFd = fcntl(fenceFD, F_DUPFD_CLOEXEC, 0); + if (fenceFD.isValid()) { + dupFd = fcntl(fenceFD.get(), F_DUPFD_CLOEXEC, 0); if (dupFd < 0) { Debug::log(ERR, "createEGLSync: dup failed"); return nullptr; @@ -3037,27 +3034,26 @@ SP CHyprOpenGLImpl::createEGLSync(int fenceFD) { // we need to flush otherwise we might not get a valid fd glFlush(); - int fd = g_pHyprOpenGL->m_sProc.eglDupNativeFenceFDANDROID(g_pHyprOpenGL->m_pEglDisplay, sync); - if (fd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { + CFileDescriptor fd(g_pHyprOpenGL->m_sProc.eglDupNativeFenceFDANDROID(g_pHyprOpenGL->m_pEglDisplay, sync)); + if (fd.get() == EGL_NO_NATIVE_FENCE_FD_ANDROID) { Debug::log(ERR, "eglDupNativeFenceFDANDROID failed"); return nullptr; } auto eglsync = SP(new CEGLSync); eglsync->sync = sync; - eglsync->m_iFd = fd; + eglsync->m_iFd = std::move(fd); return eglsync; } bool CHyprOpenGLImpl::waitForTimelinePoint(SP timeline, uint64_t point) { - int fd = timeline->exportAsSyncFileFD(point); - if (fd < 0) { + CFileDescriptor fd(timeline->exportAsSyncFileFD(point)); + if (!fd.isValid()) { Debug::log(ERR, "waitForTimelinePoint: failed to get a fd from explicit timeline"); return false; } - auto sync = g_pHyprOpenGL->createEGLSync(fd); - close(fd); + auto sync = g_pHyprOpenGL->createEGLSync(std::move(fd)); if (!sync) { Debug::log(ERR, "waitForTimelinePoint: failed to get an eglsync from explicit timeline"); return false; @@ -3138,13 +3134,10 @@ CEGLSync::~CEGLSync() { if (g_pHyprOpenGL->m_sProc.eglDestroySyncKHR(g_pHyprOpenGL->m_pEglDisplay, sync) != EGL_TRUE) Debug::log(ERR, "eglDestroySyncKHR failed"); - - if (m_iFd >= 0) - close(m_iFd); } int CEGLSync::fd() { - return m_iFd; + return m_iFd.get(); } bool CEGLSync::wait() { diff --git a/src/render/OpenGL.hpp b/src/render/OpenGL.hpp index c594a7cc..80026dcc 100644 --- a/src/render/OpenGL.hpp +++ b/src/render/OpenGL.hpp @@ -137,7 +137,7 @@ class CEGLSync { private: CEGLSync() = default; - int m_iFd = -1; + CFileDescriptor m_iFd; friend class CHyprOpenGLImpl; }; @@ -208,14 +208,14 @@ class CHyprOpenGLImpl { uint32_t getPreferredReadFormat(PHLMONITOR pMonitor); std::vector getDRMFormats(); EGLImageKHR createEGLImage(const Aquamarine::SDMABUFAttrs& attrs); - SP createEGLSync(int fenceFD); + SP createEGLSync(CFileDescriptor fenceFD = {}); bool waitForTimelinePoint(SP timeline, uint64_t point); SCurrentRenderData m_RenderData; GLint m_iCurrentOutputFb = 0; - int m_iGBMFD = -1; + CFileDescriptor m_iGBMFD; gbm_device* m_pGbmDevice = nullptr; EGLContext m_pEglContext = nullptr; EGLDisplay m_pEglDisplay = nullptr; diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 42545bc1..ad35e2d5 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -1553,7 +1553,7 @@ bool CHyprRenderer::commitPendingAndDoExplicitSync(PHLMONITOR pMonitor) { Debug::log(TRACE, "Aquamarine did not return an explicit out fence"); Debug::log(TRACE, "Explicit: {} presented", explicitPresented.size()); - auto sync = g_pHyprOpenGL->createEGLSync(-1); + auto sync = g_pHyprOpenGL->createEGLSync(); if (!sync) Debug::log(TRACE, "Explicit: can't add sync, EGLSync failed"); @@ -2802,7 +2802,7 @@ void CHyprRenderer::endRender() { auto explicitOptions = getExplicitSyncSettings(); if (PMONITOR->inTimeline && explicitOptions.explicitEnabled && explicitOptions.explicitKMSEnabled) { - auto sync = g_pHyprOpenGL->createEGLSync(-1); + auto sync = g_pHyprOpenGL->createEGLSync(); if (!sync) { Debug::log(ERR, "renderer: couldn't create an EGLSync for out in endRender"); return;