Refactor: BLF Use Vector for Glyph Cache List #118174

Closed
Harley Acheson wants to merge 3 commits from Harley/blender:BlfGlyphCacheList into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
2 changed files with 13 additions and 19 deletions
Showing only changes of commit 3ee1c4e62e - Show all commits

View File

@ -131,24 +131,20 @@ static void blf_glyph_cache_free(GlyphCacheBLF *gc)
/** \name Glyph Cache List
* \{ */
GlyphCacheListBLF::GlyphCacheListBLF(FontBLF *font) : list{}, font(font)
{
BLI_mutex_init(&glyph_cache_mutex);
}
GlyphCacheListBLF::GlyphCacheListBLF(FontBLF *font) : list{}, font(font) {}
GlyphCacheListBLF ::~GlyphCacheListBLF()
{
clear();
BLI_mutex_end(&glyph_cache_mutex);
};
GlyphCacheBLF *GlyphCacheListBLF::acquire()
{
BLI_mutex_lock(&glyph_cache_mutex);
glyph_cache_mutex.lock();
GlyphCacheBLF *gc = nullptr;
for (auto &entry : list) {
for (GlyphCacheBLF *entry : list) {
if (entry->size == font->size && (entry->bold == ((font->flags & BLF_BOLD) != 0)) &&
(entry->italic == ((font->flags & BLF_ITALIC) != 0)) &&
(entry->char_weight == font->char_weight) && (entry->char_slant == font->char_slant) &&
@ -161,7 +157,7 @@ GlyphCacheBLF *GlyphCacheListBLF::acquire()
if (!gc) {
gc = blf_glyph_cache_new(font);
list.push_back(gc);
list.append(gc);
}
return gc;
@ -169,17 +165,17 @@ GlyphCacheBLF *GlyphCacheListBLF::acquire()
void GlyphCacheListBLF::release()
{
BLI_mutex_unlock(&glyph_cache_mutex);
this->glyph_cache_mutex.unlock();
}
void GlyphCacheListBLF::clear()
{
BLI_mutex_lock(&glyph_cache_mutex);
for (auto &entry : list) {
glyph_cache_mutex.lock();
for (GlyphCacheBLF *entry : list) {
blf_glyph_cache_free(entry);
}
list.clear();
BLI_mutex_unlock(&glyph_cache_mutex);
glyph_cache_mutex.unlock();
}
/**

View File

@ -8,7 +8,9 @@
#pragma once
#include <vector>
#include <mutex>
#include "BLI_vector.hh"
#include "GPU_texture.h"
#include "GPU_vertex_buffer.h"
@ -109,13 +111,9 @@ typedef struct KerningCacheBLF {
} KerningCacheBLF;
Harley marked this conversation as resolved Outdated

private is redundant for classes which are private by default

`private` is redundant for classes which are private by default
class GlyphCacheListBLF {
Harley marked this conversation as resolved
Review

blender::Vector is generally preferable since it has methods to work better with other Blender containers, and it has an inline buffer to avoid allocations for small numbers of elements

`blender::Vector` is generally preferable since it has methods to work better with other Blender containers, and it has an inline buffer to avoid allocations for small numbers of elements
private:
std::vector<GlyphCacheBLF *> list;
blender::Vector<GlyphCacheBLF *> list;
FontBLF *font;
Review

Sorry, I didn't realize this pointer here as the resolution to the need to pass "font" to the functions. I'll propose an alternative change to see what you think

Sorry, I didn't realize this pointer here as the resolution to the need to pass "font" to the functions. I'll propose an alternative change to see what you think
/** Mutex lock for glyph cache. */
ThreadMutex glyph_cache_mutex;
std::mutex glyph_cache_mutex;
public:
Harley marked this conversation as resolved
Review

It would probably be simpler/easy to switch to std::mutex now too :)

It would probably be simpler/easy to switch to `std::mutex` now too :)
GlyphCacheListBLF(FontBLF *font);