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

+17 -19
View File
@@ -1,4 +1,7 @@
use crate::{color::Srgba, dpi::LogicalPoint, fonts::rasterizer::RasterizedGlyph};
use crate::{
color::Srgba,
dpi::{LogicalPoint, LogicalRect, NormalizedRect},
};
use ordered_float::OrderedFloat;
use parley::{FontFamily, FontFamilyName};
@@ -99,33 +102,28 @@ impl std::hash::Hash for LayoutJob<'_> {
}
}
#[derive(Default)]
pub struct LayoutRow {
pub glyphs: Vec<RasterizedGlyph>,
pub struct PositionedGlyph {
// relative to layout origin
// TODO: introduce new "layout space"?
pub rect: LogicalRect<f32>,
pub uv: NormalizedRect,
pub layer_idx: u32,
pub color: Srgba,
}
pub struct Layout {
pub job_hash: u64,
pub glyphs: Vec<PositionedGlyph>,
pub width: f32,
// kept for parley's cursor helper
pub parley_layout: parley::Layout<Srgba>,
pub rows: Vec<LayoutRow>,
pub num_of_vertices: usize,
pub num_of_indices: usize,
}
impl Layout {
pub fn width(&self) -> f32 {
self.parley_layout.full_width()
self.width
}
/// Returns text index at cursor position
pub fn cursor_index(&self, cursor: LogicalPoint<f32>) -> usize {
let cursor = parley::Cursor::from_point(&self.parley_layout, cursor.x, cursor.y);
cursor.index()
}
}
impl std::hash::Hash for Layout {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.job_hash.hash(state);
pub fn cursor_index_at(&self, point: LogicalPoint<f32>) -> usize {
parley::Cursor::from_point(&self.parley_layout, point.x, point.y).index()
}
}
+45
View File
@@ -0,0 +1,45 @@
use std::rc::Rc;
use crate::fonts::Layout;
struct CachedLayout {
layout: Rc<Layout>,
// used to identify unused layouts
generation: u32,
}
#[derive(Default)]
pub struct LayoutCache {
cache: nohash_hasher::IntMap<u64, CachedLayout>,
current_generation: u32,
}
impl LayoutCache {
pub fn get(&mut self, hash: u64) -> Option<Rc<Layout>> {
match self.cache.entry(hash) {
std::collections::hash_map::Entry::Occupied(entry) => {
let cached = entry.into_mut();
cached.generation = self.current_generation;
Some(Rc::clone(&cached.layout))
}
std::collections::hash_map::Entry::Vacant(_) => None,
}
}
pub fn insert(&mut self, hash: u64, layout: Rc<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);
}
}