euv-core 0.48.3

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::*;

/// Available layout space passed down during a layout pass and cached for clean-subtree skipping.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct AvailableSpace {
    /// Available horizontal extent in logical pixels.
    #[get(type(copy))]
    pub(crate) width: f32,
    /// Available vertical extent in logical pixels.
    #[get(type(copy))]
    pub(crate) height: f32,
    /// Root viewport size for `vw`/`vh` length resolution.
    #[get(type(copy))]
    pub(crate) viewport: Size,
    /// Origin X coordinate where this node is placed.
    #[get(type(copy))]
    pub(crate) origin_x: f32,
    /// Origin Y coordinate where this node is placed.
    #[get(type(copy))]
    pub(crate) origin_y: f32,
    /// Whether the parent stretched this node's width to fill `width` when its declared width is `Length::Auto`.
    #[get(type(copy))]
    #[new(skip)]
    pub(crate) stretch_width: bool,
    /// Whether the parent stretched this node's height to fill `height` when its declared height is `Length::Auto`.
    #[get(type(copy))]
    #[new(skip)]
    pub(crate) stretch_height: bool,
}

/// Internal container constraint bundle passed to child layout solvers.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub(crate) struct ContainerFrame {
    /// Parent layout properties.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) parent_layout: LayoutStyle,
    /// Inner available dimensions inside padding and border.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) inner_avail: Size,
    /// Root viewport logical size.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) viewport: Size,
    /// Content origin in root coordinates.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) content_origin: Point,
    /// Whether the container's outer width is definite (explicit length, aspect ratio, or parent stretch).
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) definite_width: bool,
    /// Whether the container's outer height is definite (explicit length, aspect ratio, or parent stretch).
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) definite_height: bool,
    /// Inner minimum content dimensions derived from `min_width` / `min_height` after subtracting padding and border.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) min_inner_size: Size,
}

/// Telemetry counters recorded per layout pass.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
pub struct LayoutStats {
    /// Number of nodes laid out during this pass.
    #[get(type(copy))]
    pub(crate) nodes_computed: usize,
    /// Number of clean subtrees skipped in O(1) via constraint caching.
    #[get(type(copy))]
    pub(crate) subtrees_skipped: usize,
    /// Number of nodes whose final layout geometry actually changed during this pass.
    #[get(type(copy))]
    #[new(skip)]
    pub(crate) nodes_changed: usize,
}

/// Cumulative layout invalidation and effectiveness telemetry separating runs from actual geometric mutations.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LayoutTelemetry {
    /// Cumulative layout-invalidating scene mutations (`layout.invalidations`).
    #[get(type(copy))]
    pub(crate) invalidations: u64,
    /// Cumulative executed layout passes (`layout.passes`).
    #[get(type(copy))]
    pub(crate) passes: u64,
    /// Cumulative layout passes that produced at least one geometric change (`layout.changed_passes`).
    #[get(type(copy))]
    pub(crate) changed_passes: u64,
    /// Cumulative count of nodes whose layout box actually changed (`layout.changed_nodes_total`).
    #[get(type(copy))]
    pub(crate) changed_nodes_total: u64,
}

/// Per-pass split layout cache and interval text cache hit/miss diagnostics.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SplitCacheStats {
    /// Number of O(1) measure cache hits in `SplitLayoutCache::measure_slots`.
    pub(crate) measure_hits: u64,
    /// Number of measure cache misses requiring computation in `ComputeSize` mode.
    pub(crate) measure_misses: u64,
    /// Number of O(1) final layout cache hits in `SplitLayoutCache::final_slots`.
    pub(crate) final_hits: u64,
    /// Number of final layout misses requiring `PerformLayout` execution.
    pub(crate) final_misses: u64,
    /// Number of `Display::None` subtrees skipped in O(1) while preserving descendant caches.
    pub(crate) display_none_skips: u64,
    /// Number of text measurements served in O(1) by interval or wrapped hits in `TextSizeCache`.
    pub(crate) text_interval_hits: u64,
    /// Number of text measurements that reused cached per-node text hashes without re-hashing.
    pub(crate) text_hash_reuses: u64,
}

/// Compact value key packing all layout-relevant typography properties of `TextStyle`.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub(crate) struct TextStyleKey {
    /// Normalized IEEE-754 bits of `TextStyle::font_size`.
    pub(crate) font_size_bits: u32,
    /// Normalized IEEE-754 bits of `TextStyle::line_height`.
    pub(crate) line_height_bits: u32,
    /// Normalized IEEE-754 bits of `TextStyle::letter_spacing`.
    pub(crate) letter_spacing_bits: u32,
    /// Packed typography flags (`family`, `weight`, `overflow`, `italic`, and occupied bit).
    pub(crate) style_flags: u16,
}

