1use super::*;
2
3impl WorkerLayoutScratch {
5 pub(crate) fn begin_pass(&mut self) {
7 let Self {
8 flow_scratch,
9 abs_scratch,
10 flex_items_scratch,
11 flex_lines_scratch,
12 translate_arena_scratch,
13 translate_bfs_scratch,
14 nodes_computed,
15 nodes_changed,
16 simd_translations,
17 simd_text_measures,
18 ..
19 } = self;
20 flow_scratch.clear();
21 abs_scratch.clear();
22 flex_items_scratch.clear();
23 flex_lines_scratch.clear();
24 translate_arena_scratch.clear();
25 translate_bfs_scratch.clear();
26 *nodes_computed = 0;
27 *nodes_changed = 0;
28 *simd_translations = 0;
29 *simd_text_measures = 0;
30 }
31
32 pub(crate) fn measure_text_cached(
45 &mut self,
46 text: &str,
47 is_ascii: bool,
48 text_style: TextStyle,
49 max_width: Option<f32>,
50 ) -> TextSizeMeasurement {
51 let (hash_a, hash_b): (u64, u64) = TextSizeCache::hash_text_128(text.as_bytes());
52 let Self {
53 text_size_cache,
54 simd_text_measures,
55 ..
56 } = self;
57 let mut used_simd: bool = false;
58 let measured: TextSizeMeasurement = text_size_cache.get_or_measure_hashed(
59 text,
60 (hash_a, hash_b, text.len() as u32),
61 text_style,
62 max_width,
63 |sample_text: &str, sample_style: TextStyle, width_limit: Option<f32>| -> Size {
64 let (measured_size, hit_simd): (Size, bool) =
65 measure_text_simd_or_fallback(sample_text, is_ascii, sample_style, width_limit);
66 if hit_simd {
67 used_simd = true;
68 }
69 measured_size
70 },
71 );
72 if used_simd {
73 *simd_text_measures = simd_text_measures.saturating_add(1);
74 }
75 measured
76 }
77}
78
79unsafe impl Sync for WorkerScratchBank {}
81
82impl Default for WorkerScratchBank {
84 fn default() -> Self {
90 let mut lanes: Vec<UnsafeCell<WorkerLayoutScratch>> =
91 Vec::with_capacity(PARALLEL_SCRATCH_LANES);
92 for _ in 0..PARALLEL_SCRATCH_LANES {
93 lanes.push(UnsafeCell::new(WorkerLayoutScratch::default()));
94 }
95 Self { lanes }
96 }
97}
98
99impl Clone for WorkerScratchBank {
101 fn clone(&self) -> Self {
107 Self::default()
108 }
109}
110
111impl WorkerScratchBank {
113 pub(crate) fn begin_pass(&mut self) {
115 let Self { lanes } = self;
116 for lane_cell in lanes.iter_mut() {
117 lane_cell.get_mut().begin_pass();
118 }
119 }
120
121 #[cfg_attr(not(target_arch = "wasm32"), inline)]
131 pub(crate) fn lane_ptr(&self, lane_index: usize) -> *mut WorkerLayoutScratch {
132 let Self { lanes } = self;
133 let clamped: usize = lane_index.min(lanes.len().saturating_sub(1));
134 lanes[clamped].get()
135 }
136
137 pub(crate) fn aggregate_metrics(&mut self) -> (usize, usize, usize, usize) {
143 let Self { lanes } = self;
144 let mut computed_total: usize = 0;
145 let mut changed_total: usize = 0;
146 let mut simd_trans_total: usize = 0;
147 let mut simd_text_total: usize = 0;
148 for lane_cell in lanes.iter_mut() {
149 let lane: &WorkerLayoutScratch = lane_cell.get_mut();
150 computed_total = computed_total.saturating_add(lane.nodes_computed);
151 changed_total = changed_total.saturating_add(lane.nodes_changed);
152 simd_trans_total = simd_trans_total.saturating_add(lane.simd_translations);
153 simd_text_total = simd_text_total.saturating_add(lane.simd_text_measures);
154 }
155 (
156 computed_total,
157 changed_total,
158 simd_trans_total,
159 simd_text_total,
160 )
161 }
162}
163
164impl ParallelOutputArena {
166 pub(crate) fn prepare_from_soa(&mut self, soa: &SceneLayoutSoa) {
172 let src_boxes: &[LayoutBox] = soa.get_layout_boxes().as_slice();
173 let arena_len: usize = src_boxes.len();
174 let Self {
175 boxes,
176 constraints,
177 subtree_bounds,
178 results,
179 } = self;
180 boxes.clear();
181 boxes.extend_from_slice(src_boxes);
182 constraints.clear();
183 constraints.resize(arena_len, None);
184 subtree_bounds.clear();
185 subtree_bounds.resize(arena_len, Rect::default());
186 results.clear();
187 results.resize(arena_len, ParallelNodeResult::default());
188 }
189
190 pub(crate) fn as_disjoint_view(&mut self) -> DisjointOutputView {
196 let Self {
197 boxes,
198 constraints,
199 subtree_bounds,
200 results,
201 } = self;
202 DisjointOutputView {
203 boxes_ptr: boxes.as_mut_ptr(),
204 constraints_ptr: constraints.as_mut_ptr(),
205 subtree_bounds_ptr: subtree_bounds.as_mut_ptr(),
206 results_ptr: results.as_mut_ptr(),
207 arena_len: boxes.len(),
208 }
209 }
210}
211
212unsafe impl Send for DisjointOutputView {}
214
215unsafe impl Sync for DisjointOutputView {}
217
218impl DisjointOutputView {
220 #[cfg_attr(not(target_arch = "wasm32"), inline)]
230 pub(crate) unsafe fn read_box(&self, arena_index: usize) -> LayoutBox {
231 let Self {
232 boxes_ptr,
233 arena_len,
234 ..
235 } = self;
236 if arena_index >= *arena_len {
237 return LayoutBox::default();
238 }
239 unsafe { *boxes_ptr.add(arena_index) }
241 }
242
243 #[cfg_attr(not(target_arch = "wasm32"), inline)]
250 pub(crate) unsafe fn write_box(&self, arena_index: usize, next_box: LayoutBox) {
251 let Self {
252 boxes_ptr,
253 arena_len,
254 ..
255 } = self;
256 if arena_index < *arena_len {
257 unsafe {
259 *boxes_ptr.add(arena_index) = next_box;
260 }
261 }
262 }
263
264 #[cfg_attr(not(target_arch = "wasm32"), inline)]
271 pub(crate) unsafe fn write_constraint(&self, arena_index: usize, space: AvailableSpace) {
272 let Self {
273 constraints_ptr,
274 arena_len,
275 ..
276 } = self;
277 if arena_index < *arena_len {
278 unsafe {
280 *constraints_ptr.add(arena_index) = Some(space);
281 }
282 }
283 }
284
285 #[cfg_attr(not(target_arch = "wasm32"), inline)]
295 pub(crate) unsafe fn read_constraint(&self, arena_index: usize) -> Option<AvailableSpace> {
296 let Self {
297 constraints_ptr,
298 arena_len,
299 ..
300 } = self;
301 if arena_index >= *arena_len {
302 return None;
303 }
304 unsafe { *constraints_ptr.add(arena_index) }
306 }
307
308 #[cfg_attr(not(target_arch = "wasm32"), inline)]
318 pub(crate) unsafe fn read_subtree_bounds(&self, arena_index: usize) -> Rect {
319 let Self {
320 subtree_bounds_ptr,
321 arena_len,
322 ..
323 } = self;
324 if arena_index >= *arena_len {
325 return Rect::default();
326 }
327 unsafe { *subtree_bounds_ptr.add(arena_index) }
329 }
330
331 #[cfg_attr(not(target_arch = "wasm32"), inline)]
338 pub(crate) unsafe fn write_subtree_bounds(&self, arena_index: usize, bounds: Rect) {
339 let Self {
340 subtree_bounds_ptr,
341 arena_len,
342 ..
343 } = self;
344 if arena_index < *arena_len {
345 unsafe {
347 *subtree_bounds_ptr.add(arena_index) = bounds;
348 }
349 }
350 }
351
352 #[cfg_attr(not(target_arch = "wasm32"), inline)]
362 pub(crate) unsafe fn read_result(&self, arena_index: usize) -> ParallelNodeResult {
363 let Self {
364 results_ptr,
365 arena_len,
366 ..
367 } = self;
368 if arena_index >= *arena_len {
369 return ParallelNodeResult::default();
370 }
371 unsafe { *results_ptr.add(arena_index) }
373 }
374
375 #[cfg_attr(not(target_arch = "wasm32"), inline)]
382 pub(crate) unsafe fn write_result(&self, arena_index: usize, result: ParallelNodeResult) {
383 let Self {
384 results_ptr,
385 arena_len,
386 ..
387 } = self;
388 if arena_index < *arena_len {
389 unsafe {
391 *results_ptr.add(arena_index) = result;
392 }
393 }
394 }
395}
396
397#[cfg(not(target_arch = "wasm32"))]
399unsafe impl Send for DisjointOutcomeView {}
400
401#[cfg(not(target_arch = "wasm32"))]
403unsafe impl Sync for DisjointOutcomeView {}
404
405#[cfg(not(target_arch = "wasm32"))]
407impl DisjointOutcomeView {
408 #[cfg_attr(not(target_arch = "wasm32"), inline)]
415 pub(crate) unsafe fn write_outcome(&self, task_index: usize, outcome: ParallelChildOutcome) {
416 let Self {
417 outcomes_ptr,
418 outcomes_len,
419 } = self;
420 if task_index < *outcomes_len {
421 unsafe {
423 *outcomes_ptr.add(task_index) = outcome;
424 }
425 }
426 }
427}
428
429impl Default for ParallelLayoutSolver {
431 fn default() -> Self {
437 Self {
438 scratch_bank: WorkerScratchBank::default(),
439 output_arena: ParallelOutputArena::default(),
440 #[cfg(not(target_arch = "wasm32"))]
441 child_tasks: Vec::new(),
442 #[cfg(not(target_arch = "wasm32"))]
443 child_outcomes: Vec::new(),
444 last_stats: ParallelLayoutStats::default(),
445 }
446 }
447}
448
449impl Clone for ParallelLayoutSolver {
451 fn clone(&self) -> Self {
457 let Self {
458 output_arena,
459 last_stats,
460 ..
461 } = self;
462 Self {
463 scratch_bank: WorkerScratchBank::default(),
464 output_arena: output_arena.clone(),
465 #[cfg(not(target_arch = "wasm32"))]
466 child_tasks: Vec::new(),
467 #[cfg(not(target_arch = "wasm32"))]
468 child_outcomes: Vec::new(),
469 last_stats: *last_stats,
470 }
471 }
472}
473
474impl ParallelLayoutSolver {
476 #[cfg_attr(not(target_arch = "wasm32"), inline)]
482 #[must_use]
483 pub fn last_stats(&self) -> ParallelLayoutStats {
484 let Self { last_stats, .. } = self;
485 *last_stats
486 }
487
488 pub(crate) fn compute_synced(
501 &mut self,
502 tree: &mut SceneTree,
503 soa: &mut SceneLayoutSoa,
504 split_cache: &mut SplitLayoutCache,
505 viewport: Size,
506 ) -> LayoutStats {
507 let _: SoaSyncMode = soa.sync_from_tree(tree);
508 if soa.get_topology().node_count() == 0 {
509 return LayoutStats::default();
510 }
511 let Self {
512 scratch_bank,
513 output_arena,
514 #[cfg(not(target_arch = "wasm32"))]
515 child_tasks,
516 #[cfg(not(target_arch = "wasm32"))]
517 child_outcomes,
518 last_stats,
519 } = self;
520 scratch_bank.begin_pass();
521 output_arena.prepare_from_soa(soa);
522 #[cfg(not(target_arch = "wasm32"))]
523 {
524 child_tasks.clear();
525 child_outcomes.clear();
526 }
527 let output_view: DisjointOutputView = output_arena.as_disjoint_view();
528 #[cfg(not(target_arch = "wasm32"))]
529 let pool: &EngineWorkerPool = global_engine_worker_pool();
530 #[cfg(not(target_arch = "wasm32"))]
531 let available_threads: usize = pool.total_threads();
532 #[cfg(target_arch = "wasm32")]
533 let available_threads: usize = 1;
534 #[cfg(not(target_arch = "wasm32"))]
535 let mut dispatched_tasks: usize = 0;
536 #[cfg(target_arch = "wasm32")]
537 let dispatched_tasks: usize = 0;
538 let root_space: AvailableSpace = AvailableSpace::root(viewport);
539 #[cfg(not(target_arch = "wasm32"))]
540 let _: (Size, u8, f32) = {
541 let mut fork_ctx: ParallelForkContext<'_> = ParallelForkContext {
542 soa,
543 output_view,
544 scratch_bank,
545 pool,
546 child_tasks,
547 child_outcomes,
548 dispatched_tasks: &mut dispatched_tasks,
549 };
550 Self::solve_node_dispatch(&mut fork_ctx, 0, root_space, LayoutRunMode::PerformLayout)
551 };
552 #[cfg(target_arch = "wasm32")]
553 let _: (Size, u8, f32) = {
554 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
556 Self::solve_node_serial(
557 soa,
558 output_view,
559 lane_zero,
560 0,
561 root_space,
562 LayoutRunMode::PerformLayout,
563 )
564 };
565 let (nodes_computed, nodes_changed, simd_translations, simd_text_measures): (
566 usize,
567 usize,
568 usize,
569 usize,
570 ) = scratch_bank.aggregate_metrics();
571 Self::commit_arena_to_tree(tree, soa, output_arena, split_cache);
572 soa.sync_gpu_outputs_from_boxes();
573 *last_stats = ParallelLayoutStats::new(
574 dispatched_tasks > 0,
575 dispatched_tasks,
576 available_threads,
577 simd_translations,
578 simd_text_measures,
579 nodes_computed,
580 nodes_changed,
581 );
582 let mut layout_stats: LayoutStats = LayoutStats::new(nodes_computed, 0);
583 let _: &mut LayoutStats = layout_stats.set_nodes_changed(nodes_changed);
584 layout_stats
585 }
586
587 fn commit_arena_to_tree(
596 tree: &mut SceneTree,
597 soa: &mut SceneLayoutSoa,
598 arena: &ParallelOutputArena,
599 split_cache: &mut SplitLayoutCache,
600 ) {
601 let SceneLayoutSoa {
602 topology,
603 node_ids,
604 layout_boxes,
605 dirty_masks,
606 ..
607 } = soa;
608 let bfs_to_arena: &[u32] = topology.get_bfs_to_arena().as_slice();
609 let arena_len: usize = arena.boxes.len();
610 let soa_boxes: &mut [LayoutBox] = layout_boxes.as_mut_slice();
611 let soa_dirty: &mut [u16] = dirty_masks.as_mut_slice();
612 for &arena_u32 in bfs_to_arena.iter() {
613 let slot_idx: usize = arena_u32 as usize;
614 if slot_idx >= arena_len {
615 continue;
616 }
617 let node_res: ParallelNodeResult = arena.results[slot_idx];
618 if !node_res.written {
619 continue;
620 }
621 let out_box: LayoutBox = arena.boxes[slot_idx];
622 let out_space_opt: Option<AvailableSpace> = arena.constraints[slot_idx];
623 let out_sub_bounds: Rect = arena.subtree_bounds[slot_idx];
624 soa_boxes[slot_idx] = out_box;
625 soa_dirty[slot_idx] &= !DIRTY_LAYOUT_PASS_MASK;
626 let node_id: NodeId = node_ids[slot_idx];
627 let (_, content_gen): (usize, u32) = split_cache.prepare_node(node_id, true);
628 if let Some(node) = tree.get_mut_nodes().get_mut(slot_idx) {
629 let mut had_hidden_sub: bool = false;
630 if node_res.hidden {
631 had_hidden_sub = node.get_dirty().intersects(DIRTY_SUBTREE_LAYOUT);
632 }
633 let _: &mut LayoutBox = node
634 .get_mut_layout_box()
635 .set_bounds(out_box.get_bounds())
636 .set_content_bounds(out_box.get_content_bounds())
637 .set_content_size(out_box.get_content_size());
638 let _: &mut SceneNode = node.set_last_constraint(out_space_opt);
639 node.get_mut_dirty().remove(DIRTY_LAYOUT_PASS_MASK);
640 if had_hidden_sub {
641 tree.set_has_stale_hidden_dirty(true);
642 }
643 }
644 if let Some(meta) = tree.get_mut_meta().get_mut(slot_idx) {
645 let next_dirty: u16 = meta.get_dirty() & !DIRTY_LAYOUT_PASS_MASK;
646 let _: &mut NodeMeta = meta.set_dirty(next_dirty);
647 }
648 if let Some(space) = out_space_opt {
649 split_cache.store_final(
650 slot_idx,
651 FinalLayoutSlot {
652 space,
653 subtree_gen: content_gen,
654 size: node_res.size,
655 bounds: node_res.natural_bounds,
656 content_bounds: node_res.natural_content_bounds,
657 content_size: out_box.get_content_size(),
658 dep_flags: node_res.dep_flags,
659 min_avail_w: node_res.min_avail_w,
660 post_stretched: node_res.post_stretched,
661 hidden: node_res.hidden,
662 },
663 );
664 if !node_res.hidden {
665 split_cache.store_measure(
666 slot_idx,
667 space,
668 LayoutRunMode::PerformLayout,
669 content_gen,
670 (node_res.size, node_res.dep_flags, node_res.min_avail_w),
671 );
672 }
673 }
674 split_cache.store_subtree_meta(slot_idx, out_sub_bounds, node_res.has_nonzero_z);
675 }
676 }
677
678 #[cfg_attr(not(target_arch = "wasm32"), inline)]
689 fn rect_approx_eq(first: Rect, second: Rect) -> bool {
690 (first.get_x() - second.get_x()).abs() <= 0.001
691 && (first.get_y() - second.get_y()).abs() <= 0.001
692 && (first.get_width() - second.get_width()).abs() <= 0.001
693 && (first.get_height() - second.get_height()).abs() <= 0.001
694 }
695
696 #[cfg_attr(not(target_arch = "wasm32"), inline)]
707 fn size_approx_eq(first: Size, second: Size) -> bool {
708 (first.get_width() - second.get_width()).abs() <= 0.001
709 && (first.get_height() - second.get_height()).abs() <= 0.001
710 }
711
712 #[cfg_attr(not(target_arch = "wasm32"), inline)]
722 fn is_viewport_length(length: Length) -> bool {
723 matches!(length, Length::ViewportWidth(_) | Length::ViewportHeight(_))
724 }
725
726 fn translate_subtree_bfs(
737 soa: &SceneLayoutSoa,
738 output_view: DisjointOutputView,
739 scratch: &mut WorkerLayoutScratch,
740 root_bfs: u32,
741 delta_x: f32,
742 delta_y: f32,
743 ) {
744 if delta_x == 0.0 && delta_y == 0.0 {
745 return;
746 }
747 let topology: &FlatBfsTopology = soa.get_topology();
748 let bfs_to_arena: &[u32] = topology.get_bfs_to_arena().as_slice();
749 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
750 let WorkerLayoutScratch {
751 translate_arena_scratch,
752 translate_bfs_scratch,
753 simd_translations,
754 ..
755 } = scratch;
756 translate_arena_scratch.clear();
757 translate_bfs_scratch.clear();
758 translate_bfs_scratch.push(root_bfs);
759 let mut is_root: bool = true;
760 while let Some(cur_bfs) = translate_bfs_scratch.pop() {
761 let arena_u32: u32 = bfs_to_arena[cur_bfs as usize];
762 let arena_idx: usize = arena_u32 as usize;
763 let style: LayoutStyle = layout_styles[arena_idx];
764 if !is_root && style.get_position() == Position::Fixed {
765 continue;
766 }
767 is_root = false;
768 unsafe {
770 if let Some(mut saved_space) = output_view.read_constraint(arena_idx) {
771 let next_ox: f32 = saved_space.get_origin_x() + delta_x;
772 let next_oy: f32 = saved_space.get_origin_y() + delta_y;
773 let _: &mut AvailableSpace =
774 saved_space.set_origin_x(next_ox).set_origin_y(next_oy);
775 output_view.write_constraint(arena_idx, saved_space);
776 }
777 }
778 if style.get_display() == Display::None {
779 continue;
780 }
781 translate_arena_scratch.push(arena_u32);
782 unsafe {
784 let prev_sub: Rect = output_view.read_subtree_bounds(arena_idx);
785 if prev_sub.get_width() > 0.0 || prev_sub.get_height() > 0.0 {
786 output_view.write_subtree_bounds(
787 arena_idx,
788 simd_translate_rect(prev_sub, delta_x, delta_y),
789 );
790 }
791 let mut prev_res: ParallelNodeResult = output_view.read_result(arena_idx);
792 if prev_res.written && !prev_res.hidden {
793 prev_res.natural_bounds =
794 simd_translate_rect(prev_res.natural_bounds, delta_x, delta_y);
795 prev_res.natural_content_bounds =
796 simd_translate_rect(prev_res.natural_content_bounds, delta_x, delta_y);
797 output_view.write_result(arena_idx, prev_res);
798 }
799 }
800 let (child_start, child_end): (usize, usize) =
801 topology.children_bfs_range(cur_bfs as usize);
802 for child_bfs in child_start..child_end {
803 translate_bfs_scratch.push(child_bfs as u32);
804 }
805 }
806 let shifted_count: usize = unsafe {
808 simd_translate_subtree_ptr_4x(
809 output_view.boxes_ptr,
810 output_view.arena_len,
811 translate_arena_scratch.as_slice(),
812 delta_x,
813 delta_y,
814 )
815 };
816 *simd_translations = simd_translations.saturating_add(shifted_count);
817 }
818
819 #[cfg(not(target_arch = "wasm32"))]
832 fn solve_node_dispatch(
833 fork_ctx: &mut ParallelForkContext<'_>,
834 bfs_index: u32,
835 space: AvailableSpace,
836 mode: LayoutRunMode,
837 ) -> (Size, u8, f32) {
838 let soa: &SceneLayoutSoa = fork_ctx.soa;
839 let output_view: DisjointOutputView = fork_ctx.output_view;
840 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
841 let pool: &EngineWorkerPool = fork_ctx.pool;
842 let topology: &FlatBfsTopology = soa.get_topology();
843 let subtree_weight: usize = topology
844 .get_subtree_sizes()
845 .get(bfs_index as usize)
846 .copied()
847 .unwrap_or(1) as usize;
848 if pool.worker_count() == 0 || subtree_weight < PARALLEL_LAYOUT_MIN_SUBTREE_NODES {
849 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
851 return Self::solve_node_serial(soa, output_view, lane_zero, bfs_index, space, mode);
852 }
853 let arena_idx: usize = topology.get_bfs_to_arena()[bfs_index as usize] as usize;
854 let layout_style: LayoutStyle = soa.get_layout_styles()[arena_idx];
855 if layout_style.get_display() == Display::None {
856 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
858 return Self::solve_node_serial(soa, output_view, lane_zero, bfs_index, space, mode);
859 }
860 let kind_tag: SoaNodeKindTag = soa.get_node_kinds()[arena_idx];
861 let (child_start, child_end): (usize, usize) =
862 topology.children_bfs_range(bfs_index as usize);
863 let child_count: usize = child_end.saturating_sub(child_start);
864 if child_count == 0
865 || matches!(
866 kind_tag,
867 SoaNodeKindTag::Text | SoaNodeKindTag::Image | SoaNodeKindTag::Input
868 )
869 {
870 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
872 return Self::solve_node_serial(soa, output_view, lane_zero, bfs_index, space, mode);
873 }
874 let constraints: ResolvedNodeConstraints =
875 Self::resolve_container_constraints(soa, arena_idx, layout_style, space);
876 let node_overflow: Overflow = layout_style.get_overflow();
877 let node_flex_grow: f32 = layout_style.get_flex_grow();
878 let width_independent: bool = constraints.explicit_w.is_some()
879 || (constraints.stretched_w
880 && constraints.resolved_w.is_some()
881 && (node_overflow != Overflow::Visible || node_flex_grow > 0.0));
882 if mode == LayoutRunMode::ComputeSize
883 && width_independent
884 && constraints.resolved_h.is_some()
885 {
886 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
888 lane_zero.nodes_computed = lane_zero.nodes_computed.saturating_add(1);
889 let final_w: f32 = constraints
890 .resolved_w
891 .unwrap_or(0.0)
892 .clamp(constraints.min_w, constraints.max_w)
893 .max(0.0);
894 let final_h: f32 = constraints
895 .resolved_h
896 .unwrap_or(0.0)
897 .clamp(constraints.min_h, constraints.max_h)
898 .max(0.0);
899 return (Size::new(final_w, final_h), constraints.own_deps, 0.0);
900 }
901 let min_inner_size: Size = Size::new(
902 (constraints.min_w - constraints.chrome_h).max(0.0),
903 (constraints.min_h - constraints.chrome_v).max(0.0),
904 );
905 let frame: ContainerFrame = ContainerFrame::new(
906 layout_style,
907 Size::new(constraints.inner_avail_w, constraints.inner_avail_h),
908 space.get_viewport(),
909 Point::new(constraints.content_origin_x, constraints.content_origin_y),
910 constraints.resolved_w.is_some(),
911 constraints.resolved_h.is_some(),
912 min_inner_size,
913 );
914 let (content_size, child_deps, child_min_aw): (Size, u8, f32) =
915 Self::solve_children_dispatch(fork_ctx, (child_start, child_end), frame, mode);
916 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
918 lane_zero.nodes_computed = lane_zero.nodes_computed.saturating_add(1);
919 Self::finalize_node_box(
920 soa,
921 output_view,
922 lane_zero,
923 (layout_style, space, mode, arena_idx),
924 constraints,
925 (content_size, child_deps, child_min_aw),
926 Some((child_start, child_end)),
927 )
928 }
929
930 #[cfg(not(target_arch = "wasm32"))]
943 fn solve_children_dispatch(
944 fork_ctx: &mut ParallelForkContext<'_>,
945 child_range: (usize, usize),
946 frame: ContainerFrame,
947 mode: LayoutRunMode,
948 ) -> (Size, u8, f32) {
949 let (child_start, child_end): (usize, usize) = child_range;
950 let child_count: usize = child_end.saturating_sub(child_start);
951 if child_count == 0 {
952 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
953 }
954 let soa: &SceneLayoutSoa = fork_ctx.soa;
955 let output_view: DisjointOutputView = fork_ctx.output_view;
956 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
957 let topology: &FlatBfsTopology = soa.get_topology();
958 let bfs_to_arena: &[u32] = topology.get_bfs_to_arena().as_slice();
959 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
960 let node_kinds: &[SoaNodeKindTag] = soa.get_node_kinds().as_slice();
961 let text_styles: &[TextStyle] = soa.get_text_styles().as_slice();
962 let parent_layout: LayoutStyle = frame.get_parent_layout();
963 let inner_avail: Size = frame.get_inner_avail();
964 let viewport: Size = frame.get_viewport();
965 let content_origin: Point = frame.get_content_origin();
966 let flex_dir_opt: Option<FlexDirection> = match parent_layout.get_display() {
967 Display::Grid => None,
968 Display::Block => Some(FlexDirection::Column),
969 Display::Flex | Display::None => Some(parent_layout.get_flex_direction()),
970 };
971 let is_row_dir: bool = matches!(
972 flex_dir_opt,
973 Some(FlexDirection::Row | FlexDirection::RowReverse)
974 );
975 let is_reverse_dir: bool = matches!(
976 flex_dir_opt,
977 Some(FlexDirection::RowReverse | FlexDirection::ColumnReverse)
978 );
979 let parent_align: AlignItems = parent_layout.get_align_items();
980 let parent_height_auto: bool = parent_layout.get_height() == Length::Auto;
981 let parent_bounded_main: bool = if is_row_dir {
982 frame.get_definite_width() || parent_layout.get_max_width() != Length::Auto
983 } else {
984 frame.get_definite_height() || parent_layout.get_max_height() != Length::Auto
985 };
986 let mut single_pass_ok: bool = flex_dir_opt.is_some()
987 && parent_layout.get_flex_wrap() != FlexWrap::Wrap
988 && !is_reverse_dir
989 && parent_layout.get_justify_content() == JustifyContent::Start;
990 let mut shrinkable_flow_children: usize = 0;
991 let mut flow_children: Vec<u32> = Vec::with_capacity(child_count);
992 let mut abs_children: Vec<u32> = Vec::new();
993 let mut flow_subtree_nodes: usize = 0;
994 for (cur_bfs, &arena_u32) in bfs_to_arena
995 .iter()
996 .enumerate()
997 .take(child_end)
998 .skip(child_start)
999 {
1000 let arena_idx: usize = arena_u32 as usize;
1001 let child_layout: LayoutStyle = layout_styles[arena_idx];
1002 if child_layout.get_display() == Display::None {
1003 if mode == LayoutRunMode::PerformLayout {
1004 let lane_zero: &mut WorkerLayoutScratch =
1006 unsafe { &mut *scratch_bank.lane_ptr(0) };
1007 let _: (Size, u8, f32) = Self::solve_node_serial(
1008 soa,
1009 output_view,
1010 lane_zero,
1011 cur_bfs as u32,
1012 AvailableSpace::new(0.0, 0.0, viewport, 0.0, 0.0),
1013 LayoutRunMode::PerformLayout,
1014 );
1015 }
1016 continue;
1017 }
1018 match child_layout.get_position() {
1019 Position::Relative => {
1020 if single_pass_ok {
1021 if parent_bounded_main && child_layout.get_flex_shrink() > 0.0 {
1022 let can_shrink_child: bool = if is_row_dir {
1023 child_layout.get_min_width() != Length::Auto
1024 || child_layout.get_overflow() != Overflow::Visible
1025 || node_kinds[arena_idx] != SoaNodeKindTag::Text
1026 || text_styles[arena_idx].get_overflow() != TextOverflow::Clip
1027 } else {
1028 child_layout.get_min_height() != Length::Auto
1029 || child_layout.get_overflow() != Overflow::Visible
1030 };
1031 if can_shrink_child {
1032 shrinkable_flow_children =
1033 shrinkable_flow_children.saturating_add(1);
1034 }
1035 }
1036 if child_layout.get_flex_grow() > 0.0
1037 || child_layout.get_flex_basis() != Length::Auto
1038 {
1039 single_pass_ok = false;
1040 } else {
1041 let align: AlignItems = match child_layout.get_align_self() {
1042 AlignSelf::Auto => parent_align,
1043 AlignSelf::Stretch => AlignItems::Stretch,
1044 AlignSelf::Start => AlignItems::Start,
1045 AlignSelf::Center => AlignItems::Center,
1046 AlignSelf::End => AlignItems::End,
1047 };
1048 let non_start_align: bool =
1049 matches!(align, AlignItems::Center | AlignItems::End);
1050 let auto_row_stretch: bool = align == AlignItems::Stretch
1051 && is_row_dir
1052 && child_layout.get_height() == Length::Auto
1053 && (parent_height_auto
1054 || frame.get_min_inner_size().get_height() > 0.0);
1055 if non_start_align || auto_row_stretch {
1056 single_pass_ok = false;
1057 }
1058 }
1059 }
1060 flow_children.push(cur_bfs as u32);
1061 let child_weight: usize = topology
1062 .get_subtree_sizes()
1063 .get(cur_bfs)
1064 .copied()
1065 .unwrap_or(1) as usize;
1066 flow_subtree_nodes = flow_subtree_nodes.saturating_add(child_weight);
1067 }
1068 Position::Absolute | Position::Fixed => {
1069 if mode == LayoutRunMode::PerformLayout {
1070 abs_children.push(cur_bfs as u32);
1071 }
1072 }
1073 }
1074 }
1075 let flow_count: usize = flow_children.len();
1076 if single_pass_ok && shrinkable_flow_children > 0 && (!is_row_dir || flow_count >= 2) {
1077 single_pass_ok = false;
1078 }
1079 let can_fork_here: bool = flow_count >= PARALLEL_LAYOUT_MIN_CHILDREN
1080 && flow_subtree_nodes >= PARALLEL_LAYOUT_MIN_SUBTREE_NODES;
1081 let flow_result: (Size, u8, f32) = if let Some(flex_dir) = flex_dir_opt {
1082 if single_pass_ok && can_fork_here {
1083 Self::solve_flex_single_pass_parallel(
1084 fork_ctx,
1085 flow_children.as_slice(),
1086 frame,
1087 flex_dir,
1088 mode,
1089 )
1090 } else if single_pass_ok {
1091 Self::solve_flex_single_pass_recurse(
1092 fork_ctx,
1093 flow_children.as_slice(),
1094 frame,
1095 flex_dir,
1096 mode,
1097 )
1098 } else {
1099 Self::solve_flex_two_pass_dispatch(
1100 fork_ctx,
1101 flow_children.as_slice(),
1102 frame,
1103 flex_dir,
1104 mode,
1105 can_fork_here,
1106 )
1107 }
1108 } else if can_fork_here {
1109 Self::solve_grid_parallel(fork_ctx, flow_children.as_slice(), frame, mode)
1110 } else {
1111 Self::solve_grid_recurse(fork_ctx, flow_children.as_slice(), frame, mode)
1112 };
1113 if mode == LayoutRunMode::PerformLayout && !abs_children.is_empty() {
1114 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
1116 Self::solve_absolute_children_serial(
1117 soa,
1118 output_view,
1119 lane_zero,
1120 abs_children.as_slice(),
1121 inner_avail,
1122 viewport,
1123 content_origin,
1124 );
1125 }
1126 flow_result
1127 }
1128
1129 #[cfg(not(target_arch = "wasm32"))]
1135 fn run_parallel_child_wave(fork_ctx: &mut ParallelForkContext<'_>) {
1136 let soa: &SceneLayoutSoa = fork_ctx.soa;
1137 let output_view: DisjointOutputView = fork_ctx.output_view;
1138 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
1139 let pool: &EngineWorkerPool = fork_ctx.pool;
1140 let tasks: &[ParallelChildTask] = fork_ctx.child_tasks.as_slice();
1141 fork_ctx.child_outcomes.clear();
1142 fork_ctx
1143 .child_outcomes
1144 .resize(tasks.len(), ParallelChildOutcome::default());
1145 let outcome_view: DisjointOutcomeView = DisjointOutcomeView {
1146 outcomes_ptr: fork_ctx.child_outcomes.as_mut_ptr(),
1147 outcomes_len: fork_ctx.child_outcomes.len(),
1148 };
1149 *fork_ctx.dispatched_tasks = fork_ctx.dispatched_tasks.saturating_add(tasks.len());
1150 pool.parallel_work_steal(
1151 tasks,
1152 |task_idx: usize, task: &ParallelChildTask, worker_lane: usize| {
1153 let scratch: &mut WorkerLayoutScratch =
1155 unsafe { &mut *scratch_bank.lane_ptr(worker_lane) };
1156 let (size, dep_flags, min_avail_w): (Size, u8, f32) = Self::solve_node_serial(
1157 soa,
1158 output_view,
1159 scratch,
1160 task.bfs_index,
1161 task.space,
1162 task.mode,
1163 );
1164 unsafe {
1166 outcome_view.write_outcome(
1167 task_idx,
1168 ParallelChildOutcome {
1169 size,
1170 dep_flags,
1171 min_avail_w,
1172 },
1173 );
1174 }
1175 },
1176 );
1177 }
1178
1179 #[cfg(not(target_arch = "wasm32"))]
1192 fn solve_grid_parallel(
1193 fork_ctx: &mut ParallelForkContext<'_>,
1194 flow_children: &[u32],
1195 frame: ContainerFrame,
1196 mode: LayoutRunMode,
1197 ) -> (Size, u8, f32) {
1198 if flow_children.is_empty() {
1199 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1200 }
1201 let soa: &SceneLayoutSoa = fork_ctx.soa;
1202 let output_view: DisjointOutputView = fork_ctx.output_view;
1203 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
1204 let parent_layout: LayoutStyle = frame.get_parent_layout();
1205 let inner_avail: Size = frame.get_inner_avail();
1206 let viewport: Size = frame.get_viewport();
1207 let content_origin: Point = frame.get_content_origin();
1208 let cols: usize = (parent_layout.get_grid_columns() as usize).max(1);
1209 let col_gap: f32 = parent_layout.get_column_gap();
1210 let row_gap: f32 = parent_layout.get_row_gap();
1211 let total_col_gaps: f32 = col_gap * ((cols.saturating_sub(1)) as f32);
1212 let col_w: f32 = ((inner_avail.get_width() - total_col_gaps) / (cols as f32)).max(0.0);
1213 let col_step: f32 = col_w + col_gap;
1214 let origin_x: f32 = content_origin.get_x();
1215 let origin_y: f32 = content_origin.get_y();
1216 let inner_h: f32 = inner_avail.get_height();
1217 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
1218 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
1219 fork_ctx.child_tasks.clear();
1220 for (item_pos, &child_bfs) in flow_children.iter().enumerate() {
1221 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1222 let auto_w: bool = layout_styles[arena_idx].get_width() == Length::Auto;
1223 let col_idx: usize = item_pos % cols;
1224 let pos_x: f32 = origin_x + (col_idx as f32) * col_step;
1225 let cell_space: AvailableSpace =
1226 AvailableSpace::new(col_w, inner_h, viewport, pos_x, 0.0)
1227 .with_stretch(auto_w, false);
1228 fork_ctx.child_tasks.push(ParallelChildTask {
1229 bfs_index: child_bfs,
1230 space: cell_space,
1231 mode,
1232 });
1233 }
1234 Self::run_parallel_child_wave(fork_ctx);
1235 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
1237 let mut y_cursor: f32 = 0.0;
1238 let mut grid_deps: u8 = LAYOUT_DEP_AVAIL_W;
1239 let mut idx: usize = 0;
1240 let flow_len: usize = flow_children.len();
1241 while idx < flow_len {
1242 let row_end: usize = (idx + cols).min(flow_len);
1243 let mut row_h: f32 = 0.0;
1244 let mut min_row_h: f32 = f32::INFINITY;
1245 let pos_y: f32 = origin_y + y_cursor;
1246 for &outcome in &fork_ctx.child_outcomes[idx..row_end] {
1247 grid_deps |= outcome.dep_flags;
1248 let child_h: f32 = outcome.size.get_height();
1249 row_h = row_h.max(child_h);
1250 min_row_h = min_row_h.min(child_h);
1251 }
1252 if mode == LayoutRunMode::PerformLayout {
1253 for pos in idx..row_end {
1254 let child_bfs: u32 = flow_children[pos];
1255 let outcome: ParallelChildOutcome = fork_ctx.child_outcomes[pos];
1256 if pos_y.abs() > 0.0001 {
1257 Self::translate_subtree_bfs(
1258 soa,
1259 output_view,
1260 lane_zero,
1261 child_bfs,
1262 0.0,
1263 pos_y,
1264 );
1265 }
1266 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1267 let width_needs_adjust: bool = (outcome.size.get_width() - col_w).abs() > 0.001;
1268 let height_needs_adjust: bool =
1269 row_end - idx > 1 && min_row_h < row_h && outcome.size.get_height() < row_h;
1270 if width_needs_adjust || height_needs_adjust {
1271 Self::apply_post_stretch(
1272 soa,
1273 output_view,
1274 arena_idx,
1275 if width_needs_adjust {
1276 Some(col_w)
1277 } else {
1278 None
1279 },
1280 if height_needs_adjust {
1281 Some(row_h)
1282 } else {
1283 None
1284 },
1285 );
1286 }
1287 }
1288 }
1289 y_cursor += row_h;
1290 if row_end < flow_len {
1291 y_cursor += row_gap;
1292 }
1293 idx = row_end;
1294 }
1295 (Size::new(inner_avail.get_width(), y_cursor), grid_deps, 0.0)
1296 }
1297
1298 #[cfg(not(target_arch = "wasm32"))]
1311 fn solve_grid_recurse(
1312 fork_ctx: &mut ParallelForkContext<'_>,
1313 flow_children: &[u32],
1314 frame: ContainerFrame,
1315 mode: LayoutRunMode,
1316 ) -> (Size, u8, f32) {
1317 let soa: &SceneLayoutSoa = fork_ctx.soa;
1318 let output_view: DisjointOutputView = fork_ctx.output_view;
1319 let parent_layout: LayoutStyle = frame.get_parent_layout();
1320 let inner_avail: Size = frame.get_inner_avail();
1321 let viewport: Size = frame.get_viewport();
1322 let content_origin: Point = frame.get_content_origin();
1323 let cols: usize = (parent_layout.get_grid_columns() as usize).max(1);
1324 let col_gap: f32 = parent_layout.get_column_gap();
1325 let row_gap: f32 = parent_layout.get_row_gap();
1326 let total_col_gaps: f32 = col_gap * ((cols.saturating_sub(1)) as f32);
1327 let col_w: f32 = ((inner_avail.get_width() - total_col_gaps) / (cols as f32)).max(0.0);
1328 let col_step: f32 = col_w + col_gap;
1329 let origin_x: f32 = content_origin.get_x();
1330 let origin_y: f32 = content_origin.get_y();
1331 let inner_h: f32 = inner_avail.get_height();
1332 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
1333 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
1334 let mut y_cursor: f32 = 0.0;
1335 let mut grid_deps: u8 = LAYOUT_DEP_AVAIL_W;
1336 let mut idx: usize = 0;
1337 let flow_len: usize = flow_children.len();
1338 while idx < flow_len {
1339 let row_end: usize = (idx + cols).min(flow_len);
1340 let mut row_h: f32 = 0.0;
1341 let mut min_row_h: f32 = f32::INFINITY;
1342 let pos_y: f32 = origin_y + y_cursor;
1343 let mut pos_x: f32 = origin_x;
1344 for &child_bfs in &flow_children[idx..row_end] {
1345 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1346 let auto_w: bool = layout_styles[arena_idx].get_width() == Length::Auto;
1347 let cell_space: AvailableSpace =
1348 AvailableSpace::new(col_w, inner_h, viewport, pos_x, pos_y)
1349 .with_stretch(auto_w, false);
1350 let (child_size, c_deps, _): (Size, u8, f32) =
1351 Self::solve_node_dispatch(fork_ctx, child_bfs, cell_space, mode);
1352 grid_deps |= c_deps;
1353 if mode == LayoutRunMode::PerformLayout
1354 && (child_size.get_width() - col_w).abs() > 0.001
1355 {
1356 Self::apply_post_stretch(soa, output_view, arena_idx, Some(col_w), None);
1357 }
1358 let child_h: f32 = child_size.get_height();
1359 row_h = row_h.max(child_h);
1360 min_row_h = min_row_h.min(child_h);
1361 pos_x += col_step;
1362 }
1363 if mode == LayoutRunMode::PerformLayout && row_end - idx > 1 && min_row_h < row_h {
1364 for &child_bfs in &flow_children[idx..row_end] {
1365 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1366 let cur_h: f32 = unsafe { output_view.read_box(arena_idx) }
1368 .get_bounds()
1369 .get_height();
1370 if cur_h < row_h {
1371 Self::apply_post_stretch(soa, output_view, arena_idx, None, Some(row_h));
1372 }
1373 }
1374 }
1375 y_cursor += row_h;
1376 if row_end < flow_len {
1377 y_cursor += row_gap;
1378 }
1379 idx = row_end;
1380 }
1381 (Size::new(inner_avail.get_width(), y_cursor), grid_deps, 0.0)
1382 }
1383
1384 #[cfg(not(target_arch = "wasm32"))]
1398 fn solve_flex_single_pass_parallel(
1399 fork_ctx: &mut ParallelForkContext<'_>,
1400 flow_children: &[u32],
1401 frame: ContainerFrame,
1402 direction: FlexDirection,
1403 mode: LayoutRunMode,
1404 ) -> (Size, u8, f32) {
1405 if flow_children.is_empty() {
1406 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1407 }
1408 let soa: &SceneLayoutSoa = fork_ctx.soa;
1409 let output_view: DisjointOutputView = fork_ctx.output_view;
1410 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
1411 let parent_layout: LayoutStyle = frame.get_parent_layout();
1412 let inner_avail: Size = frame.get_inner_avail();
1413 let viewport: Size = frame.get_viewport();
1414 let content_origin: Point = frame.get_content_origin();
1415 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
1416 let main_gap: f32 = if is_row {
1417 parent_layout.get_column_gap()
1418 } else {
1419 parent_layout.get_row_gap()
1420 };
1421 let cross_avail: f32 = if is_row {
1422 inner_avail.get_height()
1423 } else {
1424 inner_avail.get_width()
1425 };
1426 let parent_align: AlignItems = parent_layout.get_align_items();
1427 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
1428 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
1429 fork_ctx.child_tasks.clear();
1430 let mut agg_deps: u8 = LAYOUT_DEP_NONE;
1431 for &child_bfs in flow_children.iter() {
1432 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1433 let child_layout: LayoutStyle = layout_styles[arena_idx];
1434 let margin: Edges = child_layout.get_margin();
1435 let align: AlignItems = match child_layout.get_align_self() {
1436 AlignSelf::Auto => parent_align,
1437 AlignSelf::Stretch => AlignItems::Stretch,
1438 AlignSelf::Start => AlignItems::Start,
1439 AlignSelf::Center => AlignItems::Center,
1440 AlignSelf::End => AlignItems::End,
1441 };
1442 let stretch_w: bool =
1443 !is_row && align == AlignItems::Stretch && child_layout.get_width() == Length::Auto;
1444 let stretch_h: bool =
1445 is_row && align == AlignItems::Stretch && child_layout.get_height() == Length::Auto;
1446 if stretch_w {
1447 agg_deps |= LAYOUT_DEP_AVAIL_W;
1448 }
1449 if stretch_h {
1450 agg_deps |= LAYOUT_DEP_AVAIL_H;
1451 }
1452 let space_w: f32 = if stretch_w {
1453 (inner_avail.get_width() - margin.horizontal()).max(0.0)
1454 } else {
1455 inner_avail.get_width()
1456 };
1457 let space_h: f32 = if stretch_h {
1458 (inner_avail.get_height() - margin.vertical()).max(0.0)
1459 } else {
1460 inner_avail.get_height()
1461 };
1462 let local_origin_x: f32 = if is_row {
1463 0.0
1464 } else {
1465 content_origin.get_x() + margin.get_left()
1466 };
1467 let local_origin_y: f32 = if is_row {
1468 content_origin.get_y() + margin.get_top()
1469 } else {
1470 0.0
1471 };
1472 let child_space: AvailableSpace =
1473 AvailableSpace::new(space_w, space_h, viewport, local_origin_x, local_origin_y)
1474 .with_stretch(stretch_w, stretch_h);
1475 fork_ctx.child_tasks.push(ParallelChildTask {
1476 bfs_index: child_bfs,
1477 space: child_space,
1478 mode,
1479 });
1480 }
1481 Self::run_parallel_child_wave(fork_ctx);
1482 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
1484 let mut main_cursor: f32 = 0.0;
1485 let mut max_cross: f32 = 0.0;
1486 let mut agg_min_aw: f32 = 0.0;
1487 let mut stretched_records: Vec<(usize, Size, Edges)> = Vec::new();
1488 let count_all: usize = flow_children.len();
1489 for (offset, &child_bfs) in flow_children.iter().enumerate() {
1490 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1491 let child_layout: LayoutStyle = layout_styles[arena_idx];
1492 let margin: Edges = child_layout.get_margin();
1493 let task: ParallelChildTask = fork_ctx.child_tasks[offset];
1494 let outcome: ParallelChildOutcome = fork_ctx.child_outcomes[offset];
1495 agg_deps |= outcome.dep_flags;
1496 if outcome.min_avail_w > 0.0 {
1497 agg_min_aw = agg_min_aw.max(outcome.min_avail_w + margin.horizontal());
1498 }
1499 let stretch_w: bool = task.space.get_stretch_width();
1500 let stretch_h: bool = task.space.get_stretch_height();
1501 let target_w: f32 = if stretch_w {
1502 task.space.get_width().max(outcome.size.get_width())
1503 } else {
1504 outcome.size.get_width()
1505 };
1506 let target_h: f32 = if stretch_h {
1507 task.space.get_height().max(outcome.size.get_height())
1508 } else {
1509 outcome.size.get_height()
1510 };
1511 if mode == LayoutRunMode::PerformLayout {
1512 let (shift_x, shift_y): (f32, f32) = if is_row {
1513 (
1514 content_origin.get_x() + main_cursor + margin.get_left(),
1515 0.0,
1516 )
1517 } else {
1518 (0.0, content_origin.get_y() + main_cursor + margin.get_top())
1519 };
1520 if shift_x.abs() > 0.0001 || shift_y.abs() > 0.0001 {
1521 Self::translate_subtree_bfs(
1522 soa,
1523 output_view,
1524 lane_zero,
1525 child_bfs,
1526 shift_x,
1527 shift_y,
1528 );
1529 }
1530 if (stretch_w && target_w > outcome.size.get_width() + 0.01)
1531 || (stretch_h && target_h > outcome.size.get_height() + 0.01)
1532 {
1533 Self::apply_post_stretch(
1534 soa,
1535 output_view,
1536 arena_idx,
1537 Some(target_w),
1538 Some(target_h),
1539 );
1540 }
1541 if stretch_w {
1542 stretched_records.push((arena_idx, Size::new(target_w, target_h), margin));
1543 }
1544 }
1545 if is_row {
1546 main_cursor += target_w + margin.horizontal();
1547 max_cross = max_cross.max(target_h + margin.vertical());
1548 } else {
1549 main_cursor += target_h + margin.vertical();
1550 max_cross = max_cross.max(target_w + margin.horizontal());
1551 }
1552 if offset + 1 < count_all {
1553 main_cursor += main_gap;
1554 }
1555 }
1556 if mode == LayoutRunMode::PerformLayout
1557 && !stretched_records.is_empty()
1558 && max_cross > cross_avail + 0.5
1559 {
1560 for (arena_idx, rec_size, rec_margin) in stretched_records.iter().copied() {
1561 let widened: f32 = (max_cross - rec_margin.horizontal()).max(rec_size.get_width());
1562 Self::apply_post_stretch(soa, output_view, arena_idx, Some(widened), None);
1563 }
1564 }
1565 let out_size: Size = if is_row {
1566 Size::new(main_cursor, max_cross)
1567 } else {
1568 Size::new(max_cross, main_cursor)
1569 };
1570 (out_size, agg_deps, agg_min_aw)
1571 }
1572
1573 #[cfg(not(target_arch = "wasm32"))]
1587 fn solve_flex_single_pass_recurse(
1588 fork_ctx: &mut ParallelForkContext<'_>,
1589 flow_children: &[u32],
1590 frame: ContainerFrame,
1591 direction: FlexDirection,
1592 mode: LayoutRunMode,
1593 ) -> (Size, u8, f32) {
1594 let soa: &SceneLayoutSoa = fork_ctx.soa;
1595 let output_view: DisjointOutputView = fork_ctx.output_view;
1596 let parent_layout: LayoutStyle = frame.get_parent_layout();
1597 let inner_avail: Size = frame.get_inner_avail();
1598 let viewport: Size = frame.get_viewport();
1599 let content_origin: Point = frame.get_content_origin();
1600 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
1601 let main_gap: f32 = if is_row {
1602 parent_layout.get_column_gap()
1603 } else {
1604 parent_layout.get_row_gap()
1605 };
1606 let cross_avail: f32 = if is_row {
1607 inner_avail.get_height()
1608 } else {
1609 inner_avail.get_width()
1610 };
1611 let parent_align: AlignItems = parent_layout.get_align_items();
1612 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
1613 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
1614 let mut main_cursor: f32 = 0.0;
1615 let mut max_cross: f32 = 0.0;
1616 let mut agg_deps: u8 = LAYOUT_DEP_NONE;
1617 let mut agg_min_aw: f32 = 0.0;
1618 let mut stretched_records: Vec<(usize, Size, Edges)> = Vec::new();
1619 let count_all: usize = flow_children.len();
1620 for (offset, &child_bfs) in flow_children.iter().enumerate() {
1621 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
1622 let child_layout: LayoutStyle = layout_styles[arena_idx];
1623 let margin: Edges = child_layout.get_margin();
1624 let align: AlignItems = match child_layout.get_align_self() {
1625 AlignSelf::Auto => parent_align,
1626 AlignSelf::Stretch => AlignItems::Stretch,
1627 AlignSelf::Start => AlignItems::Start,
1628 AlignSelf::Center => AlignItems::Center,
1629 AlignSelf::End => AlignItems::End,
1630 };
1631 let (pos_x, pos_y): (f32, f32) = if is_row {
1632 (
1633 content_origin.get_x() + main_cursor + margin.get_left(),
1634 content_origin.get_y() + margin.get_top(),
1635 )
1636 } else {
1637 (
1638 content_origin.get_x() + margin.get_left(),
1639 content_origin.get_y() + main_cursor + margin.get_top(),
1640 )
1641 };
1642 let stretch_w: bool =
1643 !is_row && align == AlignItems::Stretch && child_layout.get_width() == Length::Auto;
1644 let stretch_h: bool =
1645 is_row && align == AlignItems::Stretch && child_layout.get_height() == Length::Auto;
1646 if stretch_w {
1647 agg_deps |= LAYOUT_DEP_AVAIL_W;
1648 }
1649 if stretch_h {
1650 agg_deps |= LAYOUT_DEP_AVAIL_H;
1651 }
1652 let space_w: f32 = if stretch_w {
1653 (inner_avail.get_width() - margin.horizontal()).max(0.0)
1654 } else {
1655 inner_avail.get_width()
1656 };
1657 let space_h: f32 = if stretch_h {
1658 (inner_avail.get_height() - margin.vertical()).max(0.0)
1659 } else {
1660 inner_avail.get_height()
1661 };
1662 let final_space: AvailableSpace =
1663 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
1664 .with_stretch(stretch_w, stretch_h);
1665 let (placed, c_deps, c_min_aw): (Size, u8, f32) =
1666 Self::solve_node_dispatch(fork_ctx, child_bfs, final_space, mode);
1667 agg_deps |= c_deps;
1668 if c_min_aw > 0.0 {
1669 agg_min_aw = agg_min_aw.max(c_min_aw + margin.horizontal());
1670 }
1671 let target_w: f32 = if stretch_w {
1672 space_w.max(placed.get_width())
1673 } else {
1674 placed.get_width()
1675 };
1676 let target_h: f32 = if stretch_h {
1677 space_h.max(placed.get_height())
1678 } else {
1679 placed.get_height()
1680 };
1681 if mode == LayoutRunMode::PerformLayout {
1682 if (stretch_w && target_w > placed.get_width() + 0.01)
1683 || (stretch_h && target_h > placed.get_height() + 0.01)
1684 {
1685 Self::apply_post_stretch(
1686 soa,
1687 output_view,
1688 arena_idx,
1689 Some(target_w),
1690 Some(target_h),
1691 );
1692 }
1693 if stretch_w {
1694 stretched_records.push((arena_idx, Size::new(target_w, target_h), margin));
1695 }
1696 }
1697 if is_row {
1698 main_cursor += target_w + margin.horizontal();
1699 max_cross = max_cross.max(target_h + margin.vertical());
1700 } else {
1701 main_cursor += target_h + margin.vertical();
1702 max_cross = max_cross.max(target_w + margin.horizontal());
1703 }
1704 if offset + 1 < count_all {
1705 main_cursor += main_gap;
1706 }
1707 }
1708 if mode == LayoutRunMode::PerformLayout
1709 && !stretched_records.is_empty()
1710 && max_cross > cross_avail + 0.5
1711 {
1712 for (arena_idx, rec_size, rec_margin) in stretched_records.iter().copied() {
1713 let widened: f32 = (max_cross - rec_margin.horizontal()).max(rec_size.get_width());
1714 Self::apply_post_stretch(soa, output_view, arena_idx, Some(widened), None);
1715 }
1716 }
1717 let out_size: Size = if is_row {
1718 Size::new(main_cursor, max_cross)
1719 } else {
1720 Size::new(max_cross, main_cursor)
1721 };
1722 (out_size, agg_deps, agg_min_aw)
1723 }
1724
1725 #[cfg(not(target_arch = "wasm32"))]
1740 fn solve_flex_two_pass_dispatch(
1741 fork_ctx: &mut ParallelForkContext<'_>,
1742 flow_children: &[u32],
1743 frame: ContainerFrame,
1744 direction: FlexDirection,
1745 mode: LayoutRunMode,
1746 can_fork_here: bool,
1747 ) -> (Size, u8, f32) {
1748 if flow_children.is_empty() {
1749 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1750 }
1751 let soa: &SceneLayoutSoa = fork_ctx.soa;
1752 let output_view: DisjointOutputView = fork_ctx.output_view;
1753 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
1754 let is_reverse: bool = matches!(
1755 direction,
1756 FlexDirection::RowReverse | FlexDirection::ColumnReverse
1757 );
1758 let ordered: Vec<u32> = if is_reverse {
1759 flow_children.iter().copied().rev().collect()
1760 } else {
1761 flow_children.to_vec()
1762 };
1763 if !can_fork_here {
1764 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
1766 let flow_start: usize = lane_zero.flow_scratch.len();
1767 lane_zero.flow_scratch.extend_from_slice(ordered.as_slice());
1768 let flow_end: usize = lane_zero.flow_scratch.len();
1769 let result: (Size, u8, f32) = Self::solve_flex_serial(
1770 soa,
1771 output_view,
1772 lane_zero,
1773 (flow_start, flow_end),
1774 frame,
1775 direction,
1776 (false, false, mode),
1777 );
1778 lane_zero.flow_scratch.truncate(flow_start);
1779 return result;
1780 }
1781 let parent_layout: LayoutStyle = frame.get_parent_layout();
1782 let inner_avail: Size = frame.get_inner_avail();
1783 let viewport: Size = frame.get_viewport();
1784 let content_origin: Point = frame.get_content_origin();
1785 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
1786 let main_gap: f32 = if is_row {
1787 parent_layout.get_column_gap()
1788 } else {
1789 parent_layout.get_row_gap()
1790 };
1791 let cross_gap: f32 = if is_row {
1792 parent_layout.get_row_gap()
1793 } else {
1794 parent_layout.get_column_gap()
1795 };
1796 let main_avail: f32 = if is_row {
1797 inner_avail.get_width()
1798 } else {
1799 inner_avail.get_height()
1800 };
1801 let cross_avail: f32 = if is_row {
1802 inner_avail.get_height()
1803 } else {
1804 inner_avail.get_width()
1805 };
1806 let wrap_enabled: bool = parent_layout.get_flex_wrap() == FlexWrap::Wrap;
1807 let probe_template: AvailableSpace = AvailableSpace::new(
1808 inner_avail.get_width(),
1809 inner_avail.get_height(),
1810 viewport,
1811 content_origin.get_x(),
1812 content_origin.get_y(),
1813 );
1814 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
1815 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
1816 let node_kinds: &[SoaNodeKindTag] = soa.get_node_kinds().as_slice();
1817 let text_styles: &[TextStyle] = soa.get_text_styles().as_slice();
1818 let mut records: Vec<ParallelFlexItem> = Vec::with_capacity(ordered.len());
1819 let mut item_meta: Vec<PreparedFlexItemMeta> = Vec::with_capacity(ordered.len());
1820 let mut pass1_deps: u8 = LAYOUT_DEP_NONE;
1821 fork_ctx.child_tasks.clear();
1822 for &child_bfs in ordered.iter() {
1823 let (rec, meta, dep_bits): (ParallelFlexItem, PreparedFlexItemMeta, u8) =
1824 Self::prepare_flex_item(
1825 child_bfs,
1826 layout_styles[bfs_to_arena[child_bfs as usize] as usize],
1827 node_kinds[bfs_to_arena[child_bfs as usize] as usize],
1828 text_styles[bfs_to_arena[child_bfs as usize] as usize],
1829 (is_row, wrap_enabled, main_avail, cross_avail, viewport),
1830 );
1831 pass1_deps |= dep_bits;
1832 if meta.zero_basis_size.is_none() {
1833 fork_ctx.child_tasks.push(ParallelChildTask {
1834 bfs_index: child_bfs,
1835 space: probe_template,
1836 mode: LayoutRunMode::ComputeSize,
1837 });
1838 }
1839 records.push(rec);
1840 item_meta.push(meta);
1841 }
1842 if !fork_ctx.child_tasks.is_empty() {
1843 Self::run_parallel_child_wave(fork_ctx);
1844 }
1845 let mut probe_cursor: usize = 0;
1846 let mut total_grow: f32 = 0.0;
1847 let mut single_line_main: f32 = 0.0;
1848 let mut single_line_cross: f32 = 0.0;
1849 let mut pass1_min_aw: f32 = 0.0;
1850 for (item_idx, rec) in records.iter_mut().enumerate() {
1851 let meta: PreparedFlexItemMeta = item_meta[item_idx];
1852 total_grow += rec.get_flex_grow();
1853 let mut child_size: Size = if let Some(zero_size) = meta.zero_basis_size {
1854 zero_size
1855 } else {
1856 let outcome: ParallelChildOutcome = fork_ctx.child_outcomes[probe_cursor];
1857 probe_cursor = probe_cursor.saturating_add(1);
1858 pass1_deps |= outcome.dep_flags;
1859 if outcome.min_avail_w > 0.0 {
1860 pass1_min_aw =
1861 pass1_min_aw.max(outcome.min_avail_w + rec.get_margin().horizontal());
1862 }
1863 outcome.size
1864 };
1865 if let Some(basis_raw) = meta.explicit_basis_main
1866 && meta.zero_basis_size.is_none()
1867 {
1868 let mut clamped_basis: f32 = basis_raw.max(meta.explicit_min_main.unwrap_or(0.0));
1869 if let Some(max_bound) = meta.explicit_max_main {
1870 clamped_basis = clamped_basis.min(max_bound);
1871 }
1872 if is_row {
1873 child_size.set_width(clamped_basis);
1874 } else {
1875 child_size.set_height(clamped_basis);
1876 }
1877 }
1878 let resolved_min_main: f32 = if let Some(explicit_min) = meta.explicit_min_main {
1879 explicit_min.max(0.0)
1880 } else if rec.get_overflow() == Overflow::Scroll
1881 || (rec.get_overflow() == Overflow::Hidden && !meta.has_px_main)
1882 {
1883 0.0
1884 } else if is_row {
1885 if meta.is_unwrapped_clip_text {
1886 child_size.get_width()
1887 } else {
1888 0.0
1889 }
1890 } else {
1891 child_size.get_height()
1892 };
1893 let _: &mut ParallelFlexItem = rec.set_min_main(resolved_min_main).set_size(child_size);
1894 if !wrap_enabled {
1895 if is_row {
1896 single_line_main += child_size.get_width() + rec.get_margin().horizontal();
1897 single_line_cross = single_line_cross
1898 .max(child_size.get_height() + rec.get_margin().vertical());
1899 } else {
1900 single_line_main += child_size.get_height() + rec.get_margin().vertical();
1901 single_line_cross = single_line_cross
1902 .max(child_size.get_width() + rec.get_margin().horizontal());
1903 }
1904 }
1905 }
1906 let mut lines: Vec<(usize, usize)> = Vec::new();
1907 let mut wrap_deps: u8 = pass1_deps;
1908 let mut wrap_min_aw: f32 = pass1_min_aw;
1909 if !wrap_enabled {
1910 lines.push((0, records.len()));
1911 } else {
1912 let mut cur_line_start: usize = 0;
1913 let mut line_main: f32 = 0.0;
1914 for (item_idx, item) in records.iter().enumerate() {
1915 let item_main: f32 = if is_row {
1916 item.get_size().get_width() + item.get_margin().horizontal()
1917 } else {
1918 item.get_size().get_height() + item.get_margin().vertical()
1919 };
1920 let line_empty: bool = item_idx == cur_line_start;
1921 let needed: f32 = if line_empty {
1922 item_main
1923 } else {
1924 line_main + main_gap + item_main
1925 };
1926 if !line_empty && needed > main_avail && main_avail > 0.0 {
1927 lines.push((cur_line_start, item_idx));
1928 cur_line_start = item_idx;
1929 line_main = item_main;
1930 } else {
1931 line_main = needed;
1932 }
1933 }
1934 if cur_line_start < records.len() {
1935 lines.push((cur_line_start, records.len()));
1936 }
1937 if is_row {
1938 if lines.len() > 1 {
1939 wrap_deps |= LAYOUT_DEP_AVAIL_W;
1940 } else {
1941 wrap_min_aw = wrap_min_aw.max(line_main + 0.5);
1942 }
1943 } else {
1944 wrap_deps |= LAYOUT_DEP_AVAIL_H;
1945 }
1946 }
1947 let line_count: usize = lines.len();
1948 let mut cross_cursor: f32 = 0.0;
1949 let mut max_main_extent: f32 = 0.0;
1950 for (line_offset, &(line_start, line_end)) in lines.iter().enumerate() {
1951 let line_grow: f32 = if !wrap_enabled {
1952 total_grow
1953 } else {
1954 records[line_start..line_end]
1955 .iter()
1956 .map(ParallelFlexItem::get_flex_grow)
1957 .sum()
1958 };
1959 let mut line_ctx: FlexLineContext = FlexLineContext::new(
1960 frame,
1961 is_row,
1962 main_gap,
1963 main_avail,
1964 cross_avail,
1965 line_grow,
1966 mode,
1967 );
1968 let _: &mut FlexLineContext = line_ctx
1969 .set_wrap_enabled(wrap_enabled)
1970 .set_line_count(line_count);
1971 let precomputed: Option<(f32, f32)> = if !wrap_enabled {
1972 Some((single_line_main, single_line_cross))
1973 } else {
1974 None
1975 };
1976 let (line_size, l_deps, l_min_aw): (Size, u8, f32) = Self::solve_flex_line_parallel(
1977 fork_ctx,
1978 &mut records[line_start..line_end],
1979 cross_cursor,
1980 precomputed,
1981 line_ctx,
1982 );
1983 wrap_deps |= l_deps;
1984 wrap_min_aw = wrap_min_aw.max(l_min_aw);
1985 if !wrap_enabled {
1986 return (line_size, wrap_deps, wrap_min_aw);
1987 }
1988 if is_row {
1989 max_main_extent = max_main_extent.max(line_size.get_width());
1990 cross_cursor += line_size.get_height();
1991 } else {
1992 max_main_extent = max_main_extent.max(line_size.get_height());
1993 cross_cursor += line_size.get_width();
1994 }
1995 if line_offset + 1 < line_count {
1996 cross_cursor += cross_gap;
1997 }
1998 }
1999 let out_size: Size = if is_row {
2000 Size::new(max_main_extent, cross_cursor)
2001 } else {
2002 Size::new(cross_cursor, max_main_extent)
2003 };
2004 (out_size, wrap_deps, wrap_min_aw)
2005 }
2006
2007 #[cfg(not(target_arch = "wasm32"))]
2021 fn solve_flex_line_parallel(
2022 fork_ctx: &mut ParallelForkContext<'_>,
2023 line_items: &mut [ParallelFlexItem],
2024 cross_cursor: f32,
2025 precomputed: Option<(f32, f32)>,
2026 ctx: FlexLineContext,
2027 ) -> (Size, u8, f32) {
2028 let soa: &SceneLayoutSoa = fork_ctx.soa;
2029 let output_view: DisjointOutputView = fork_ctx.output_view;
2030 let scratch_bank: &WorkerScratchBank = fork_ctx.scratch_bank;
2031 let lane_zero: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
2033 let (
2034 final_used_main,
2035 line_cross,
2036 line_deps,
2037 line_min_aw,
2038 line_shrunk,
2039 effective_main_avail,
2040 ): (f32, f32, u8, f32, bool, f32) = Self::resolve_flex_line_sizes(
2041 soa,
2042 output_view,
2043 lane_zero,
2044 line_items,
2045 precomputed,
2046 ctx,
2047 );
2048 let is_row: bool = ctx.get_is_row();
2049 let mode: LayoutRunMode = ctx.get_mode();
2050 if mode == LayoutRunMode::ComputeSize {
2051 let sz: Size = if is_row {
2052 Size::new(final_used_main, line_cross)
2053 } else {
2054 Size::new(line_cross, final_used_main)
2055 };
2056 return (sz, line_deps, line_min_aw);
2057 }
2058 let frame: ContainerFrame = ctx.get_frame();
2059 let parent_layout: LayoutStyle = frame.get_parent_layout();
2060 let inner_avail: Size = frame.get_inner_avail();
2061 let viewport: Size = frame.get_viewport();
2062 let content_origin: Point = frame.get_content_origin();
2063 let main_gap: f32 = ctx.get_main_gap();
2064 let cross_avail: f32 = ctx.get_cross_avail();
2065 let wrap_enabled: bool = ctx.get_wrap_enabled();
2066 let line_count: usize = ctx.get_line_count();
2067 let count: usize = line_items.len();
2068 let rem: f32 = (effective_main_avail - final_used_main).max(0.0);
2069 let (mut main_cursor, extra_step): (f32, f32) = match parent_layout.get_justify_content() {
2070 JustifyContent::Start => (0.0, 0.0),
2071 JustifyContent::Center => (rem * 0.5, 0.0),
2072 JustifyContent::End => (rem, 0.0),
2073 JustifyContent::SpaceBetween => {
2074 if count > 1 {
2075 (0.0, rem / ((count - 1) as f32))
2076 } else {
2077 (0.0, 0.0)
2078 }
2079 }
2080 JustifyContent::SpaceAround => {
2081 let slot: f32 = rem / (count.max(1) as f32);
2082 (slot * 0.5, slot)
2083 }
2084 JustifyContent::SpaceEvenly => {
2085 let slot: f32 = rem / ((count + 1) as f32);
2086 (slot, slot)
2087 }
2088 };
2089 let min_cross: f32 = if is_row {
2090 frame.get_min_inner_size().get_height()
2091 } else {
2092 frame.get_min_inner_size().get_width()
2093 };
2094 let definite_cross: bool = !is_row || frame.get_definite_height();
2095 let effective_line_cross: f32 = if line_count == 1 {
2096 let floor_cross: f32 = if definite_cross && (!wrap_enabled || is_row) {
2097 cross_avail.max(min_cross)
2098 } else {
2099 min_cross
2100 };
2101 line_cross.max(floor_cross)
2102 } else {
2103 line_cross
2104 };
2105 let parent_align: AlignItems = parent_layout.get_align_items();
2106 fork_ctx.child_tasks.clear();
2107 let mut placement_meta: Vec<FlexPlacementMeta> = Vec::with_capacity(count);
2108 for (idx, rec) in line_items.iter().copied().enumerate() {
2109 let child_size: Size = rec.get_size();
2110 let margin: Edges = rec.get_margin();
2111 let width_auto: bool = (rec.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0;
2112 let height_auto: bool = (rec.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0;
2113 let item_cross: f32 = if is_row {
2114 child_size.get_height()
2115 } else {
2116 child_size.get_width()
2117 };
2118 let align: AlignItems = match rec.get_align_self() {
2119 AlignSelf::Auto => parent_align,
2120 AlignSelf::Stretch => AlignItems::Stretch,
2121 AlignSelf::Start => AlignItems::Start,
2122 AlignSelf::Center => AlignItems::Center,
2123 AlignSelf::End => AlignItems::End,
2124 };
2125 let avail_item_cross: f32 = (effective_line_cross
2126 - if is_row {
2127 margin.vertical()
2128 } else {
2129 margin.horizontal()
2130 })
2131 .max(0.0);
2132 let cross_offset: f32 = match align {
2133 AlignItems::Start | AlignItems::Stretch => 0.0,
2134 AlignItems::Center => ((avail_item_cross - item_cross) * 0.5).max(0.0),
2135 AlignItems::End => (avail_item_cross - item_cross).max(0.0),
2136 };
2137 let main_resized_item: bool = rec.get_flex_grow() > 0.0 || line_shrunk;
2138 let auto_cross_grow: bool =
2139 main_resized_item && if is_row { height_auto } else { width_auto };
2140 let target_w: f32 = if !is_row && align == AlignItems::Stretch && width_auto {
2141 avail_item_cross.max(child_size.get_width())
2142 } else {
2143 child_size.get_width()
2144 };
2145 let target_h: f32 = if is_row && align == AlignItems::Stretch && height_auto {
2146 avail_item_cross.max(child_size.get_height())
2147 } else {
2148 child_size.get_height()
2149 };
2150 let (pos_x, pos_y): (f32, f32) = if is_row {
2151 (
2152 content_origin.get_x() + main_cursor + margin.get_left(),
2153 content_origin.get_y() + cross_cursor + cross_offset + margin.get_top(),
2154 )
2155 } else {
2156 (
2157 content_origin.get_x() + cross_cursor + cross_offset + margin.get_left(),
2158 content_origin.get_y() + main_cursor + margin.get_top(),
2159 )
2160 };
2161 let stretch_child_w: bool = !(auto_cross_grow && !is_row)
2162 && ((!is_row && align == AlignItems::Stretch && width_auto)
2163 || (is_row && main_resized_item && width_auto));
2164 let stretch_child_h: bool = !(auto_cross_grow && is_row)
2165 && ((is_row && align == AlignItems::Stretch && height_auto)
2166 || (!is_row && main_resized_item && height_auto));
2167 let space_w: f32 =
2168 if !stretch_child_w && (rec.get_flags() & FLEX_ITEM_WIDTH_PERCENT) != 0 {
2169 inner_avail.get_width()
2170 } else if auto_cross_grow && !is_row {
2171 cross_avail
2172 } else {
2173 target_w
2174 };
2175 let space_h: f32 =
2176 if !stretch_child_h && (rec.get_flags() & FLEX_ITEM_HEIGHT_PERCENT) != 0 {
2177 inner_avail.get_height()
2178 } else if auto_cross_grow && is_row {
2179 cross_avail
2180 } else {
2181 target_h
2182 };
2183 let final_space: AvailableSpace =
2184 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
2185 .with_stretch(stretch_child_w, stretch_child_h);
2186 fork_ctx.child_tasks.push(ParallelChildTask {
2187 bfs_index: rec.get_bfs_index(),
2188 space: final_space,
2189 mode: LayoutRunMode::PerformLayout,
2190 });
2191 placement_meta.push(FlexPlacementMeta {
2192 target_w,
2193 target_h,
2194 avail_item_cross,
2195 cross_offset,
2196 align,
2197 auto_cross_grow,
2198 main_resized_item,
2199 });
2200 let advance: f32 = if is_row {
2201 target_w + margin.horizontal()
2202 } else {
2203 target_h + margin.vertical()
2204 };
2205 main_cursor += advance;
2206 if idx + 1 < count {
2207 main_cursor += main_gap + extra_step;
2208 }
2209 }
2210 Self::run_parallel_child_wave(fork_ctx);
2211 let lane_zero_after: &mut WorkerLayoutScratch = unsafe { &mut *scratch_bank.lane_ptr(0) };
2213 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
2214 let mut actual_line_cross: f32 = line_cross;
2215 for (idx, rec) in line_items.iter().copied().enumerate() {
2216 let FlexPlacementMeta {
2217 target_w,
2218 target_h,
2219 avail_item_cross,
2220 cross_offset,
2221 align,
2222 auto_cross_grow,
2223 main_resized_item,
2224 } = placement_meta[idx];
2225 let placed_final: Size = fork_ctx.child_outcomes[idx].size;
2226 if auto_cross_grow && matches!(align, AlignItems::Center | AlignItems::End) {
2227 let actual_item_cross: f32 = if is_row {
2228 placed_final.get_height()
2229 } else {
2230 placed_final.get_width()
2231 };
2232 let refined_offset: f32 = match align {
2233 AlignItems::Center => ((avail_item_cross - actual_item_cross) * 0.5).max(0.0),
2234 AlignItems::End => (avail_item_cross - actual_item_cross).max(0.0),
2235 AlignItems::Start | AlignItems::Stretch => cross_offset,
2236 };
2237 let delta_cross: f32 = refined_offset - cross_offset;
2238 if delta_cross.abs() > 0.001 {
2239 let (dx, dy): (f32, f32) = if is_row {
2240 (0.0, delta_cross)
2241 } else {
2242 (delta_cross, 0.0)
2243 };
2244 Self::translate_subtree_bfs(
2245 soa,
2246 output_view,
2247 lane_zero_after,
2248 rec.get_bfs_index(),
2249 dx,
2250 dy,
2251 );
2252 }
2253 }
2254 let margin: Edges = rec.get_margin();
2255 let is_clipped: bool =
2256 matches!(rec.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2257 let (desired_w, desired_h): (f32, f32) = if auto_cross_grow {
2258 if is_row {
2259 actual_line_cross =
2260 actual_line_cross.max(placed_final.get_height() + margin.vertical());
2261 (target_w, placed_final.get_height())
2262 } else {
2263 actual_line_cross =
2264 actual_line_cross.max(placed_final.get_width() + margin.horizontal());
2265 (placed_final.get_width(), target_h)
2266 }
2267 } else if is_clipped || main_resized_item {
2268 (target_w, target_h)
2269 } else {
2270 (
2271 target_w.max(placed_final.get_width()),
2272 target_h.max(placed_final.get_height()),
2273 )
2274 };
2275 let arena_idx: usize = bfs_to_arena[rec.get_bfs_index() as usize] as usize;
2276 let cur_bounds: Rect = unsafe { output_view.read_box(arena_idx) }.get_bounds();
2278 if (cur_bounds.get_width() - desired_w).abs() > 0.001
2279 || (cur_bounds.get_height() - desired_h).abs() > 0.001
2280 {
2281 Self::apply_post_stretch(
2282 soa,
2283 output_view,
2284 arena_idx,
2285 Some(desired_w),
2286 Some(desired_h),
2287 );
2288 }
2289 }
2290 let sz: Size = if is_row {
2291 Size::new(final_used_main, actual_line_cross)
2292 } else {
2293 Size::new(actual_line_cross, final_used_main)
2294 };
2295 (sz, line_deps, line_min_aw)
2296 }
2297
2298 #[cfg_attr(not(target_arch = "wasm32"), inline)]
2311 fn resolve_container_constraints(
2312 soa: &SceneLayoutSoa,
2313 arena_idx: usize,
2314 layout_style: LayoutStyle,
2315 space: AvailableSpace,
2316 ) -> ResolvedNodeConstraints {
2317 let w_len: Length = layout_style.get_width();
2318 let h_len: Length = layout_style.get_height();
2319 let min_w_len: Length = layout_style.get_min_width();
2320 let max_w_len: Length = layout_style.get_max_width();
2321 let min_h_len: Length = layout_style.get_min_height();
2322 let max_h_len: Length = layout_style.get_max_height();
2323 let mut own_deps: u8 = LAYOUT_DEP_NONE;
2324 if matches!(w_len, Length::Percent(_))
2325 || matches!(min_w_len, Length::Percent(_))
2326 || matches!(max_w_len, Length::Percent(_))
2327 {
2328 own_deps |= LAYOUT_DEP_AVAIL_W;
2329 }
2330 if matches!(h_len, Length::Percent(_))
2331 || matches!(min_h_len, Length::Percent(_))
2332 || matches!(max_h_len, Length::Percent(_))
2333 {
2334 own_deps |= LAYOUT_DEP_AVAIL_H;
2335 }
2336 if Self::is_viewport_length(w_len)
2337 || Self::is_viewport_length(h_len)
2338 || Self::is_viewport_length(min_w_len)
2339 || Self::is_viewport_length(max_w_len)
2340 || Self::is_viewport_length(min_h_len)
2341 || Self::is_viewport_length(max_h_len)
2342 {
2343 own_deps |= LAYOUT_DEP_VIEWPORT;
2344 }
2345 let padding: Edges = layout_style.get_padding();
2346 let border_w: f32 = soa.get_border_widths()[arena_idx];
2347 let chrome_h: f32 = padding.horizontal() + border_w * 2.0;
2348 let chrome_v: f32 = padding.vertical() + border_w * 2.0;
2349 let explicit_w: Option<f32> = w_len.resolve(space.get_width(), space.get_viewport());
2350 let explicit_h: Option<f32> = h_len.resolve(space.get_height(), space.get_viewport());
2351 let mut resolved_w: Option<f32> = explicit_w;
2352 let mut resolved_h: Option<f32> = explicit_h;
2353 let stretched_w: bool = resolved_w.is_none() && space.get_stretch_width();
2354 let stretched_h: bool = resolved_h.is_none() && space.get_stretch_height();
2355 if stretched_w {
2356 resolved_w = Some(space.get_width());
2357 own_deps |= LAYOUT_DEP_AVAIL_W;
2358 }
2359 if stretched_h {
2360 resolved_h = Some(space.get_height());
2361 own_deps |= LAYOUT_DEP_AVAIL_H;
2362 }
2363 let min_w: f32 = min_w_len
2364 .resolve(space.get_width(), space.get_viewport())
2365 .unwrap_or(0.0);
2366 let min_h: f32 = min_h_len
2367 .resolve(space.get_height(), space.get_viewport())
2368 .unwrap_or(0.0);
2369 let max_w: f32 = max_w_len
2370 .resolve(space.get_width(), space.get_viewport())
2371 .unwrap_or(f32::INFINITY)
2372 .max(min_w);
2373 let max_h: f32 = max_h_len
2374 .resolve(space.get_height(), space.get_viewport())
2375 .unwrap_or(f32::INFINITY)
2376 .max(min_h);
2377 if let Some(ratio) = layout_style.aspect_ratio_value()
2378 && ratio > 0.0
2379 {
2380 if let Some(width_val) = resolved_w
2381 && resolved_h.is_none()
2382 {
2383 resolved_h = Some(width_val.clamp(min_w, max_w) / ratio);
2384 } else if let Some(height_val) = resolved_h
2385 && resolved_w.is_none()
2386 {
2387 resolved_w = Some(height_val.clamp(min_h, max_h) * ratio);
2388 }
2389 }
2390 let avail_w_outer: f32 = resolved_w
2391 .unwrap_or(space.get_width())
2392 .max(min_w)
2393 .min(max_w);
2394 let avail_h_outer: f32 = resolved_h
2395 .unwrap_or(space.get_height())
2396 .max(min_h)
2397 .min(max_h);
2398 let inner_avail_w: f32 = (avail_w_outer - chrome_h).max(0.0);
2399 let inner_avail_h: f32 = (avail_h_outer - chrome_v).max(0.0);
2400 let content_origin_x: f32 = space.get_origin_x() + padding.get_left() + border_w;
2401 let content_origin_y: f32 = space.get_origin_y() + padding.get_top() + border_w;
2402 ResolvedNodeConstraints {
2403 own_deps,
2404 resolved_w,
2405 resolved_h,
2406 explicit_w,
2407 stretched_w,
2408 min_w,
2409 max_w,
2410 min_h,
2411 max_h,
2412 chrome_h,
2413 chrome_v,
2414 inner_avail_w,
2415 inner_avail_h,
2416 content_origin_x,
2417 content_origin_y,
2418 }
2419 }
2420
2421 fn finalize_node_box(
2437 soa: &SceneLayoutSoa,
2438 output_view: DisjointOutputView,
2439 scratch: &mut WorkerLayoutScratch,
2440 node_ctx: (LayoutStyle, AvailableSpace, LayoutRunMode, usize),
2441 constraints: ResolvedNodeConstraints,
2442 content_tuple: (Size, u8, f32),
2443 child_range: Option<(usize, usize)>,
2444 ) -> (Size, u8, f32) {
2445 let (layout_style, space, mode, arena_idx): (
2446 LayoutStyle,
2447 AvailableSpace,
2448 LayoutRunMode,
2449 usize,
2450 ) = node_ctx;
2451 let ResolvedNodeConstraints {
2452 own_deps,
2453 resolved_w,
2454 resolved_h,
2455 explicit_w,
2456 stretched_w,
2457 min_w,
2458 max_w,
2459 min_h,
2460 max_h,
2461 chrome_h,
2462 chrome_v,
2463 content_origin_x,
2464 content_origin_y,
2465 ..
2466 } = constraints;
2467 let (content_size, child_deps, child_min_aw): (Size, u8, f32) = content_tuple;
2468 let node_overflow: Overflow = layout_style.get_overflow();
2469 let node_flex_grow: f32 = layout_style.get_flex_grow();
2470 let content_outer_w: f32 = content_size.get_width() + chrome_h;
2471 let content_outer_h: f32 = content_size.get_height() + chrome_v;
2472 let mut final_w: f32 = match (explicit_w, stretched_w) {
2473 (Some(span_width), _) => span_width,
2474 (None, true) => {
2475 let base_w: f32 = resolved_w.unwrap_or(content_outer_w);
2476 if node_overflow == Overflow::Visible
2477 && node_flex_grow <= 0.0
2478 && content_outer_w > base_w + 0.5
2479 {
2480 content_outer_w
2481 } else {
2482 base_w
2483 }
2484 }
2485 (None, false) => content_outer_w,
2486 };
2487 let mut final_h: f32 = resolved_h.unwrap_or(content_outer_h);
2488 if let Some(ratio) = layout_style.aspect_ratio_value()
2489 && ratio > 0.0
2490 {
2491 if resolved_w.is_some() && resolved_h.is_none() {
2492 final_h = final_w / ratio;
2493 } else if resolved_h.is_some() && resolved_w.is_none() {
2494 final_w = final_h * ratio;
2495 }
2496 }
2497 final_w = final_w.clamp(min_w, max_w).max(0.0);
2498 final_h = final_h.clamp(min_h, max_h).max(0.0);
2499 let final_size: Size = Size::new(final_w, final_h);
2500 let shields_avail_w: bool = resolved_w.is_some() && (own_deps & LAYOUT_DEP_AVAIL_W) == 0;
2501 let shields_avail_h: bool = resolved_h.is_some() && (own_deps & LAYOUT_DEP_AVAIL_H) == 0;
2502 let mut final_deps: u8 = own_deps | (child_deps & LAYOUT_DEP_VIEWPORT);
2503 let mut final_min_aw: f32 = 0.0;
2504 if !shields_avail_w {
2505 if (child_deps & LAYOUT_DEP_AVAIL_W) != 0 {
2506 final_deps |= LAYOUT_DEP_AVAIL_W;
2507 } else if child_min_aw > 0.0 {
2508 final_min_aw = child_min_aw + chrome_h;
2509 }
2510 }
2511 if !shields_avail_h {
2512 final_deps |= child_deps & LAYOUT_DEP_AVAIL_H;
2513 }
2514 if mode == LayoutRunMode::ComputeSize {
2515 return (final_size, final_deps, final_min_aw);
2516 }
2517 let bounds: Rect = Rect::new(space.get_origin_x(), space.get_origin_y(), final_w, final_h);
2518 let content_bounds: Rect = Rect::new(
2519 content_origin_x,
2520 content_origin_y,
2521 (final_w - chrome_h).max(0.0),
2522 (final_h - chrome_v).max(0.0),
2523 );
2524 let prev_box: LayoutBox = soa.get_layout_boxes()[arena_idx];
2525 let changed: bool = !Self::rect_approx_eq(prev_box.get_bounds(), bounds)
2526 || !Self::rect_approx_eq(prev_box.get_content_bounds(), content_bounds)
2527 || !Self::size_approx_eq(prev_box.get_content_size(), content_size);
2528 if changed {
2529 scratch.nodes_changed = scratch.nodes_changed.saturating_add(1);
2530 }
2531 let mut next_box: LayoutBox = prev_box;
2532 let _: &mut LayoutBox = next_box
2533 .set_bounds(bounds)
2534 .set_content_bounds(content_bounds)
2535 .set_content_size(content_size);
2536 unsafe {
2538 output_view.write_box(arena_idx, next_box);
2539 output_view.write_constraint(arena_idx, space);
2540 }
2541 let node_transform: Transform2D = soa.get_transforms()[arena_idx];
2542 let node_scroll: Point = prev_box.get_scroll_offset();
2543 let self_is_portal: bool = soa.get_node_kinds()[arena_idx] == SoaNodeKindTag::Portal
2544 || layout_style.get_position() == Position::Fixed;
2545 let own_vis: Rect = shifted_node_bounds(bounds, Point::default(), node_transform);
2546 let is_clipped: bool = matches!(node_overflow, Overflow::Hidden | Overflow::Scroll);
2547 let mut sub_bounds: Rect = own_vis;
2548 let mut has_nonzero_z: bool = false;
2549 if let Some((c_start, c_end)) = child_range {
2550 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
2551 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
2552 let node_kinds: &[SoaNodeKindTag] = soa.get_node_kinds().as_slice();
2553 let transforms: &[Transform2D] = soa.get_transforms().as_slice();
2554 let tx: f32 = node_transform.get_translate_x() - node_scroll.get_x();
2555 let ty: f32 = node_transform.get_translate_y() - node_scroll.get_y();
2556 for &c_arena_u32 in &bfs_to_arena[c_start..c_end] {
2557 let c_arena: usize = c_arena_u32 as usize;
2558 let c_layout: LayoutStyle = layout_styles[c_arena];
2559 if c_layout.get_z_index() != 0 {
2560 has_nonzero_z = true;
2561 }
2562 let c_is_portal: bool = node_kinds[c_arena] == SoaNodeKindTag::Portal
2563 || c_layout.get_position() == Position::Fixed;
2564 if !is_clipped
2565 && c_layout.get_display() != Display::None
2566 && (self_is_portal || !c_is_portal)
2567 {
2568 let child_sb: Rect = unsafe {
2570 let stored: Rect = output_view.read_subtree_bounds(c_arena);
2571 if stored.get_width() > 0.0 || stored.get_height() > 0.0 {
2572 stored
2573 } else {
2574 shifted_node_bounds(
2575 output_view.read_box(c_arena).get_bounds(),
2576 Point::default(),
2577 transforms[c_arena],
2578 )
2579 }
2580 };
2581 if child_sb.get_width() > 0.0 && child_sb.get_height() > 0.0 {
2582 let shifted_child: Rect = simd_translate_rect(child_sb, tx, ty);
2583 sub_bounds = sub_bounds.union(shifted_child);
2584 }
2585 }
2586 }
2587 }
2588 unsafe {
2590 output_view.write_subtree_bounds(arena_idx, sub_bounds);
2591 output_view.write_result(
2592 arena_idx,
2593 ParallelNodeResult {
2594 size: final_size,
2595 natural_bounds: bounds,
2596 natural_content_bounds: content_bounds,
2597 dep_flags: final_deps,
2598 min_avail_w: final_min_aw,
2599 post_stretched: false,
2600 hidden: false,
2601 written: true,
2602 has_nonzero_z,
2603 },
2604 );
2605 }
2606 (final_size, final_deps, final_min_aw)
2607 }
2608
2609 fn apply_post_stretch(
2619 soa: &SceneLayoutSoa,
2620 output_view: DisjointOutputView,
2621 arena_idx: usize,
2622 new_width: Option<f32>,
2623 new_height: Option<f32>,
2624 ) {
2625 let layout_style: LayoutStyle = soa.get_layout_styles()[arena_idx];
2626 let padding: Edges = layout_style.get_padding();
2627 let border_w: f32 = soa.get_border_widths()[arena_idx];
2628 let transform_val: Transform2D = soa.get_transforms()[arena_idx];
2629 let is_clipped: bool = matches!(
2630 layout_style.get_overflow(),
2631 Overflow::Hidden | Overflow::Scroll
2632 );
2633 unsafe {
2635 let mut cur_box: LayoutBox = output_view.read_box(arena_idx);
2636 let mut bounds: Rect = cur_box.get_bounds();
2637 let mut content_bounds: Rect = cur_box.get_content_bounds();
2638 if let Some(target_w) = new_width {
2639 let _: &mut Rect = bounds.set_width(target_w);
2640 let _: &mut Rect = content_bounds
2641 .set_width((target_w - padding.horizontal() - border_w * 2.0).max(0.0));
2642 }
2643 if let Some(target_h) = new_height {
2644 let _: &mut Rect = bounds.set_height(target_h);
2645 let _: &mut Rect = content_bounds
2646 .set_height((target_h - padding.vertical() - border_w * 2.0).max(0.0));
2647 }
2648 let _: &mut LayoutBox = cur_box
2649 .set_bounds(bounds)
2650 .set_content_bounds(content_bounds);
2651 output_view.write_box(arena_idx, cur_box);
2652 let own_vis: Rect = shifted_node_bounds(bounds, Point::default(), transform_val);
2653 let next_sub: Rect = if is_clipped {
2654 own_vis
2655 } else {
2656 output_view.read_subtree_bounds(arena_idx).union(own_vis)
2657 };
2658 output_view.write_subtree_bounds(arena_idx, next_sub);
2659 let mut res: ParallelNodeResult = output_view.read_result(arena_idx);
2660 res.post_stretched = true;
2661 output_view.write_result(arena_idx, res);
2662 }
2663 }
2664
2665 fn solve_node_serial(
2680 soa: &SceneLayoutSoa,
2681 output_view: DisjointOutputView,
2682 scratch: &mut WorkerLayoutScratch,
2683 bfs_index: u32,
2684 space: AvailableSpace,
2685 mode: LayoutRunMode,
2686 ) -> (Size, u8, f32) {
2687 scratch.nodes_computed = scratch.nodes_computed.saturating_add(1);
2688 let topology: &FlatBfsTopology = soa.get_topology();
2689 let arena_idx: usize = topology.get_bfs_to_arena()[bfs_index as usize] as usize;
2690 let layout_style: LayoutStyle = soa.get_layout_styles()[arena_idx];
2691 if layout_style.get_display() == Display::None {
2692 if mode == LayoutRunMode::PerformLayout {
2693 let prev_box: LayoutBox = soa.get_layout_boxes()[arena_idx];
2694 if !Self::rect_approx_eq(prev_box.get_bounds(), Rect::default()) {
2695 scratch.nodes_changed = scratch.nodes_changed.saturating_add(1);
2696 }
2697 let mut hidden_box: LayoutBox = prev_box;
2698 let _: &mut LayoutBox = hidden_box
2699 .set_bounds(Rect::default())
2700 .set_content_bounds(Rect::default());
2701 unsafe {
2703 output_view.write_box(arena_idx, hidden_box);
2704 output_view.write_constraint(arena_idx, space);
2705 output_view.write_subtree_bounds(arena_idx, Rect::default());
2706 output_view.write_result(
2707 arena_idx,
2708 ParallelNodeResult {
2709 size: Size::default(),
2710 natural_bounds: Rect::default(),
2711 natural_content_bounds: Rect::default(),
2712 dep_flags: LAYOUT_DEP_NONE,
2713 min_avail_w: 0.0,
2714 post_stretched: false,
2715 hidden: true,
2716 written: true,
2717 has_nonzero_z: false,
2718 },
2719 );
2720 }
2721 }
2722 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
2723 }
2724 let constraints: ResolvedNodeConstraints =
2725 Self::resolve_container_constraints(soa, arena_idx, layout_style, space);
2726 let node_overflow: Overflow = layout_style.get_overflow();
2727 let node_flex_grow: f32 = layout_style.get_flex_grow();
2728 let width_independent: bool = constraints.explicit_w.is_some()
2729 || (constraints.stretched_w
2730 && constraints.resolved_w.is_some()
2731 && (node_overflow != Overflow::Visible || node_flex_grow > 0.0));
2732 let height_independent: bool =
2733 constraints.resolved_h.is_some() && layout_style.get_height() != Length::Auto;
2734 if mode == LayoutRunMode::ComputeSize
2735 && width_independent
2736 && constraints.resolved_h.is_some()
2737 {
2738 let final_w: f32 = constraints
2739 .resolved_w
2740 .unwrap_or(0.0)
2741 .clamp(constraints.min_w, constraints.max_w)
2742 .max(0.0);
2743 let final_h: f32 = constraints
2744 .resolved_h
2745 .unwrap_or(0.0)
2746 .clamp(constraints.min_h, constraints.max_h)
2747 .max(0.0);
2748 return (Size::new(final_w, final_h), constraints.own_deps, 0.0);
2749 }
2750 let skip_leaf_measure: bool =
2751 node_overflow != Overflow::Scroll && width_independent && height_independent;
2752 let kind_tag: SoaNodeKindTag = soa.get_node_kinds()[arena_idx];
2753 let text_style: TextStyle = soa.get_text_styles()[arena_idx];
2754 let text_slice: SoaTextSlice = soa.get_text_slices()[arena_idx];
2755 let mut child_deps: u8 = LAYOUT_DEP_NONE;
2756 let mut child_min_aw: f32 = 0.0;
2757 let mut child_range: Option<(usize, usize)> = None;
2758 let content_size: Size = match kind_tag {
2759 SoaNodeKindTag::Text => {
2760 if skip_leaf_measure {
2761 Size::new(constraints.inner_avail_w, constraints.inner_avail_h)
2762 } else {
2763 let sample_str: &str = soa.text_for_arena(arena_idx);
2764 let measured: TextSizeMeasurement = scratch.measure_text_cached(
2765 sample_str,
2766 text_slice.get_is_ascii(),
2767 text_style,
2768 Some(constraints.inner_avail_w.max(1.0)),
2769 );
2770 if text_style.get_overflow() != TextOverflow::Clip {
2771 if measured.unwrapped && measured.max_content_w.is_finite() {
2772 child_min_aw = measured.max_content_w + 0.5;
2773 } else {
2774 child_deps |= LAYOUT_DEP_AVAIL_W;
2775 }
2776 }
2777 measured.size
2778 }
2779 }
2780 SoaNodeKindTag::Image => soa.get_image_sizes()[arena_idx],
2781 SoaNodeKindTag::Input => {
2782 if skip_leaf_measure {
2783 Size::new(constraints.inner_avail_w, constraints.inner_avail_h)
2784 } else {
2785 let sample_str: &str = soa.text_for_arena(arena_idx);
2786 let measured: TextSizeMeasurement = scratch.measure_text_cached(
2787 sample_str,
2788 text_slice.get_is_ascii(),
2789 text_style,
2790 Some(constraints.inner_avail_w.max(1.0)),
2791 );
2792 if text_style.get_overflow() != TextOverflow::Clip {
2793 if measured.unwrapped && measured.max_content_w.is_finite() {
2794 child_min_aw = measured.max_content_w + 0.5;
2795 } else {
2796 child_deps |= LAYOUT_DEP_AVAIL_W;
2797 }
2798 }
2799 Size::new(
2800 measured.size.get_width().max(120.0),
2801 measured.size.get_height().max(20.0),
2802 )
2803 }
2804 }
2805 SoaNodeKindTag::Container
2806 | SoaNodeKindTag::ScrollView
2807 | SoaNodeKindTag::Portal
2808 | SoaNodeKindTag::Fragment
2809 | SoaNodeKindTag::Canvas => {
2810 let (c_start, c_end): (usize, usize) =
2811 topology.children_bfs_range(bfs_index as usize);
2812 if c_start >= c_end {
2813 Size::default()
2814 } else {
2815 child_range = Some((c_start, c_end));
2816 let min_inner_size: Size = Size::new(
2817 (constraints.min_w - constraints.chrome_h).max(0.0),
2818 (constraints.min_h - constraints.chrome_v).max(0.0),
2819 );
2820 let frame: ContainerFrame = ContainerFrame::new(
2821 layout_style,
2822 Size::new(constraints.inner_avail_w, constraints.inner_avail_h),
2823 space.get_viewport(),
2824 Point::new(constraints.content_origin_x, constraints.content_origin_y),
2825 constraints.resolved_w.is_some(),
2826 constraints.resolved_h.is_some(),
2827 min_inner_size,
2828 );
2829 let (size, c_deps, c_min_aw): (Size, u8, f32) = Self::solve_children_serial(
2830 soa,
2831 output_view,
2832 scratch,
2833 (c_start, c_end),
2834 frame,
2835 mode,
2836 );
2837 child_deps = c_deps;
2838 child_min_aw = c_min_aw;
2839 size
2840 }
2841 }
2842 };
2843 Self::finalize_node_box(
2844 soa,
2845 output_view,
2846 scratch,
2847 (layout_style, space, mode, arena_idx),
2848 constraints,
2849 (content_size, child_deps, child_min_aw),
2850 child_range,
2851 )
2852 }
2853
2854 fn solve_children_serial(
2869 soa: &SceneLayoutSoa,
2870 output_view: DisjointOutputView,
2871 scratch: &mut WorkerLayoutScratch,
2872 child_range: (usize, usize),
2873 frame: ContainerFrame,
2874 mode: LayoutRunMode,
2875 ) -> (Size, u8, f32) {
2876 let (child_start, child_end): (usize, usize) = child_range;
2877 if child_start >= child_end {
2878 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
2879 }
2880 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
2881 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
2882 let node_kinds: &[SoaNodeKindTag] = soa.get_node_kinds().as_slice();
2883 let text_styles: &[TextStyle] = soa.get_text_styles().as_slice();
2884 let parent_layout: LayoutStyle = frame.get_parent_layout();
2885 let inner_avail: Size = frame.get_inner_avail();
2886 let viewport: Size = frame.get_viewport();
2887 let content_origin: Point = frame.get_content_origin();
2888 let flex_dir_opt: Option<FlexDirection> = match parent_layout.get_display() {
2889 Display::Grid => None,
2890 Display::Block => Some(FlexDirection::Column),
2891 Display::Flex | Display::None => Some(parent_layout.get_flex_direction()),
2892 };
2893 let is_row_dir: bool = matches!(
2894 flex_dir_opt,
2895 Some(FlexDirection::Row | FlexDirection::RowReverse)
2896 );
2897 let is_reverse_dir: bool = matches!(
2898 flex_dir_opt,
2899 Some(FlexDirection::RowReverse | FlexDirection::ColumnReverse)
2900 );
2901 let parent_align: AlignItems = parent_layout.get_align_items();
2902 let parent_height_auto: bool = parent_layout.get_height() == Length::Auto;
2903 let parent_bounded_main: bool = if is_row_dir {
2904 frame.get_definite_width() || parent_layout.get_max_width() != Length::Auto
2905 } else {
2906 frame.get_definite_height() || parent_layout.get_max_height() != Length::Auto
2907 };
2908 let mut single_pass_ok: bool = flex_dir_opt.is_some()
2909 && parent_layout.get_flex_wrap() != FlexWrap::Wrap
2910 && !is_reverse_dir
2911 && parent_layout.get_justify_content() == JustifyContent::Start;
2912 let mut shrinkable_flow_children: usize = 0;
2913 let flow_start: usize = scratch.flow_scratch.len();
2914 let abs_start: usize = scratch.abs_scratch.len();
2915 for (cur_bfs, &arena_u32) in bfs_to_arena
2916 .iter()
2917 .enumerate()
2918 .take(child_end)
2919 .skip(child_start)
2920 {
2921 let arena_idx: usize = arena_u32 as usize;
2922 let child_layout: LayoutStyle = layout_styles[arena_idx];
2923 if child_layout.get_display() == Display::None {
2924 if mode == LayoutRunMode::PerformLayout {
2925 let _: (Size, u8, f32) = Self::solve_node_serial(
2926 soa,
2927 output_view,
2928 scratch,
2929 cur_bfs as u32,
2930 AvailableSpace::new(0.0, 0.0, viewport, 0.0, 0.0),
2931 LayoutRunMode::PerformLayout,
2932 );
2933 }
2934 continue;
2935 }
2936 match child_layout.get_position() {
2937 Position::Relative => {
2938 if single_pass_ok {
2939 if parent_bounded_main && child_layout.get_flex_shrink() > 0.0 {
2940 let can_shrink_child: bool = if is_row_dir {
2941 child_layout.get_min_width() != Length::Auto
2942 || child_layout.get_overflow() != Overflow::Visible
2943 || node_kinds[arena_idx] != SoaNodeKindTag::Text
2944 || text_styles[arena_idx].get_overflow() != TextOverflow::Clip
2945 } else {
2946 child_layout.get_min_height() != Length::Auto
2947 || child_layout.get_overflow() != Overflow::Visible
2948 };
2949 if can_shrink_child {
2950 shrinkable_flow_children =
2951 shrinkable_flow_children.saturating_add(1);
2952 }
2953 }
2954 if child_layout.get_flex_grow() > 0.0
2955 || child_layout.get_flex_basis() != Length::Auto
2956 {
2957 single_pass_ok = false;
2958 } else {
2959 let align: AlignItems = match child_layout.get_align_self() {
2960 AlignSelf::Auto => parent_align,
2961 AlignSelf::Stretch => AlignItems::Stretch,
2962 AlignSelf::Start => AlignItems::Start,
2963 AlignSelf::Center => AlignItems::Center,
2964 AlignSelf::End => AlignItems::End,
2965 };
2966 let non_start_align: bool =
2967 matches!(align, AlignItems::Center | AlignItems::End);
2968 let auto_row_stretch: bool = align == AlignItems::Stretch
2969 && is_row_dir
2970 && child_layout.get_height() == Length::Auto
2971 && (parent_height_auto
2972 || frame.get_min_inner_size().get_height() > 0.0);
2973 if non_start_align || auto_row_stretch {
2974 single_pass_ok = false;
2975 }
2976 }
2977 }
2978 scratch.flow_scratch.push(cur_bfs as u32);
2979 }
2980 Position::Absolute | Position::Fixed => {
2981 if mode == LayoutRunMode::PerformLayout {
2982 scratch.abs_scratch.push(cur_bfs as u32);
2983 }
2984 }
2985 }
2986 }
2987 let flow_end: usize = scratch.flow_scratch.len();
2988 let flow_count: usize = flow_end.saturating_sub(flow_start);
2989 if single_pass_ok && shrinkable_flow_children > 0 && (!is_row_dir || flow_count >= 2) {
2990 single_pass_ok = false;
2991 }
2992 let abs_end: usize = scratch.abs_scratch.len();
2993 let flow_result: (Size, u8, f32) = if let Some(flex_dir) = flex_dir_opt {
2994 Self::solve_flex_serial(
2995 soa,
2996 output_view,
2997 scratch,
2998 (flow_start, flow_end),
2999 frame,
3000 flex_dir,
3001 (single_pass_ok, true, mode),
3002 )
3003 } else {
3004 Self::solve_grid_serial(
3005 soa,
3006 output_view,
3007 scratch,
3008 (flow_start, flow_end),
3009 frame,
3010 mode,
3011 )
3012 };
3013 scratch.flow_scratch.truncate(flow_start);
3014 if mode == LayoutRunMode::PerformLayout && abs_end > abs_start {
3015 let abs_copy: Vec<u32> = scratch.abs_scratch[abs_start..abs_end].to_vec();
3016 scratch.abs_scratch.truncate(abs_start);
3017 Self::solve_absolute_children_serial(
3018 soa,
3019 output_view,
3020 scratch,
3021 abs_copy.as_slice(),
3022 inner_avail,
3023 viewport,
3024 content_origin,
3025 );
3026 } else {
3027 scratch.abs_scratch.truncate(abs_start);
3028 }
3029 flow_result
3030 }
3031
3032 fn solve_absolute_children_serial(
3044 soa: &SceneLayoutSoa,
3045 output_view: DisjointOutputView,
3046 scratch: &mut WorkerLayoutScratch,
3047 abs_children: &[u32],
3048 inner_avail: Size,
3049 viewport: Size,
3050 content_origin: Point,
3051 ) {
3052 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
3053 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
3054 for &abs_bfs in abs_children.iter() {
3055 let arena_idx: usize = bfs_to_arena[abs_bfs as usize] as usize;
3056 let child_layout: LayoutStyle = layout_styles[arena_idx];
3057 let is_fixed: bool = child_layout.get_position() == Position::Fixed;
3058 let ref_origin: Point = if is_fixed {
3059 Point::new(0.0, 0.0)
3060 } else {
3061 content_origin
3062 };
3063 let ref_size: Size = if is_fixed { viewport } else { inner_avail };
3064 let resolved_w: Option<f32> = child_layout
3065 .get_width()
3066 .resolve(ref_size.get_width(), viewport);
3067 let resolved_h: Option<f32> = child_layout
3068 .get_height()
3069 .resolve(ref_size.get_height(), viewport);
3070 let needs_intrinsic: bool = (resolved_w.is_none()
3071 && child_layout.left_offset().is_none()
3072 && child_layout.right_offset().is_some())
3073 || (resolved_h.is_none()
3074 && child_layout.top_offset().is_none()
3075 && child_layout.bottom_offset().is_some());
3076 let probed: Size = if needs_intrinsic {
3077 let probe_space: AvailableSpace = AvailableSpace::new(
3078 ref_size.get_width(),
3079 ref_size.get_height(),
3080 viewport,
3081 ref_origin.get_x(),
3082 ref_origin.get_y(),
3083 );
3084 Self::solve_node_serial(
3085 soa,
3086 output_view,
3087 scratch,
3088 abs_bfs,
3089 probe_space,
3090 LayoutRunMode::ComputeSize,
3091 )
3092 .0
3093 } else {
3094 Size::new(ref_size.get_width(), ref_size.get_height())
3095 };
3096 let child_w: f32 = resolved_w.unwrap_or(probed.get_width());
3097 let child_h: f32 = resolved_h.unwrap_or(probed.get_height());
3098 let pos_x: f32 = if let Some(left) = child_layout.left_offset() {
3099 ref_origin.get_x() + left
3100 } else if let Some(right) = child_layout.right_offset() {
3101 ref_origin.get_x() + (ref_size.get_width() - child_w - right)
3102 } else {
3103 ref_origin.get_x()
3104 };
3105 let pos_y: f32 = if let Some(top) = child_layout.top_offset() {
3106 ref_origin.get_y() + top
3107 } else if let Some(bottom) = child_layout.bottom_offset() {
3108 ref_origin.get_y() + (ref_size.get_height() - child_h - bottom)
3109 } else {
3110 ref_origin.get_y()
3111 };
3112 let abs_space: AvailableSpace = AvailableSpace::new(
3113 ref_size.get_width(),
3114 ref_size.get_height(),
3115 viewport,
3116 pos_x,
3117 pos_y,
3118 );
3119 let _: (Size, u8, f32) = Self::solve_node_serial(
3120 soa,
3121 output_view,
3122 scratch,
3123 abs_bfs,
3124 abs_space,
3125 LayoutRunMode::PerformLayout,
3126 );
3127 }
3128 }
3129
3130 fn prepare_flex_item(
3144 child_bfs: u32,
3145 child_layout: LayoutStyle,
3146 kind_tag: SoaNodeKindTag,
3147 child_text: TextStyle,
3148 axis_ctx: (bool, bool, f32, f32, Size),
3149 ) -> (ParallelFlexItem, PreparedFlexItemMeta, u8) {
3150 let (is_row, wrap_enabled, main_avail, cross_avail, viewport): (
3151 bool,
3152 bool,
3153 f32,
3154 f32,
3155 Size,
3156 ) = axis_ctx;
3157 let width_len: Length = child_layout.get_width();
3158 let height_len: Length = child_layout.get_height();
3159 let basis_len: Length = child_layout.get_flex_basis();
3160 let has_px_main: bool =
3161 matches!(if is_row { width_len } else { height_len }, Length::Px(_));
3162 let min_main_len: Length = if is_row {
3163 child_layout.get_min_width()
3164 } else {
3165 child_layout.get_min_height()
3166 };
3167 let max_main_len: Length = if is_row {
3168 child_layout.get_max_width()
3169 } else {
3170 child_layout.get_max_height()
3171 };
3172 let mut deps: u8 = LAYOUT_DEP_NONE;
3173 if matches!(min_main_len, Length::Percent(_))
3174 || matches!(max_main_len, Length::Percent(_))
3175 || matches!(basis_len, Length::Percent(_))
3176 {
3177 deps |= if is_row {
3178 LAYOUT_DEP_AVAIL_W
3179 } else {
3180 LAYOUT_DEP_AVAIL_H
3181 };
3182 }
3183 if Self::is_viewport_length(min_main_len)
3184 || Self::is_viewport_length(max_main_len)
3185 || Self::is_viewport_length(basis_len)
3186 {
3187 deps |= LAYOUT_DEP_VIEWPORT;
3188 }
3189 let explicit_min_main: Option<f32> = min_main_len.resolve(main_avail, viewport);
3190 let explicit_max_main: Option<f32> = max_main_len.resolve(main_avail, viewport);
3191 let explicit_basis_main: Option<f32> = basis_len.resolve(main_avail, viewport);
3192 let is_unwrapped_clip_text: bool =
3193 kind_tag == SoaNodeKindTag::Text && child_text.get_overflow() == TextOverflow::Clip;
3194 let grow: f32 = child_layout.get_flex_grow();
3195 let shrink: f32 = child_layout.get_flex_shrink().max(0.0);
3196 let width_auto: bool = width_len == Length::Auto;
3197 let height_auto: bool = height_len == Length::Auto;
3198 let mut factor_val: u16 = 0;
3199 if width_auto {
3200 factor_val |= FLEX_ITEM_WIDTH_AUTO;
3201 }
3202 if height_auto {
3203 factor_val |= FLEX_ITEM_HEIGHT_AUTO;
3204 }
3205 if matches!(width_len, Length::Percent(_))
3206 || matches!(child_layout.get_min_width(), Length::Percent(_))
3207 || matches!(child_layout.get_max_width(), Length::Percent(_))
3208 {
3209 factor_val |= FLEX_ITEM_WIDTH_PERCENT;
3210 }
3211 if matches!(height_len, Length::Percent(_))
3212 || matches!(child_layout.get_min_height(), Length::Percent(_))
3213 || matches!(child_layout.get_max_height(), Length::Percent(_))
3214 {
3215 factor_val |= FLEX_ITEM_HEIGHT_PERCENT;
3216 }
3217 let mut zero_basis_size: Option<Size> = None;
3218 if let Some(basis_raw) = explicit_basis_main {
3219 let mut clamped_basis: f32 = basis_raw.max(explicit_min_main.unwrap_or(0.0));
3220 if let Some(max_bound) = explicit_max_main {
3221 clamped_basis = clamped_basis.min(max_bound);
3222 }
3223 if is_row {
3224 if matches!(height_len, Length::Percent(_)) {
3225 deps |= LAYOUT_DEP_AVAIL_H;
3226 }
3227 if Self::is_viewport_length(height_len) {
3228 deps |= LAYOUT_DEP_VIEWPORT;
3229 }
3230 if let Some(resolved_h) = height_len.resolve(cross_avail, viewport) {
3231 zero_basis_size = Some(Size::new(clamped_basis, resolved_h));
3232 } else if grow > 0.0 {
3233 zero_basis_size = Some(Size::new(clamped_basis, 0.0));
3234 }
3235 } else {
3236 if matches!(width_len, Length::Percent(_)) {
3237 deps |= LAYOUT_DEP_AVAIL_W;
3238 }
3239 if Self::is_viewport_length(width_len) {
3240 deps |= LAYOUT_DEP_VIEWPORT;
3241 }
3242 if let Some(resolved_w) = width_len.resolve(cross_avail, viewport) {
3243 zero_basis_size = Some(Size::new(resolved_w, clamped_basis));
3244 } else if grow > 0.0 {
3245 zero_basis_size = Some(Size::new(0.0, clamped_basis));
3246 }
3247 }
3248 } else {
3249 let zero_basis: bool =
3250 !wrap_enabled && grow > 0.0 && if is_row { width_auto } else { height_auto };
3251 if zero_basis {
3252 if is_row {
3253 if matches!(height_len, Length::Percent(_)) {
3254 deps |= LAYOUT_DEP_AVAIL_H;
3255 }
3256 if Self::is_viewport_length(height_len) {
3257 deps |= LAYOUT_DEP_VIEWPORT;
3258 }
3259 let height_val: f32 = height_len.resolve(cross_avail, viewport).unwrap_or(0.0);
3260 zero_basis_size = Some(Size::new(0.0, height_val));
3261 } else {
3262 if matches!(width_len, Length::Percent(_)) {
3263 deps |= LAYOUT_DEP_AVAIL_W;
3264 }
3265 if Self::is_viewport_length(width_len) {
3266 deps |= LAYOUT_DEP_VIEWPORT;
3267 }
3268 let width_val: f32 = width_len.resolve(cross_avail, viewport).unwrap_or(0.0);
3269 zero_basis_size = Some(Size::new(width_val, 0.0));
3270 }
3271 }
3272 }
3273 let mut rec: ParallelFlexItem = ParallelFlexItem::new(
3274 child_bfs,
3275 Size::default(),
3276 child_layout.get_margin(),
3277 child_layout.get_align_self(),
3278 child_layout.get_overflow(),
3279 factor_val,
3280 );
3281 let _: &mut ParallelFlexItem = rec.set_flex_grow(grow).set_flex_shrink(shrink);
3282 (
3283 rec,
3284 PreparedFlexItemMeta {
3285 zero_basis_size,
3286 explicit_basis_main,
3287 explicit_min_main,
3288 explicit_max_main,
3289 is_unwrapped_clip_text,
3290 has_px_main,
3291 },
3292 deps,
3293 )
3294 }
3295
3296 fn resolve_flex_line_sizes(
3311 soa: &SceneLayoutSoa,
3312 output_view: DisjointOutputView,
3313 scratch: &mut WorkerLayoutScratch,
3314 line_items: &mut [ParallelFlexItem],
3315 precomputed: Option<(f32, f32)>,
3316 ctx: FlexLineContext,
3317 ) -> (f32, f32, u8, f32, bool, f32) {
3318 let frame: ContainerFrame = ctx.get_frame();
3319 let is_row: bool = ctx.get_is_row();
3320 let wrap_enabled: bool = ctx.get_wrap_enabled();
3321 let main_gap: f32 = ctx.get_main_gap();
3322 let main_avail: f32 = ctx.get_main_avail();
3323 let cross_avail: f32 = ctx.get_cross_avail();
3324 let total_grow: f32 = ctx.get_total_grow();
3325 let parent_layout: LayoutStyle = frame.get_parent_layout();
3326 let viewport: Size = frame.get_viewport();
3327 let content_origin: Point = frame.get_content_origin();
3328 let count: usize = line_items.len();
3329 let gaps_total: f32 = if count > 1 {
3330 main_gap * ((count - 1) as f32)
3331 } else {
3332 0.0
3333 };
3334 let mut line_deps: u8 = LAYOUT_DEP_NONE;
3335 let mut line_min_aw: f32 = 0.0;
3336 let has_definite_or_grow: bool = frame.get_definite_width() || total_grow > 0.0;
3337 let effective_main_avail: f32 = if is_row {
3338 if has_definite_or_grow || wrap_enabled {
3339 if total_grow > 0.0 {
3340 line_deps |= LAYOUT_DEP_AVAIL_W;
3341 }
3342 main_avail
3343 } else {
3344 let min_w_len: Length = parent_layout.get_min_width();
3345 if matches!(min_w_len, Length::Percent(_)) || Self::is_viewport_length(min_w_len) {
3346 line_deps |= LAYOUT_DEP_VIEWPORT;
3347 }
3348 frame.get_min_inner_size().get_width()
3349 }
3350 } else if frame.get_definite_height() {
3351 if total_grow > 0.0 {
3352 line_deps |= LAYOUT_DEP_AVAIL_H;
3353 }
3354 main_avail
3355 } else {
3356 let min_h_len: Length = parent_layout.get_min_height();
3357 if matches!(min_h_len, Length::Percent(_)) || Self::is_viewport_length(min_h_len) {
3358 line_deps |= LAYOUT_DEP_VIEWPORT;
3359 }
3360 frame.get_min_inner_size().get_height()
3361 };
3362 let has_main_cap: bool = parent_layout.get_overflow() != Overflow::Scroll
3363 && if is_row {
3364 frame.get_definite_width() || parent_layout.get_max_width() != Length::Auto
3365 } else {
3366 frame.get_definite_height() || parent_layout.get_max_height() != Length::Auto
3367 };
3368 let pre_overflows: bool = !wrap_enabled
3369 && has_main_cap
3370 && precomputed
3371 .map(|(items_main, _): (f32, f32)| items_main + gaps_total > main_avail + 0.5)
3372 .unwrap_or(false);
3373 let mut line_shrunk: bool = false;
3374 let (final_used_main, line_cross): (f32, f32) = if let Some((items_main, pre_cross)) =
3375 precomputed
3376 && total_grow == 0.0
3377 && !pre_overflows
3378 {
3379 (items_main + gaps_total, pre_cross)
3380 } else {
3381 let used_main: f32 = if let Some((items_main, _)) = precomputed {
3382 items_main + gaps_total
3383 } else {
3384 let mut acc: f32 = gaps_total;
3385 for item in line_items.iter() {
3386 let sz: f32 = if is_row {
3387 item.get_size().get_width() + item.get_margin().horizontal()
3388 } else {
3389 item.get_size().get_height() + item.get_margin().vertical()
3390 };
3391 acc += sz;
3392 }
3393 acc
3394 };
3395 let free_main: f32 = (effective_main_avail - used_main).max(0.0);
3396 let mut any_main_resized: bool = false;
3397 if total_grow > 0.0 && free_main > 0.0 {
3398 for item in line_items.iter_mut() {
3399 let grow: f32 = item.get_flex_grow();
3400 if grow > 0.0 {
3401 let extra: f32 = free_main * (grow / total_grow);
3402 if is_row {
3403 let next_w: f32 = item.get_size().get_width() + extra;
3404 item.get_mut_size().set_width(next_w);
3405 } else {
3406 let next_h: f32 = item.get_size().get_height() + extra;
3407 item.get_mut_size().set_height(next_h);
3408 }
3409 any_main_resized = true;
3410 }
3411 }
3412 } else if !wrap_enabled && has_main_cap && used_main > main_avail + 0.5 {
3413 line_deps |= if is_row {
3414 LAYOUT_DEP_AVAIL_W
3415 } else {
3416 LAYOUT_DEP_AVAIL_H
3417 };
3418 let mut rem_overflow: f32 = used_main - main_avail;
3419 for _ in 0..4usize {
3420 if rem_overflow <= 0.05 {
3421 break;
3422 }
3423 let mut weighted_sum: f32 = 0.0;
3424 for item in line_items.iter() {
3425 let cur_main: f32 = if is_row {
3426 item.get_size().get_width()
3427 } else {
3428 item.get_size().get_height()
3429 };
3430 if item.get_flex_shrink() > 0.0 && cur_main > item.get_min_main() + 0.05 {
3431 weighted_sum += item.get_flex_shrink() * cur_main.max(1.0);
3432 }
3433 }
3434 if weighted_sum <= 0.0 {
3435 break;
3436 }
3437 let pass_overflow: f32 = rem_overflow;
3438 let mut absorbed: f32 = 0.0;
3439 for item in line_items.iter_mut() {
3440 let cur_main: f32 = if is_row {
3441 item.get_size().get_width()
3442 } else {
3443 item.get_size().get_height()
3444 };
3445 let min_m: f32 = item.get_min_main();
3446 let shrink_f: f32 = item.get_flex_shrink();
3447 if shrink_f > 0.0 && cur_main > min_m + 0.05 {
3448 let weight_val: f32 = shrink_f * cur_main.max(1.0);
3449 let raw_cut: f32 = pass_overflow * (weight_val / weighted_sum);
3450 let next_main: f32 = (cur_main - raw_cut).max(min_m);
3451 let cut: f32 = cur_main - next_main;
3452 if cut > 0.01 {
3453 if is_row {
3454 item.get_mut_size().set_width(next_main);
3455 } else {
3456 item.get_mut_size().set_height(next_main);
3457 }
3458 absorbed += cut;
3459 any_main_resized = true;
3460 line_shrunk = true;
3461 }
3462 }
3463 }
3464 if absorbed <= 0.01 {
3465 break;
3466 }
3467 rem_overflow = (rem_overflow - absorbed).max(0.0);
3468 }
3469 }
3470 if total_grow > 0.0 || any_main_resized {
3471 for item in line_items.iter_mut() {
3472 let resized_item: bool = item.get_flex_grow() > 0.0 || any_main_resized;
3473 let auto_cross: bool = resized_item
3474 && if is_row {
3475 (item.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0
3476 } else {
3477 (item.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0
3478 };
3479 if auto_cross {
3480 let probe_w: f32 = if is_row {
3481 item.get_size().get_width()
3482 } else {
3483 cross_avail
3484 };
3485 let probe_h: f32 = if is_row {
3486 cross_avail
3487 } else {
3488 item.get_size().get_height()
3489 };
3490 let stretch_pw: bool =
3491 is_row && (item.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0;
3492 let stretch_ph: bool =
3493 !is_row && (item.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0;
3494 let probe_sp: AvailableSpace = AvailableSpace::new(
3495 probe_w,
3496 probe_h,
3497 viewport,
3498 content_origin.get_x(),
3499 content_origin.get_y(),
3500 )
3501 .with_stretch(stretch_pw, stretch_ph);
3502 let (measured, c_deps, c_min_aw): (Size, u8, f32) = Self::solve_node_serial(
3503 soa,
3504 output_view,
3505 scratch,
3506 item.get_bfs_index(),
3507 probe_sp,
3508 LayoutRunMode::ComputeSize,
3509 );
3510 line_deps |= c_deps;
3511 if c_min_aw > 0.0 {
3512 line_min_aw =
3513 line_min_aw.max(c_min_aw + item.get_margin().horizontal());
3514 }
3515 if is_row {
3516 item.get_mut_size().set_height(measured.get_height());
3517 } else {
3518 item.get_mut_size().set_width(measured.get_width());
3519 }
3520 }
3521 }
3522 }
3523 let mut sum_main: f32 = gaps_total;
3524 let mut max_cross: f32 = 0.0;
3525 for item in line_items.iter() {
3526 if is_row {
3527 sum_main += item.get_size().get_width() + item.get_margin().horizontal();
3528 max_cross =
3529 max_cross.max(item.get_size().get_height() + item.get_margin().vertical());
3530 } else {
3531 sum_main += item.get_size().get_height() + item.get_margin().vertical();
3532 max_cross =
3533 max_cross.max(item.get_size().get_width() + item.get_margin().horizontal());
3534 }
3535 }
3536 (sum_main, max_cross)
3537 };
3538 (
3539 final_used_main,
3540 line_cross,
3541 line_deps,
3542 line_min_aw,
3543 line_shrunk,
3544 effective_main_avail,
3545 )
3546 }
3547
3548 fn solve_flex_serial(
3564 soa: &SceneLayoutSoa,
3565 output_view: DisjointOutputView,
3566 scratch: &mut WorkerLayoutScratch,
3567 flow_range: (usize, usize),
3568 frame: ContainerFrame,
3569 direction: FlexDirection,
3570 mode_flags: (bool, bool, LayoutRunMode),
3571 ) -> (Size, u8, f32) {
3572 let (flow_start, flow_end): (usize, usize) = flow_range;
3573 let (single_pass_ok, apply_reverse, mode): (bool, bool, LayoutRunMode) = mode_flags;
3574 let count_all: usize = flow_end.saturating_sub(flow_start);
3575 if count_all == 0 {
3576 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
3577 }
3578 let parent_layout: LayoutStyle = frame.get_parent_layout();
3579 let inner_avail: Size = frame.get_inner_avail();
3580 let viewport: Size = frame.get_viewport();
3581 let content_origin: Point = frame.get_content_origin();
3582 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
3583 let is_reverse: bool = matches!(
3584 direction,
3585 FlexDirection::RowReverse | FlexDirection::ColumnReverse
3586 );
3587 let main_gap: f32 = if is_row {
3588 parent_layout.get_column_gap()
3589 } else {
3590 parent_layout.get_row_gap()
3591 };
3592 let cross_gap: f32 = if is_row {
3593 parent_layout.get_row_gap()
3594 } else {
3595 parent_layout.get_column_gap()
3596 };
3597 let main_avail: f32 = if is_row {
3598 inner_avail.get_width()
3599 } else {
3600 inner_avail.get_height()
3601 };
3602 let cross_avail: f32 = if is_row {
3603 inner_avail.get_height()
3604 } else {
3605 inner_avail.get_width()
3606 };
3607 if apply_reverse && is_reverse {
3608 scratch.flow_scratch[flow_start..flow_end].reverse();
3609 }
3610 let wrap_enabled: bool = parent_layout.get_flex_wrap() == FlexWrap::Wrap;
3611 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
3612 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
3613 let node_kinds: &[SoaNodeKindTag] = soa.get_node_kinds().as_slice();
3614 let text_styles: &[TextStyle] = soa.get_text_styles().as_slice();
3615 if single_pass_ok {
3616 let parent_align: AlignItems = parent_layout.get_align_items();
3617 let mut main_cursor: f32 = 0.0;
3618 let mut max_cross: f32 = 0.0;
3619 let mut agg_deps: u8 = LAYOUT_DEP_NONE;
3620 let mut agg_min_aw: f32 = 0.0;
3621 let records_start: usize = scratch.flex_items_scratch.len();
3622 for offset in 0..count_all {
3623 let child_bfs: u32 = scratch.flow_scratch[flow_start + offset];
3624 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
3625 let child_layout: LayoutStyle = layout_styles[arena_idx];
3626 let margin: Edges = child_layout.get_margin();
3627 let align: AlignItems = match child_layout.get_align_self() {
3628 AlignSelf::Auto => parent_align,
3629 AlignSelf::Stretch => AlignItems::Stretch,
3630 AlignSelf::Start => AlignItems::Start,
3631 AlignSelf::Center => AlignItems::Center,
3632 AlignSelf::End => AlignItems::End,
3633 };
3634 let (pos_x, pos_y): (f32, f32) = if is_row {
3635 (
3636 content_origin.get_x() + main_cursor + margin.get_left(),
3637 content_origin.get_y() + margin.get_top(),
3638 )
3639 } else {
3640 (
3641 content_origin.get_x() + margin.get_left(),
3642 content_origin.get_y() + main_cursor + margin.get_top(),
3643 )
3644 };
3645 let stretch_w: bool = !is_row
3646 && align == AlignItems::Stretch
3647 && child_layout.get_width() == Length::Auto;
3648 let stretch_h: bool = is_row
3649 && align == AlignItems::Stretch
3650 && child_layout.get_height() == Length::Auto;
3651 if stretch_w {
3652 agg_deps |= LAYOUT_DEP_AVAIL_W;
3653 }
3654 if stretch_h {
3655 agg_deps |= LAYOUT_DEP_AVAIL_H;
3656 }
3657 let space_w: f32 = if stretch_w {
3658 (inner_avail.get_width() - margin.horizontal()).max(0.0)
3659 } else {
3660 inner_avail.get_width()
3661 };
3662 let space_h: f32 = if stretch_h {
3663 (inner_avail.get_height() - margin.vertical()).max(0.0)
3664 } else {
3665 inner_avail.get_height()
3666 };
3667 let final_space: AvailableSpace =
3668 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
3669 .with_stretch(stretch_w, stretch_h);
3670 let (placed, c_deps, c_min_aw): (Size, u8, f32) = Self::solve_node_serial(
3671 soa,
3672 output_view,
3673 scratch,
3674 child_bfs,
3675 final_space,
3676 mode,
3677 );
3678 agg_deps |= c_deps;
3679 if c_min_aw > 0.0 {
3680 agg_min_aw = agg_min_aw.max(c_min_aw + margin.horizontal());
3681 }
3682 let target_w: f32 = if stretch_w {
3683 space_w.max(placed.get_width())
3684 } else {
3685 placed.get_width()
3686 };
3687 let target_h: f32 = if stretch_h {
3688 space_h.max(placed.get_height())
3689 } else {
3690 placed.get_height()
3691 };
3692 if mode == LayoutRunMode::PerformLayout {
3693 if (stretch_w && target_w > placed.get_width() + 0.01)
3694 || (stretch_h && target_h > placed.get_height() + 0.01)
3695 {
3696 Self::apply_post_stretch(
3697 soa,
3698 output_view,
3699 arena_idx,
3700 Some(target_w),
3701 Some(target_h),
3702 );
3703 }
3704 if stretch_w {
3705 scratch.flex_items_scratch.push(ParallelFlexItem::new(
3706 child_bfs,
3707 Size::new(target_w, target_h),
3708 margin,
3709 AlignSelf::Stretch,
3710 child_layout.get_overflow(),
3711 0,
3712 ));
3713 }
3714 }
3715 if is_row {
3716 main_cursor += target_w + margin.horizontal();
3717 max_cross = max_cross.max(target_h + margin.vertical());
3718 } else {
3719 main_cursor += target_h + margin.vertical();
3720 max_cross = max_cross.max(target_w + margin.horizontal());
3721 }
3722 if offset + 1 < count_all {
3723 main_cursor += main_gap;
3724 }
3725 }
3726 let records_end: usize = scratch.flex_items_scratch.len();
3727 if mode == LayoutRunMode::PerformLayout
3728 && records_end > records_start
3729 && max_cross > cross_avail + 0.5
3730 {
3731 for &rec in &scratch.flex_items_scratch[records_start..records_end] {
3732 let arena_idx: usize = bfs_to_arena[rec.get_bfs_index() as usize] as usize;
3733 let widened: f32 =
3734 (max_cross - rec.get_margin().horizontal()).max(rec.get_size().get_width());
3735 Self::apply_post_stretch(soa, output_view, arena_idx, Some(widened), None);
3736 }
3737 }
3738 scratch.flex_items_scratch.truncate(records_start);
3739 let out_size: Size = if is_row {
3740 Size::new(main_cursor, max_cross)
3741 } else {
3742 Size::new(max_cross, main_cursor)
3743 };
3744 return (out_size, agg_deps, agg_min_aw);
3745 }
3746 let items_start: usize = scratch.flex_items_scratch.len();
3747 let probe_template: AvailableSpace = AvailableSpace::new(
3748 inner_avail.get_width(),
3749 inner_avail.get_height(),
3750 viewport,
3751 content_origin.get_x(),
3752 content_origin.get_y(),
3753 );
3754 let mut total_grow: f32 = 0.0;
3755 let mut single_line_main: f32 = 0.0;
3756 let mut single_line_cross: f32 = 0.0;
3757 let mut pass1_deps: u8 = LAYOUT_DEP_NONE;
3758 let mut pass1_min_aw: f32 = 0.0;
3759 for flow_idx in flow_start..flow_end {
3760 let child_bfs: u32 = scratch.flow_scratch[flow_idx];
3761 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
3762 let (mut rec, meta, dep_bits): (ParallelFlexItem, PreparedFlexItemMeta, u8) =
3763 Self::prepare_flex_item(
3764 child_bfs,
3765 layout_styles[arena_idx],
3766 node_kinds[arena_idx],
3767 text_styles[arena_idx],
3768 (is_row, wrap_enabled, main_avail, cross_avail, viewport),
3769 );
3770 let PreparedFlexItemMeta {
3771 zero_basis_size,
3772 explicit_basis_main,
3773 explicit_min_main,
3774 explicit_max_main,
3775 is_unwrapped_clip_text,
3776 has_px_main,
3777 } = meta;
3778 pass1_deps |= dep_bits;
3779 total_grow += rec.get_flex_grow();
3780 let mut child_size: Size = if let Some(zb) = zero_basis_size {
3781 zb
3782 } else {
3783 let (measured, c_deps, c_min_aw): (Size, u8, f32) = Self::solve_node_serial(
3784 soa,
3785 output_view,
3786 scratch,
3787 child_bfs,
3788 probe_template,
3789 LayoutRunMode::ComputeSize,
3790 );
3791 pass1_deps |= c_deps;
3792 if c_min_aw > 0.0 {
3793 pass1_min_aw = pass1_min_aw.max(c_min_aw + rec.get_margin().horizontal());
3794 }
3795 measured
3796 };
3797 if let Some(basis_raw) = explicit_basis_main
3798 && zero_basis_size.is_none()
3799 {
3800 let mut clamped_basis: f32 = basis_raw.max(explicit_min_main.unwrap_or(0.0));
3801 if let Some(max_bound) = explicit_max_main {
3802 clamped_basis = clamped_basis.min(max_bound);
3803 }
3804 if is_row {
3805 child_size.set_width(clamped_basis);
3806 } else {
3807 child_size.set_height(clamped_basis);
3808 }
3809 }
3810 let resolved_min_main: f32 = if let Some(explicit_min) = explicit_min_main {
3811 explicit_min.max(0.0)
3812 } else if rec.get_overflow() == Overflow::Scroll
3813 || (rec.get_overflow() == Overflow::Hidden && !has_px_main)
3814 {
3815 0.0
3816 } else if is_row {
3817 if is_unwrapped_clip_text {
3818 child_size.get_width()
3819 } else {
3820 0.0
3821 }
3822 } else {
3823 child_size.get_height()
3824 };
3825 let _: &mut ParallelFlexItem = rec.set_min_main(resolved_min_main).set_size(child_size);
3826 if !wrap_enabled {
3827 if is_row {
3828 single_line_main += child_size.get_width() + rec.get_margin().horizontal();
3829 single_line_cross = single_line_cross
3830 .max(child_size.get_height() + rec.get_margin().vertical());
3831 } else {
3832 single_line_main += child_size.get_height() + rec.get_margin().vertical();
3833 single_line_cross = single_line_cross
3834 .max(child_size.get_width() + rec.get_margin().horizontal());
3835 }
3836 }
3837 scratch.flex_items_scratch.push(rec);
3838 }
3839 let items_end: usize = scratch.flex_items_scratch.len();
3840 if !wrap_enabled {
3841 let mut line_ctx: FlexLineContext = FlexLineContext::new(
3842 frame,
3843 is_row,
3844 main_gap,
3845 main_avail,
3846 cross_avail,
3847 total_grow,
3848 mode,
3849 );
3850 let _: &mut FlexLineContext = line_ctx.set_line_count(1);
3851 let (line_size, line_deps, line_min_aw): (Size, u8, f32) = Self::solve_flex_line_serial(
3852 soa,
3853 output_view,
3854 scratch,
3855 (items_start, items_end),
3856 0.0,
3857 Some((single_line_main, single_line_cross)),
3858 line_ctx,
3859 );
3860 scratch.flex_items_scratch.truncate(items_start);
3861 return (
3862 line_size,
3863 pass1_deps | line_deps,
3864 pass1_min_aw.max(line_min_aw),
3865 );
3866 }
3867 let lines_start: usize = scratch.flex_lines_scratch.len();
3868 let mut cur_line_start: usize = items_start;
3869 let mut line_main: f32 = 0.0;
3870 for item_idx in items_start..items_end {
3871 let item: ParallelFlexItem = scratch.flex_items_scratch[item_idx];
3872 let item_main: f32 = if is_row {
3873 item.get_size().get_width() + item.get_margin().horizontal()
3874 } else {
3875 item.get_size().get_height() + item.get_margin().vertical()
3876 };
3877 let line_empty: bool = item_idx == cur_line_start;
3878 let needed: f32 = if line_empty {
3879 item_main
3880 } else {
3881 line_main + main_gap + item_main
3882 };
3883 if !line_empty && needed > main_avail && main_avail > 0.0 {
3884 scratch.flex_lines_scratch.push((cur_line_start, item_idx));
3885 cur_line_start = item_idx;
3886 line_main = item_main;
3887 } else {
3888 line_main = needed;
3889 }
3890 }
3891 if cur_line_start < items_end {
3892 scratch.flex_lines_scratch.push((cur_line_start, items_end));
3893 }
3894 let lines_end: usize = scratch.flex_lines_scratch.len();
3895 let line_count: usize = lines_end.saturating_sub(lines_start);
3896 let mut cross_cursor: f32 = 0.0;
3897 let mut max_main_extent: f32 = 0.0;
3898 let mut wrap_deps: u8 = pass1_deps;
3899 let mut wrap_min_aw: f32 = pass1_min_aw;
3900 if is_row {
3901 if line_count > 1 {
3902 wrap_deps |= LAYOUT_DEP_AVAIL_W;
3903 } else {
3904 wrap_min_aw = wrap_min_aw.max(line_main + 0.5);
3905 }
3906 } else {
3907 wrap_deps |= LAYOUT_DEP_AVAIL_H;
3908 }
3909 for line_offset in 0..line_count {
3910 let line_range: (usize, usize) = scratch.flex_lines_scratch[lines_start + line_offset];
3911 let mut line_grow: f32 = 0.0;
3912 for item in &scratch.flex_items_scratch[line_range.0..line_range.1] {
3913 line_grow += item.get_flex_grow();
3914 }
3915 let mut line_ctx: FlexLineContext = FlexLineContext::new(
3916 frame,
3917 is_row,
3918 main_gap,
3919 main_avail,
3920 cross_avail,
3921 line_grow,
3922 mode,
3923 );
3924 let _: &mut FlexLineContext =
3925 line_ctx.set_wrap_enabled(true).set_line_count(line_count);
3926 let (line_size, l_deps, l_min_aw): (Size, u8, f32) = Self::solve_flex_line_serial(
3927 soa,
3928 output_view,
3929 scratch,
3930 line_range,
3931 cross_cursor,
3932 None,
3933 line_ctx,
3934 );
3935 wrap_deps |= l_deps;
3936 wrap_min_aw = wrap_min_aw.max(l_min_aw);
3937 if is_row {
3938 max_main_extent = max_main_extent.max(line_size.get_width());
3939 cross_cursor += line_size.get_height();
3940 } else {
3941 max_main_extent = max_main_extent.max(line_size.get_height());
3942 cross_cursor += line_size.get_width();
3943 }
3944 if line_offset + 1 < line_count {
3945 cross_cursor += cross_gap;
3946 }
3947 }
3948 scratch.flex_lines_scratch.truncate(lines_start);
3949 scratch.flex_items_scratch.truncate(items_start);
3950 let out_size: Size = if is_row {
3951 Size::new(max_main_extent, cross_cursor)
3952 } else {
3953 Size::new(cross_cursor, max_main_extent)
3954 };
3955 (out_size, wrap_deps, wrap_min_aw)
3956 }
3957
3958 fn solve_flex_line_serial(
3974 soa: &SceneLayoutSoa,
3975 output_view: DisjointOutputView,
3976 scratch: &mut WorkerLayoutScratch,
3977 line_range: (usize, usize),
3978 cross_cursor: f32,
3979 precomputed: Option<(f32, f32)>,
3980 ctx: FlexLineContext,
3981 ) -> (Size, u8, f32) {
3982 let (line_start, line_end): (usize, usize) = line_range;
3983 let mut local_items: Vec<ParallelFlexItem> =
3984 scratch.flex_items_scratch[line_start..line_end].to_vec();
3985 let (
3986 final_used_main,
3987 line_cross,
3988 line_deps,
3989 line_min_aw,
3990 line_shrunk,
3991 effective_main_avail,
3992 ): (f32, f32, u8, f32, bool, f32) = Self::resolve_flex_line_sizes(
3993 soa,
3994 output_view,
3995 scratch,
3996 local_items.as_mut_slice(),
3997 precomputed,
3998 ctx,
3999 );
4000 let is_row: bool = ctx.get_is_row();
4001 let mode: LayoutRunMode = ctx.get_mode();
4002 if mode == LayoutRunMode::ComputeSize {
4003 let sz: Size = if is_row {
4004 Size::new(final_used_main, line_cross)
4005 } else {
4006 Size::new(line_cross, final_used_main)
4007 };
4008 return (sz, line_deps, line_min_aw);
4009 }
4010 let frame: ContainerFrame = ctx.get_frame();
4011 let parent_layout: LayoutStyle = frame.get_parent_layout();
4012 let inner_avail: Size = frame.get_inner_avail();
4013 let viewport: Size = frame.get_viewport();
4014 let content_origin: Point = frame.get_content_origin();
4015 let main_gap: f32 = ctx.get_main_gap();
4016 let cross_avail: f32 = ctx.get_cross_avail();
4017 let wrap_enabled: bool = ctx.get_wrap_enabled();
4018 let line_count: usize = ctx.get_line_count();
4019 let count: usize = local_items.len();
4020 let rem: f32 = (effective_main_avail - final_used_main).max(0.0);
4021 let (mut main_cursor, extra_step): (f32, f32) = match parent_layout.get_justify_content() {
4022 JustifyContent::Start => (0.0, 0.0),
4023 JustifyContent::Center => (rem * 0.5, 0.0),
4024 JustifyContent::End => (rem, 0.0),
4025 JustifyContent::SpaceBetween => {
4026 if count > 1 {
4027 (0.0, rem / ((count - 1) as f32))
4028 } else {
4029 (0.0, 0.0)
4030 }
4031 }
4032 JustifyContent::SpaceAround => {
4033 let slot: f32 = rem / (count.max(1) as f32);
4034 (slot * 0.5, slot)
4035 }
4036 JustifyContent::SpaceEvenly => {
4037 let slot: f32 = rem / ((count + 1) as f32);
4038 (slot, slot)
4039 }
4040 };
4041 let min_cross: f32 = if is_row {
4042 frame.get_min_inner_size().get_height()
4043 } else {
4044 frame.get_min_inner_size().get_width()
4045 };
4046 let definite_cross: bool = !is_row || frame.get_definite_height();
4047 let effective_line_cross: f32 = if line_count == 1 {
4048 let floor_cross: f32 = if definite_cross && (!wrap_enabled || is_row) {
4049 cross_avail.max(min_cross)
4050 } else {
4051 min_cross
4052 };
4053 line_cross.max(floor_cross)
4054 } else {
4055 line_cross
4056 };
4057 let mut actual_line_cross: f32 = line_cross;
4058 let parent_align: AlignItems = parent_layout.get_align_items();
4059 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
4060 for (idx, rec) in local_items.iter().copied().enumerate() {
4061 let child_bfs: u32 = rec.get_bfs_index();
4062 let child_size: Size = rec.get_size();
4063 let margin: Edges = rec.get_margin();
4064 let width_auto: bool = (rec.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0;
4065 let height_auto: bool = (rec.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0;
4066 let item_cross: f32 = if is_row {
4067 child_size.get_height()
4068 } else {
4069 child_size.get_width()
4070 };
4071 let align: AlignItems = match rec.get_align_self() {
4072 AlignSelf::Auto => parent_align,
4073 AlignSelf::Stretch => AlignItems::Stretch,
4074 AlignSelf::Start => AlignItems::Start,
4075 AlignSelf::Center => AlignItems::Center,
4076 AlignSelf::End => AlignItems::End,
4077 };
4078 let avail_item_cross: f32 = (effective_line_cross
4079 - if is_row {
4080 margin.vertical()
4081 } else {
4082 margin.horizontal()
4083 })
4084 .max(0.0);
4085 let cross_offset: f32 = match align {
4086 AlignItems::Start | AlignItems::Stretch => 0.0,
4087 AlignItems::Center => ((avail_item_cross - item_cross) * 0.5).max(0.0),
4088 AlignItems::End => (avail_item_cross - item_cross).max(0.0),
4089 };
4090 let main_resized_item: bool = rec.get_flex_grow() > 0.0 || line_shrunk;
4091 let auto_cross_grow: bool =
4092 main_resized_item && if is_row { height_auto } else { width_auto };
4093 let target_w: f32 = if !is_row && align == AlignItems::Stretch && width_auto {
4094 avail_item_cross.max(child_size.get_width())
4095 } else {
4096 child_size.get_width()
4097 };
4098 let target_h: f32 = if is_row && align == AlignItems::Stretch && height_auto {
4099 avail_item_cross.max(child_size.get_height())
4100 } else {
4101 child_size.get_height()
4102 };
4103 let (pos_x, pos_y): (f32, f32) = if is_row {
4104 (
4105 content_origin.get_x() + main_cursor + margin.get_left(),
4106 content_origin.get_y() + cross_cursor + cross_offset + margin.get_top(),
4107 )
4108 } else {
4109 (
4110 content_origin.get_x() + cross_cursor + cross_offset + margin.get_left(),
4111 content_origin.get_y() + main_cursor + margin.get_top(),
4112 )
4113 };
4114 let stretch_child_w: bool = !(auto_cross_grow && !is_row)
4115 && ((!is_row && align == AlignItems::Stretch && width_auto)
4116 || (is_row && main_resized_item && width_auto));
4117 let stretch_child_h: bool = !(auto_cross_grow && is_row)
4118 && ((is_row && align == AlignItems::Stretch && height_auto)
4119 || (!is_row && main_resized_item && height_auto));
4120 let space_w: f32 =
4121 if !stretch_child_w && (rec.get_flags() & FLEX_ITEM_WIDTH_PERCENT) != 0 {
4122 inner_avail.get_width()
4123 } else if auto_cross_grow && !is_row {
4124 cross_avail
4125 } else {
4126 target_w
4127 };
4128 let space_h: f32 =
4129 if !stretch_child_h && (rec.get_flags() & FLEX_ITEM_HEIGHT_PERCENT) != 0 {
4130 inner_avail.get_height()
4131 } else if auto_cross_grow && is_row {
4132 cross_avail
4133 } else {
4134 target_h
4135 };
4136 let final_space: AvailableSpace =
4137 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
4138 .with_stretch(stretch_child_w, stretch_child_h);
4139 let (placed_final, _, _): (Size, u8, f32) = Self::solve_node_serial(
4140 soa,
4141 output_view,
4142 scratch,
4143 child_bfs,
4144 final_space,
4145 LayoutRunMode::PerformLayout,
4146 );
4147 if auto_cross_grow && matches!(align, AlignItems::Center | AlignItems::End) {
4148 let actual_item_cross: f32 = if is_row {
4149 placed_final.get_height()
4150 } else {
4151 placed_final.get_width()
4152 };
4153 let refined_offset: f32 = match align {
4154 AlignItems::Center => ((avail_item_cross - actual_item_cross) * 0.5).max(0.0),
4155 AlignItems::End => (avail_item_cross - actual_item_cross).max(0.0),
4156 AlignItems::Start | AlignItems::Stretch => cross_offset,
4157 };
4158 let delta_cross: f32 = refined_offset - cross_offset;
4159 if delta_cross.abs() > 0.001 {
4160 let (dx, dy): (f32, f32) = if is_row {
4161 (0.0, delta_cross)
4162 } else {
4163 (delta_cross, 0.0)
4164 };
4165 Self::translate_subtree_bfs(soa, output_view, scratch, child_bfs, dx, dy);
4166 }
4167 }
4168 let is_clipped: bool =
4169 matches!(rec.get_overflow(), Overflow::Hidden | Overflow::Scroll);
4170 let (desired_w, desired_h): (f32, f32) = if auto_cross_grow {
4171 if is_row {
4172 actual_line_cross =
4173 actual_line_cross.max(placed_final.get_height() + margin.vertical());
4174 (target_w, placed_final.get_height())
4175 } else {
4176 actual_line_cross =
4177 actual_line_cross.max(placed_final.get_width() + margin.horizontal());
4178 (placed_final.get_width(), target_h)
4179 }
4180 } else if is_clipped || main_resized_item {
4181 (target_w, target_h)
4182 } else {
4183 (
4184 target_w.max(placed_final.get_width()),
4185 target_h.max(placed_final.get_height()),
4186 )
4187 };
4188 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
4189 let cur_bounds: Rect = unsafe { output_view.read_box(arena_idx) }.get_bounds();
4191 if (cur_bounds.get_width() - desired_w).abs() > 0.001
4192 || (cur_bounds.get_height() - desired_h).abs() > 0.001
4193 {
4194 Self::apply_post_stretch(
4195 soa,
4196 output_view,
4197 arena_idx,
4198 Some(desired_w),
4199 Some(desired_h),
4200 );
4201 }
4202 let advance: f32 = if is_row {
4203 target_w + margin.horizontal()
4204 } else {
4205 target_h + margin.vertical()
4206 };
4207 main_cursor += advance;
4208 if idx + 1 < count {
4209 main_cursor += main_gap + extra_step;
4210 }
4211 }
4212 let sz: Size = if is_row {
4213 Size::new(final_used_main, actual_line_cross)
4214 } else {
4215 Size::new(actual_line_cross, final_used_main)
4216 };
4217 (sz, line_deps, line_min_aw)
4218 }
4219
4220 fn solve_grid_serial(
4235 soa: &SceneLayoutSoa,
4236 output_view: DisjointOutputView,
4237 scratch: &mut WorkerLayoutScratch,
4238 flow_range: (usize, usize),
4239 frame: ContainerFrame,
4240 mode: LayoutRunMode,
4241 ) -> (Size, u8, f32) {
4242 let (flow_start, flow_end): (usize, usize) = flow_range;
4243 if flow_start >= flow_end {
4244 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
4245 }
4246 let parent_layout: LayoutStyle = frame.get_parent_layout();
4247 let inner_avail: Size = frame.get_inner_avail();
4248 let viewport: Size = frame.get_viewport();
4249 let content_origin: Point = frame.get_content_origin();
4250 let cols: usize = (parent_layout.get_grid_columns() as usize).max(1);
4251 let col_gap: f32 = parent_layout.get_column_gap();
4252 let row_gap: f32 = parent_layout.get_row_gap();
4253 let total_col_gaps: f32 = col_gap * ((cols.saturating_sub(1)) as f32);
4254 let col_w: f32 = ((inner_avail.get_width() - total_col_gaps) / (cols as f32)).max(0.0);
4255 let col_step: f32 = col_w + col_gap;
4256 let origin_x: f32 = content_origin.get_x();
4257 let origin_y: f32 = content_origin.get_y();
4258 let inner_h: f32 = inner_avail.get_height();
4259 let bfs_to_arena: &[u32] = soa.get_topology().get_bfs_to_arena().as_slice();
4260 let layout_styles: &[LayoutStyle] = soa.get_layout_styles().as_slice();
4261 let mut y_cursor: f32 = 0.0;
4262 let mut idx: usize = flow_start;
4263 let mut grid_deps: u8 = LAYOUT_DEP_AVAIL_W;
4264 while idx < flow_end {
4265 let row_end: usize = (idx + cols).min(flow_end);
4266 let mut row_h: f32 = 0.0;
4267 let mut min_row_h: f32 = f32::INFINITY;
4268 let pos_y: f32 = origin_y + y_cursor;
4269 let mut pos_x: f32 = origin_x;
4270 for pos in idx..row_end {
4271 let child_bfs: u32 = scratch.flow_scratch[pos];
4272 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
4273 let auto_w: bool = layout_styles[arena_idx].get_width() == Length::Auto;
4274 let cell_space: AvailableSpace =
4275 AvailableSpace::new(col_w, inner_h, viewport, pos_x, pos_y)
4276 .with_stretch(auto_w, false);
4277 let (child_size, c_deps, _): (Size, u8, f32) =
4278 Self::solve_node_serial(soa, output_view, scratch, child_bfs, cell_space, mode);
4279 grid_deps |= c_deps;
4280 if mode == LayoutRunMode::PerformLayout
4281 && (child_size.get_width() - col_w).abs() > 0.001
4282 {
4283 Self::apply_post_stretch(soa, output_view, arena_idx, Some(col_w), None);
4284 }
4285 let child_h: f32 = child_size.get_height();
4286 row_h = row_h.max(child_h);
4287 min_row_h = min_row_h.min(child_h);
4288 pos_x += col_step;
4289 }
4290 if mode == LayoutRunMode::PerformLayout && row_end - idx > 1 && min_row_h < row_h {
4291 for &child_bfs in &scratch.flow_scratch[idx..row_end] {
4292 let arena_idx: usize = bfs_to_arena[child_bfs as usize] as usize;
4293 let cur_h: f32 = unsafe { output_view.read_box(arena_idx) }
4295 .get_bounds()
4296 .get_height();
4297 if cur_h < row_h {
4298 Self::apply_post_stretch(soa, output_view, arena_idx, None, Some(row_h));
4299 }
4300 }
4301 }
4302 y_cursor += row_h;
4303 if row_end < flow_end {
4304 y_cursor += row_gap;
4305 }
4306 idx = row_end;
4307 }
4308 (Size::new(inner_avail.get_width(), y_cursor), grid_deps, 0.0)
4309 }
4310}