euv_core/layout/partition/
fn.rs1use super::*;
2
3#[must_use]
13pub fn is_wrapping_text_node(node: &SceneNode) -> bool {
14 match node.get_kind() {
15 NodeKind::Text(content) => {
16 !content.is_empty() && node.get_style().get_text().get_overflow() == TextOverflow::Wrap
17 }
18 _ => false,
19 }
20}
21
22#[must_use]
32pub fn is_node_gpu_layout_supported(node: &SceneNode) -> bool {
33 match node.get_kind() {
34 NodeKind::Input(_) | NodeKind::Canvas(CanvasCaching::DynamicPerFrame) => return false,
35 _ => {}
36 }
37 let layout: LayoutStyle = node.get_style().get_layout();
38 match layout.get_display() {
39 Display::Flex => {
40 if layout.get_flex_wrap() != FlexWrap::NoWrap {
41 return false;
42 }
43 matches!(
44 layout.get_flex_direction(),
45 FlexDirection::Row | FlexDirection::Column
46 )
47 }
48 Display::Block | Display::Grid | Display::None => true,
49 }
50}
51
52#[must_use]
63pub fn measure_subtree_metrics(tree: &SceneTree, root: NodeId) -> SubtreePartitionMetrics {
64 let mut stack: Vec<(NodeId, usize)> = Vec::with_capacity(64);
65 stack.push((root, 0));
66 let mut total_nodes: usize = 0;
67 let mut dirty_nodes: usize = 0;
68 let mut wrapping_text_nodes: usize = 0;
69 let mut unsupported_mode_nodes: usize = 0;
70 let mut max_depth: usize = 0;
71 while let Some((node_id, depth)) = stack.pop() {
72 let Some(node) = tree.get_node(node_id) else {
73 continue;
74 };
75 total_nodes = total_nodes.saturating_add(1);
76 if depth > max_depth {
77 max_depth = depth;
78 }
79 if node.get_dirty().intersects(SUBTREE_LAYOUT_DIRTY_MASK) {
80 dirty_nodes = dirty_nodes.saturating_add(1);
81 }
82 if is_wrapping_text_node(node) {
83 wrapping_text_nodes = wrapping_text_nodes.saturating_add(1);
84 }
85 if !is_node_gpu_layout_supported(node) {
86 unsupported_mode_nodes = unsupported_mode_nodes.saturating_add(1);
87 }
88 for child_id in tree.children_iter(node_id) {
89 stack.push((child_id, depth.saturating_add(1)));
90 }
91 }
92 let wrapping_text_ratio: f32 = if total_nodes == 0 {
93 0.0
94 } else {
95 wrapping_text_nodes as f32 / total_nodes as f32
96 };
97 SubtreePartitionMetrics {
98 total_nodes,
99 dirty_nodes,
100 wrapping_text_nodes,
101 unsupported_mode_nodes,
102 max_depth,
103 wrapping_text_ratio,
104 }
105}
106
107pub fn evaluate_subtree_gpu_eligibility(
119 metrics: SubtreePartitionMetrics,
120 boundary: Option<LayoutBoundary>,
121 config: SubtreePartitionConfig,
122) -> Result<SubtreeLane, GpuSubtreeRejectReason> {
123 if let Some(bound) = boundary
124 && matches!(
125 bound.get_hint(),
126 LayoutBoundaryHint::ForceCpu | LayoutBoundaryHint::PreferCpu
127 )
128 {
129 return Err(GpuSubtreeRejectReason::ExplicitCpuBoundary);
130 }
131 if !config.get_hardware_compute_ready() {
132 return Err(GpuSubtreeRejectReason::HardwareUnavailable);
133 }
134 if config.get_thermal_throttled() {
135 return Err(GpuSubtreeRejectReason::ThermalThrottled);
136 }
137 if metrics.get_unsupported_mode_nodes() > 0 {
138 return Err(GpuSubtreeRejectReason::UnsupportedLayoutMode);
139 }
140 if metrics.get_max_depth() > config.get_max_gpu_depth() {
141 return Err(GpuSubtreeRejectReason::ExceedsMaxDepth);
142 }
143 if metrics.get_wrapping_text_ratio() > config.get_max_text_wrap_ratio() {
144 return Err(GpuSubtreeRejectReason::TextWrapRatioExceeded);
145 }
146 let effective_nodes: usize = metrics.get_dirty_nodes().max(metrics.get_total_nodes());
147 let min_required: usize = match boundary.map(|bound: LayoutBoundary| bound.get_hint()) {
148 Some(LayoutBoundaryHint::PreferGpu) => config.get_min_gpu_nodes() / 2,
149 _ => config.get_min_gpu_nodes(),
150 };
151 if effective_nodes < min_required {
152 return Err(GpuSubtreeRejectReason::BelowNodeThreshold);
153 }
154 Ok(SubtreeLane::GpuSubtree)
155}
156
157#[must_use]
169fn resolve_gpu_length(length: Length, parent_extent: f32, viewport_extent: f32) -> f32 {
170 match length {
171 Length::Auto => -1.0,
172 Length::Px(px_val) => px_val.max(0.0),
173 Length::Percent(pct_val) => (parent_extent * pct_val * 0.01).max(0.0),
174 Length::Fraction(frac_val) => (parent_extent * frac_val).max(0.0),
175 Length::ViewportWidth(vw_val) | Length::ViewportHeight(vw_val) => {
176 (viewport_extent * vw_val * 0.01).max(0.0)
177 }
178 }
179}
180
181#[must_use]
191fn map_style_to_gpu_layout_mode(layout: LayoutStyle) -> GpuLayoutMode {
192 if layout.get_position() == Position::Absolute || layout.get_position() == Position::Fixed {
193 return GpuLayoutMode::Absolute;
194 }
195 match layout.get_display() {
196 Display::Grid => GpuLayoutMode::Grid,
197 Display::Block => GpuLayoutMode::Block,
198 Display::Flex => match layout.get_flex_direction() {
199 FlexDirection::Column | FlexDirection::ColumnReverse => GpuLayoutMode::FlexColumn,
200 FlexDirection::Row | FlexDirection::RowReverse => GpuLayoutMode::FlexRow,
201 },
202 Display::None => GpuLayoutMode::Block,
203 }
204}
205
206#[must_use]
216fn map_justify_to_gpu(justify: JustifyContent) -> GpuJustifyMode {
217 match justify {
218 JustifyContent::Start => GpuJustifyMode::Start,
219 JustifyContent::Center => GpuJustifyMode::Center,
220 JustifyContent::End => GpuJustifyMode::End,
221 JustifyContent::SpaceBetween
222 | JustifyContent::SpaceAround
223 | JustifyContent::SpaceEvenly => GpuJustifyMode::SpaceBetween,
224 }
225}
226
227#[must_use]
237fn map_align_to_gpu(align: AlignItems) -> GpuAlignMode {
238 match align {
239 AlignItems::Stretch => GpuAlignMode::Stretch,
240 AlignItems::Start => GpuAlignMode::Start,
241 AlignItems::Center => GpuAlignMode::Center,
242 AlignItems::End => GpuAlignMode::End,
243 }
244}
245
246#[must_use]
261pub fn build_gpu_compute_scene_from_subtree(
262 tree: &SceneTree,
263 root: NodeId,
264 viewport_width: f32,
265 viewport_height: f32,
266 scroll_x: f32,
267 scroll_y: f32,
268) -> GpuComputeSceneInput {
269 let mut bfs_nodes: Vec<(NodeId, u32, usize)> = Vec::new();
270 bfs_nodes.push((root, u32::MAX, 0));
271 let mut level_offsets: Vec<u32> = vec![0];
272 let mut cursor: usize = 0;
273 let mut current_depth: usize = 0;
274 while cursor < bfs_nodes.len() {
275 let (node_id, _, depth): (NodeId, u32, usize) = bfs_nodes[cursor];
276 if depth > current_depth {
277 while current_depth < depth {
278 level_offsets.push(cursor as u32);
279 current_depth = current_depth.saturating_add(1);
280 }
281 }
282 let parent_flat: u32 = cursor as u32;
283 for child_id in tree.children_iter(node_id) {
284 bfs_nodes.push((child_id, parent_flat, depth.saturating_add(1)));
285 }
286 cursor = cursor.saturating_add(1);
287 }
288 level_offsets.push(bfs_nodes.len() as u32);
289 let node_count: usize = bfs_nodes.len();
290 let mut first_child_indices: Vec<u32> = vec![0; node_count];
291 let mut child_counts: Vec<u32> = vec![0; node_count];
292 for (flat_idx, &(_, parent_idx, _)) in bfs_nodes.iter().enumerate() {
293 if parent_idx != u32::MAX {
294 let parent_usize: usize = parent_idx as usize;
295 if child_counts[parent_usize] == 0 {
296 first_child_indices[parent_usize] = flat_idx as u32;
297 }
298 child_counts[parent_usize] = child_counts[parent_usize].saturating_add(1);
299 }
300 }
301 let mut nodes: Vec<GpuLayoutNodeInput> = Vec::with_capacity(node_count);
302 let mut visuals: Vec<GpuVisualStyleInput> = Vec::with_capacity(node_count);
303 for (flat_idx, &(node_id, parent_idx, _)) in bfs_nodes.iter().enumerate() {
304 let Some(scene_node) = tree.get_node(node_id) else {
305 nodes.push(GpuLayoutNodeInput::default());
306 visuals.push(GpuVisualStyleInput::default());
307 continue;
308 };
309 let style: Style = scene_node.get_style();
310 let layout: LayoutStyle = style.get_layout();
311 let paint: PaintStyle = style.get_paint();
312 let mode: GpuLayoutMode = map_style_to_gpu_layout_mode(layout);
313 let justify: GpuJustifyMode = map_justify_to_gpu(layout.get_justify_content());
314 let align: GpuAlignMode = map_align_to_gpu(layout.get_align_items());
315 let visible: bool = layout.get_display() != Display::None && paint.get_opacity() > 0.001;
316 let fill_color: Color = paint.background_color().unwrap_or(COLOR_TRANSPARENT);
317 let border: Border = paint.get_border();
318 let emit_visual: bool = visible && (fill_color.get_alpha() > 0 || border.get_width() > 0.0);
319 let dirty: bool = scene_node.get_dirty().intersects(SUBTREE_LAYOUT_DIRTY_MASK);
320 let flags_kind: u32 =
321 pack_gpu_layout_flags(mode, justify, align, visible, emit_visual, dirty);
322 let width_px: f32 = resolve_gpu_length(layout.get_width(), viewport_width, viewport_width);
323 let height_px: f32 =
324 resolve_gpu_length(layout.get_height(), viewport_height, viewport_height);
325 let min_w: f32 =
326 resolve_gpu_length(layout.get_min_width(), viewport_width, viewport_width).max(0.0);
327 let min_h: f32 =
328 resolve_gpu_length(layout.get_min_height(), viewport_height, viewport_height).max(0.0);
329 let padding: Edges = layout.get_padding();
330 let pad_h: f32 = padding.get_left();
331 let pad_v: f32 = padding.get_top();
332 let gap_main: f32 = if layout.get_row_gap() > 0.0 {
333 layout.get_row_gap()
334 } else {
335 layout.get_column_gap()
336 };
337 let flex_or_col_gap: f32 = if mode == GpuLayoutMode::Grid {
338 layout.get_column_gap()
339 } else {
340 layout.get_flex_grow()
341 };
342 let extra0: f32 = if mode == GpuLayoutMode::Grid {
343 layout.get_grid_columns().max(1) as f32
344 } else if mode == GpuLayoutMode::Absolute {
345 layout.left_offset().unwrap_or(0.0)
346 } else {
347 0.0
348 };
349 let extra1: f32 = if mode == GpuLayoutMode::Absolute {
350 layout.top_offset().unwrap_or(0.0)
351 } else {
352 layout.aspect_ratio_value().unwrap_or(0.0)
353 };
354 let input: GpuLayoutNodeInput = GpuLayoutNodeInput {
355 parent_bfs: parent_idx,
356 first_child: first_child_indices[flat_idx],
357 child_count: child_counts[flat_idx],
358 flags_kind,
359 pref_min_size: [width_px.max(0.0), height_px.max(0.0), min_w, min_h],
360 pad_gap_flex: [pad_h, pad_v, gap_main, flex_or_col_gap],
361 extra_or_intrinsic: [extra0, extra1, 0.0, 0.0],
362 };
363 let radius: CornerRadius = paint.get_radius();
364 let radii_border: [u16; 4] = [
365 GpuPrimitiveInstance::pack_u16_q4(radius.get_top_left()),
366 GpuPrimitiveInstance::pack_u16_q4(radius.get_top_right()),
367 GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_right()),
368 GpuPrimitiveInstance::pack_u16_q4(radius.get_bottom_left()),
369 ];
370 let visual: GpuVisualStyleInput = GpuVisualStyleInput {
371 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(fill_color),
372 aux_color_rgba: GpuPrimitiveInstance::pack_color_rgba8(border.get_color()),
373 uv_rect: [0, 0, u16::MAX, u16::MAX],
374 radii_border,
375 kind_flags: GpuPrimitiveInstance::pack_kind_flags(PrimKind::RoundedRect, 0, 0, 0),
376 extra_param: GpuPrimitiveInstance::pack_f16x2(border.get_width(), 0.0),
377 clip_ancestor_bfs: u32::MAX,
378 scroll_layer_id: 0,
379 opacity_q16: u16::MAX as u32,
380 z_order_bias: 0,
381 };
382 nodes.push(input);
383 visuals.push(visual);
384 }
385 GpuComputeSceneInput {
386 nodes,
387 visuals,
388 level_offsets,
389 viewport_size: [viewport_width.max(1.0), viewport_height.max(1.0)],
390 root_boundary_rect: [
391 -scroll_x,
392 -scroll_y,
393 viewport_width.max(1.0),
394 viewport_height.max(1.0),
395 ],
396 dt_secs: 1.0 / 120.0,
397 pick_point: None,
398 clear_color: COLOR_TRANSPARENT,
399 }
400}