Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
//! What the host asks about the rows of its document's layout: what a layout node asks about its own row (its flags,
//! links and style), the row a node is bound to, and the shape and paint facts of a row. The host answers from the rows
//! the document's render state published, which it asks for again only after the host wrote them, so none of these
//! reaches the arena. What a row is and the row a node is bound to are read through [`RowIdentities`], which the host
//! reads again only after a write that changed them: installing a style, which a style update does for every element
//! it restyles, does not.
//!
//! The entries that find the layout node of an element, a document or a row take the read the host began, and the
//! reads of a node found so spend that read (see [`NodeRead`]).
use super::node_data::{CompositorAnimationFrameKind, FfiNodeLink, NodeSlotId};
use super::row_reads::{RowIdentities, RowSnapshot};
use crate::css::style::tree::StyleNodeID;
use crate::painting::paint_read::{GeometryRead, PaintRead, PaintSource};
use crate::render_state::{BegunRead, DocumentHost, RenderWait};
use std::ffi::c_void;
use std::rc::Rc;
crate::render_state::held_node_entries!();
/// Answers `answer` from the render state of `host`'s document and `args`, as of every write the host made, spending
/// `wait`.
pub(crate) fn read_arena<A, R>(
host: &DocumentHost,
wait: impl RenderWait,
args: A,
answer: fn(&mut super::LayoutNodeArena, A) -> R,
) -> R {
host.ask(wait, |state| answer(state.arena_mut(), args))
}
/// The rows of `host`'s document as of every write the host made, spending `wait`.
fn rows(host: &DocumentHost, wait: impl RenderWait) -> Rc<RowSnapshot> {
host.fresh_rows(wait)
}
/// The style record each row of `host`'s document has, as of every write the host made, for a read of the row `id`,
/// spending `wait`.
fn styles(host: &DocumentHost, id: NodeSlotId, wait: impl RenderWait) -> crate::layout::row_reads::RowStyles {
host.row_styles_of(id, wait)
}
/// What each row of `host`'s document is, and the row each node is bound to, as of every write the host made, spending
/// `wait`.
fn identities(host: &DocumentHost, wait: impl RenderWait) -> RowIdentities {
host.row_identities(wait)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_flags(host: &DocumentHost, id: NodeSlotId) -> u32 {
rows(host, node_read()).flags(id)
}
/// The row's flags that say what node it stands for (see [`super::node_data::NodeFlag::IDENTITY`]); every other flag
/// reads as unset.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_identity_flags(host: &DocumentHost, id: NodeSlotId) -> u32 {
identities(host, node_read()).identity_flags(id)
}
/// The layout node of the row `id` links to, made if nothing has asked for it yet, or null.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_link_shell(host: &DocumentHost, id: NodeSlotId, link: FfiNodeLink) -> *mut c_void {
let linked = rows(host, node_read()).link(id, link);
shell_of(host, node_read(), (!linked.is_invalid()).then_some(linked))
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_generated_for(host: &DocumentHost, id: NodeSlotId) -> u8 {
identities(host, node_read()).generated_for(id)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_has_compositor_animation_frame(
host: &DocumentHost,
id: NodeSlotId,
kind: CompositorAnimationFrameKind,
) -> bool {
rows(host, node_read()).has_compositor_animation_frame(id, kind)
}
/// The style node of the row, or 0 for a row that is gone or carries none.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_style_node(host: &DocumentHost, id: NodeSlotId) -> u32 {
identities(host, node_read())
.style_node(id)
.map_or(0, crate::css::style::tree::StyleNodeID::raw)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_style_record(host: &DocumentHost, id: NodeSlotId) -> u64 {
styles(host, id, node_read()).style_record(id)
}
/// Whether the arena derived the style record of the row `id`, which only a job changes but for the layout node's own
/// writes, and a job tells the layout node of.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_style_is_derived(host: &DocumentHost, id: NodeSlotId) -> bool {
styles(host, id, node_read()).style_is_derived(id)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_style(host: &DocumentHost, id: NodeSlotId) -> *const c_void {
styles(host, id, node_read())
.style(id)
.map_or(std::ptr::null(), |style| style.as_ptr())
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_is_atomic_inline(host: &DocumentHost, id: NodeSlotId) -> bool {
rows(host, node_read()).is_atomic_inline(id)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_is_fragmented_inline(host: &DocumentHost, id: NodeSlotId) -> bool {
rows(host, node_read()).is_fragmented_inline(id)
}
/// Answers `read` from the rows of `host`'s document through the paint side's reads, for a layout node the host holds.
fn read_rows<R>(host: &DocumentHost, read: impl FnOnce(&PaintSource<'_>) -> R) -> R {
host.read_rows(node_read(), false, read)
}
/// The layout node of the live row `id`, made if nothing has asked for it yet, or null for none, spending `wait`.
fn shell_of(host: &DocumentHost, wait: impl RenderWait, id: Option<NodeSlotId>) -> *mut c_void {
let Some(id) = id else {
return std::ptr::null_mut();
};
bound_shell(host, wait, |_| Some(id))
}
/// The layout node of the live row `bound` finds among what each row is and the row each node is bound to, made if
/// nothing has asked for it yet, or null for none, spending `wait`: the identities are read once for both.
fn bound_shell(
host: &DocumentHost,
wait: impl RenderWait,
bound: impl FnOnce(&RowIdentities) -> Option<NodeSlotId>,
) -> *mut c_void {
let facts = {
let identities = identities(host, wait);
bound(&identities).and_then(|id| identities.shell_facts(id))
};
let Some(facts) = facts else {
return std::ptr::null_mut();
};
// The factory reads the rows again, through the shared host.
host.host_tables().shell_of(facts)
}
/// The layout node of the row the element or text node with `style_node` is bound to, made if nothing has asked for it
/// yet, or null if the node has no row.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_bound_shell(host: &DocumentHost, read: &BegunRead, style_node: u32) -> *mut c_void {
let Some(style_node) = StyleNodeID::from_raw(style_node) else {
return std::ptr::null_mut();
};
bound_shell(host, read, |identities| identities.bound_row(style_node))
}
/// Whether a row of `host`'s document may make an element contentful, as paint timing defines it: by holding text that
/// is not all white space or replaced content, or by observing an image its style holds. It errs towards yes, as the
/// rows do not say whether an image is available, and it makes no layout node. It follows `is_contentful()` of
/// PaintTiming.cpp.
#[unsafe(no_mangle)]
pub extern "C" fn layout_rows_may_be_contentful(host: &DocumentHost, read: &BegunRead) -> bool {
// NB: The rows come first, as bringing them up to date may give a row its image observers.
let rows = rows(host, read);
!host.host_tables().image_observer_sets.borrow().is_empty() || rows.any_row_may_be_contentful()
}
/// Whether a text row of `host`'s document may have been given new rendered text since paint timing last took the text
/// rows, which it answers without asking the render state: it errs towards yes while a frame flies.
#[unsafe(no_mangle)]
pub extern "C" fn layout_text_rows_may_await_first_paint(host: &DocumentHost) -> bool {
host.text_rows_may_await_first_paint()
}
/// Hands `push_row` each text row of `host`'s document that was given new rendered text since the last take, once and
/// as the layout node of its text node (see [`crate::layout::LayoutNodeArena::take_text_rows_for_first_paint`]).
///
/// # Safety
///
/// `push_row` must be safe to call with `context`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn layout_take_text_rows_for_first_paint(
host: &DocumentHost,
read: &BegunRead,
context: *mut c_void,
push_row: unsafe extern "C" fn(*mut c_void, NodeSlotId),
) {
let rows = read_arena(host, read, (), |arena, ()| arena.take_text_rows_for_first_paint());
for row in rows {
// SAFETY: The caller vouches for calling push_row with context.
unsafe { push_row(context, row) };
}
}
/// Stops noting the text rows of `host`'s document for paint timing, which will never take them again.
#[unsafe(no_mangle)]
pub extern "C" fn layout_stop_noting_text_rows_for_first_paint(host: &DocumentHost, read: &BegunRead) {
read_arena(host, read, (), |arena, ()| {
arena.stop_noting_text_rows_for_first_paint();
});
}
/// The content size of the committed box of the row the element with `style_node` is bound to, or none where the
/// element has no row or its row no committed box. It makes no layout node, so a host callback a layout round makes
/// on the render owner may ask it.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_bound_committed_content_size(
host: &DocumentHost,
read: &BegunRead,
style_node: u32,
size: &mut crate::layout::FfiCssPixelSize,
) -> bool {
let Some(style_node) = StyleNodeID::from_raw(style_node) else {
return false;
};
let Some(row) = identities(host, read).bound_row(style_node) else {
return false;
};
host.read_rows(read, false, |rows| {
if !rows.paintable_row_is_populated(row) {
return false;
}
*size = crate::painting::paintable_geometry::committed_content_size(rows, row);
true
})
}
/// The layout node of the row the pseudo-element of kind `generated_for` on the element with `style_node` is bound to,
/// made if nothing has asked for it yet, or null.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_bound_pseudo_element_shell(
host: &DocumentHost,
read: &BegunRead,
style_node: u32,
generated_for: u8,
) -> *mut c_void {
let Some(style_node) = StyleNodeID::from_raw(style_node) else {
return std::ptr::null_mut();
};
bound_shell(host, read, |identities| {
identities.bound_pseudo_element_row(style_node, generated_for)
})
}
/// The layout node of the viewport row the document is bound to, made if nothing has asked for it yet, or null.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_bound_viewport_shell(host: &DocumentHost, read: &BegunRead) -> *mut c_void {
bound_shell(host, read, RowIdentities::bound_viewport_row)
}
/// The read of the layout node the host holds, which the host lends the entries it calls about the node's document.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_read_of_held_node(host: &DocumentHost) -> *const BegunRead {
host.begun_read_of_held_node(node_read())
}
/// Whether the row `id` is the one the element or text node with `style_node` is bound to, or, with no style node, the
/// viewport row the document is bound to.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_is_bound_to(host: &DocumentHost, id: NodeSlotId, style_node: u32) -> bool {
let identities = identities(host, node_read());
let bound = match StyleNodeID::from_raw(style_node) {
Some(style_node) => identities.bound_row(style_node),
None => identities.bound_viewport_row(),
};
bound == Some(id)
}
/// The layout node of the box whose content box the row is laid out against, or null.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_containing_block_shell_if_live(host: &DocumentHost, id: NodeSlotId) -> *mut c_void {
let containing_block = read_rows(host, |rows| rows.node_containing_block_if_live(id));
shell_of(host, node_read(), containing_block)
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_establishes_an_absolute_positioning_containing_block(
host: &DocumentHost,
id: NodeSlotId,
) -> bool {
read_rows(host, |rows| {
crate::painting::style_queries::establishes_positioning_containing_blocks(rows, id).0
})
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_establishes_a_fixed_positioning_containing_block(
host: &DocumentHost,
id: NodeSlotId,
) -> bool {
read_rows(host, |rows| {
crate::painting::style_queries::establishes_positioning_containing_blocks(rows, id).1
})
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_any_ancestor_establishes_a_fixed_position_containing_block(
host: &DocumentHost,
id: NodeSlotId,
) -> bool {
read_rows(host, |rows| {
crate::painting::style_queries::any_ancestor_establishes_a_fixed_position_containing_block(rows, id)
})
}
/// The layout node of the row whose paintable row `slot` is, or null when the row has none.
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_paintable_layout_node_shell(
host: &DocumentHost,
read: &BegunRead,
slot: NodeSlotId,
) -> *mut c_void {
let populated = rows(host, read).paintable.paintable_row_is_populated(slot);
shell_of(host, read, populated.then_some(slot))
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_paintable_is_positioned(host: &DocumentHost, slot: NodeSlotId) -> bool {
read_rows(host, |rows| {
rows.paintable_row_is_populated(slot) && crate::painting::style_queries::is_positioned(rows, slot)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_paintable_is_chrome_mirrored(host: &DocumentHost, slot: NodeSlotId) -> bool {
read_rows(host, |rows| {
crate::painting::chrome_geometry::is_chrome_mirrored(rows, slot)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn layout_row_has_css_transform(host: &DocumentHost, id: NodeSlotId) -> bool {
read_rows(host, |rows| {
rows.node_style_if_live(id)
.is_some_and(|style| crate::painting::style_queries::has_css_transform(rows, id, style))
})
}