Skip to content

Truly independent web browser

7.2 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

//! The FFI entry points of the parent module that mint a main thread token. The token's marker
//! can be made only here, and this module is private, so the parent's own code can neither mint
//! a token nor call an entry that does.

use super::*;
use crate::render_state::BegunRead;

crate::stage::main_thread_ffi_entries!();

/// Runs the document's layout update to a fixed point: style, then the layout tree build, then
/// either a partial relayout of the registered boundaries or a full pass, until nothing is
/// pending. The document-side steps run through the registered layout update host.
///
/// # Safety
///
/// `host` must be a live document host with registered layout and layout update hosts, on its document's thread
/// between `document_host_begin_update_layout` and its end.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn render_state_update_layout(
    host: &DocumentHost,
    read: &BegunRead,
    inputs: &FfiLayoutUpdateInputs,
) {
    // SAFETY: Guaranteed by the entry point's contract.
    let main_thread = unsafe { main_thread(host) };
    abort_on_panic(|| {
        update_layout(&main_thread, host, read, inputs);
        attach_owed_image_resources(&main_thread, host, read);
    });
}

/// Attaches the image resources the tree builds of a layout update owe, once its layout is done.
fn attach_owed_image_resources(main_thread: &MainThread, host: &DocumentHost, read: &BegunRead) {
    host.host_tables()
        .layout_update_host
        .get()
        .expect("the document has no layout update host")
        .attach_owed_image_resources(main_thread, host, read);
}

/// Pays what the layout round a frame ran owes the host, where the frame flew with one, landing it first with `read`:
/// the host's teardown of the layout tree pays it before the tree goes.
///
/// # Safety
///
/// `host` must be a live document host with registered layout and layout update hosts, on its document's thread.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn render_state_pay_flown_round(host: &DocumentHost, read: &BegunRead) {
    // SAFETY: Guaranteed by the entry point's contract.
    let main_thread = unsafe { main_thread(host) };
    abort_on_panic(|| {
        host.take_frame_in_with(read);
        if let Some(round) = host.take_flown_round() {
            round.pay(&main_thread, host, read);
        }
    });
}

/// Seals the plan of the clock lane of `host`'s document for the tasks after a rendering update, in `read`: the
/// elements whose running animations a tick samples, the monotonic time in milliseconds at which the document's
/// timestamps are zero, the timestamp of the next event of the animations, past which a tick samples nothing, and the
/// timestamp at which the sampled animations of the document timeline have all ended, the scroll timelines a tick
/// samples where the compositor has scrolled to, as they are now, and what the lane's hover reads, where its ticks
/// follow the pointer. `update` is the serial number of the rendering update. A document whose layout is not up to date
/// gets no plan.
///
/// # Safety
///
/// As for [`render_state_update_layout`], with no layout update running, and `elements` must hold `count` style nodes
/// and `scroll_timelines` `scroll_timeline_count` live timelines. `hover` must be null or point at inputs as
/// `HoverPlan::from_ffi` takes them.
#[unsafe(no_mangle)]
#[allow(clippy::too_many_arguments)]
pub unsafe extern "C" fn render_state_seal_clock_plan(
    host: &DocumentHost,
    read: &BegunRead,
    elements: *const u32,
    count: usize,
    time_origin: f64,
    last_end: f64,
    scroll_timelines: *const *const crate::animations::timeline::TimelineCell,
    scroll_timeline_count: usize,
    hover: *const crate::render_state::FfiHoverPlanInputs,
    update: u64,
) {
    // SAFETY: Guaranteed by the entry point's contract.
    let main_thread = unsafe { main_thread(host) };
    // SAFETY: Guaranteed by the caller.
    let elements = unsafe { ak::slice_from_raw_parts(elements, count) };
    // SAFETY: Guaranteed by the caller.
    let scroll_timelines = unsafe { ak::slice_from_raw_parts(scroll_timelines, scroll_timeline_count) };
    abort_on_panic(|| {
        let elements: Vec<_> = elements
            .iter()
            .filter_map(|&element| StyleNodeID::from_raw(element))
            .collect();
        // SAFETY: Guaranteed by the caller.
        let mut hover = unsafe { hover.as_ref() }.map(|inputs| {
            let scroll_offsets = host.scroll_state().device_offsets().to_vec();
            unsafe { crate::render_state::HoverPlan::from_ffi(inputs, scroll_offsets) }
                .with_style_inputs(host.style_inputs())
        });
        let round = seal_clock_round(&main_thread, host, read);
        if elements.is_empty()
            && round.is_some()
            && let Some(hover) = &mut hover
        {
            host.run(read, false, |state| hover.seal_read_only_frame(state));
        }
        host.seal_clock_plan(
            round.map(|round| {
                crate::render_state::ClockPlan::new(
                    elements,
                    time_origin,
                    last_end,
                    scroll_timelines
                        .iter()
                        // SAFETY: Guaranteed by the caller.
                        .filter_map(|&timeline| unsafe { &*timeline }.clock_snapshot())
                        .collect(),
                    round,
                    hover,
                )
            }),
            update,
        );
    });
}

/// Lets the first round of the layout of a rendering update whose style is up to date fly beside the host, in `read`,
/// where the document's layout is not up to date and `blocker` is none: the host's next layout update pays it first.
/// Answers whether the round flies.
///
/// # Safety
///
/// As for [`render_state_update_layout`], with no layout update running and no frame of the document in flight.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn render_state_let_first_layout_round_fly(
    host: &DocumentHost,
    read: &BegunRead,
    inputs: &FfiLayoutUpdateInputs,
    blocker: crate::painting::ffi::FfiFlightBlocker,
) -> bool {
    // SAFETY: Guaranteed by the entry point's contract.
    let main_thread = unsafe { main_thread(host) };
    let Some(license) = FlightLicense::for_blocker(blocker) else {
        return false;
    };
    abort_on_panic(|| fly_first_round(&main_thread, host, read, inputs, &license))
}

/// Takes in the layout round that flew in the frame of `host`'s document, in `read`, and pays it, where it left the
/// document laid out as of the frame. Answers whether it did; what the host wrote since the frame flew stays the next
/// layout update's either way.
///
/// # Safety
///
/// As for [`render_state_update_layout`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn render_state_take_flown_layout_in(host: &DocumentHost, read: &BegunRead) -> bool {
    // SAFETY: Guaranteed by the entry point's contract.
    let main_thread = unsafe { main_thread(host) };
    abort_on_panic(|| {
        let laid_out = take_flown_layout_in(&main_thread, host, read);
        attach_owed_image_resources(&main_thread, host, read);
        laid_out
    })
}