Fix TextLayout Cache not invalidating during Engine destroy (this generated crashes in the unit-tests).

This commit is contained in:
Martín Lucas Golini
2026-03-28 14:13:54 -03:00
parent f9023edcbe
commit 18bb2dcc37
3 changed files with 17 additions and 3 deletions

View File

@@ -48,6 +48,7 @@ class EE_API TextLayout {
LineWrapMode lineWrapMode = LineWrapMode::NoWrap, Uint32 wrapWidth = 0,
bool keepIndentation = false, Float initialXOffset = 0 );
static void clearLayoutCache();
protected:
static void wrapLayout( const String::View& string, TextLayout&, LineWrapMode lineWrapMode,
Float wrapWidth, Float vspace, bool keepIndentation, Font* font,

View File

@@ -236,6 +236,17 @@ static inline Uint64 textLayoutHash( const String::View& string, Font* font,
std::hash<Float>()( initialXOffset ) );
}
static LRULayoutCache& getLayoutCache( bool invalidate = false ) {
static LRULayoutCache sLayoutCache;
if ( invalidate )
sLayoutCache.clear();
return sLayoutCache;
}
void TextLayout::clearLayoutCache() {
getLayoutCache( true );
}
TextLayout::Cache TextLayout::layout( const String::View& string, Font* font,
const Uint32& characterSize, const Uint32& style,
const Uint32& tabWidth, const Float& outlineThickness,
@@ -243,7 +254,6 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font,
TextDirection baseDirection, LineWrapMode wrapMode,
Uint32 wrapWidth, bool keepIndentation,
Float initialXOffset ) {
static LRULayoutCache sLayoutCache;
if ( !font || string.empty() ) {
auto layout = std::make_shared<TextLayout>();
@@ -258,7 +268,7 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font,
tabOffset, baseDirection, wrapMode, wrapWidth, keepIndentation,
initialXOffset );
auto cacheHit = sLayoutCache.get( hash );
auto cacheHit = getLayoutCache().get( hash );
if ( cacheHit.has_value() )
return *cacheHit;
}
@@ -513,7 +523,7 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font,
characterSize, style, tabWidth, outlineThickness, hspace );
}
sLayoutCache.put( hash, resultPtr );
getLayoutCache().put( hash, resultPtr );
return resultPtr;
}

View File

@@ -4,6 +4,7 @@
#include <eepp/graphics/ninepatchmanager.hpp>
#include <eepp/graphics/renderer/renderer.hpp>
#include <eepp/graphics/shaderprogrammanager.hpp>
#include <eepp/graphics/textlayout.hpp>
#include <eepp/graphics/textureatlasmanager.hpp>
#include <eepp/graphics/texturefactory.hpp>
#include <eepp/graphics/vertexbuffermanager.hpp>
@@ -117,6 +118,8 @@ Engine::~Engine() {
ParserMatcherManager::destroySingleton();
Log::destroySingleton();
TextLayout::clearLayoutCache();
}
void Engine::destroy() {