1use super::*;
2
3#[cfg_attr(not(target_arch = "wasm32"), inline)]
13#[must_use]
14pub fn soa_kind_from_node_kind(kind: &NodeKind) -> SoaNodeKindTag {
15 match kind {
16 NodeKind::View => SoaNodeKindTag::Container,
17 NodeKind::Text(_) => SoaNodeKindTag::Text,
18 NodeKind::Image(_) => SoaNodeKindTag::Image,
19 NodeKind::Input(_) => SoaNodeKindTag::Input,
20 NodeKind::Canvas(_) => SoaNodeKindTag::Canvas,
21 NodeKind::Portal => SoaNodeKindTag::Portal,
22 NodeKind::ScrollView => SoaNodeKindTag::ScrollView,
23 NodeKind::Fragment => SoaNodeKindTag::Fragment,
24 }
25}
26
27#[cfg_attr(not(target_arch = "wasm32"), inline)]
37#[must_use]
38pub fn encode_gpu_length(length: Length) -> (u32, f32) {
39 match length {
40 Length::Auto => (GPU_SIZE_MODE_AUTO, 0.0),
41 Length::Px(value) => (GPU_SIZE_MODE_PX, value),
42 Length::Percent(value) => (GPU_SIZE_MODE_PERCENT, value),
43 Length::Fraction(value) | Length::ViewportWidth(value) | Length::ViewportHeight(value) => {
44 (GPU_SIZE_MODE_VIEWPORT, value)
45 }
46 }
47}
48
49#[must_use]
62pub fn pack_gpu_layout_node_input(
63 topology_links: (u32, u32, u32),
64 layout_style: LayoutStyle,
65 kind_tag: SoaNodeKindTag,
66 leaf_meta: (f32, u16, Size, bool),
67) -> GpuLayoutNodeInput {
68 let (parent_bfs, first_child, child_count): (u32, u32, u32) = topology_links;
69 let (border_width, dirty_mask, intrinsic_size, wrapping_text): (f32, u16, Size, bool) =
70 leaf_meta;
71 let (width_mode, pref_width): (u32, f32) = encode_gpu_length(layout_style.get_width());
72 let (height_mode, pref_height): (u32, f32) = encode_gpu_length(layout_style.get_height());
73 let (_, min_width): (u32, f32) = encode_gpu_length(layout_style.get_min_width());
74 let (_, min_height): (u32, f32) = encode_gpu_length(layout_style.get_min_height());
75 let mut flags_kind: u32 = (layout_style.get_flags() & 0x000F_FFFF)
76 | ((width_mode & 0x3) << GPU_LAYOUT_WIDTH_MODE_SHIFT)
77 | ((height_mode & 0x3) << GPU_LAYOUT_HEIGHT_MODE_SHIFT);
78 if (dirty_mask & DIRTY_LAYOUT_PASS_MASK) != 0 {
79 flags_kind |= GPU_SOA_FLAG_DIRTY;
80 }
81 if intrinsic_size.get_width() > 0.0 || intrinsic_size.get_height() > 0.0 {
82 flags_kind |= GPU_LAYOUT_FLAG_PREMEASURED_LEAF;
83 }
84 if wrapping_text {
85 flags_kind |= GPU_LAYOUT_FLAG_WRAPPING_TEXT;
86 }
87 if kind_tag == SoaNodeKindTag::Portal || layout_style.get_position() == Position::Fixed {
88 flags_kind |= GPU_LAYOUT_FLAG_IS_PORTAL;
89 }
90 if kind_tag == SoaNodeKindTag::ScrollView || layout_style.get_overflow() == Overflow::Scroll {
91 flags_kind |= GPU_LAYOUT_FLAG_IS_SCROLL;
92 }
93 let padding: Edges = layout_style.get_padding();
94 let chrome_h: f32 = padding.horizontal() + border_width * 2.0;
95 let chrome_v: f32 = padding.vertical() + border_width * 2.0;
96 let is_row: bool = matches!(
97 layout_style.get_flex_direction(),
98 FlexDirection::Row | FlexDirection::RowReverse
99 );
100 let main_gap: f32 = if layout_style.get_display() == Display::Grid {
101 layout_style.get_row_gap()
102 } else if is_row {
103 layout_style.get_column_gap()
104 } else {
105 layout_style.get_row_gap()
106 };
107 let flex_or_col_gap: f32 = if layout_style.get_display() == Display::Grid {
108 layout_style.get_column_gap()
109 } else {
110 layout_style.get_flex_grow()
111 };
112 let aspect_ratio: f32 = layout_style.aspect_ratio_value().unwrap_or(0.0);
113 GpuLayoutNodeInput {
114 parent_bfs,
115 first_child,
116 child_count,
117 flags_kind,
118 pref_min_size: [pref_width, pref_height, min_width, min_height],
119 pad_gap_flex: [chrome_h, chrome_v, main_gap, flex_or_col_gap],
120 extra_or_intrinsic: [
121 layout_style.get_grid_columns() as f32,
122 aspect_ratio,
123 intrinsic_size.get_width(),
124 intrinsic_size.get_height(),
125 ],
126 }
127}
128
129#[must_use]
142pub fn pack_gpu_visual_style_input(
143 paint_style: &PaintStyle,
144 layout_style: LayoutStyle,
145 clip_ancestor_bfs: u32,
146 scroll_layer_id: u32,
147) -> GpuVisualStyleInput {
148 let background: Color = paint_style.background_color().unwrap_or(COLOR_TRANSPARENT);
149 let border: Border = paint_style.get_border();
150 let radius: CornerRadius = paint_style.get_radius();
151 let color_rgba: u32 = GpuPrimitiveInstance::pack_color_rgba8(background);
152 let aux_color_rgba: u32 = GpuPrimitiveInstance::pack_color_rgba8(border.get_color());
153 let radii_border: [u16; 4] = [
154 GpuPrimitiveInstance::pack_u16_q4(radius.get_top_left()),
155 GpuPrimitiveInstance::pack_u16_q4(radius.get_top_right()),
156 GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_right()),
157 GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_left()),
158 ];
159 let kind_flags: u32 =
160 GpuPrimitiveInstance::pack_kind_flags(PrimKind::RoundedRect, 0, 0, scroll_layer_id as u8);
161 let extra_param: u32 = GpuPrimitiveInstance::pack_f16x2(border.get_width(), 0.0);
162 let opacity_clamped: f32 = paint_style.get_opacity().clamp(0.0, 1.0);
163 let opacity_q16: u32 = (opacity_clamped * GPU_VISUAL_OPACITY_Q16_MAX).round() as u32;
164 GpuVisualStyleInput {
165 color_rgba,
166 aux_color_rgba,
167 uv_rect: [0, 0, 0, 0],
168 radii_border,
169 kind_flags,
170 extra_param,
171 clip_ancestor_bfs,
172 scroll_layer_id,
173 opacity_q16,
174 z_order_bias: layout_style.get_z_index(),
175 }
176}