Refactor draw command recording.

Remove distinct draw primitives and unify them into one shared form.
This commit is contained in:
meehl committed 2026-08-15 19:19:55 +02:00
1 parent a6d18a320c
commit 3fc87339ca
17 files changed
+397 -638

No files matched your search

+25 -84
View File
@@ -1,8 +1,9 @@
use crate::{
color::Srgba,
dpi::{LogicalPoint, LogicalVector},
dpi::LogicalVector,
fonts::{
atlas::FontAtlas, glyph_key::SubpixelBin, layout::LayoutRow, rasterizer::GlyphRasterizer,
atlas::FontAtlas, glyph_key::SubpixelBin, layout::PositionedGlyph,
layout_cache::LayoutCache, rasterizer::GlyphRasterizer,
},
renderer::image::ImageDelta,
util::calculate_hash,
@@ -13,13 +14,14 @@ use parley::{
FontContext, FontFamily, FontFamilyName, FontWeight, GenericFamily, LayoutContext,
StyleProperty, TextStyle, fontique::Blob,
};
use std::sync::Arc;
use std::{rc::Rc, sync::Arc};
pub use layout::{Alignment, FontStyle, Layout, LayoutJob};
mod atlas;
mod glyph_key;
mod layout;
mod layout_cache;
mod rasterizer;
/// Data of a .ttf or .otf file
@@ -65,7 +67,7 @@ impl Fonts {
layout_context: LayoutContext::new(),
atlas: FontAtlas::new(1024, 1024),
glyph_rasterizer: GlyphRasterizer::new(),
layout_cache: LayoutCache::default(),
layout_cache: Default::default(),
};
fonts.register_fonts();
@@ -182,13 +184,21 @@ impl Fonts {
}
}
pub fn layout(&mut self, job: LayoutJob, pixels_per_point: f32) -> Arc<Layout> {
pub fn layout(&mut self, job: LayoutJob, pixels_per_point: f32) -> Rc<Layout> {
let hash = calculate_hash(&(&job, OrderedFloat(pixels_per_point)));
if let Some(cached_layout) = self.layout_cache.get(hash) {
return cached_layout;
}
let layout = Rc::new(self.build_layout(job, pixels_per_point));
self.layout_cache.insert(hash, Rc::clone(&layout));
layout
}
fn build_layout(&mut self, job: LayoutJob, pixels_per_point: f32) -> Layout {
let default_style = TextStyle::default();
let style = TextStyle {
font_family: job.font_family,
@@ -233,13 +243,9 @@ impl Fonts {
parley_layout.align(alignment, parley::AlignmentOptions::default());
}
let mut layout_rows = Vec::new();
let mut num_of_vertices = 0;
let mut num_of_indices = 0;
let mut glyphs = Vec::new();
for line in parley_layout.lines() {
let mut layout_row = LayoutRow::default();
for item in line.items() {
let parley::PositionedLayoutItem::GlyphRun(run) = item else {
continue;
@@ -255,85 +261,20 @@ impl Fonts {
continue;
};
layout_row.glyphs.push(glyph);
num_of_vertices += 4;
num_of_indices += 6;
glyphs.push(PositionedGlyph {
rect: glyph.rect,
uv: glyph.uv,
layer_idx: glyph.texture_layer_idx,
color: glyph.color,
});
}
}
if !layout_row.glyphs.is_empty() {
layout_rows.push(layout_row);
}
}
let layout = Arc::new(Layout {
job_hash: hash,
Layout {
width: parley_layout.width(),
glyphs,
parley_layout,
rows: layout_rows,
num_of_vertices,
num_of_indices,
});
self.layout_cache.insert(hash, Arc::clone(&layout));
layout
}
/// Width of laid out text
pub fn get_text_width(&mut self, job: LayoutJob, pixels_per_point: f32) -> i32 {
let layout = self.layout(job, pixels_per_point);
layout.width() as i32
}
/// Text index at cursor location
pub fn get_text_index_at_cursor(
&mut self,
job: LayoutJob,
cursor: LogicalPoint<f32>,
pixels_per_point: f32,
) -> usize {
let layout = self.layout(job, pixels_per_point);
layout.cursor_index(cursor)
}
}
struct CachedLayout {
generation: u32,
layout: Arc<Layout>,
}
#[derive(Default)]
struct LayoutCache {
current_generation: u32,
cache: nohash_hasher::IntMap<u64, CachedLayout>,
}
impl LayoutCache {
fn get(&mut self, hash: u64) -> Option<Arc<Layout>> {
match self.cache.entry(hash) {
std::collections::hash_map::Entry::Occupied(entry) => {
let cached = entry.into_mut();
cached.generation = self.current_generation;
Some(Arc::clone(&cached.layout))
}
std::collections::hash_map::Entry::Vacant(_) => None,
}
}
fn insert(&mut self, hash: u64, layout: Arc<Layout>) {
self.cache.insert(
hash,
CachedLayout {
generation: self.current_generation,
layout,
},
);
}
/// Removes unused layouts
pub fn flush(&mut self) {
self.cache
.retain(|_key, cached| cached.generation == self.current_generation);
self.current_generation = self.current_generation.wrapping_add(1);
}
}