Skip to content

text-freetype2: Evict old glyphs when the atlas is full - #196

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/195-freetype-glyph-atlas
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/195-freetype-glyph-atlas

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Description

A FreeType 2 text source whose text keeps changing, for example one reading a now-playing file, starts dropping letters after a while. Once that starts, every letter it has not drawn before is left out until the font or antialiasing setting is changed. With this change the source makes room for the new text, and every letter is drawn.

The cause: each source caches the glyphs it has drawn in a single 2048x2048 texture atlas. The atlas is only emptied when the font or antialiasing changes, never when the text changes. When a glyph doesn't fit, cache_glyphs() logs "Out of space trying to render glyphs" and stops, and fill_vertex_buffer() skips every character that has no cached glyph. At the v2 source's default size of 256 px, the standard ASCII set the source caches up front already fills the atlas to y=1200 of 2048. A few dozen more letters (Cyrillic in the report, but any script works) fill the rest.

  1. test/cmocka: Add RED FreeType 2 glyph atlas tests: moves the atlas packing out of cache_glyphs() into glyph_atlas_cache() in plugins/text-freetype2/glyph-atlas.c, with no FreeType or graphics calls. FreeType rendering, storing a glyph and clearing the atlas are passed in as callbacks. The function keeps today's behaviour. It adds test_glyph_atlas, which packs fixed 10x10 glyphs into a 64x64 atlas. 2 of its 5 tests fail on this commit:
    • new_text_after_full_atlas_is_drawn: "glyph U+041C of the current text was never cached"
    • texts_keep_rotating_without_losing_letters: "glyph U+0419 of the current text was never cached"
  2. text-freetype2: Evict old glyphs when the atlas is full: when a glyph of the current text doesn't fit, the atlas is emptied once and the current text is cached again from the start. Emptying it frees the cached glyph records and zeroes the pixel buffer. An atlas that was already empty when the text arrived is not emptied again, so a single text with more glyphs than fit at its size still stops with the warning instead of looping. All 5 tests pass.

Motivation and Context

Fixes #195 (upstream obsproject/obs-studio#11819, reported by iarspider)

Why this one: upstream confirmed it in February 2025, and there is still no upstream fix PR. It affects anyone with a text source whose text keeps changing, such as now-playing titles or chat logs. The default 256 px size makes it likely: replaying the reporter's 1055 titles in file order, the 17th already loses letters. Nothing on screen says why; the only sign is one warning line in the log.

How Has This Been Tested?

Automated (cmocka; no FreeType or graphics device needed):

Test Checks RED commit GREEN commit
text_that_fits_is_cached_once glyphs are cached once and a cached text adds nothing passes passes
atlas_holds_its_capacity 25 glyphs fill the 64x64 atlas without clearing it passes passes
new_text_after_full_atlas_is_drawn after Latin text fills the atlas, every glyph of a Cyrillic title is cached and none overlap fails passes
texts_keep_rotating_without_losing_letters ten titles of 8 new letters each (80 glyphs in a 25-glyph atlas) are each fully drawable fails passes
text_larger_than_atlas_is_not_retried_forever a single text bigger than the atlas stops with out_of_space and does not clear an atlas that started empty passes passes

I built and ran both commits on Arch Linux with FreeType 2.14.3. The rest of ctest (61 tests in all) passes on the GREEN commit, and text-freetype2 builds without warnings.

Replay: a headless harness renders real glyphs with FreeType through glyph_atlas_cache() in the same way cache_glyphs() does. It uses Liberation Sans, the default "Sans Serif" match here. It caches the standard glyph set first, then feeds the reporter's record_archive.txt (1055 titles, 136 distinct characters) one title at a time, and counts titles with at least one uncached letter:

Font size Before: titles with missing letters First affected title After: titles with missing letters Times the atlas was emptied (after)
32 px 0 none 0 0
128 px 0 none 0 0
256 px (default) 201 of 1055 #17 0 6
400 px 540 of 1055 #1 0 212

