Skip to main content

euv_core/layout/parallel/
struct.rs

1use super::*;
2
3/// Per-pass execution diagnostics recorded by `ParallelLayoutSolver`.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
5pub struct ParallelLayoutStats {
6    /// Whether this layout pass forked work onto `EngineWorkerPool` worker threads.
7    #[get(type(copy))]
8    pub(crate) used_parallel: bool,
9    /// Total child subtree tasks dispatched across parallel fork-join waves.
10    #[get(type(copy))]
11    pub(crate) parallel_tasks_dispatched: usize,
12    /// Total concurrent threads available (`1 + worker_count`) during this pass.
13    #[get(type(copy))]
14    pub(crate) worker_threads_available: usize,
15    /// Number of nodes translated via SIMD `simd_translate_subtree_4x`.
16    #[get(type(copy))]
17    pub(crate) simd_translations: usize,
18    /// Number of ASCII text measurements accelerated via `simd_measure_ascii_advance`.
19    #[get(type(copy))]
20    pub(crate) simd_text_measures: usize,
21    /// Number of nodes laid out during this pass.
22    #[get(type(copy))]
23    pub(crate) nodes_computed: usize,
24    /// Number of nodes whose final layout geometry changed.
25    #[get(type(copy))]
26    pub(crate) nodes_changed: usize,
27}
28
29/// Compact flex item scratch record indexed by BFS slot for lock-free parallel layout workers.
30#[derive(Clone, Copy, Data, Debug, Default, New)]
31pub(crate) struct ParallelFlexItem {
32    /// Target child BFS node index.
33    #[get(pub(crate), type(copy))]
34    #[set(pub(crate))]
35    pub(crate) bfs_index: u32,
36    /// Current resolved item size updated during grow/shrink and re-probe passes.
37    #[get(pub(crate), type(copy))]
38    #[get_mut(pub(crate))]
39    #[set(pub(crate))]
40    pub(crate) size: Size,
41    /// Outer margin insets copied from `LayoutStyle`.
42    #[get(pub(crate), type(copy))]
43    #[set(pub(crate))]
44    pub(crate) margin: Edges,
45    /// Flex grow factor copied from `LayoutStyle`.
46    #[get(pub(crate), type(copy))]
47    #[set(pub(crate))]
48    #[new(skip)]
49    pub(crate) flex_grow: f32,
50    /// Flex shrink factor copied from `LayoutStyle`.
51    #[get(pub(crate), type(copy))]
52    #[set(pub(crate))]
53    #[new(skip)]
54    pub(crate) flex_shrink: f32,
55    /// Minimum main-axis floor for CSS flex-shrink clamping.
56    #[get(pub(crate), type(copy))]
57    #[set(pub(crate))]
58    #[new(skip)]
59    pub(crate) min_main: f32,
60    /// Effective `align-self` copied from `LayoutStyle`.
61    #[get(pub(crate), type(copy))]
62    #[set(pub(crate))]
63    pub(crate) align_self: AlignSelf,
64    /// Overflow clipping behavior copied from `LayoutStyle`.
65    #[get(pub(crate), type(copy))]
66    #[set(pub(crate))]
67    pub(crate) overflow: Overflow,
68    /// Packed `FLEX_ITEM_*` constraint bits.
69    #[get(pub(crate), type(copy))]
70    #[set(pub(crate))]
71    pub(crate) flags: u16,
72}
73
74/// Reusable thread-private scratch buffers and L1 interval text cache owned by one `EngineWorkerPool` lane.
75#[derive(Clone, Debug, Default)]
76pub struct WorkerLayoutScratch {
77    /// Stack of normal-flow child BFS indices for active container recursion frames.
78    pub(crate) flow_scratch: Vec<u32>,
79    /// Stack of absolute/fixed overlay child BFS indices for active container recursion frames.
80    pub(crate) abs_scratch: Vec<u32>,
81    /// Stack of flex item records for active flex recursion frames.
82    pub(crate) flex_items_scratch: Vec<ParallelFlexItem>,
83    /// Stack of flex line `(start, end)` ranges into `flex_items_scratch`.
84    pub(crate) flex_lines_scratch: Vec<(usize, usize)>,
85    /// Iterative subtree traversal stack collecting arena indices for SIMD batch translation.
86    pub(crate) translate_arena_scratch: Vec<u32>,
87    /// Iterative BFS traversal stack used to collect descendants without recursion.
88    pub(crate) translate_bfs_scratch: Vec<u32>,
89    /// Thread-local lock-free 2-way set-associative interval text measurement cache.
90    pub(crate) text_size_cache: TextSizeCache,
91    /// Count of nodes computed on this worker lane during the active pass.
92    pub(crate) nodes_computed: usize,
93    /// Count of nodes whose bounds changed on this worker lane during the active pass.
94    pub(crate) nodes_changed: usize,
95    /// Count of nodes shifted via SIMD translation on this worker lane.
96    pub(crate) simd_translations: usize,
97    /// Count of ASCII text spans measured via SIMD on this worker lane.
98    pub(crate) simd_text_measures: usize,
99}
100
101/// Lock-free array of per-lane `WorkerLayoutScratch` slots indexed exclusively by `EngineWorkerPool` lane ID.
102#[derive(Debug)]
103pub(crate) struct WorkerScratchBank {
104    /// Fixed array of lane-exclusive scratch cells (`0` = caller, `1..=ENGINE_WORKER_MAX_THREADS` = workers).
105    pub(crate) lanes: Vec<UnsafeCell<WorkerLayoutScratch>>,
106}
107
108/// Per-node metadata emitted into `ParallelOutputArena` for seamless `SplitLayoutCache` synchronization.
109#[derive(Clone, Copy, Debug, Default)]
110pub(crate) struct ParallelNodeResult {
111    /// Committed border-box size prior to parent post-stretch.
112    pub(crate) size: Size,
113    /// Pre-stretch border-box rectangle preserved for `SplitLayoutCache` restoration.
114    pub(crate) natural_bounds: Rect,
115    /// Pre-stretch content-box rectangle preserved for `SplitLayoutCache` restoration.
116    pub(crate) natural_content_bounds: Rect,
117    /// Computed layout dependency mask (`LAYOUT_DEP_*`).
118    pub(crate) dep_flags: u8,
119    /// Invariant minimum available width for unwrapped text subtrees.
120    pub(crate) min_avail_w: f32,
121    /// Whether the parent post-stretched this node's final bounds.
122    pub(crate) post_stretched: bool,
123    /// Whether this node resolved under `Display::None`.
124    pub(crate) hidden: bool,
125    /// Whether this slot was written during the active parallel layout pass.
126    pub(crate) written: bool,
127    /// Whether any direct child has non-zero `z_index`.
128    pub(crate) has_nonzero_z: bool,
129}
130
131/// Arena-indexed output buffers populated lock-free across pairwise-disjoint sibling subtrees.
132#[derive(Clone, Debug, Default)]
133pub(crate) struct ParallelOutputArena {
134    /// Computed `LayoutBox` per arena slot.
135    pub(crate) boxes: Vec<LayoutBox>,
136    /// Computed `AvailableSpace` constraint per arena slot.
137    pub(crate) constraints: Vec<Option<AvailableSpace>>,
138    /// Computed conservative subtree bounding box per arena slot.
139    pub(crate) subtree_bounds: Vec<Rect>,
140    /// Computed per-node final metadata and `SplitLayoutCache` sync flags.
141    pub(crate) results: Vec<ParallelNodeResult>,
142}
143
144/// Raw disjoint-index writer handle shared across worker threads executing strictly disjoint sibling subtrees.
145#[derive(Clone, Copy, Debug)]
146pub(crate) struct DisjointOutputView {
147    /// Base pointer to `ParallelOutputArena::boxes`.
148    pub(crate) boxes_ptr: *mut LayoutBox,
149    /// Base pointer to `ParallelOutputArena::constraints`.
150    pub(crate) constraints_ptr: *mut Option<AvailableSpace>,
151    /// Base pointer to `ParallelOutputArena::subtree_bounds`.
152    pub(crate) subtree_bounds_ptr: *mut Rect,
153    /// Base pointer to `ParallelOutputArena::results`.
154    pub(crate) results_ptr: *mut ParallelNodeResult,
155    /// Arena slot length shared by all four output slices.
156    pub(crate) arena_len: usize,
157}
158
159/// Per-task placement record written back by parallel Grid or single-pass Flex workers.
160#[cfg(not(target_arch = "wasm32"))]
161#[derive(Clone, Copy, Debug, Default)]
162pub(crate) struct ParallelChildOutcome {
163    /// Computed border-box size returned by `solve_node_soa`.
164    pub(crate) size: Size,
165    /// Computed layout dependency mask.
166    pub(crate) dep_flags: u8,
167    /// Invariant minimum available width.
168    pub(crate) min_avail_w: f32,
169}
170
171/// Per-task input specification dispatched for parallel Grid or Flex child subtrees.
172#[cfg(not(target_arch = "wasm32"))]
173#[derive(Clone, Copy, Debug, Default)]
174pub(crate) struct ParallelChildTask {
175    /// Target child BFS node index.
176    pub(crate) bfs_index: u32,
177    /// Incoming constraint space (local `(0, 0)` origin for pre-translation passes or final origin for Flex Pass 3).
178    pub(crate) space: AvailableSpace,
179    /// Execution run mode (`ComputeSize` or `PerformLayout`).
180    pub(crate) mode: LayoutRunMode,
181}
182
183/// Disjoint raw-pointer view into `ParallelLayoutSolver::child_outcomes` for lock-free per-task writes.
184#[cfg(not(target_arch = "wasm32"))]
185#[derive(Clone, Copy, Debug)]
186pub(crate) struct DisjointOutcomeView {
187    /// Base pointer to contiguous per-task `ParallelChildOutcome` slots.
188    pub(crate) outcomes_ptr: *mut ParallelChildOutcome,
189    /// Total allocated task outcome count.
190    pub(crate) outcomes_len: usize,
191}
192
193/// Shared fork-join execution context passed down the caller-thread parallel dispatch tree.
194#[cfg(not(target_arch = "wasm32"))]
195pub(crate) struct ParallelForkContext<'a> {
196    /// Read-only `Send + Sync` layout SoA slab.
197    pub(crate) soa: &'a SceneLayoutSoa,
198    /// Disjoint arena output writer shared across sibling workers.
199    pub(crate) output_view: DisjointOutputView,
200    /// Lane-exclusive worker scratch bank.
201    pub(crate) scratch_bank: &'a WorkerScratchBank,
202    /// Unified work-stealing worker pool.
203    pub(crate) pool: &'a EngineWorkerPool,
204    /// Reusable task list for the active parallel wave.
205    pub(crate) child_tasks: &'a mut Vec<ParallelChildTask>,
206    /// Reusable per-task outcome buffer for the active parallel wave.
207    pub(crate) child_outcomes: &'a mut Vec<ParallelChildOutcome>,
208    /// Cumulative dispatched parallel task counter for this pass.
209    pub(crate) dispatched_tasks: &'a mut usize,
210}
211
212/// Resolved container box sizing, clamping, chrome, and content origin parameters.
213#[derive(Clone, Copy, Debug, Default)]
214pub(crate) struct ResolvedNodeConstraints {
215    /// Own layout dependency flags (`LAYOUT_DEP_*`).
216    pub(crate) own_deps: u8,
217    /// Resolved outer width (explicit or stretched).
218    pub(crate) resolved_w: Option<f32>,
219    /// Resolved outer height (explicit or stretched).
220    pub(crate) resolved_h: Option<f32>,
221    /// Explicit CSS outer width prior to stretching.
222    pub(crate) explicit_w: Option<f32>,
223    /// Whether outer width came from parent stretch rather than explicit style.
224    pub(crate) stretched_w: bool,
225    /// Minimum outer width floor.
226    pub(crate) min_w: f32,
227    /// Maximum outer width ceiling.
228    pub(crate) max_w: f32,
229    /// Minimum outer height floor.
230    pub(crate) min_h: f32,
231    /// Maximum outer height ceiling.
232    pub(crate) max_h: f32,
233    /// Horizontal padding plus border width.
234    pub(crate) chrome_h: f32,
235    /// Vertical padding plus border width.
236    pub(crate) chrome_v: f32,
237    /// Inner available width for descendants.
238    pub(crate) inner_avail_w: f32,
239    /// Inner available height for descendants.
240    pub(crate) inner_avail_h: f32,
241    /// Logical X origin of the content box.
242    pub(crate) content_origin_x: f32,
243    /// Logical Y origin of the content box.
244    pub(crate) content_origin_y: f32,
245}
246
247/// Precomputed Pass-1 flex item probe metadata returned by `prepare_flex_item`.
248#[derive(Clone, Copy, Debug, Default)]
249pub(crate) struct PreparedFlexItemMeta {
250    /// Known direct fixed/zero-basis size when Pass-1 probing can be skipped in O(1).
251    pub(crate) zero_basis_size: Option<Size>,
252    /// Resolved `flex-basis` override in logical pixels.
253    pub(crate) explicit_basis_main: Option<f32>,
254    /// Resolved minimum main-axis clamp in logical pixels.
255    pub(crate) explicit_min_main: Option<f32>,
256    /// Resolved maximum main-axis clamp in logical pixels.
257    pub(crate) explicit_max_main: Option<f32>,
258    /// Whether child is a `Text` node with `TextOverflow::Clip`.
259    pub(crate) is_unwrapped_clip_text: bool,
260    /// Whether child specifies a `Length::Px` main-axis dimension.
261    pub(crate) has_px_main: bool,
262}
263
264/// Per-item Pass-3 placement metadata retained across the parallel flex line wave.
265#[cfg(not(target_arch = "wasm32"))]
266#[derive(Clone, Copy, Debug)]
267pub(crate) struct FlexPlacementMeta {
268    /// Target resolved width prior to post-join refinement.
269    pub(crate) target_w: f32,
270    /// Target resolved height prior to post-join refinement.
271    pub(crate) target_h: f32,
272    /// Available cross-axis slot after outer margin deduction.
273    pub(crate) avail_item_cross: f32,
274    /// Initial cross-axis alignment offset before auto-cross refinement.
275    pub(crate) cross_offset: f32,
276    /// Effective cross-axis alignment resolved from `align-self`/`align-items`.
277    pub(crate) align: AlignItems,
278    /// Whether this item has auto cross dimension under grow/shrink main-axis resizing.
279    pub(crate) auto_cross_grow: bool,
280    /// Whether this item was resized along the main axis via `flex-grow` or `flex-shrink`.
281    pub(crate) main_resized_item: bool,
282}
283
284/// Lock-free parallel CPU dirty-subtree layout solver backed by `EngineWorkerPool` and SIMD micro-kernels.
285#[derive(Debug)]
286pub struct ParallelLayoutSolver {
287    /// Reusable per-worker lane scratch bank eliminating allocations and mutexes during parallel waves.
288    pub(crate) scratch_bank: WorkerScratchBank,
289    /// Reusable output arena written lock-free by workers and committed to `SceneTree` and `SplitLayoutCache`.
290    pub(crate) output_arena: ParallelOutputArena,
291    /// Reusable scratch slice of parallel child dispatch tasks for the active top-level wave.
292    #[cfg(not(target_arch = "wasm32"))]
293    pub(crate) child_tasks: Vec<ParallelChildTask>,
294    /// Reusable scratch slice of per-task outcomes written to disjoint indices by workers.
295    #[cfg(not(target_arch = "wasm32"))]
296    pub(crate) child_outcomes: Vec<ParallelChildOutcome>,
297    /// Diagnostics captured from the most recent `compute_tree` invocation.
298    pub(crate) last_stats: ParallelLayoutStats,
299}