Skip to content

Truly independent web browser

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

use crate::layout::replaced_sizing::SizeWithAspectRatio;
use crate::painting::host::{
    FfiCanvasPaintFacts, FfiFormControlPaintFacts, FfiNavigableContainerPaintFacts, FfiReplacedImagePaintFacts,
    FfiVideoPaintFacts, FfiVideoRepresentation,
};
use crate::painting::image_content::ImageContent;
use crate::painting::record::change_ticks::RowPaintChange;
use libgfx_rust::image_frame::ImageFrameHandle;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct VideoFrameFacts {
    pub src_width: i32,
    pub src_height: i32,
    pub sink_handle: u64,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum VideoPaintFacts {
    VideoFrame(Option<VideoFrameFacts>),
    PosterFrame(Option<ImageFrameHandle>),
    TransparentBlack,
}

impl Default for VideoPaintFacts {
    fn default() -> Self {
        Self::VideoFrame(None)
    }
}

impl VideoPaintFacts {
    /// # Safety
    ///
    /// `facts.poster_frame` must be null or point to a live `Gfx::DecodedImageFrame`.
    pub(crate) unsafe fn from_ffi(facts: &FfiVideoPaintFacts) -> Self {
        match facts.representation {
            FfiVideoRepresentation::VideoFrame => Self::VideoFrame(facts.has_video_frame.then_some(VideoFrameFacts {
                src_width: facts.video_src_width,
                src_height: facts.video_src_height,
                sink_handle: facts.video_sink_handle,
            })),
            FfiVideoRepresentation::PosterFrame => Self::PosterFrame(
                (!facts.poster_frame.is_null()).then(|| unsafe { ImageFrameHandle::retain(facts.poster_frame) }),
            ),
            FfiVideoRepresentation::TransparentBlack => Self::TransparentBlack,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ImagePaintFacts {
    pub natural: SizeWithAspectRatio,
    pub content: ImageContent,
    /// How an SVG image element fits the image into its rectangle, in the encoding of its `preserveAspectRatio`
    /// attribute facts.
    pub preserve_aspect_ratio_align: u8,
    pub preserve_aspect_ratio_meet_or_slice: u8,
}

impl Default for ImagePaintFacts {
    fn default() -> Self {
        Self {
            natural: SizeWithAspectRatio::default(),
            content: ImageContent::default(),
            preserve_aspect_ratio_align: crate::layout::svg_formatting_context::PRESERVE_ASPECT_RATIO_X_MID_Y_MID,
            preserve_aspect_ratio_meet_or_slice: crate::layout::svg_formatting_context::MEET_OR_SLICE_MEET,
        }
    }
}

impl ImagePaintFacts {
    /// # Safety
    ///
    /// `facts.content.frame` must be null or point to a live `Gfx::DecodedImageFrame`.
    pub(crate) unsafe fn from_ffi(facts: &FfiReplacedImagePaintFacts) -> Self {
        Self {
            natural: SizeWithAspectRatio::from_ffi(&facts.natural),
            content: unsafe { ImageContent::from_ffi(&facts.content) },
            preserve_aspect_ratio_align: facts.preserve_aspect_ratio_align,
            preserve_aspect_ratio_meet_or_slice: facts.preserve_aspect_ratio_meet_or_slice,
        }
    }
}

/// Each replaced row's paint facts. A publication shares the table, so a write copies it only while
/// a publication still holds it.
pub(crate) type ReplacedPaintFactsTable =
    crate::cow_map::CowMap<crate::layout::node_data::NodeSlotId, ReplacedPaintFacts>;

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ReplacedPaintFacts {
    FormControl(FfiFormControlPaintFacts),
    Canvas(FfiCanvasPaintFacts),
    NavigableContainer(FfiNavigableContainerPaintFacts),
    Image(ImagePaintFacts),
    Video(VideoPaintFacts),
    FrameSet(std::sync::Arc<crate::css::style::tree::FrameSetLayout>),
}

impl ReplacedPaintFacts {
    /// What changes of a row where its facts change to these. A canvas or a navigable container caches what it paints
    /// and is hit-tested with beside its row, which goes with the facts. Whether a navigable container's content is
    /// composited also decides which wheel region its scroll metadata records.
    pub(crate) fn paint_change_when_changed(&self) -> RowPaintChange {
        match self {
            Self::FormControl(_) => RowPaintChange::DRAWING,
            Self::Canvas(_) => RowPaintChange::DRAWING | RowPaintChange::HIT_TESTING,
            Self::NavigableContainer(_) => {
                RowPaintChange::DRAWING | RowPaintChange::HIT_TESTING | RowPaintChange::SCROLL_METADATA
            }
            Self::Image(_) | Self::Video(_) | Self::FrameSet(_) => RowPaintChange::DRAWING,
        }
    }

    pub(crate) fn frameset(self) -> Option<std::sync::Arc<crate::css::style::tree::FrameSetLayout>> {
        match self {
            Self::FrameSet(layout) => Some(layout),
            _ => None,
        }
    }

    pub(crate) fn image(self) -> Option<ImagePaintFacts> {
        match self {
            Self::Image(facts) => Some(facts),
            _ => None,
        }
    }

    pub(crate) fn video(self) -> Option<VideoPaintFacts> {
        match self {
            Self::Video(facts) => Some(facts),
            _ => None,
        }
    }

    pub(crate) fn form_control(self) -> Option<FfiFormControlPaintFacts> {
        match self {
            Self::FormControl(facts) => Some(facts),
            _ => None,
        }
    }

    pub(crate) fn navigable_container(self) -> Option<FfiNavigableContainerPaintFacts> {
        match self {
            Self::NavigableContainer(facts) => Some(facts),
            _ => None,
        }
    }
}