/// Interval-aware set-associative cache slot mapping `(text_hash, style_key)` to unwrapped and wrapped metrics.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct TextSizeEntry {
    /// Primary 64-bit hash of the measured UTF-8 text bytes.
    pub(crate) hash_a: u64,
    /// Secondary independent 64-bit hash of the measured UTF-8 text bytes.
    pub(crate) hash_b: u64,
    /// Exact byte length of the measured text string.
    pub(crate) text_len: u32,
    /// Packed typography style key (excluding `max_width` so all wrap widths share one entry).
    pub(crate) style_key: TextStyleKey,
    /// Populated field bitmask (`TEXT_SIZE_ENTRY_HAS_*`).
    pub(crate) entry_flags: u8,
    /// Minimum content width observed under tight wrapping constraints.
    pub(crate) min_content_w: f32,
    /// Maximum unconstrained single/raw-line content width.
    pub(crate) max_content_w: f32,
    /// Unwrapped bounding height matching `max_content_w`.
    pub(crate) single_line_h: f32,
    /// Exact width returned for `max_width == None` when trailing/leading spaces exist.
    pub(crate) none_w: f32,
    /// Exact height returned for `max_width == None`.
    pub(crate) none_h: f32,
    /// Normalized IEEE-754 bits of the most recent wrapped/ellipsized `max_width` query.
    pub(crate) last_max_w_bits: u32,
    /// Cached wrapped/ellipsized width for `last_max_w_bits`.
    pub(crate) last_wrapped_w: f32,
    /// Cached wrapped/ellipsized height for `last_max_w_bits`.
    pub(crate) last_wrapped_h: f32,
}

/// Measurement outcome returned by `TextSizeCache::get_or_measure_hashed` including wrap-interval metadata.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct TextSizeMeasurement {
    /// Measured bounding dimensions.
    pub(crate) size: Size,
    /// Whether the measurement is valid for all larger wrap widths (`avail_w >= max_content_w`).
    pub(crate) unwrapped: bool,
    /// Known unwrapped width threshold (`f32::INFINITY` when not yet determined).
    pub(crate) max_content_w: f32,
    /// Whether the measurement was served from cache without invoking the measurer callback.
    pub(crate) cache_hit: bool,
}

/// Bounded 2-way set-associative interval-aware cache mapping `(text_hash, style_key)` to text metrics.
#[derive(Clone, Debug, Default)]
pub(crate) struct TextSizeCache {
    /// Lazily allocated flat table of `TEXT_SIZE_CACHE_SLOTS` entries (`2` ways per set).
    pub(crate) entries: Vec<TextSizeEntry>,
}

/// Single LRU measure cache slot storing an intrinsic sizing result independently of `FinalLayoutSlot`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct MeasureCacheSlot {
    /// Whether this measure slot holds a populated entry.
    pub(crate) occupied: bool,
    /// Stretched known width (`f32::NAN` when not stretched by parent).
    pub(crate) known_w: f32,
    /// Stretched known height (`f32::NAN` when not stretched by parent).
    pub(crate) known_h: f32,
    /// Available width constraint used during measurement.
    pub(crate) avail_w: f32,
    /// Available height constraint used during measurement.
    pub(crate) avail_h: f32,
    /// Root viewport width used during measurement.
    pub(crate) viewport_w: f32,
    /// Root viewport height used during measurement.
    pub(crate) viewport_h: f32,
    /// Minimum available width for which an unwrapped measurement stays invariant.
    pub(crate) min_avail_w: f32,
    /// Run mode recorded when this slot was populated.
    pub(crate) run_mode: LayoutRunMode,
    /// Node content/style generation guarding against stale dirty entries.
    pub(crate) content_gen: u32,
    /// Dependency mask (`LAYOUT_DEP_*`) recording which constraint axes affected `size`.
    pub(crate) dep_flags: u8,
    /// Cached measured border-box dimensions.
    pub(crate) size: Size,
}

/// Separate final-layout cache slot storing committed `PerformLayout` geometry for O(1) clean-subtree reuse.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct FinalLayoutSlot {
    /// Exact incoming available space (including origin and stretch flags) for this committed layout.
    pub(crate) space: AvailableSpace,
    /// Node subtree generation matching this committed layout snapshot.
    pub(crate) subtree_gen: u32,
    /// Border-box dimensions computed by `layout_node` prior to any parent post-stretch.
    pub(crate) size: Size,
    /// Committed border-box rectangle written by `layout_node`.
    pub(crate) bounds: Rect,
    /// Committed content-box rectangle written by `layout_node`.
    pub(crate) content_bounds: Rect,
    /// Committed intrinsic content dimensions written by `layout_node`.
    pub(crate) content_size: Size,
    /// Dependency mask (`LAYOUT_DEP_*`) computed during this layout pass.
    pub(crate) dep_flags: u8,
    /// Minimum available width for which an unwrapped measurement stays invariant.
    pub(crate) min_avail_w: f32,
    /// Whether the enclosing parent container post-stretched `bounds` after `layout_node` returned.
    pub(crate) post_stretched: bool,
    /// Whether this node was committed under `Display::None` (preserving descendant warm caches).
    pub(crate) hidden: bool,
}

