From 92e61afda22df57226deec9ff66d2056b1892f30 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 21 Oct 2026 06:53:49 +0000 Subject: [PATCH] epaint: rasterize glyphs on a single-threaded vello context With vello_cpu's multithreading feature on anywhere in the build, RenderContext::new starts a thread pool per glyph and render panics without a flush. The epaint tests now run with that feature on. --- Cargo.lock | 22 ++++++++++++++++++++++ crates/epaint/Cargo.toml | 4 ++++ crates/epaint/src/text/font_face.rs | 20 ++++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49783a14..5cc641f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3412,6 +3412,15 @@ dependencies = [ "libredox", ] +[[package]] +name = "ordered-channel" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95be4d57809897b5a7539fc15a7dfe0e84141bc3dfaa2e9b1b27caa90acf61ab" +dependencies = [ + "crossbeam-channel", +] + [[package]] name = "ordered-float" version = "5.5.0" @@ -4889,6 +4898,15 @@ dependencies = [ "syn 3.0.5", ] +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + [[package]] name = "tiff" version = "0.11.3" @@ -5347,9 +5365,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace506ca414548966fcf3c283be94eb1cad8254de488791ee698bf438b0890a9" dependencies = [ "bytemuck", + "crossbeam-channel", "glifo", "hashbrown 0.17.1", + "ordered-channel", "png 0.18.1", + "rayon", + "thread_local", "vello_common", ] diff --git a/crates/epaint/Cargo.toml b/crates/epaint/Cargo.toml index 653df9b7..ff2522e1 100644 --- a/crates/epaint/Cargo.toml +++ b/crates/epaint/Cargo.toml @@ -118,6 +118,10 @@ criterion.workspace = true mimalloc.workspace = true similar-asserts.workspace = true +# Runs the glyph rasterizer through vello_cpu's multi-threaded dispatcher, which apps can turn on. +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +vello_cpu = { workspace = true, features = ["multithreading"] } + [[bench]] name = "benchmark" diff --git a/crates/epaint/src/text/font_face.rs b/crates/epaint/src/text/font_face.rs index fc8154e0..fb88f1f6 100644 --- a/crates/epaint/src/text/font_face.rs +++ b/crates/epaint/src/text/font_face.rs @@ -178,7 +178,7 @@ impl FontCell { return None; } - let mut ctx = vello_cpu::RenderContext::new(width, height); + let mut ctx = glyph_render_context(width, height); ctx.set_transform(kurbo::Affine::translate((-bounds.x0, -bounds.y0))); ctx.set_paint(color::OpaqueColor::::WHITE); ctx.fill_path(&path); @@ -251,7 +251,7 @@ impl FontCell { ); let coords: Vec = location.coords().iter().map(|c| c.to_bits()).collect(); - let mut ctx = vello_cpu::RenderContext::new(canvas_size, canvas_size); + let mut ctx = glyph_render_context(canvas_size, canvas_size); let mut resources = vello_cpu::Resources::new(); ctx.set_paint(color::OpaqueColor::::WHITE); ctx.glyph_run(&mut resources, &font) @@ -767,3 +767,19 @@ pub(crate) struct ShapedCluster<'a> { /// See [`ShapedGlyph::is_cjk`]. pub is_cjk: bool, } + +/// A single-threaded vello context for one glyph. +/// +/// `RenderContext::new` uses worker threads when another crate in the build turns on `vello_cpu`'s +/// `multithreading` feature: that builds a thread pool per glyph and panics in `render` without a +/// `flush` first. +fn glyph_render_context(width: u16, height: u16) -> vello_cpu::RenderContext { + vello_cpu::RenderContext::new_with( + width, + height, + vello_cpu::RenderSettings { + num_threads: 0, + ..Default::default() + }, + ) +}