Cleanup: Store BLF Glyph cache with C++ Vector, use std::mutex #118222

Merged
Hans Goudey merged 2 commits from HooglyBoogly/blender:cleanup-glyph-cache-vector into main 2024-02-19 03:34:44 +01:00
3 changed files with 28 additions and 39 deletions

View File

@ -1376,7 +1376,6 @@ static void blf_font_fill(FontBLF *font)
font->char_width = 1.0f;
font->char_spacing = 0.0f;
BLI_listbase_clear(&font->cache);
font->kerning_cache = nullptr;
#if BLF_BLUR_ENABLE
font->blur = 0;
@ -1736,7 +1735,7 @@ static FontBLF *blf_font_new_impl(const char *filepath,
const size_t mem_size,
void *ft_library)
{
FontBLF *font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new");
FontBLF *font = MEM_new<FontBLF>(__func__);
font->mem_name = mem_name ? BLI_strdup(mem_name) : nullptr;
font->filepath = filepath ? BLI_strdup(filepath) : nullptr;
@ -1756,8 +1755,6 @@ static FontBLF *blf_font_new_impl(const char *filepath,
font->ft_lib = ft_library ? (FT_Library)ft_library : ft_lib;
BLI_mutex_init(&font->glyph_cache_mutex);
/* If we have static details about this font file, we don't have to load the Face yet. */
bool face_needed = true;
@ -1854,9 +1851,7 @@ void blf_font_free(FontBLF *font)
MEM_freeN(font->mem_name);
}
BLI_mutex_end(&font->glyph_cache_mutex);
MEM_freeN(font);
MEM_delete(font);
}
/** \} */

View File

@ -75,28 +75,24 @@ static float from_16dot16(FT_Fixed value)
/** \name Glyph Cache
* \{ */
static GlyphCacheBLF *blf_glyph_cache_find(const FontBLF *font, const float size)
static GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, const float size)
{
GlyphCacheBLF *gc = (GlyphCacheBLF *)font->cache.first;
while (gc) {
for (std::unique_ptr<GlyphCacheBLF> &gc : font->cache) {
if (gc->size == size && (gc->bold == ((font->flags & BLF_BOLD) != 0)) &&
(gc->italic == ((font->flags & BLF_ITALIC) != 0)) &&
(gc->char_weight == font->char_weight) && (gc->char_slant == font->char_slant) &&
(gc->char_width == font->char_width) && (gc->char_spacing == font->char_spacing))
{
return gc;
return gc.get();
}
gc = gc->next;
}
return nullptr;
}
static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
{
GlyphCacheBLF *gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
std::unique_ptr<GlyphCacheBLF> gc = std::make_unique<GlyphCacheBLF>(GlyphCacheBLF{});
gc->next = nullptr;
gc->prev = nullptr;
gc->size = font->size;
gc->bold = ((font->flags & BLF_BOLD) != 0);
gc->italic = ((font->flags & BLF_ITALIC) != 0);
@ -125,13 +121,14 @@ static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
gc->fixed_width = 1;
}
BLI_addhead(&font->cache, gc);
return gc;
font->cache.append(std::move(gc));
return font->cache.last().get();
}
GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
{
BLI_mutex_lock(&font->glyph_cache_mutex);
font->glyph_cache_mutex.lock();
GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size);
@ -144,34 +141,28 @@ GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
void blf_glyph_cache_release(FontBLF *font)
{
BLI_mutex_unlock(&font->glyph_cache_mutex);
font->glyph_cache_mutex.unlock();
}
static void blf_glyph_cache_free(GlyphCacheBLF *gc)
GlyphCacheBLF::~GlyphCacheBLF()
{
for (uint i = 0; i < ARRAY_SIZE(gc->bucket); i++) {
while (GlyphBLF *g = static_cast<GlyphBLF *>(BLI_pophead(&gc->bucket[i]))) {
for (uint i = 0; i < ARRAY_SIZE(this->bucket); i++) {
while (GlyphBLF *g = static_cast<GlyphBLF *>(BLI_pophead(&this->bucket[i]))) {
blf_glyph_free(g);
}
}
if (gc->texture) {
GPU_texture_free(gc->texture);
if (this->texture) {
GPU_texture_free(this->texture);
}
if (gc->bitmap_result) {
MEM_freeN(gc->bitmap_result);
if (this->bitmap_result) {
MEM_freeN(this->bitmap_result);
}
MEM_freeN(gc);
}
void blf_glyph_cache_clear(FontBLF *font)
{
BLI_mutex_lock(&font->glyph_cache_mutex);
while (GlyphCacheBLF *gc = static_cast<GlyphCacheBLF *>(BLI_pophead(&font->cache))) {
blf_glyph_cache_free(gc);
}
BLI_mutex_unlock(&font->glyph_cache_mutex);
std::lock_guard lock{font->glyph_cache_mutex};
font->cache.clear_and_shrink();
}
/**

View File

@ -8,6 +8,10 @@
#pragma once
#include <mutex>
#include "BLI_vector.hh"
#include "GPU_texture.h"
#include "GPU_vertex_buffer.h"
@ -113,9 +117,6 @@ struct KerningCacheBLF {
};
struct GlyphCacheBLF {
GlyphCacheBLF *next;
GlyphCacheBLF *prev;
/** Font size. */
float size;
@ -139,6 +140,8 @@ struct GlyphCacheBLF {
int bitmap_len;
int bitmap_len_landed;
int bitmap_len_alloc;
~GlyphCacheBLF();
};
struct GlyphBLF {
@ -361,7 +364,7 @@ struct FontBLF {
* List of glyph caches (#GlyphCacheBLF) for this font for size, DPI, bold, italic.
* Use blf_glyph_cache_acquire(font) and blf_glyph_cache_release(font) to access cache!
*/
ListBase cache;
blender::Vector<std::unique_ptr<GlyphCacheBLF>> cache;
/** Cache of unscaled kerning values. Will be NULL if font does not have kerning. */
KerningCacheBLF *kerning_cache;
@ -385,5 +388,5 @@ struct FontBLF {
FontBufInfoBLF buf_info;
/** Mutex lock for glyph cache. */
ThreadMutex glyph_cache_mutex;
std::mutex glyph_cache_mutex;
};