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

/// Maps a `NodeKind` variant into its compact `Send + Sync` `SoaNodeKindTag`.
///
/// # Arguments
///
/// - `&NodeKind` - Retained scene node kind reference.
///
/// # Returns
///
/// - `SoaNodeKindTag` - Compact `Copy` tag matching `kind`.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn soa_kind_from_node_kind(kind: &NodeKind) -> SoaNodeKindTag {
    match kind {
        NodeKind::View => SoaNodeKindTag::Container,
        NodeKind::Text(_) => SoaNodeKindTag::Text,
        NodeKind::Image(_) => SoaNodeKindTag::Image,
        NodeKind::Input(_) => SoaNodeKindTag::Input,
        NodeKind::Canvas(_) => SoaNodeKindTag::Canvas,
        NodeKind::Portal => SoaNodeKindTag::Portal,
        NodeKind::ScrollView => SoaNodeKindTag::ScrollView,
        NodeKind::Fragment => SoaNodeKindTag::Fragment,
    }
}

/// Encodes a CSS `Length` into a 2-bit GPU sizing mode tag and scalar float payload.
///
/// # Arguments
///
/// - `Length` - Source layout length specification.
///
/// # Returns
///
/// - `(u32, f32)` - Pair of `(GPU_SIZE_MODE_*, numeric_payload)`.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn encode_gpu_length(length: Length) -> (u32, f32) {
    match length {
        Length::Auto => (GPU_SIZE_MODE_AUTO, 0.0),
        Length::Px(value) => (GPU_SIZE_MODE_PX, value),
        Length::Percent(value) => (GPU_SIZE_MODE_PERCENT, value),
        Length::Fraction(value) | Length::ViewportWidth(value) | Length::ViewportHeight(value) => {
            (GPU_SIZE_MODE_VIEWPORT, value)
        }
    }
}

/// Packs one scene node's BFS topology links, `LayoutStyle`, and intrinsic metadata into a 64-byte `GpuLayoutNodeInput`.
///
/// # Arguments
///
/// - `(u32, u32, u32)` - Topology tuple `(parent_bfs, first_child, child_count)`.
/// - `LayoutStyle` - Node layout style.
/// - `SoaNodeKindTag` - Compact semantic node kind tag.
/// - `(f32, u16, Size, bool)` - Leaf metadata tuple `(border_width, dirty_mask, intrinsic_size, wrapping_text)`.
///
/// # Returns
///
/// - `GpuLayoutNodeInput` - Packed 64-byte GPU compute layout input record.
#[must_use]
pub fn pack_gpu_layout_node_input(
    topology_links: (u32, u32, u32),
    layout_style: LayoutStyle,
    kind_tag: SoaNodeKindTag,
    leaf_meta: (f32, u16, Size, bool),
) -> GpuLayoutNodeInput {
    let (parent_bfs, first_child, child_count): (u32, u32, u32) = topology_links;
    let (border_width, dirty_mask, intrinsic_size, wrapping_text): (f32, u16, Size, bool) =
        leaf_meta;
    let (width_mode, pref_width): (u32, f32) = encode_gpu_length(layout_style.get_width());
    let (height_mode, pref_height): (u32, f32) = encode_gpu_length(layout_style.get_height());
    let (_, min_width): (u32, f32) = encode_gpu_length(layout_style.get_min_width());
    let (_, min_height): (u32, f32) = encode_gpu_length(layout_style.get_min_height());
    let mut flags_kind: u32 = (layout_style.get_flags() & 0x000F_FFFF)
        | ((width_mode & 0x3) << GPU_LAYOUT_WIDTH_MODE_SHIFT)
        | ((height_mode & 0x3) << GPU_LAYOUT_HEIGHT_MODE_SHIFT);
    if (dirty_mask & DIRTY_LAYOUT_PASS_MASK) != 0 {
        flags_kind |= GPU_SOA_FLAG_DIRTY;
    }
    if intrinsic_size.get_width() > 0.0 || intrinsic_size.get_height() > 0.0 {
        flags_kind |= GPU_LAYOUT_FLAG_PREMEASURED_LEAF;
    }
    if wrapping_text {
        flags_kind |= GPU_LAYOUT_FLAG_WRAPPING_TEXT;
    }
    if kind_tag == SoaNodeKindTag::Portal || layout_style.get_position() == Position::Fixed {
        flags_kind |= GPU_LAYOUT_FLAG_IS_PORTAL;
    }
    if kind_tag == SoaNodeKindTag::ScrollView || layout_style.get_overflow() == Overflow::Scroll {
        flags_kind |= GPU_LAYOUT_FLAG_IS_SCROLL;
    }
    let padding: Edges = layout_style.get_padding();
    let chrome_h: f32 = padding.horizontal() + border_width * 2.0;
    let chrome_v: f32 = padding.vertical() + border_width * 2.0;
    let is_row: bool = matches!(
        layout_style.get_flex_direction(),
        FlexDirection::Row | FlexDirection::RowReverse
    );
    let main_gap: f32 = if layout_style.get_display() == Display::Grid {
        layout_style.get_row_gap()
    } else if is_row {
        layout_style.get_column_gap()
    } else {
        layout_style.get_row_gap()
    };
    let flex_or_col_gap: f32 = if layout_style.get_display() == Display::Grid {
        layout_style.get_column_gap()
    } else {
        layout_style.get_flex_grow()
    };
    let aspect_ratio: f32 = layout_style.aspect_ratio_value().unwrap_or(0.0);
    GpuLayoutNodeInput {
        parent_bfs,
        first_child,
        child_count,
        flags_kind,
        pref_min_size: [pref_width, pref_height, min_width, min_height],
        pad_gap_flex: [chrome_h, chrome_v, main_gap, flex_or_col_gap],
        extra_or_intrinsic: [
            layout_style.get_grid_columns() as f32,
            aspect_ratio,
            intrinsic_size.get_width(),
            intrinsic_size.get_height(),
        ],
    }
}

