euv-core 0.53.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::*;

/// Per-pass execution diagnostics recorded by `ParallelLayoutSolver`.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
pub struct ParallelLayoutStats {
    /// Whether this layout pass forked work onto `EngineWorkerPool` worker threads.
    #[get(type(copy))]
    pub(crate) used_parallel: bool,
    /// Total child subtree tasks dispatched across parallel fork-join waves.
    #[get(type(copy))]
    pub(crate) parallel_tasks_dispatched: usize,
    /// Total concurrent threads available (`1 + worker_count`) during this pass.
    #[get(type(copy))]
    pub(crate) worker_threads_available: usize,
    /// Number of nodes translated via SIMD `simd_translate_subtree_4x`.
    #[get(type(copy))]
    pub(crate) simd_translations: usize,
    /// Number of ASCII text measurements accelerated via `simd_measure_ascii_advance`.
    #[get(type(copy))]
    pub(crate) simd_text_measures: usize,
    /// Number of nodes laid out during this pass.
    #[get(type(copy))]
    pub(crate) nodes_computed: usize,
    /// Number of nodes whose final layout geometry changed.
    #[get(type(copy))]
    pub(crate) nodes_changed: usize,
}

/// Compact flex item scratch record indexed by BFS slot for lock-free parallel layout workers.
#[derive(Clone, Copy, Data, Debug, Default, New)]
pub(crate) struct ParallelFlexItem {
    /// Target child BFS node index.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) bfs_index: u32,
    /// Current resolved item size updated during grow/shrink and re-probe passes.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) size: Size,
    /// Outer margin insets copied from `LayoutStyle`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) margin: Edges,
    /// Flex grow factor copied from `LayoutStyle`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    #[new(skip)]
    pub(crate) flex_grow: f32,
    /// Flex shrink factor copied from `LayoutStyle`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    #[new(skip)]
    pub(crate) flex_shrink: f32,
    /// Minimum main-axis floor for CSS flex-shrink clamping.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    #[new(skip)]
    pub(crate) min_main: f32,
    /// Effective `align-self` copied from `LayoutStyle`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) align_self: AlignSelf,
    /// Overflow clipping behavior copied from `LayoutStyle`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) overflow: Overflow,
    /// Packed `FLEX_ITEM_*` constraint bits.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) flags: u16,
}

/// Reusable thread-private scratch buffers and L1 interval text cache owned by one `EngineWorkerPool` lane.
#[derive(Clone, Debug, Default)]
pub struct WorkerLayoutScratch {
    /// Stack of normal-flow child BFS indices for active container recursion frames.
    pub(crate) flow_scratch: Vec<u32>,
    /// Stack of absolute/fixed overlay child BFS indices for active container recursion frames.
    pub(crate) abs_scratch: Vec<u32>,
    /// Stack of flex item records for active flex recursion frames.
    pub(crate) flex_items_scratch: Vec<ParallelFlexItem>,
    /// Stack of flex line `(start, end)` ranges into `flex_items_scratch`.
    pub(crate) flex_lines_scratch: Vec<(usize, usize)>,
    /// Iterative subtree traversal stack collecting arena indices for SIMD batch translation.
    pub(crate) translate_arena_scratch: Vec<u32>,
    /// Iterative BFS traversal stack used to collect descendants without recursion.
    pub(crate) translate_bfs_scratch: Vec<u32>,
    /// Thread-local lock-free 2-way set-associative interval text measurement cache.
    pub(crate) text_size_cache: TextSizeCache,
    /// Count of nodes computed on this worker lane during the active pass.
    pub(crate) nodes_computed: usize,
    /// Count of nodes whose bounds changed on this worker lane during the active pass.
    pub(crate) nodes_changed: usize,
    /// Count of nodes shifted via SIMD translation on this worker lane.
    pub(crate) simd_translations: usize,
    /// Count of ASCII text spans measured via SIMD on this worker lane.
    pub(crate) simd_text_measures: usize,
}

/// Lock-free array of per-lane `WorkerLayoutScratch` slots indexed exclusively by `EngineWorkerPool` lane ID.
#[derive(Debug)]
pub(crate) struct WorkerScratchBank {
    /// Fixed array of lane-exclusive scratch cells (`0` = caller, `1..=ENGINE_WORKER_MAX_THREADS` = workers).
    pub(crate) lanes: Vec<UnsafeCell<WorkerLayoutScratch>>,
}

