video: truetype: Add a scratch buffer to use malloc() less

The stb_truetype library performs around 5 allocations per character
rendered, totalling approximately 26KB of temporary memory. This creates
significant malloc/free overhead and heap fragmentation.

Add a scratch buffer mechanism that pre-allocates memory once during
probe and reuses it for each character. The buffer is reset at the start
of each putc_xy() call, and allocations come from this buffer using a
simple bump allocator with 8-byte alignment.

If the scratch buffer is exhausted (e.g. for very complex glyphs), the
allocator falls back to malloc transparently.

The scratch buffer is controlled by two new Kconfig options:
- CONSOLE_TRUETYPE_SCRATCH: Enable/disable the feature (default y)
- CONSOLE_TRUETYPE_SCRATCH_SIZE: Buffer size in bytes (default 32KB)

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2026-01-02 06:29:55 -07:00
parent a7adec5ae2
commit 3f453d0110
4 changed files with 136 additions and 4 deletions

View File

@@ -85,6 +85,15 @@ CONFIG_CONSOLE_TRUETYPE_GLYPH_BUF enables a pre-allocated buffer for glyph
rendering, avoiding malloc/free per character. The buffer starts at 4KB and
grows as needed via realloc().
CONFIG_CONSOLE_TRUETYPE_SCRATCH enables a scratch buffer for internal stbtt
allocations. Without this, the TrueType library performs around 5 allocations
per character (totalling ~26KB), creating malloc/free overhead and heap
fragmentation. With the scratch buffer, memory is allocated once at probe time
and reused for each character. CONFIG_CONSOLE_TRUETYPE_SCRATCH_SIZE sets the
buffer size (default 32KB), which is sufficient for most Latin characters.
Complex glyphs (CJK, emoji) or very large font sizes may need 64KB or more.
Allocations exceeding the buffer size fall back to malloc transparently.
CONFIG_VIDEO_GLYPH_STATS enables tracking of glyph-rendering statistics.
Return value