From d1312745174ab977371d74f4f8d50ad27a864cb5 Mon Sep 17 00:00:00 2001 From: Daniel Mircea Date: Tue, 21 Nov 2023 21:11:06 +0200 Subject: [PATCH] Implement the ability to release specific textures from memory (#543) --- Include/RmlUi/Core/Core.h | 2 ++ Source/Core/Core.cpp | 5 +++++ Source/Core/TextureDatabase.cpp | 12 ++++++++++++ Source/Core/TextureDatabase.h | 3 +++ 4 files changed, 22 insertions(+) diff --git a/Include/RmlUi/Core/Core.h b/Include/RmlUi/Core/Core.h index beb142a47..77948f0fc 100644 --- a/Include/RmlUi/Core/Core.h +++ b/Include/RmlUi/Core/Core.h @@ -150,6 +150,8 @@ RMLUICORE_API EventId RegisterEventType(const String& type, bool interruptible, RMLUICORE_API StringList GetTextureSourceList(); /// Forces all texture handles loaded and generated by RmlUi to be released. RMLUICORE_API void ReleaseTextures(); +/// Releases a specified texture by name from memory, returning 'true' if successful and 'false' if not found. +RMLUICORE_API bool ReleaseTexture(const String& name); /// Forces all compiled geometry handles generated by RmlUi to be released. RMLUICORE_API void ReleaseCompiledGeometry(); /// Releases unused font textures and rendered glyphs to free up memory, and regenerates actively used fonts. diff --git a/Source/Core/Core.cpp b/Source/Core/Core.cpp index c58e0f900..6d3422486 100644 --- a/Source/Core/Core.cpp +++ b/Source/Core/Core.cpp @@ -355,6 +355,11 @@ void ReleaseTextures() TextureDatabase::ReleaseTextures(); } +bool ReleaseTexture(const String& source) +{ + return TextureDatabase::ReleaseTexture(source); +} + void ReleaseCompiledGeometry() { return GeometryDatabase::ReleaseAll(); diff --git a/Source/Core/TextureDatabase.cpp b/Source/Core/TextureDatabase.cpp index bd7c5839d..825b0b1bf 100644 --- a/Source/Core/TextureDatabase.cpp +++ b/Source/Core/TextureDatabase.cpp @@ -134,6 +134,18 @@ void TextureDatabase::ReleaseTextures() } } +bool TextureDatabase::ReleaseTexture(const String& source) +{ + auto it = texture_database->textures.find(source); + if (it != texture_database->textures.end()) + { + it->second->Release(); + return true; + } + + return false; +} + bool TextureDatabase::AllTexturesReleased() { if (texture_database) diff --git a/Source/Core/TextureDatabase.h b/Source/Core/TextureDatabase.h index 0a3fc0ef7..2c6c300eb 100644 --- a/Source/Core/TextureDatabase.h +++ b/Source/Core/TextureDatabase.h @@ -52,6 +52,9 @@ class TextureDatabase { /// Release all textures in the database. static void ReleaseTextures(); + /// Release a given texture from the database. + static bool ReleaseTexture(const String& source); + /// Adds a texture resource with a callback function and stores it as a weak (raw) pointer in the database. static void AddCallbackTexture(TextureResource* texture);