use super::*;
#[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,
}
}
#[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)
}
}
}
#[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(),
],
}
}
#[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(),
}
}