Browse Source

Implement the ability to release specific textures from memory (#543)

Daniel Mircea 2 years ago
parent
commit
d131274517

+ 2 - 0
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.

+ 5 - 0
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();

+ 12 - 0
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)

+ 3 - 0
Source/Core/TextureDatabase.h

@@ -52,6 +52,9 @@ public:
 	/// 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);