layout/
layout_box_base.rs1use std::fmt::{Debug, Formatter};
6use std::sync::atomic::{AtomicBool, Ordering};
7
8use app_units::Au;
9use atomic_refcell::AtomicRefCell;
10use layout_api::LayoutDamage;
11use malloc_size_of_derive::MallocSizeOf;
12use servo_arc::Arc;
13use style::properties::ComputedValues;
14
15use crate::context::LayoutContext;
16use crate::dom::{LayoutBox, WeakLayoutBox};
17use crate::formatting_contexts::Baselines;
18use crate::fragment_tree::{BaseFragmentInfo, CollapsedBlockMargins, Fragment, SpecificLayoutInfo};
19use crate::positioned::PositioningContext;
20use crate::sizing::{ComputeInlineContentSizes, InlineContentSizesResult, SizeConstraint};
21use crate::{ConstraintSpace, ContainingBlockSize};
22
23#[derive(MallocSizeOf)]
30pub(crate) struct LayoutBoxBase {
31 pub base_fragment_info: BaseFragmentInfo,
32 pub style: Arc<ComputedValues>,
33 pub cached_inline_content_size:
34 AtomicRefCell<Option<Box<(SizeConstraint, InlineContentSizesResult)>>>,
35 pub outer_inline_content_sizes_depend_on_content: AtomicBool,
36 pub cached_layout_result: AtomicRefCell<Option<Box<CacheableLayoutResultAndInputs>>>,
37 pub fragments: AtomicRefCell<Vec<Fragment>>,
38 pub parent_box: Option<WeakLayoutBox>,
39}
40
41impl LayoutBoxBase {
42 pub(crate) fn new(base_fragment_info: BaseFragmentInfo, style: Arc<ComputedValues>) -> Self {
43 Self {
44 base_fragment_info,
45 style,
46 cached_inline_content_size: AtomicRefCell::default(),
47 outer_inline_content_sizes_depend_on_content: AtomicBool::new(true),
48 cached_layout_result: AtomicRefCell::default(),
49 fragments: AtomicRefCell::default(),
50 parent_box: None,
51 }
52 }
53
54 pub(crate) fn inline_content_sizes(
57 &self,
58 layout_context: &LayoutContext,
59 constraint_space: &ConstraintSpace,
60 layout_box: &impl ComputeInlineContentSizes,
61 ) -> InlineContentSizesResult {
62 let mut cache = self.cached_inline_content_size.borrow_mut();
63 if let Some(cached_inline_content_size) = cache.as_ref() {
64 let (previous_cb_block_size, result) = **cached_inline_content_size;
65 if !result.depends_on_block_constraints ||
66 previous_cb_block_size == constraint_space.block_size
67 {
68 return result;
69 }
70 }
72
73 let result =
74 layout_box.compute_inline_content_sizes_with_fixup(layout_context, constraint_space);
75 *cache = Some(Box::new((constraint_space.block_size, result)));
76 result
77 }
78
79 pub(crate) fn fragments(&self) -> Vec<Fragment> {
80 self.fragments.borrow().clone()
81 }
82
83 pub(crate) fn add_fragment(&self, fragment: Fragment) {
84 self.fragments.borrow_mut().push(fragment);
85 }
86
87 pub(crate) fn set_fragment(&self, fragment: Fragment) {
88 *self.fragments.borrow_mut() = vec![fragment];
89 }
90
91 pub(crate) fn clear_fragments(&self) {
92 self.fragments.borrow_mut().clear();
93 }
94
95 pub(crate) fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
96 self.style = new_style.clone();
97 for fragment in self.fragments.borrow_mut().iter_mut() {
98 if let Some(mut base) = fragment.base_mut() {
99 base.repair_style(new_style);
100 }
101 }
102 }
103
104 #[expect(unused)]
105 pub(crate) fn parent_box(&self) -> Option<LayoutBox> {
106 self.parent_box.as_ref().and_then(WeakLayoutBox::upgrade)
107 }
108
109 pub(crate) fn add_damage(
110 &self,
111 element_damage: LayoutDamage,
112 damage_from_children: LayoutDamage,
113 ) -> LayoutDamage {
114 self.clear_fragments();
115 *self.cached_layout_result.borrow_mut() = None;
116
117 if !element_damage.is_empty() ||
118 damage_from_children.contains(LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES)
119 {
120 *self.cached_inline_content_size.borrow_mut() = None;
121 }
122
123 let mut damage_for_parent = element_damage | damage_from_children;
124
125 damage_for_parent.set(
136 LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES,
137 !element_damage.is_empty() ||
138 (!self.base_fragment_info.is_anonymous() &&
139 self.outer_inline_content_sizes_depend_on_content
140 .load(Ordering::Relaxed)),
141 );
142
143 damage_for_parent
144 }
145}
146
147impl Debug for LayoutBoxBase {
148 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
149 f.debug_struct("LayoutBoxBase").finish()
150 }
151}
152
153#[derive(Clone, MallocSizeOf)]
154pub(crate) struct CacheableLayoutResult {
155 pub fragments: Vec<Fragment>,
156
157 pub content_block_size: Au,
159
160 pub collapsible_margins_in_children: CollapsedBlockMargins,
163
164 pub content_inline_size_for_table: Option<Au>,
168
169 pub baselines: Baselines,
173
174 pub depends_on_block_constraints: bool,
176
177 pub specific_layout_info: Option<SpecificLayoutInfo>,
179}
180
181#[derive(MallocSizeOf)]
183pub(crate) struct CacheableLayoutResultAndInputs {
184 pub result: CacheableLayoutResult,
186
187 pub containing_block_for_children_size: ContainingBlockSize,
190
191 pub positioning_context: PositioningContext,
194}