euv-core 0.47.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Persistent per-frame scratch and string-recycling arena retained inside `DisplayList` for zero-alloc steady-state frames.
#[derive(Debug)]
pub(crate) struct DisplayListFrameArena {
    /// Recycled `String` backing buffers reused for `DrawCommand::TextRun` and `DrawCommand::TextCaret`.
    pub(crate) string_pool: Vec<String>,
    /// Persistent scratch queue collecting deferred top-layer portal/fixed nodes across frames.
    pub(crate) portals_scratch: Vec<(i32, usize, NodeId)>,
    /// Persistent stack-allocated scratch buffer for nested z-index child sorting across frames.
    pub(crate) z_sort_scratch: Vec<(i32, usize, NodeId)>,
    /// Persistent scratch buffer of ancestor handles leading to live `Portal` / `Position::Fixed` nodes across frames.
    pub(crate) portal_ancestors_scratch: Vec<NodeId>,
    /// Monotonic version counter bumped only when the base scene slice `[0..portal_split_index]` changes.
    pub(crate) base_layer_version: u64,
    /// Previous frame's streaming digest over the base scene slice for O(1) portal backdrop cache matching.
    pub(crate) prev_base_digest: u64,
    /// Current frame's incrementally folded digest over the base scene slice.
    pub(crate) cur_base_digest: u64,
    /// Current frame's incrementally folded fast digest over all emitted commands.
    pub(crate) running_fast_hash: u64,
    /// True once `portal_split_index` has been finalized for the current frame.
    pub(crate) base_layer_finalized: bool,
}

/// Reusable backend-agnostic command buffer emitted once per frame for Software, Hardware, or Hybrid execution.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct DisplayList {
    /// Ordered drawing commands.
    pub(crate) commands: Vec<DrawCommand>,
    /// Logical viewport size associated with this display list.
    #[get(type(copy))]
    pub(crate) viewport: Size,
    /// Scale factor mapping logical coordinates to physical pixels.
    #[get(type(copy))]
    pub(crate) scale_factor: f32,
    /// Command index separating base-scene commands `[0..portal_split_index]` from top-layer portal/fixed overlays.
    #[get(type(copy))]
    pub(crate) portal_split_index: usize,
    /// Persistent per-frame arena holding recycled strings, scratch queues, and O(1) streaming digests.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) arena: DisplayListFrameArena,
}

/// Cross-frame retained `CachedDrawCommand` fragment descriptor referencing ranges inside `PaintFragmentCache`'s flat slabs.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct RetainedPaintFragment {
    /// True once this slot holds an active cached subtree fragment.
    pub(crate) occupied: bool,
    /// Stable node identity (index and generation) owning this fragment.
    pub(crate) node_id: NodeId,
    /// Quantized physical DPI scale (`(scale * 1000.0).round() as u32`) supporting fractional DPRs (`1.25x`, `1.5x`, `2.625x`, `3.0x`).
    pub(crate) phys_scale_milli: u32,
    /// Bit representation of accumulated ancestor opacity at capture time.
    pub(crate) opacity_bits: u32,
    /// Physical displacement `(offset_x_phys, offset_y_phys)` at capture time.
    pub(crate) offset_phys: Point,
    /// Deterministic fingerprint of container and descendant layout geometry.
    pub(crate) geom_hash: u64,
    /// Deterministic fingerprint of container and descendant paint/text styles and content.
    pub(crate) style_text_hash: u64,
    /// Start index into `PaintFragmentCache::cmd_slab`.
    pub(crate) cmd_start: u32,
    /// Active command count stored inside `cmd_slab`.
    pub(crate) cmd_len: u32,
    /// Reserved command capacity owned by this slot for zero-growth in-place updates.
    pub(crate) cmd_cap: u32,
    /// Start byte offset into `PaintFragmentCache::text_slab`.
    pub(crate) text_start: u32,
    /// Reserved UTF-8 byte capacity owned by this slot for zero-growth in-place updates.
    pub(crate) text_cap: u32,
}

/// Per-canvas output stability record distinguishing static icon/brand vector canvases from animated/dynamic canvases.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CanvasStabilityEntry {
    /// Canvas scene node identity.
    pub(crate) node_id: NodeId,
    /// Quantized physical DPI scale at observation time.
    pub(crate) phys_scale_milli: u32,
    /// Canvas logical bounds at observation time.
    pub(crate) bounds: Rect,
    /// `SceneTree::revision` at last observation.
    pub(crate) tree_revision: u64,
    /// Deterministic 64-bit hash of vector commands emitted by the canvas callback.
    pub(crate) cmd_hash: u64,
    /// Consecutive identical observations confirming static pure-vector output.
    pub(crate) stable_hits: u16,
    /// Set permanently when the canvas emits raster images or mutates output at the same tree revision.
    pub(crate) proven_dynamic: bool,
    /// True once this slot holds an active record.
    pub(crate) occupied: bool,
}