/// Cached per-node 128-bit text hash avoiding repeat UTF-8 byte hashing across layout probes and resizes.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct NodeTextHashSlot {
    /// Live `NodeId::generation` matching this cached text hash.
    pub(crate) node_gen: u32,
    /// Primary 64-bit hash of the node's text bytes.
    pub(crate) hash_a: u64,
    /// Secondary independent 64-bit hash of the node's text bytes.
    pub(crate) hash_b: u64,
    /// Exact byte length of the hashed text slice.
    pub(crate) text_len: u32,
    /// Backing UTF-8 buffer pointer address guarding against unflagged buffer replacement.
    pub(crate) text_ptr: usize,
}

/// Flat Structure-of-Arrays (SoA) per-node layout cache, spatial subtree bounds, and generation-stamped dedup table.
#[derive(Clone, Debug, Default)]
pub(crate) struct SplitLayoutCache {
    /// Live `NodeId::generation` stored per arena slot (`0` = empty/unpopulated).
    pub(crate) node_gens: Vec<u32>,
    /// Monotonic per-node content/subtree generation bumped on dirty recomputation.
    pub(crate) content_gens: Vec<u32>,
    /// Layout pass epoch when a dirty node's stale cache slots were invalidated.
    pub(crate) invalidated_epochs: Vec<u32>,
    /// 4-slot LRU measure cache per node, never overwritten by `final_slots` or vice-versa.
    pub(crate) measure_slots: Vec<[MeasureCacheSlot; MEASURE_CACHE_SLOTS]>,
    /// Separate final-layout cache slot per node.
    pub(crate) final_slots: Vec<Option<FinalLayoutSlot>>,
    /// Per-node cached 128-bit text hashes avoiding repeat hashing on unchanged text nodes.
    pub(crate) text_hashes: Vec<NodeTextHashSlot>,
    /// Per-container visual/interactive subtree bounding box in root logical coordinates for O(1) hit-test pruning.
    pub(crate) subtree_bounds: Vec<Rect>,
    /// Packed per-node subtree flags (`SUBTREE_FLAG_VALID | SUBTREE_FLAG_NONZERO_Z_CHILD`).
    pub(crate) subtree_flags: Vec<u8>,
    /// Generation-stamped snapshot dedup table (`snapshot_seen_gen[idx] == snapshot_gen` means already recorded).
    pub(crate) snapshot_seen_gen: Vec<u32>,
    /// Monotonic layout pass epoch incremented on every `LayoutEngine::compute_inner` call.
    pub(crate) pass_epoch: u32,
    /// Monotonic snapshot dedup generation incremented on every damage-tracked layout pass.
    pub(crate) snapshot_gen: u32,
    /// `SceneTree::revision` recorded at the end of the latest completed layout pass.
    pub(crate) synced_tree_revision: u64,
    /// `SceneTree::root` recorded at the end of the latest completed layout pass.
    pub(crate) synced_root: NodeId,
}

/// Compact single-cache-line scratch record tracking a flex child across probe and placement passes.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct FlexItemRecord {
    /// Scene node handle of the flex child.
    pub(crate) node_id: NodeId,
    /// Current target size updated by flex grow and auto-cross re-probing.
    pub(crate) size: Size,
    /// Outer margin insets copied once from `LayoutStyle`.
    pub(crate) margin: Edges,
    /// Flex grow factor copied once from `LayoutStyle`.
    pub(crate) flex_grow: f32,
    /// Effective `align-self` override copied once from `LayoutStyle`.
    pub(crate) align_self: AlignSelf,
    /// Overflow clipping behavior copied once from `LayoutStyle`.
    pub(crate) overflow: Overflow,
    /// Packed boolean flags (`FLEX_ITEM_*`) describing child sizing constraints.
    pub(crate) flags: u16,
}

/// Shared axis and container parameters passed to `LayoutEngine::layout_flex_line`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct FlexLineContext {
    /// Enclosing flex container constraints and origin.
    pub(crate) frame: ContainerFrame,
    /// `true` when main axis is horizontal (`Row`/`RowReverse`).
    pub(crate) is_row: bool,
    /// `true` when multi-line flex wrapping is enabled.
    pub(crate) wrap_enabled: bool,
    /// Main-axis gap between adjacent items in logical pixels.
    pub(crate) main_gap: f32,
    /// Inner available main-axis extent of the container.
    pub(crate) main_avail: f32,
    /// Inner available cross-axis extent of the container.
    pub(crate) cross_avail: f32,
    /// Aggregate `flex_grow` sum across items in the container.
    pub(crate) total_grow: f32,
    /// Total number of flex lines in the container.
    pub(crate) line_count: usize,
    /// Active layout execution mode (`ComputeSize` vs `PerformLayout`).
    pub(crate) mode: LayoutRunMode,
}