/// Per-node metadata emitted into `ParallelOutputArena` for seamless `SplitLayoutCache` synchronization.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ParallelNodeResult {
    /// Committed border-box size prior to parent post-stretch.
    pub(crate) size: Size,
    /// Pre-stretch border-box rectangle preserved for `SplitLayoutCache` restoration.
    pub(crate) natural_bounds: Rect,
    /// Pre-stretch content-box rectangle preserved for `SplitLayoutCache` restoration.
    pub(crate) natural_content_bounds: Rect,
    /// Computed layout dependency mask (`LAYOUT_DEP_*`).
    pub(crate) dep_flags: u8,
    /// Invariant minimum available width for unwrapped text subtrees.
    pub(crate) min_avail_w: f32,
    /// Whether the parent post-stretched this node's final bounds.
    pub(crate) post_stretched: bool,
    /// Whether this node resolved under `Display::None`.
    pub(crate) hidden: bool,
    /// Whether this slot was written during the active parallel layout pass.
    pub(crate) written: bool,
    /// Whether any direct child has non-zero `z_index`.
    pub(crate) has_nonzero_z: bool,
}

/// Arena-indexed output buffers populated lock-free across pairwise-disjoint sibling subtrees.
#[derive(Clone, Debug, Default)]
pub(crate) struct ParallelOutputArena {
    /// Computed `LayoutBox` per arena slot.
    pub(crate) boxes: Vec<LayoutBox>,
    /// Computed `AvailableSpace` constraint per arena slot.
    pub(crate) constraints: Vec<Option<AvailableSpace>>,
    /// Computed conservative subtree bounding box per arena slot.
    pub(crate) subtree_bounds: Vec<Rect>,
    /// Computed per-node final metadata and `SplitLayoutCache` sync flags.
    pub(crate) results: Vec<ParallelNodeResult>,
}

/// Raw disjoint-index writer handle shared across worker threads executing strictly disjoint sibling subtrees.
#[derive(Clone, Copy, Debug)]
pub(crate) struct DisjointOutputView {
    /// Base pointer to `ParallelOutputArena::boxes`.
    pub(crate) boxes_ptr: *mut LayoutBox,
    /// Base pointer to `ParallelOutputArena::constraints`.
    pub(crate) constraints_ptr: *mut Option<AvailableSpace>,
    /// Base pointer to `ParallelOutputArena::subtree_bounds`.
    pub(crate) subtree_bounds_ptr: *mut Rect,
    /// Base pointer to `ParallelOutputArena::results`.
    pub(crate) results_ptr: *mut ParallelNodeResult,
    /// Arena slot length shared by all four output slices.
    pub(crate) arena_len: usize,
}

/// Per-task placement record written back by parallel Grid or single-pass Flex workers.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ParallelChildOutcome {
    /// Computed border-box size returned by `solve_node_soa`.
    pub(crate) size: Size,
    /// Computed layout dependency mask.
    pub(crate) dep_flags: u8,
    /// Invariant minimum available width.
    pub(crate) min_avail_w: f32,
}

/// Per-task input specification dispatched for parallel Grid or Flex child subtrees.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ParallelChildTask {
    /// Target child BFS node index.
    pub(crate) bfs_index: u32,
    /// Incoming constraint space (local `(0, 0)` origin for pre-translation passes or final origin for Flex Pass 3).
    pub(crate) space: AvailableSpace,
    /// Execution run mode (`ComputeSize` or `PerformLayout`).
    pub(crate) mode: LayoutRunMode,
}

/// Disjoint raw-pointer view into `ParallelLayoutSolver::child_outcomes` for lock-free per-task writes.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct DisjointOutcomeView {
    /// Base pointer to contiguous per-task `ParallelChildOutcome` slots.
    pub(crate) outcomes_ptr: *mut ParallelChildOutcome,
    /// Total allocated task outcome count.
    pub(crate) outcomes_len: usize,
}

/// Shared fork-join execution context passed down the caller-thread parallel dispatch tree.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) struct ParallelForkContext<'a> {
    /// Read-only `Send + Sync` layout SoA slab.
    pub(crate) soa: &'a SceneLayoutSoa,
    /// Disjoint arena output writer shared across sibling workers.
    pub(crate) output_view: DisjointOutputView,
    /// Lane-exclusive worker scratch bank.
    pub(crate) scratch_bank: &'a WorkerScratchBank,
    /// Unified work-stealing worker pool.
    pub(crate) pool: &'a EngineWorkerPool,
    /// Reusable task list for the active parallel wave.
    pub(crate) child_tasks: &'a mut Vec<ParallelChildTask>,
    /// Reusable per-task outcome buffer for the active parallel wave.
    pub(crate) child_outcomes: &'a mut Vec<ParallelChildOutcome>,
    /// Cumulative dispatched parallel task counter for this pass.
    pub(crate) dispatched_tasks: &'a mut usize,
}