/// Whole-frame scene snapshot signature used for O(1) full-`DisplayList` retention when `SceneTree` is unchanged.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct PaintSceneSignature {
    /// Address of the rendered `SceneTree` instance to prevent cross-tree aliasing.
    pub(crate) tree_ptr: usize,
    /// Monotonic `SceneTree::revision` at last paint.
    pub(crate) tree_revision: u64,
    /// Viewport width bits.
    pub(crate) viewport_w_bits: u32,
    /// Viewport height bits.
    pub(crate) viewport_h_bits: u32,
    /// Active DPI scale bits.
    pub(crate) scale_bits: u32,
    /// Packed RGBA clear color.
    pub(crate) clear_rgba: u32,
    /// Packed focused node handle (`u64::MAX` when `None`).
    pub(crate) focused_packed: u64,
    /// Packed hovered node handle (`u64::MAX` when `None`).
    pub(crate) hovered_packed: u64,
    /// Packed pressed node handle (`u64::MAX` when `None`).
    pub(crate) pressed_packed: u64,
    /// Active caret blink visibility flag.
    pub(crate) caret_visible: bool,
    /// Deterministic digest of active text selection range.
    pub(crate) text_sel_sig: u64,
}

/// Bounded cross-frame cache retaining whole display lists and clean nested subtree `DrawCommand` fragments.
#[derive(Clone, Debug, Default, Getter)]
pub struct PaintFragmentCache {
    /// True once `last_scene_sig` has been populated for the current `DisplayList`.
    #[get(skip)]
    pub(crate) has_last_scene: bool,
    /// Last rendered whole-scene signature for O(1) zero-rebuild display list reuse.
    #[get(skip)]
    pub(crate) last_scene_sig: PaintSceneSignature,
    /// True when the last rendered frame visited at least one dynamic (non-static) `NodeKind::Canvas`.
    #[get(skip)]
    pub(crate) last_has_canvas: bool,
    /// Bounded power-of-two direct-mapped table of retained subtree fragment descriptors.
    #[get(skip)]
    pub(crate) slots: Vec<RetainedPaintFragment>,
    /// Pre-allocated contiguous slab storing cached fragment commands without per-slot `Vec` allocations.
    #[get(skip)]
    pub(crate) cmd_slab: Vec<CachedDrawCommand>,
    /// Pre-allocated contiguous UTF-8 byte slab storing cached fragment text without per-text `String` allocations.
    #[get(skip)]
    pub(crate) text_slab: Vec<u8>,
    /// Direct-mapped stability table allowing static vector icon/brand canvases to preserve whole-list reuse.
    #[get(skip)]
    pub(crate) canvas_stability: [CanvasStabilityEntry; CANVAS_STABILITY_SLOTS],
    /// Cumulative count of whole-frame `DisplayList` retention hits.
    #[get(type(copy))]
    pub(crate) whole_list_hits: u64,
    /// Cumulative count of retained subtree fragment reuse hits.
    #[get(type(copy))]
    pub(crate) fragment_hits: u64,
    /// Cumulative count of retained subtree fragment capture misses.
    #[get(type(copy))]
    pub(crate) fragment_misses: u64,
    /// Cumulative count of direct-mapped fragment slot displacements.
    #[get(type(copy))]
    pub(crate) fragment_evictions: u64,
}

/// Stateless scene-to-display-list compiler supporting z-order sorting, scroll clipping, frustum culling, and retained fragment caching.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PaintPass;

/// Borrowed reusable scratch buffers threaded through recursive `PaintPass::emit_node` calls.
#[derive(Debug)]
pub(crate) struct PaintScratch<'a> {
    /// Optional deferred top-layer portal queue (`None` while painting top-layer portals).
    pub(crate) portals: Option<&'a mut Vec<(i32, usize, NodeId)>>,
    /// Stack-sliced scratch vector reused for nested z-index child sorting.
    pub(crate) z_sort: &'a mut Vec<(i32, usize, NodeId)>,
    /// Ancestor handles on paths to live `Portal` / `Position::Fixed` descendants for lossless clipped-subtree culling.
    pub(crate) portal_ancestors: &'a [NodeId],
    /// Optional cross-frame retained fragment cache (`Some` during retained builds).
    pub(crate) fragments: Option<&'a mut PaintFragmentCache>,
    /// Flag set whenever any visited `NodeKind::Canvas` is dynamic or still awaiting static verification.
    pub(crate) saw_canvas: &'a mut bool,
}