From 640420eaaa61b38307b16aa6e5b14180f26bc4b8 Mon Sep 17 00:00:00 2001 From: Mads Buvik Sandvei Date: Wed, 16 Dec 2020 21:46:52 +0100 Subject: [PATCH 1/2] No longer delete and recreate CopyFramebufferToTextureCallback, remove oneshot functionality. Instead just remove the callback after the traversals. Use addInitialDrawCallback instead of setInitialDrawCallback to avoid messing with existing callbacks. --- apps/openmw/mwgui/loadingscreen.cpp | 17 +++++++++-------- apps/openmw/mwgui/loadingscreen.hpp | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/apps/openmw/mwgui/loadingscreen.cpp b/apps/openmw/mwgui/loadingscreen.cpp index cd0384bb0..2e2de578e 100644 --- a/apps/openmw/mwgui/loadingscreen.cpp +++ b/apps/openmw/mwgui/loadingscreen.cpp @@ -137,15 +137,11 @@ namespace MWGui public: CopyFramebufferToTextureCallback(osg::Texture2D* texture) : mTexture(texture) - , oneshot(true) { } void operator () (osg::RenderInfo& renderInfo) const override { - if (!oneshot) - return; - oneshot = false; int w = renderInfo.getCurrentCamera()->getViewport()->width(); int h = renderInfo.getCurrentCamera()->getViewport()->height(); mTexture->copyTexImage2D(*renderInfo.getState(), 0, 0, w, h); @@ -153,7 +149,6 @@ namespace MWGui private: osg::ref_ptr mTexture; - mutable bool oneshot; }; class DontComputeBoundCallback : public osg::Node::ComputeBoundingSphereCallback @@ -322,9 +317,12 @@ namespace MWGui mGuiTexture.reset(new osgMyGUI::OSGTexture(mTexture)); } - // Notice that the next time this is called, the current CopyFramebufferToTextureCallback will be deleted - // so there's no memory leak as at most one object of type CopyFramebufferToTextureCallback is allocated at a time. - mViewer->getCamera()->setInitialDrawCallback(new CopyFramebufferToTextureCallback(mTexture)); + if (!mCopyFramebufferToTextureCallback) + { + mCopyFramebufferToTextureCallback = new CopyFramebufferToTextureCallback(mTexture); + } + + mViewer->getCamera()->addInitialDrawCallback(mCopyFramebufferToTextureCallback); mBackgroundImage->setBackgroundImage(""); mBackgroundImage->setVisible(false); @@ -367,6 +365,9 @@ namespace MWGui mViewer->renderingTraversals(); mViewer->advance(mViewer->getFrameStamp()->getSimulationTime()); + if(mCopyFramebufferToTextureCallback) + mViewer->getCamera()->removeInitialDrawCallback(mCopyFramebufferToTextureCallback); + mLastRenderTime = mTimer.time_m(); } diff --git a/apps/openmw/mwgui/loadingscreen.hpp b/apps/openmw/mwgui/loadingscreen.hpp index 2577827aa..ac911ab60 100644 --- a/apps/openmw/mwgui/loadingscreen.hpp +++ b/apps/openmw/mwgui/loadingscreen.hpp @@ -28,6 +28,7 @@ namespace Resource namespace MWGui { class BackgroundImage; + class CopyFramebufferToTextureCallback; class LoadingScreen : public WindowBase, public Loading::Listener { @@ -84,6 +85,7 @@ namespace MWGui std::vector mSplashScreens; osg::ref_ptr mTexture; + osg::ref_ptr mCopyFramebufferToTextureCallback; std::unique_ptr mGuiTexture; void changeWallpaper(); From bc961a13f54842aa1dd75a459fabfe98ccce2cad Mon Sep 17 00:00:00 2001 From: Mads Buvik Sandvei Date: Wed, 16 Dec 2020 22:03:16 +0100 Subject: [PATCH 2/2] avoid redundant calls to removeInitialDrawCallback --- apps/openmw/mwgui/loadingscreen.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwgui/loadingscreen.cpp b/apps/openmw/mwgui/loadingscreen.cpp index 2e2de578e..20586ef4a 100644 --- a/apps/openmw/mwgui/loadingscreen.cpp +++ b/apps/openmw/mwgui/loadingscreen.cpp @@ -365,8 +365,11 @@ namespace MWGui mViewer->renderingTraversals(); mViewer->advance(mViewer->getFrameStamp()->getSimulationTime()); - if(mCopyFramebufferToTextureCallback) + if (mCopyFramebufferToTextureCallback) + { mViewer->getCamera()->removeInitialDrawCallback(mCopyFramebufferToTextureCallback); + mCopyFramebufferToTextureCallback = nullptr; + } mLastRenderTime = mTimer.time_m(); }