use std::fmt::{Debug, Formatter};
use std::sync::atomic::{AtomicBool, Ordering};
use app_units::Au;
use atomic_refcell::AtomicRefCell;
use layout_api::LayoutDamage;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
use style::properties::ComputedValues;
use crate::context::LayoutContext;
use crate::dom::{LayoutBox, WeakLayoutBox};
use crate::formatting_contexts::Baselines;
use crate::fragment_tree::{BaseFragmentInfo, CollapsedBlockMargins, Fragment, SpecificLayoutInfo};
use crate::positioned::PositioningContext;
use crate::sizing::{ComputeInlineContentSizes, InlineContentSizesResult, SizeConstraint};
use crate::{ConstraintSpace, ContainingBlockSize};
#[derive(MallocSizeOf)]
pub(crate) struct LayoutBoxBase {
pub base_fragment_info: BaseFragmentInfo,
pub style: Arc<ComputedValues>,
pub cached_inline_content_size:
AtomicRefCell<Option<Box<(SizeConstraint, InlineContentSizesResult)>>>,
pub outer_inline_content_sizes_depend_on_content: AtomicBool,
pub cached_layout_result: AtomicRefCell<Option<Box<CacheableLayoutResultAndInputs>>>,
pub fragments: AtomicRefCell<Vec<Fragment>>,
pub parent_box: Option<WeakLayoutBox>,
}
impl LayoutBoxBase {
pub(crate) fn new(base_fragment_info: BaseFragmentInfo, style: Arc<ComputedValues>) -> Self {
Self {
base_fragment_info,
style,
cached_inline_content_size: AtomicRefCell::default(),
outer_inline_content_sizes_depend_on_content: AtomicBool::new(true),
cached_layout_result: AtomicRefCell::default(),
fragments: AtomicRefCell::default(),
parent_box: None,
}
}
pub(crate) fn inline_content_sizes(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
layout_box: &impl ComputeInlineContentSizes,
) -> InlineContentSizesResult {
let mut cache = self.cached_inline_content_size.borrow_mut();
if let Some(cached_inline_content_size) = cache.as_ref() {
let (previous_cb_block_size, result) = **cached_inline_content_size;
if !result.depends_on_block_constraints ||
previous_cb_block_size == constraint_space.block_size
{
return result;
}
}
let result =
layout_box.compute_inline_content_sizes_with_fixup(layout_context, constraint_space);
*cache = Some(Box::new((constraint_space.block_size, result)));
result
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
self.fragments.borrow().clone()
}
pub(crate) fn add_fragment(&self, fragment: Fragment) {
self.fragments.borrow_mut().push(fragment);
}
pub(crate) fn set_fragment(&self, fragment: Fragment) {
*self.fragments.borrow_mut() = vec![fragment];
}
pub(crate) fn clear_fragments(&self) {
self.fragments.borrow_mut().clear();
}
pub(crate) fn clear_fragments_and_fragment_cache(&self) {
self.fragments.borrow_mut().clear();
*self.cached_layout_result.borrow_mut() = None;
}
pub(crate) fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
self.style = new_style.clone();
for fragment in self.fragments.borrow_mut().iter_mut() {
if let Some(mut base) = fragment.base_mut() {
base.repair_style(new_style);
}
}
}
#[expect(unused)]
pub(crate) fn parent_box(&self) -> Option<LayoutBox> {
self.parent_box.as_ref().and_then(WeakLayoutBox::upgrade)
}
pub(crate) fn add_damage(
&self,
element_damage: LayoutDamage,
damage_from_children: LayoutDamage,
) -> LayoutDamage {
self.clear_fragments_and_fragment_cache();
if !element_damage.is_empty() ||
damage_from_children.contains(LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES)
{
*self.cached_inline_content_size.borrow_mut() = None;
}
let mut damage_for_parent = element_damage | damage_from_children;
damage_for_parent.set(
LayoutDamage::RECOMPUTE_INLINE_CONTENT_SIZES,
!element_damage.is_empty() ||
(!self.base_fragment_info.is_anonymous() &&
self.outer_inline_content_sizes_depend_on_content
.load(Ordering::Relaxed)),
);
damage_for_parent
}
}
impl Debug for LayoutBoxBase {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("LayoutBoxBase").finish()
}
}
#[derive(Clone, MallocSizeOf)]
pub(crate) struct CacheableLayoutResult {
pub fragments: Vec<Fragment>,
pub content_block_size: Au,
pub collapsible_margins_in_children: CollapsedBlockMargins,
pub content_inline_size_for_table: Option<Au>,
pub baselines: Baselines,
pub depends_on_block_constraints: bool,
pub specific_layout_info: Option<SpecificLayoutInfo>,
}
#[derive(MallocSizeOf)]
pub(crate) struct CacheableLayoutResultAndInputs {
pub result: CacheableLayoutResult,
pub containing_block_for_children_size: ContainingBlockSize,
pub positioning_context: PositioningContext,
}