clang-format 22.1.3 and gersemi 0.25.0 (./lint's C and CMake checks) pass on the changed files. The Rust steps of ./lint and ./unittest need soldr, which isn't installed here, and the Tier 2 Docker lane couldn't run without Docker access. This PR changes no Rust.

Not tested:

  • Windows and macOS. The new code is plain C, and the test only needs cmocka.
  • A running OBS with a live text source. The replay drives the packing with real FreeType rendering but without the graphics texture upload.
  • Fonts other than Liberation Sans.
  • A single text with more distinct glyphs than fit in 2048x2048 at its size, for example a very long multilingual text at a large size. It still loses letters as before, with the same warning. Fixing that would need a larger or multi-page atlas, which could be a follow-up.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machine.

🤖 Generated with Claude Code

A FreeType 2 text source caches every glyph it has drawn in one
2048x2048 atlas and only empties it when the font or antialiasing
changes. A source reading a file that keeps changing therefore fills
the atlas, and from then on every new letter is skipped with only an
"Out of space trying to render glyphs" warning in the log (upstream
obsproject/obs-studio#11819, reported by iarspider). At the default
256 px size the standard ASCII set alone uses most of the atlas.

Move the atlas packing out of cache_glyphs() into glyph-atlas.c, free
of FreeType and graphics calls, and test it with fixed-size glyphs.
The function keeps today's behaviour (stop at the first glyph that does
not fit, never evict), so 2 of the 5 tests fail:

- new_text_after_full_atlas_is_drawn
- texts_keep_rotating_without_losing_letters

Refs obs-rust#195.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 04:50
When a glyph of the current text does not fit in the source's atlas,
empty the atlas once and cache the current text again, so glyphs of
earlier texts make room for it instead of new letters being skipped
until the font changes (upstream obsproject/obs-studio#11819, reported
by iarspider). An atlas that was already empty when the text arrived is
not cleared again: a text with more glyphs than fit at its size still
stops with the "Out of space" warning rather than looping.

new_text_after_full_atlas_is_drawn and
texts_keep_rotating_without_losing_letters now pass; 5 of 5 tests pass.

Fixes obs-rust#195.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=5ec92a6bd2fce8c1720c3efcd0b61e134dc8d170 secs=106 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791695031,"gate":"rust/all/fmt","host":"linux-x86_64","key":"7a2b3cf7ebcdb5a8fbf224947731272211ca277dcd3072c193dcecf48875a9b0","lane":"lint","parents":["d94bdbd0f89e12315ecfdf7e140959c73eb5113e"],"secs":15,"stamp":"4791d58b61f8de2acd86c29caec45236","tree":"5ec92a6bd2fce8c1720c3efcd0b61e134dc8d170","v":1,"via":"run"}
Ci-Attestation: {"at":1791695031,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"7a2b3cf7ebcdb5a8fbf224947731272211ca277dcd3072c193dcecf48875a9b0","lane":"lint","parents":["d94bdbd0f89e12315ecfdf7e140959c73eb5113e"],"secs":15,"stamp":"79cfd47fd161716d09fc1a738f9465ed","tree":"5ec92a6bd2fce8c1720c3efcd0b61e134dc8d170","v":1,"via":"run"}
Ci-Attestation: {"at":1791695031,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"8a24c8171e0fd3cce5f1a5116ef1b2d46335f2a140a61756c426464d6221ee24","lane":"rust-test","parents":["d94bdbd0f89e12315ecfdf7e140959c73eb5113e"],"secs":30,"stamp":"a4073f3b80f5d3fb156010b3fb975846","tree":"5ec92a6bd2fce8c1720c3efcd0b61e134dc8d170","v":1,"via":"run"}
Ci-Attestation: {"at":1791695031,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"787348aaa3944da2e0a259d8b86a3f6671e73ead1bcfc15f57cc4bcef22fca4a","lane":"tier2-linux","parents":["d94bdbd0f89e12315ecfdf7e140959c73eb5113e"],"secs":60,"stamp":"6278b9d9aaca814d5377b2b629def250","tree":"5ec92a6bd2fce8c1720c3efcd0b61e134dc8d170","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is f586019c7.

  • RED (d94bdbd0f): Tier 2 passed 60 of 61. test_glyph_atlas failed 2 of 5 (new_text_after_full_atlas_is_drawn, texts_keep_rotating_without_losing_letters).
  • GREEN (c39d0226c): Tier 2 passed 61 of 61 with ENABLE_RUST_LIBOBS OFF and 61 of 61 with it ON.
  • Local gate: lint, rust-test and tier2-linux all passed.

Merging once CI is green.

@zackees
zackees force-pushed the fix/195-freetype-glyph-atlas branch from c39d022 to f586019 Compare October 11, 2026 05:03
@zackees
zackees merged commit dbe3c36 into obs-rust:master Oct 11, 2026
23 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 18, 2026
get_font_path() read FC_INDEX with FcPatternGetInteger(match, FC_INDEX,
1, ...). The third argument is the position among the property's values,
and a matched pattern has one index, at position 0. The call always
failed, fc_index kept its default 0, and FT_New_Face() loaded the first
face of the file (obsproject/obs-studio#11819).

So any font that is not face 0 of its file rendered as face 0: Bold and
the other named instances of a variable font (Adwaita Sans Bold is index
458752 in AdwaitaSans-Regular.ttf), and the faces of a .ttc collection
(Noto Sans CJK SC is face 2 of NotoSansCJK-Regular.ttc; it rendered as
the JP face 0).

Port of the first commit of obsproject/obs-studio#13290 by reyohoho. The
other two commits there (a larger glyph atlas, pre-cached Cyrillic) are
covered here by obs-rust#196's atlas fix and are not part of this change.

test_text_freetype2_face_index: 2 of 2. In the app, Adwaita Sans Bold now
renders bold (513 -> 525 px wide, ink 0.185 -> 0.273 at 96 px), and Noto
Sans CJK SC renders the SC glyphs instead of JP's.

Fixes obs-rust#1409.

Co-Authored-By: reyohoho <[email protected]>
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 18, 2026
get_font_path() read FC_INDEX with FcPatternGetInteger(match, FC_INDEX,
1, ...). The third argument is the position among the property's values,
and a matched pattern has one index, at position 0. The call always
failed, fc_index kept its default 0, and FT_New_Face() loaded the first
face of the file (obsproject/obs-studio#11819).

So any font that is not face 0 of its file rendered as face 0: Bold and
the other named instances of a variable font (Adwaita Sans Bold is index
458752 in AdwaitaSans-Regular.ttf), and the faces of a .ttc collection
(Noto Sans CJK SC is face 2 of NotoSansCJK-Regular.ttc; it rendered as
the JP face 0).

Port of the first commit of obsproject/obs-studio#13290 by reyohoho. The
other two commits there (a larger glyph atlas, pre-cached Cyrillic) are
covered here by obs-rust#196's atlas fix and are not part of this change.

test_text_freetype2_face_index: 2 of 2. In the app, Adwaita Sans Bold now
renders bold (513 -> 525 px wide, ink 0.185 -> 0.273 at 96 px), and Noto
Sans CJK SC renders the SC glyphs instead of JP's.

Fixes obs-rust#1409.

Co-Authored-By: reyohoho <[email protected]>
vpacher pushed a commit to vpacher/obs-studio that referenced this pull request Oct 18, 2026
…bs-rust#1411)

* test/cmocka: Add failing test for a font that is not face 0 of its file

The Text (FreeType 2) source on Linux loads the first face of a font file
whatever face fontconfig matched (obsproject/obs-studio#11819). A face of
a .ttc/.otc collection, such as Noto Sans CJK SC, TC or KR, or a named
instance of a variable font, such as Adwaita Sans Bold, renders as face
0.

test_text_freetype2_face_index points fontconfig at a directory that
holds only text-collection.ttc, a two-face collection generated by
test/cmocka/data/text-collection.py from synthetic outlines ("OBS
Collection Fixture" Regular as face 0, Bold as face 1), and asks
get_font_path() for each style.

Fails 1 of 2: bold_resolves_to_face_1 ("Bold resolved to face 0 of
.../text-collection.ttc (expected 1)"). Regular passes. Needs no display.

Refs obs-rust#1409.

* text-freetype2: Read the matched font's face index from fontconfig

get_font_path() read FC_INDEX with FcPatternGetInteger(match, FC_INDEX,
1, ...). The third argument is the position among the property's values,
and a matched pattern has one index, at position 0. The call always
failed, fc_index kept its default 0, and FT_New_Face() loaded the first
face of the file (obsproject/obs-studio#11819).

So any font that is not face 0 of its file rendered as face 0: Bold and
the other named instances of a variable font (Adwaita Sans Bold is index
458752 in AdwaitaSans-Regular.ttf), and the faces of a .ttc collection
(Noto Sans CJK SC is face 2 of NotoSansCJK-Regular.ttc; it rendered as
the JP face 0).

Port of the first commit of obsproject/obs-studio#13290 by reyohoho. The
other two commits there (a larger glyph atlas, pre-cached Cyrillic) are
covered here by obs-rust#196's atlas fix and are not part of this change.

test_text_freetype2_face_index: 2 of 2. In the app, Adwaita Sans Bold now
renders bold (513 -> 525 px wide, ink 0.185 -> 0.273 at 96 px), and Noto
Sans CJK SC renders the SC glyphs instead of JP's.

Fixes obs-rust#1409.

Co-Authored-By: reyohoho <[email protected]>

---------

Co-authored-by: reyohoho <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: text-freetype2: letters go missing once the glyph atlas fills (upstream #11819)

2 participants