Browse Source

Font textures are now initially being generated with the ascii range of characters.

This fixes a silly regression in 2b12e0ad832117b47777d9cfce633f779df3e52f. Now the font textures are no longer being regenerated for every new ascii character encountered.

Added a unit test to ensure this behavior in the future.
Michael Ragazzon 3 years ago
parent
commit
18f6a1aad8

+ 1 - 1
Source/Core/FontEngineDefault/FontFamily.cpp

@@ -75,7 +75,7 @@ FontFaceHandleDefault* FontFamily::GetFaceHandle(Style::FontStyle style, Style::
 	if (!matching_face)
 		return nullptr;
 
-	return matching_face->GetHandle(size, false);
+	return matching_face->GetHandle(size, true);
 }
 
 // Adds a new face to the family.

+ 1 - 1
Source/Core/FontEngineDefault/FontProvider.cpp

@@ -98,7 +98,7 @@ FontFaceHandleDefault* FontProvider::GetFallbackFontFace(int index, int font_siz
 	auto& faces = FontProvider::Get().fallback_font_faces;
 
 	if (index >= 0 && index < (int)faces.size())
-		return faces[index]->GetHandle(font_size, true);
+		return faces[index]->GetHandle(font_size, false);
 
 	return nullptr;
 }

+ 9 - 0
Tests/Source/Common/TestsShell.cpp

@@ -217,3 +217,12 @@ Rml::String TestsShell::GetRenderStats()
 
 	return result;
 }
+
+TestsRenderInterface* TestsShell::GetTestsRenderInterface()
+{
+#if defined(RMLUI_TESTS_USE_SHELL)
+	return nullptr;
+#else
+	return &shell_render_interface;
+#endif
+}

+ 4 - 0
Tests/Source/Common/TestsShell.h

@@ -31,6 +31,7 @@
 
 #include <RmlUi/Core/Types.h>
 namespace Rml { class RenderInterface; }
+class TestsRenderInterface;
 
 namespace TestsShell {
 
@@ -53,6 +54,9 @@ namespace TestsShell {
 
 	// Stats only available for the dummy renderer.
 	Rml::String GetRenderStats();
+
+	// Returns nullptr if the dummy renderer is not being used.
+	TestsRenderInterface* GetTestsRenderInterface();
 }
 
 #endif

+ 60 - 3
Tests/Source/UnitTests/Core.cpp

@@ -1,4 +1,4 @@
-/*
+/*
  * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  *
  * For the latest information, see http://github.com/mikke89/RmlUi
@@ -26,13 +26,14 @@
  *
  */
 
+#include "../Common/TestsInterface.h"
 #include "../Common/TestsShell.h"
-#include <RmlUi/Core/Core.h>
 #include <RmlUi/Core/Context.h>
+#include <RmlUi/Core/Core.h>
 #include <RmlUi/Core/Element.h>
 #include <RmlUi/Core/ElementDocument.h>
-#include <doctest.h>
 #include <algorithm>
+#include <doctest.h>
 
 using namespace Rml;
 
@@ -122,3 +123,59 @@ TEST_CASE("core.texture_source_list")
 
 	TestsShell::ShutdownShell();
 }
+
+TEST_CASE("core.release_textures")
+{
+	TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
+	// This test only works with the dummy renderer.
+	if (!render_interface)
+		return;
+
+	render_interface->ResetCounters();
+	const auto& counters = render_interface->GetCounters();
+
+	Context* context = TestsShell::GetContext();
+	REQUIRE(context);
+
+	ElementDocument* document = context->LoadDocument("assets/demo.rml");
+	document->Show();
+	TestsShell::RenderLoop();
+
+	Element* element = document->GetElementById("content");
+
+	SUBCASE("ReleaseTextures")
+	{
+		// Release all textures and verify that the render interface received the release call.
+		Rml::ReleaseTextures();
+		CHECK(counters.generate_texture + counters.load_texture == counters.release_texture);
+
+		// By doing a new context Update+Render the textures should be loaded again.
+		TestsShell::RenderLoop();
+		CHECK(counters.generate_texture + counters.load_texture > counters.release_texture);
+	}
+
+	SUBCASE("FontGlyphCache")
+	{
+		const auto counter_generate_before = counters.generate_texture;
+		const auto counter_release_before = counters.release_texture;
+
+		// Verify that ASCII characters are cached during the first use of the font. Then the font texture should not be regenerated when adding ASCII
+		// characters not previously shown.
+		element->SetInnerRML("Abc!%&()");
+		TestsShell::RenderLoop();
+		CHECK(counters.generate_texture == counter_generate_before);
+
+		// However, when we display a non-ASCII character not part of the initial cache, the font texture needs to be regenerated.
+		element->SetInnerRML(reinterpret_cast<const char*>(u8"π"));
+		TestsShell::RenderLoop();
+		CHECK(counters.generate_texture == counter_generate_before + 1);
+		CHECK(counters.release_texture == counter_release_before + 1);
+	}
+
+	document->Close();
+
+	TestsShell::ShutdownShell();
+
+	// Finally, verify that all generated and loaded textures are released during shutdown.
+	CHECK(counters.generate_texture + counters.load_texture == counters.release_texture);
+}