/// Resolved container box sizing, clamping, chrome, and content origin parameters.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ResolvedNodeConstraints {
    /// Own layout dependency flags (`LAYOUT_DEP_*`).
    pub(crate) own_deps: u8,
    /// Resolved outer width (explicit or stretched).
    pub(crate) resolved_w: Option<f32>,
    /// Resolved outer height (explicit or stretched).
    pub(crate) resolved_h: Option<f32>,
    /// Explicit CSS outer width prior to stretching.
    pub(crate) explicit_w: Option<f32>,
    /// Whether outer width came from parent stretch rather than explicit style.
    pub(crate) stretched_w: bool,
    /// Minimum outer width floor.
    pub(crate) min_w: f32,
    /// Maximum outer width ceiling.
    pub(crate) max_w: f32,
    /// Minimum outer height floor.
    pub(crate) min_h: f32,
    /// Maximum outer height ceiling.
    pub(crate) max_h: f32,
    /// Horizontal padding plus border width.
    pub(crate) chrome_h: f32,
    /// Vertical padding plus border width.
    pub(crate) chrome_v: f32,
    /// Inner available width for descendants.
    pub(crate) inner_avail_w: f32,
    /// Inner available height for descendants.
    pub(crate) inner_avail_h: f32,
    /// Logical X origin of the content box.
    pub(crate) content_origin_x: f32,
    /// Logical Y origin of the content box.
    pub(crate) content_origin_y: f32,
}

/// Precomputed Pass-1 flex item probe metadata returned by `prepare_flex_item`.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct PreparedFlexItemMeta {
    /// Known direct fixed/zero-basis size when Pass-1 probing can be skipped in O(1).
    pub(crate) zero_basis_size: Option<Size>,
    /// Resolved `flex-basis` override in logical pixels.
    pub(crate) explicit_basis_main: Option<f32>,
    /// Resolved minimum main-axis clamp in logical pixels.
    pub(crate) explicit_min_main: Option<f32>,
    /// Resolved maximum main-axis clamp in logical pixels.
    pub(crate) explicit_max_main: Option<f32>,
    /// Whether child is a `Text` node with `TextOverflow::Clip`.
    pub(crate) is_unwrapped_clip_text: bool,
    /// Whether child specifies a `Length::Px` main-axis dimension.
    pub(crate) has_px_main: bool,
}

/// Per-item Pass-3 placement metadata retained across the parallel flex line wave.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct FlexPlacementMeta {
    /// Target resolved width prior to post-join refinement.
    pub(crate) target_w: f32,
    /// Target resolved height prior to post-join refinement.
    pub(crate) target_h: f32,
    /// Available cross-axis slot after outer margin deduction.
    pub(crate) avail_item_cross: f32,
    /// Initial cross-axis alignment offset before auto-cross refinement.
    pub(crate) cross_offset: f32,
    /// Effective cross-axis alignment resolved from `align-self`/`align-items`.
    pub(crate) align: AlignItems,
    /// Whether this item has auto cross dimension under grow/shrink main-axis resizing.
    pub(crate) auto_cross_grow: bool,
    /// Whether this item was resized along the main axis via `flex-grow` or `flex-shrink`.
    pub(crate) main_resized_item: bool,
}

/// Lock-free parallel CPU dirty-subtree layout solver backed by `EngineWorkerPool` and SIMD micro-kernels.
#[derive(Debug)]
pub struct ParallelLayoutSolver {
    /// Reusable per-worker lane scratch bank eliminating allocations and mutexes during parallel waves.
    pub(crate) scratch_bank: WorkerScratchBank,
    /// Reusable output arena written lock-free by workers and committed to `SceneTree` and `SplitLayoutCache`.
    pub(crate) output_arena: ParallelOutputArena,
    /// Reusable scratch slice of parallel child dispatch tasks for the active top-level wave.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) child_tasks: Vec<ParallelChildTask>,
    /// Reusable scratch slice of per-task outcomes written to disjoint indices by workers.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) child_outcomes: Vec<ParallelChildOutcome>,
    /// Diagnostics captured from the most recent `compute_tree` invocation.
    pub(crate) last_stats: ParallelLayoutStats,
}