/// Packs one scene node's paint and clip ancestry into a 48-byte `GpuVisualStyleInput` record.
///
/// # Arguments
///
/// - `&PaintStyle` - Node paint style reference.
/// - `LayoutStyle` - Node layout style providing `z_index`.
/// - `u32` - Nearest clipping ancestor BFS index (`GPU_LAYOUT_INDEX_NONE` when unclipped).
/// - `u32` - Scroll layer identifier (`0` for root layer).
///
/// # Returns
///
/// - `GpuVisualStyleInput` - Packed 48-byte GPU visual descriptor.
#[must_use]
pub fn pack_gpu_visual_style_input(
    paint_style: &PaintStyle,
    layout_style: LayoutStyle,
    clip_ancestor_bfs: u32,
    scroll_layer_id: u32,
) -> GpuVisualStyleInput {
    let background: Color = paint_style.background_color().unwrap_or(COLOR_TRANSPARENT);
    let border: Border = paint_style.get_border();
    let radius: CornerRadius = paint_style.get_radius();
    let color_rgba: u32 = GpuPrimitiveInstance::pack_color_rgba8(background);
    let aux_color_rgba: u32 = GpuPrimitiveInstance::pack_color_rgba8(border.get_color());
    let radii_border: [u16; 4] = [
        GpuPrimitiveInstance::pack_u16_q4(radius.get_top_left()),
        GpuPrimitiveInstance::pack_u16_q4(radius.get_top_right()),
        GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_right()),
        GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_left()),
    ];
    let kind_flags: u32 =
        GpuPrimitiveInstance::pack_kind_flags(PrimKind::RoundedRect, 0, 0, scroll_layer_id as u8);
    let extra_param: u32 = GpuPrimitiveInstance::pack_f16x2(border.get_width(), 0.0);
    let opacity_clamped: f32 = paint_style.get_opacity().clamp(0.0, 1.0);
    let opacity_q16: u32 = (opacity_clamped * GPU_VISUAL_OPACITY_Q16_MAX).round() as u32;
    GpuVisualStyleInput {
        color_rgba,
        aux_color_rgba,
        uv_rect: [0, 0, 0, 0],
        radii_border,
        kind_flags,
        extra_param,
        clip_ancestor_bfs,
        scroll_layer_id,
        opacity_q16,
        z_order_bias: layout_style.get_z_index(),
    }
}