1mod flatten;
9mod layout;
10mod types;
11
12use crate::arena::{ScratchArena, SmallVec};
13use crate::element::Element;
14use crate::motion::{DrawOp, MotionCache};
15use crate::style::Length;
16
17use flatten::{node_baseline_offset, pass4_vertical_assign_and_flatten};
18use layout::{
19 build_layout_tree, build_layout_tree_with_pool, pass1_horizontal_measure,
20 pass1_horizontal_measure_with_pool, pass2_horizontal_assign,
21 pass2_horizontal_assign_with_pool, pass3_vertical_measure, pass3_vertical_measure_with_pool,
22};
23pub use types::{HitRegion, InteractionState, Region};
24pub(crate) use types::LayoutNode;
25use types::{
26 clamp_h, clamp_w, outer_box_spec, SUBVIEW_FLAG_CACHED_PASS1, SUBVIEW_FLAG_OWNER,
27 SUBVIEW_FLAG_ROOT,
28};
29
30pub fn solve_and_flatten<'a, const MAX_OPS: usize, const MAX_HITS: usize>(
32 arena: &'a ScratchArena,
33 root: &'a Element<'a>,
34 viewport: Region,
35 motion: &mut MotionCache,
36 out_ops: &mut SmallVec<DrawOp<'a>, MAX_OPS>,
37 out_hits: &mut SmallVec<HitRegion<'a>, MAX_HITS>,
38) -> u32 {
39 solve_and_flatten_with_state(
40 arena,
41 root,
42 viewport,
43 InteractionState::default(),
44 motion,
45 out_ops,
46 out_hits,
47 )
48}
49
50#[inline(never)]
52pub fn solve_and_flatten_with_state<'a, const MAX_OPS: usize, const MAX_HITS: usize>(
53 arena: &'a ScratchArena,
54 root: &'a Element<'a>,
55 viewport: Region,
56 interaction: InteractionState,
57 motion: &mut MotionCache,
58 out_ops: &mut SmallVec<DrawOp<'a>, MAX_OPS>,
59 out_hits: &mut SmallVec<HitRegion<'a>, MAX_HITS>,
60) -> u32 {
61 let mut root_node = build_layout_tree(arena, root);
62
63 pass1_horizontal_measure(&mut root_node);
65
66 root_node.x = viewport.x;
68 root_node.w = match root {
69 Element::Div(d) => match d.style.width {
70 Length::Px(px) => clamp_w(root, px),
71 Length::Pct(pct) => clamp_w(root, ((viewport.w as i32 * pct as i32) / 100) as i16),
72 Length::Full | Length::Auto => clamp_w(root, viewport.w),
73 },
74 _ => viewport.w,
75 };
76 pass2_horizontal_assign(&mut root_node);
77
78 pass3_vertical_measure(&mut root_node);
80
81 reflatten_pass4_only(
83 arena,
84 &mut root_node,
85 viewport,
86 interaction,
87 motion,
88 out_ops,
89 out_hits,
90 )
91}
92
93#[allow(dead_code)]
95pub(crate) fn solve_and_flatten_retained<'a, const MAX_OPS: usize, const MAX_HITS: usize>(
96 arena: &'a ScratchArena,
97 root: &'a Element<'a>,
98 viewport: Region,
99 interaction: InteractionState,
100 motion: &mut MotionCache,
101 out_ops: &mut SmallVec<DrawOp<'a>, MAX_OPS>,
102 out_hits: &mut SmallVec<HitRegion<'a>, MAX_HITS>,
103) -> (*mut LayoutNode<'static, 'static>, u32) {
104 solve_and_flatten_retained_with_pool(
105 arena,
106 root,
107 None,
108 viewport,
109 interaction,
110 motion,
111 out_ops,
112 out_hits,
113 )
114}
115
116#[inline(never)]
119#[allow(clippy::too_many_arguments)]
120pub(crate) fn solve_and_flatten_retained_with_pool<
121 'a,
122 const MAX_OPS: usize,
123 const MAX_HITS: usize,
124>(
125 arena: &'a ScratchArena,
126 root: &'a Element<'a>,
127 pool: Option<&crate::pod::PodPool>,
128 viewport: Region,
129 interaction: InteractionState,
130 motion: &mut MotionCache,
131 out_ops: &mut SmallVec<DrawOp<'a>, MAX_OPS>,
132 out_hits: &mut SmallVec<HitRegion<'a>, MAX_HITS>,
133) -> (*mut LayoutNode<'static, 'static>, u32) {
134 if let Some(p) = pool {
135 p.begin_layout_pass();
136 }
137 let root_node = arena.alloc_temp(build_layout_tree_with_pool(arena, root, pool, true, true));
138
139 pass1_horizontal_measure_with_pool(root_node, pool);
141
142 root_node.x = viewport.x;
144 root_node.w = match root {
145 Element::Div(d) => match d.style.width {
146 Length::Px(px) => clamp_w(root, px),
147 Length::Pct(pct) => clamp_w(root, ((viewport.w as i32 * pct as i32) / 100) as i16),
148 Length::Full | Length::Auto => clamp_w(root, viewport.w),
149 },
150 _ => viewport.w,
151 };
152 pass2_horizontal_assign_with_pool(root_node, pool);
153
154 pass3_vertical_measure_with_pool(root_node, pool, true);
156
157 let dropped_ops = reflatten_pass4_only(
158 arena,
159 root_node,
160 viewport,
161 interaction,
162 motion,
163 out_ops,
164 out_hits,
165 );
166 let raw_ptr = root_node as *mut LayoutNode<'a, '_> as *mut LayoutNode<'static, 'static>;
167 (raw_ptr, dropped_ops)
168}
169
170pub(crate) fn try_relayout_dirty_subviews_in_place<'a>(
175 arena: &'a ScratchArena,
176 pool: &crate::pod::PodPool,
177 dirty_ids: &[crate::pod::PodId],
178) -> bool {
179 if dirty_ids.is_empty() || dirty_ids.len() > 8 {
180 return false;
181 }
182
183 struct PendingCommit<'a, 'b> {
184 id: crate::pod::PodId,
185 target_ptr: *mut LayoutNode<'a, 'b>,
186 new_node: LayoutNode<'a, 'b>,
187 min_w: i16,
188 pref_w: i16,
189 x: i16,
190 w: i16,
191 min_h: i16,
192 pref_h: i16,
193 }
194
195 let saved_mark = arena.save_mark();
196 let saved_temp = arena.save_temp_mark();
197 let mut commits: SmallVec<PendingCommit<'a, 'a>, 8> = SmallVec::new();
198
199 for &dirty_id in dirty_ids {
200 let Some(target_raw_ptr) = pool.can_relayout_subview_in_place(dirty_id, arena) else {
201 unsafe {
202 arena.rollback_speculative_marks(saved_mark, saved_temp);
203 }
204 return false;
205 };
206 let target_ptr = target_raw_ptr as *mut LayoutNode<'a, 'a>;
207 let old_node = unsafe { &*target_ptr };
208 let Some(new_elem_static) = pool.get_subview_cached_element(dirty_id, arena) else {
209 unsafe {
210 arena.rollback_speculative_marks(saved_mark, saved_temp);
211 }
212 return false;
213 };
214 let new_elem: &'a Element<'a> =
215 unsafe { &*(new_elem_static as *const Element<'static> as *const Element<'a>) };
216 if matches!(new_elem, Element::SubView { .. }) {
217 unsafe {
218 arena.rollback_speculative_marks(saved_mark, saved_temp);
219 }
220 return false;
221 }
222
223 let spec = outer_box_spec(new_elem);
224 if spec != outer_box_spec(old_node.element) {
225 unsafe {
226 arena.rollback_speculative_marks(saved_mark, saved_temp);
227 }
228 return false;
229 }
230
231 let (old_min_w, old_pref_w) = pool.get_subview_pass1(dirty_id);
232 let (old_x, old_w) = pool.get_subview_solved_xw(dirty_id);
233 let (old_min_h, old_pref_h) = pool.get_subview_pass3(dirty_id);
234 let old_y = old_node.y;
235 let old_h = old_node.h;
236
237 if old_x != old_node.x
238 || old_w != old_node.w
239 || old_min_h != old_node.min_size
240 || old_pref_h != old_node.pref_size
241 {
242 unsafe {
243 arena.rollback_speculative_marks(saved_mark, saved_temp);
244 }
245 return false;
246 }
247
248 let parent_non_auto_w = pool.subview_parent_non_auto_w(dirty_id);
249 let parent_non_auto_h = pool.subview_parent_non_auto_h(dirty_id);
250 let child_non_auto_w = match new_elem {
251 Element::Div(d) => {
252 let scroll_horizontal = d.style.flex_dir() == crate::style::FlexDir::Row
253 && d.grid_cols == 0
254 && !d.style.flex_wrap();
255 parent_non_auto_w
256 && !matches!(d.style.width, Length::Auto)
257 && !(d.scrollable_max().is_some() && scroll_horizontal)
258 && !d.style.flex_wrap()
259 }
260 _ => false,
261 };
262 let child_non_auto_h = match new_elem {
263 Element::Div(d) => {
264 let scroll_horizontal = d.style.flex_dir() == crate::style::FlexDir::Row
265 && d.grid_cols == 0
266 && !d.style.flex_wrap();
267 parent_non_auto_h
268 && !matches!(d.style.height, Length::Auto)
269 && !(d.scrollable_max() == Some(i16::MAX) && !scroll_horizontal)
270 && d.grid_cols == 0
271 && !d.style.flex_wrap()
272 && !d.items_baseline()
273 }
274 _ => false,
275 };
276
277 let mut new_node = build_layout_tree_with_pool(
278 arena,
279 new_elem,
280 Some(pool),
281 child_non_auto_w,
282 child_non_auto_h,
283 );
284 new_node.subview_flags = SUBVIEW_FLAG_ROOT | SUBVIEW_FLAG_OWNER;
285 new_node.subview_pod = dirty_id.0;
286
287 pass1_horizontal_measure_with_pool(&mut new_node, Some(pool));
288 let new_min_w = new_node.min_size;
289 let new_pref_w = new_node.pref_size;
290
291 let horiz_isolated = (new_min_w == old_min_w && new_pref_w == old_pref_w)
292 || (parent_non_auto_w
293 && matches!(spec.width, Length::Px(px) if old_w == clamp_w(new_elem, px))
294 && new_pref_w == old_pref_w
295 && new_min_w <= old_w)
296 || (parent_non_auto_w
297 && matches!(spec.width, Length::Full)
298 && old_w > spec.min_w
299 && new_min_w <= old_w);
300
301 if !horiz_isolated {
302 unsafe {
303 arena.rollback_speculative_marks(saved_mark, saved_temp);
304 }
305 return false;
306 }
307
308 new_node.x = old_x;
309 new_node.w = old_w;
310 pass2_horizontal_assign_with_pool(&mut new_node, Some(pool));
311 pass3_vertical_measure_with_pool(&mut new_node, Some(pool), false);
312
313 let new_min_h = new_node.min_size;
314 let new_pref_h = new_node.pref_size;
315
316 let vert_isolated = (new_min_h == old_min_h && new_pref_h == old_pref_h)
317 || (parent_non_auto_h
318 && matches!(spec.height, Length::Px(px) if old_h == clamp_h(new_elem, px))
319 && new_pref_h == old_pref_h
320 && new_min_h <= old_h)
321 || (parent_non_auto_h
322 && matches!(spec.height, Length::Full)
323 && old_h > spec.min_h
324 && new_min_h <= old_h);
325
326 if !vert_isolated {
327 unsafe {
328 arena.rollback_speculative_marks(saved_mark, saved_temp);
329 }
330 return false;
331 }
332
333 new_node.y = old_y;
334 new_node.h = old_h;
335 if !parent_non_auto_h && node_baseline_offset(&new_node) != node_baseline_offset(old_node) {
336 unsafe {
337 arena.rollback_speculative_marks(saved_mark, saved_temp);
338 }
339 return false;
340 }
341
342 new_node.subview_flags = SUBVIEW_FLAG_ROOT | SUBVIEW_FLAG_CACHED_PASS1 | SUBVIEW_FLAG_OWNER;
343 let _ = commits.push(PendingCommit {
344 id: dirty_id,
345 target_ptr,
346 new_node,
347 min_w: new_min_w,
348 pref_w: new_pref_w,
349 x: old_x,
350 w: old_w,
351 min_h: new_min_h,
352 pref_h: new_pref_h,
353 });
354 }
355
356 for commit in commits.as_mut_slice() {
357 commit_subtree_cache_ptrs(&mut commit.new_node, pool);
358 unsafe {
359 *commit.target_ptr = core::ptr::read(&commit.new_node);
360 }
361 pool.save_subview_pass1(commit.id, commit.min_w, commit.pref_w);
362 pool.save_subview_solved_xw(commit.id, commit.x, commit.w);
363 pool.save_subview_pass3_and_node(
364 commit.id,
365 commit.min_h,
366 commit.pref_h,
367 commit.target_ptr as *mut u8,
368 );
369 pool.mark_subview_subtree_clean(commit.id);
370 pool.record_relayout_boundary_hit();
371 }
372
373 true
374}
375
376fn commit_subtree_cache_ptrs(node: &mut LayoutNode<'_, '_>, pool: &crate::pod::PodPool) {
377 for child in node.children.iter_mut() {
378 if (child.subview_flags & SUBVIEW_FLAG_ROOT) != 0 {
379 let id = crate::pod::PodId(child.subview_pod);
380 pool.save_subview_pass3_and_node(
381 id,
382 child.min_size,
383 child.pref_size,
384 child as *mut LayoutNode<'_, '_> as *mut u8,
385 );
386 pool.mark_subview_subtree_clean(id);
387 }
388 if !child.children.is_empty() {
389 commit_subtree_cache_ptrs(child, pool);
390 }
391 }
392}
393
394#[inline(never)]
397#[allow(clippy::too_many_arguments)]
398pub(crate) fn reflatten_pass4_only<'a, 'b, const MAX_OPS: usize, const MAX_HITS: usize>(
399 arena: &'a ScratchArena,
400 root_node: &mut LayoutNode<'a, 'b>,
401 viewport: Region,
402 interaction: InteractionState,
403 motion: &mut MotionCache,
404 out_ops: &mut SmallVec<DrawOp<'a>, MAX_OPS>,
405 out_hits: &mut SmallVec<HitRegion<'a>, MAX_HITS>,
406) -> u32 {
407 let root = root_node.element;
408 root_node.y = viewport.y;
409 root_node.h = match root {
410 Element::Div(d) => match d.style.height {
411 Length::Px(px) => clamp_h(root, px),
412 Length::Pct(pct) => clamp_h(root, ((viewport.h as i32 * pct as i32) / 100) as i16),
413 Length::Full | Length::Auto => clamp_h(root, viewport.h),
414 },
415 _ => viewport.h,
416 };
417 let mut focus_counter: u16 = 0;
418 let mut has_overlays = false;
419 let mut dropped_ops: u32 = 0;
420 let root_owner = if (root_node.subview_flags & (SUBVIEW_FLAG_ROOT | SUBVIEW_FLAG_OWNER)) != 0 {
421 root_node.subview_pod
422 } else {
423 u16::MAX
424 };
425 motion.begin_frame();
426 pass4_vertical_assign_and_flatten(
427 arena,
428 root_node,
429 root_owner,
430 0x811c_9dc5,
431 (0, 0),
432 viewport,
433 viewport,
434 0,
435 None,
436 255,
437 (0, 0),
438 None,
439 None,
440 interaction,
441 false,
442 false,
443 &mut has_overlays,
444 &mut focus_counter,
445 &mut dropped_ops,
446 motion,
447 out_ops,
448 out_hits,
449 );
450 if has_overlays {
452 pass4_vertical_assign_and_flatten(
453 arena,
454 root_node,
455 root_owner,
456 0x811c_9dc5,
457 (0, 0),
458 viewport,
459 viewport,
460 0,
461 None,
462 255,
463 (0, 0),
464 None,
465 None,
466 interaction,
467 true,
468 false,
469 &mut has_overlays,
470 &mut focus_counter,
471 &mut dropped_ops,
472 motion,
473 out_ops,
474 out_hits,
475 );
476 }
477 motion.end_frame();
479 dropped_ops
480}
481