Merge pull request #73987 from bruvzg/ft_lock

[TextServer] Add mutex for FreeType face creation/deletion operations.
This commit is contained in:
Rémi Verschelde 2023-03-03 11:04:07 +01:00
commit a8d2e7f342
No known key found for this signature in database
GPG Key ID: C3336907360768E1
4 changed files with 92 additions and 66 deletions

View File

@ -386,6 +386,8 @@ int64_t TextServerAdvanced::_get_features() const {
void TextServerAdvanced::_free_rid(const RID &p_rid) { void TextServerAdvanced::_free_rid(const RID &p_rid) {
_THREAD_SAFE_METHOD_ _THREAD_SAFE_METHOD_
if (font_owner.owns(p_rid)) { if (font_owner.owns(p_rid)) {
MutexLock ftlock(ft_mutex);
FontAdvanced *fd = font_owner.get_or_null(p_rid); FontAdvanced *fd = font_owner.get_or_null(p_rid);
{ {
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
@ -1321,6 +1323,8 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
// Init dynamic font. // Init dynamic font.
#ifdef MODULE_FREETYPE_ENABLED #ifdef MODULE_FREETYPE_ENABLED
int error = 0; int error = 0;
{
MutexLock ftlock(ft_mutex);
if (!ft_library) { if (!ft_library) {
error = FT_Init_FreeType(&ft_library); error = FT_Init_FreeType(&ft_library);
if (error != 0) { if (error != 0) {
@ -1361,6 +1365,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
memdelete(fd); memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'."); ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
} }
}
if (p_font_data->msdf) { if (p_font_data->msdf) {
fd->oversampling = 1.0; fd->oversampling = 1.0;
@ -1786,6 +1791,8 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
} }
_FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) { _FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) {
MutexLock ftlock(ft_mutex);
for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : p_font_data->cache) { for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : p_font_data->cache) {
memdelete(E.value); memdelete(E.value);
} }
@ -1892,6 +1899,8 @@ int64_t TextServerAdvanced::_font_get_face_count(const RID &p_font_rid) const {
fargs.flags = FT_OPEN_MEMORY; fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &stream; fargs.stream = &stream;
MutexLock ftlock(ft_mutex);
FT_Face tmp_face = nullptr; FT_Face tmp_face = nullptr;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) { if (error == 0) {
@ -2283,6 +2292,7 @@ void TextServerAdvanced::_font_clear_size_cache(const RID &p_font_rid) {
ERR_FAIL_COND(!fd); ERR_FAIL_COND(!fd);
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
MutexLock ftlock(ft_mutex);
for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) { for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
memdelete(E.value); memdelete(E.value);
} }
@ -2294,6 +2304,7 @@ void TextServerAdvanced::_font_remove_size_cache(const RID &p_font_rid, const Ve
ERR_FAIL_COND(!fd); ERR_FAIL_COND(!fd);
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
MutexLock ftlock(ft_mutex);
if (fd->cache.has(p_size)) { if (fd->cache.has(p_size)) {
memdelete(fd->cache[p_size]); memdelete(fd->cache[p_size]);
fd->cache.erase(p_size); fd->cache.erase(p_size);

View File

@ -616,6 +616,8 @@ class TextServerAdvanced : public TextServerExtension {
_FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs); _FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);
Mutex ft_mutex;
// HarfBuzz bitmap font interface. // HarfBuzz bitmap font interface.
static hb_font_funcs_t *funcs; static hb_font_funcs_t *funcs;

View File

@ -113,6 +113,8 @@ int64_t TextServerFallback::_get_features() const {
void TextServerFallback::_free_rid(const RID &p_rid) { void TextServerFallback::_free_rid(const RID &p_rid) {
_THREAD_SAFE_METHOD_ _THREAD_SAFE_METHOD_
if (font_owner.owns(p_rid)) { if (font_owner.owns(p_rid)) {
MutexLock ftlock(ft_mutex);
FontFallback *fd = font_owner.get_or_null(p_rid); FontFallback *fd = font_owner.get_or_null(p_rid);
{ {
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
@ -760,6 +762,8 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
// Init dynamic font. // Init dynamic font.
#ifdef MODULE_FREETYPE_ENABLED #ifdef MODULE_FREETYPE_ENABLED
int error = 0; int error = 0;
{
MutexLock ftlock(ft_mutex);
if (!ft_library) { if (!ft_library) {
error = FT_Init_FreeType(&ft_library); error = FT_Init_FreeType(&ft_library);
if (error != 0) { if (error != 0) {
@ -800,6 +804,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
memdelete(fd); memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'."); ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
} }
}
if (p_font_data->msdf) { if (p_font_data->msdf) {
fd->oversampling = 1.0; fd->oversampling = 1.0;
@ -907,6 +912,8 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
} }
_FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) { _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
MutexLock ftlock(ft_mutex);
for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) { for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) {
memdelete(E.value); memdelete(E.value);
} }
@ -1010,6 +1017,8 @@ int64_t TextServerFallback::_font_get_face_count(const RID &p_font_rid) const {
fargs.flags = FT_OPEN_MEMORY; fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &stream; fargs.stream = &stream;
MutexLock ftlock(ft_mutex);
FT_Face tmp_face = nullptr; FT_Face tmp_face = nullptr;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) { if (error == 0) {
@ -1391,6 +1400,7 @@ void TextServerFallback::_font_clear_size_cache(const RID &p_font_rid) {
ERR_FAIL_COND(!fd); ERR_FAIL_COND(!fd);
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
MutexLock ftlock(ft_mutex);
for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) { for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
memdelete(E.value); memdelete(E.value);
} }
@ -1402,6 +1412,7 @@ void TextServerFallback::_font_remove_size_cache(const RID &p_font_rid, const Ve
ERR_FAIL_COND(!fd); ERR_FAIL_COND(!fd);
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
MutexLock ftlock(ft_mutex);
if (fd->cache.has(p_size)) { if (fd->cache.has(p_size)) {
memdelete(fd->cache[p_size]); memdelete(fd->cache[p_size]);
fd->cache.erase(p_size); fd->cache.erase(p_size);

View File

@ -531,6 +531,8 @@ class TextServerFallback : public TextServerExtension {
void _realign(ShapedTextDataFallback *p_sd) const; void _realign(ShapedTextDataFallback *p_sd) const;
Mutex ft_mutex;
protected: protected:
static void _bind_methods(){}; static void _bind_methods(){};