From bf762f566f1c8c5e9c636f0d1ad4e485ad322da9 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:11:34 +0200 Subject: [PATCH] Fix VRAM memory leak when releasing snapshots --- src/Compositor.cpp | 11 ++++++++++- src/Compositor.hpp | 2 +- src/events/Monitors.cpp | 4 +++- src/render/Framebuffer.cpp | 18 +++++++++--------- src/render/Framebuffer.hpp | 2 ++ src/render/OpenGL.cpp | 9 ++++----- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 83fcdfcb..cdcf880c 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -852,9 +852,12 @@ void CCompositor::moveWindowToTop(CWindow* pWindow) { } } -void CCompositor::cleanupFadingOut() { +void CCompositor::cleanupFadingOut(const int& monid) { for (auto& w : m_vWindowsFadingOut) { + if (w->m_iMonitorID != (long unsigned int)monid) + continue; + bool valid = windowExists(w); if (!valid || !w->m_bFadingOut || w->m_fAlpha.fl() == 0.f) { @@ -867,11 +870,16 @@ void CCompositor::cleanupFadingOut() { m_vWindowsFadingOut.erase(std::remove(m_vWindowsFadingOut.begin(), m_vWindowsFadingOut.end(), w)); Debug::log(LOG, "Cleanup: destroyed a window"); + + glFlush(); // to free mem NOW. return; } } for (auto& ls : m_vSurfacesFadingOut) { + if (ls->monitorID != monid) + continue; + if (ls->fadingOut && ls->readyToDelete && !ls->alpha.isBeingAnimated()) { for (auto& m : m_vMonitors) { for (auto& lsl : m->m_aLayerSurfaceLists) { @@ -887,6 +895,7 @@ void CCompositor::cleanupFadingOut() { Debug::log(LOG, "Cleanup: destroyed a layersurface"); + glFlush(); // to free mem NOW. return; } } diff --git a/src/Compositor.hpp b/src/Compositor.hpp index b386f542..bc967621 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -124,7 +124,7 @@ public: bool doesSeatAcceptInput(wlr_surface*); bool isWindowActive(CWindow*); void moveWindowToTop(CWindow*); - void cleanupFadingOut(); + void cleanupFadingOut(const int& monid); CWindow* getWindowInDirection(CWindow*, char); void deactivateAllWLRWorkspaces(wlr_ext_workspace_handle_v1* exclude = nullptr); CWindow* getNextWindowOnWorkspace(CWindow*); diff --git a/src/events/Monitors.cpp b/src/events/Monitors.cpp index d523aeb3..cf7e4b49 100644 --- a/src/events/Monitors.cpp +++ b/src/events/Monitors.cpp @@ -186,7 +186,6 @@ void Events::listener_monitorFrame(void* owner, void* data) { if (PMONITOR->ID == pMostHzMonitor->ID || !*PNOVFR) { // unfortunately with VFR we don't have the guarantee mostHz is going to be updated all the time, so we have to ignore that g_pCompositor->sanityCheckWorkspaces(); g_pAnimationManager->tick(); - g_pCompositor->cleanupFadingOut(); HyprCtl::tickHyprCtl(); // so that we dont get that race condition multithread bullshit @@ -229,6 +228,9 @@ void Events::listener_monitorFrame(void* owner, void* data) { return; } + // we need to cleanup fading out when rendering the appropriate context + g_pCompositor->cleanupFadingOut(PMONITOR->ID); + if (!hasChanged && *PDAMAGETRACKINGMODE != DAMAGE_TRACKING_NONE && PMONITOR->forceFullFrames == 0 && damageBlinkCleanup == 0) { pixman_region32_fini(&damage); wlr_output_rollback(PMONITOR->output); diff --git a/src/render/Framebuffer.cpp b/src/render/Framebuffer.cpp index 4a5baf50..90b41a4b 100644 --- a/src/render/Framebuffer.cpp +++ b/src/render/Framebuffer.cpp @@ -2,15 +2,14 @@ #include "OpenGL.hpp" bool CFramebuffer::alloc(int w, int h) { - bool firstAlloc = false; - if (m_iFb == (uint32_t)-1) - { + bool firstAlloc = false; + + if (m_iFb == (uint32_t)-1) { firstAlloc = true; glGenFramebuffers(1, &m_iFb); } - if (m_cTex.m_iTexID == 0) - { + if (m_cTex.m_iTexID == 0) { firstAlloc = true; glGenTextures(1, &m_cTex.m_iTexID); glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID); @@ -20,13 +19,11 @@ bool CFramebuffer::alloc(int w, int h) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } - if (firstAlloc || m_Size != Vector2D(w, h)) - { + if (firstAlloc || m_Size != Vector2D(w, h)) { glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindFramebuffer(GL_FRAMEBUFFER, m_iFb); - glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_cTex.m_iTexID, 0); @@ -37,7 +34,6 @@ bool CFramebuffer::alloc(int w, int h) { glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0); glBindFramebuffer(GL_FRAMEBUFFER, m_iFb); - glBindTexture(GL_TEXTURE_2D, m_pStencilTex->m_iTexID); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_pStencilTex->m_iTexID, 0); } @@ -78,3 +74,7 @@ void CFramebuffer::release() { m_cTex.m_iTexID = 0; m_iFb = -1; } + +CFramebuffer::~CFramebuffer() { + release(); +} \ No newline at end of file diff --git a/src/render/Framebuffer.hpp b/src/render/Framebuffer.hpp index 3d15069d..6af59ca2 100644 --- a/src/render/Framebuffer.hpp +++ b/src/render/Framebuffer.hpp @@ -6,6 +6,8 @@ class CFramebuffer { public: + ~CFramebuffer(); + bool alloc(int w, int h); void bind(); void release(); diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 4b2fabea..d60e827e 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -702,8 +702,6 @@ void CHyprOpenGLImpl::makeWindowSnapshot(CWindow* pWindow) { begin(PMONITOR, &fakeDamage, true); - pixman_region32_fini(&fakeDamage); - clear(CColor(0,0,0,0)); // JIC timespec now; @@ -742,9 +740,10 @@ void CHyprOpenGLImpl::makeWindowSnapshot(CWindow* pWindow) { #else glBindFramebuffer(GL_FRAMEBUFFER, m_iCurrentOutputFb); #endif - end(); + pixman_region32_fini(&fakeDamage); + wlr_output_rollback(PMONITOR->output); } @@ -762,8 +761,6 @@ void CHyprOpenGLImpl::makeLayerSnapshot(SLayerSurface* pLayer) { begin(PMONITOR, &fakeDamage, true); - pixman_region32_fini(&fakeDamage); - const auto PFRAMEBUFFER = &m_mLayerFramebuffers[pLayer]; glViewport(0, 0, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.x, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.y); @@ -792,6 +789,8 @@ void CHyprOpenGLImpl::makeLayerSnapshot(SLayerSurface* pLayer) { end(); + pixman_region32_fini(&fakeDamage); + wlr_output_rollback(PMONITOR->output); }