Skip to content

Truly independent web browser

LibCompositing+Compositor: Pass clip paths to the painter as contours

Aliaksandr Kalenikaed8c563c830
diff --git a/Libraries/LibCompositing/Rust/build.rs b/Libraries/LibCompositing/Rust/build.rs
index 55c05e24323..2ac9ec79760 100644
--- a/Libraries/LibCompositing/Rust/build.rs
+++ b/Libraries/LibCompositing/Rust/build.rs
@@ -106,6 +106,7 @@ fn expose_shared_abi_types_as_cpp_types(config: &mut cbindgen::Config) {
"VideoSinkResourceId",
"Canvas2DPaintKind",
"Canvas2DPatternRepetition",
+ "PathGeometryData",
]
.map(String::from),
);
@@ -157,6 +158,7 @@ fn expose_shared_abi_types_as_cpp_types(config: &mut cbindgen::Config) {
("Canvas2DPatternRepetition", "Compositing::Canvas2DPatternRepetition"),
("CapStyle", "Gfx::Path::CapStyle"),
("JoinStyle", "Gfx::Path::JoinStyle"),
+ ("PathGeometryData", "Gfx::FFI::PathGeometryData"),
] {
config.export.rename.insert(rust_name.to_string(), cpp_name.to_string());
}
diff --git a/Libraries/LibCompositing/Rust/src/host/replay.rs b/Libraries/LibCompositing/Rust/src/host/replay.rs
index 9c09769ebe2..402b4ac24c0 100644
--- a/Libraries/LibCompositing/Rust/src/host/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/host/replay.rs
@@ -7,6 +7,7 @@
use crate::display_list::commands::{DisplayListCommandType, EffectNodeIndex, ReplayClip, ReplayLayer, ReplayMask};
use crate::display_list::replay::ReplayPainter;
use libgfx_rust::path::OwnedPath;
+use libgfx_rust::path::ffi::PathGeometryData;
use libgfx_rust::{AffineTransform, FloatMatrix4x4, FloatVector3, IntRect, WindingRule};
use std::ffi::c_void;
@@ -18,8 +19,8 @@ pub struct FfiDisplayListReplayCallbacks {
pub set_matrix: unsafe extern "C" fn(*mut c_void, *const FloatMatrix4x4),
pub would_be_fully_clipped_by_painter: unsafe extern "C" fn(*mut c_void, IntRect) -> bool,
pub push_clip: unsafe extern "C" fn(*mut c_void, *const ReplayClip),
- /// Takes the path, the identity of the path, and the fill rule.
- pub push_clip_path: unsafe extern "C" fn(*mut c_void, *const c_void, u64, WindingRule),
+ /// Takes the contours of the path, the identity of the path, and the fill rule.
+ pub push_clip_path: unsafe extern "C" fn(*mut c_void, *const PathGeometryData, u64, WindingRule),
pub push_layer: unsafe extern "C" fn(*mut c_void, *const ReplayLayer),
// Arguments: the mask, and the layer whose backdrop filter the mask's layer starts with, or null.
pub push_mask: unsafe extern "C" fn(*mut c_void, *const ReplayMask, *const ReplayLayer),
@@ -54,8 +55,9 @@ impl ReplayPainter for FfiDisplayListReplayCallbacks {
}
fn push_clip_path(&mut self, path: &OwnedPath, winding_rule: WindingRule) {
- // SAFETY: The C++ painter reads the Gfx::Path synchronously; the tree keeps it alive.
- unsafe { (self.push_clip_path)(self.context, path.as_raw(), path.identity(), winding_rule) };
+ let data = path.geometry_data();
+ // SAFETY: The C++ painter reads the contours synchronously; the tree keeps the path alive.
+ unsafe { (self.push_clip_path)(self.context, &raw const data, path.identity(), winding_rule) };
}
fn push_layer(&mut self, layer: &ReplayLayer) {
diff --git a/Libraries/LibGfx/Forward.h b/Libraries/LibGfx/Forward.h
index 3d4bbbe2065..03abfe9872b 100644
--- a/Libraries/LibGfx/Forward.h
+++ b/Libraries/LibGfx/Forward.h
@@ -24,6 +24,7 @@ class Palette;
class YUVData;
class PaletteImpl;
class Path;
+struct PathData;
class SharedImage;
class SharedImageBuffer;
class ShareableBitmap;
diff --git a/Libraries/LibGfx/Path.cpp b/Libraries/LibGfx/Path.cpp
index b0c11cc38a6..85b187b3ca6 100644
--- a/Libraries/LibGfx/Path.cpp
+++ b/Libraries/LibGfx/Path.cpp
@@ -228,10 +228,8 @@ String Path::to_svg_string() const
return string;
}
-PathData Path::data() const
+PathData PathData::from_ffi(FFI::PathGeometryData const& data)
{
- FFI::PathGeometryData data;
- FFI::path_geometry_data(m_geometry, &data);
return {
.verbs = { reinterpret_cast<PathVerb const*>(data.verbs), data.verb_count },
.points = { reinterpret_cast<FloatPoint const*>(data.points), data.point_count },
@@ -240,6 +238,13 @@ PathData Path::data() const
};
}
+PathData Path::data() const
+{
+ FFI::PathGeometryData data;
+ FFI::path_geometry_data(m_geometry, &data);
+ return PathData::from_ffi(data);
+}
+
bool Path::operator==(Path const& other) const
{
return FFI::path_geometry_equals(m_geometry, other.m_geometry);
@@ -521,6 +526,7 @@ void* ladybird_gfx_path_stroke_to_fill(void const*, float thickness, i32 cap_sty
void ladybird_gfx_path_set_fill_type(void*, i32);
void* ladybird_gfx_path_create_from_glyph_runs(Gfx::FFI::FfiGlyphRun const* runs, size_t run_count);
void* ladybird_gfx_path_place_glyph_runs_along(void const* path, Gfx::FFI::FfiGlyphRun const* runs, size_t run_count, float offset);
+void ladybird_gfx_path_data(void const*, Gfx::FFI::PathGeometryData*);
void ladybird_gfx_font_glyph_outlines(void const* font, Gfx::FFI::DrawGlyph const* glyphs, size_t glyph_count, void (*visit)(void*, size_t, Gfx::FFI::PathGeometryData const*), void* context);
}
@@ -714,6 +720,20 @@ extern "C" void* ladybird_gfx_path_stroke_to_fill(void const* path, float thickn
return new Gfx::Path(static_cast<Gfx::Path const*>(path)->stroke_to_fill(style));
}
+extern "C" void ladybird_gfx_path_data(void const* path, Gfx::FFI::PathGeometryData* out_data)
+{
+ auto data = static_cast<Gfx::Path const*>(path)->data();
+ *out_data = {
+ .verbs = reinterpret_cast<u8 const*>(data.verbs.data()),
+ .verb_count = data.verbs.size(),
+ .points = reinterpret_cast<Gfx::FFI::FloatPoint const*>(data.points.data()),
+ .point_count = data.points.size(),
+ .conic_weights = data.conic_weights.data(),
+ .conic_weight_count = data.conic_weights.size(),
+ .fill_rule = static_cast<u8>(data.winding_rule),
+ };
+}
+
extern "C" bool ladybird_gfx_path_equals(void const* a, void const* b)
{
return *static_cast<Gfx::Path const*>(a) == *static_cast<Gfx::Path const*>(b);
diff --git a/Libraries/LibGfx/Path.h b/Libraries/LibGfx/Path.h
index 57b65ecbb2e..a57734f4ab1 100644
--- a/Libraries/LibGfx/Path.h
+++ b/Libraries/LibGfx/Path.h
@@ -25,6 +25,7 @@ namespace Gfx {
namespace FFI {
struct PathGeometry;
+struct PathGeometryData;
}
@@ -51,6 +52,8 @@ enum class PathVerb : u8 {
// The contours of a path, valid until the path changes or is destroyed. A path with a point that is not finite has
// none.
struct PathData {
+ static PathData from_ffi(FFI::PathGeometryData const&);
+
ReadonlySpan<PathVerb> verbs;
ReadonlySpan<FloatPoint> points;
ReadonlySpan<float> conic_weights;
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rs
index bf90df18f8a..29d2c9daf92 100644
--- a/Libraries/LibGfx/Rust/src/path.rs
+++ b/Libraries/LibGfx/Rust/src/path.rs
@@ -69,6 +69,7 @@ unsafe extern "C" {
run_count: usize,
offset: f32,
) -> *mut c_void;
+ fn ladybird_gfx_path_data(path: *const c_void, out_data: *mut ffi::PathGeometryData);
fn ladybird_gfx_font_glyph_outlines(
font: *const c_void,
glyphs: *const crate::text_layout::DrawGlyph,
@@ -825,6 +826,16 @@ impl OwnedPath {
unsafe { ladybird_gfx_path_append_svg_string(self.raw.as_ptr(), append_to_string, (&raw mut output).cast()) };
output
}
+
+ /// The contours of the path, as C++ reads them, valid until the path changes or is dropped.
+ pub fn geometry_data(&self) -> ffi::PathGeometryData {
+ let mut data = std::mem::MaybeUninit::uninit();
+ // SAFETY: The path is live for the duration of the call, which writes the data.
+ unsafe {
+ ladybird_gfx_path_data(self.raw.as_ptr(), data.as_mut_ptr());
+ data.assume_init()
+ }
+ }
}
/// A copy of the path, which has an identity of its own.
diff --git a/Services/Compositor/DisplayListPlayer.cpp b/Services/Compositor/DisplayListPlayer.cpp
index 8845d039cfa..db6d55c7e40 100644
--- a/Services/Compositor/DisplayListPlayer.cpp
+++ b/Services/Compositor/DisplayListPlayer.cpp
@@ -78,7 +78,7 @@ struct DisplayListPlayer::ReplayCallbacks {
return static_cast<DisplayListPlayer*>(context)->would_be_fully_clipped_by_painter(rect);
},
.push_clip = [](void* context, ReplayClip const* clip) { static_cast<DisplayListPlayer*>(context)->push_clip(*clip); },
- .push_clip_path = [](void* context, void const* path, u64 path_identity, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(*static_cast<Gfx::Path const*>(path), path_identity, winding_rule); },
+ .push_clip_path = [](void* context, Gfx::FFI::PathGeometryData const* path, u64 path_identity, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(Gfx::PathData::from_ffi(*path), path_identity, winding_rule); },
.push_layer = [](void* context, ReplayLayer const* layer) { static_cast<DisplayListPlayer*>(context)->push_layer(*layer); },
.push_mask = [](void* context, ReplayMask const* mask, ReplayLayer const* backdrop) { static_cast<DisplayListPlayer*>(context)->push_mask(*mask, backdrop); },
.pop_mask = [](void* context, ReplayMask const* mask, EffectNodeIndex effect) { static_cast<DisplayListPlayer*>(context)->pop_mask(*mask, effect); },
diff --git a/Services/Compositor/DisplayListPlayer.h b/Services/Compositor/DisplayListPlayer.h
index 94069386b80..d9c078f5b24 100644
--- a/Services/Compositor/DisplayListPlayer.h
+++ b/Services/Compositor/DisplayListPlayer.h
@@ -71,7 +71,7 @@ private:
virtual void push_clip(Compositing::ReplayClip const&) = 0;
// A path stays the same for as long as it has its identity.
- virtual void push_clip_path(Gfx::Path const&, u64 path_identity, Gfx::WindingRule) = 0;
+ virtual void push_clip_path(Gfx::PathData const&, u64 path_identity, Gfx::WindingRule) = 0;
virtual void push_clip_path_bytes(ReadonlyBytes, Gfx::WindingRule) = 0;
virtual void push_transform(Gfx::AffineTransform const&) = 0;
virtual void push_layer(Compositing::ReplayLayer const&) = 0;
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index 195270dda88..36c44ee9b72 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -1528,7 +1528,7 @@ void DisplayListPlayerSkia::push_clip(ReplayClip const& clip)
clip_to_rounded_rect(canvas, clip.rect, clip.corner_radii, clip.mode == ClipMode::Difference ? SkClipOp::kDifference : SkClipOp::kIntersect);
}
-void DisplayListPlayerSkia::push_clip_path(Gfx::Path const& path, u64 path_identity, Gfx::WindingRule winding_rule)
+void DisplayListPlayerSkia::push_clip_path(Gfx::PathData const& path, u64 path_identity, Gfx::WindingRule winding_rule)
{
surface().canvas().save();
// A path that stays the same keeps one Skia path, so caches that are keyed on it work.
diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.h
index ddfd3da6c9b..cc990bd7956 100644
--- a/Services/Compositor/DisplayListPlayerSkia.h
+++ b/Services/Compositor/DisplayListPlayerSkia.h
@@ -67,7 +67,7 @@ private:
bool would_be_fully_clipped_by_painter(Gfx::IntRect) const override;
void push_clip(Compositing::ReplayClip const&) override;
- void push_clip_path(Gfx::Path const&, u64 path_identity, Gfx::WindingRule) override;
+ void push_clip_path(Gfx::PathData const&, u64 path_identity, Gfx::WindingRule) override;
void push_clip_path_bytes(ReadonlyBytes, Gfx::WindingRule) override;
void push_transform(Gfx::AffineTransform const&) override;
void push_layer(Compositing::ReplayLayer const&) override;
diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cpp
index 2eabda2219e..44180cddbca 100644
--- a/Services/Compositor/DisplayListRasterCache.cpp
+++ b/Services/Compositor/DisplayListRasterCache.cpp
@@ -442,7 +442,7 @@ sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage c
return blob;
}
-SkPath DisplayListRasterCache::clip_path(u64 path_identity, Gfx::Path const& path)
+SkPath DisplayListRasterCache::clip_path(u64 path_identity, Gfx::PathData const& path)
{
if (auto it = m_clip_paths.find(path_identity); it != m_clip_paths.end())
return it->value->path;
diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.h
index f97ed8d4ac1..0d040388fc0 100644
--- a/Services/Compositor/DisplayListRasterCache.h
+++ b/Services/Compositor/DisplayListRasterCache.h
@@ -79,7 +79,7 @@ public:
// The Skia path for a path that stays the same for as long as it has this identity. Keeping one Skia path lets
// the caches that are keyed on it work.
- SkPath clip_path(u64 path_identity, Gfx::Path const&);
+ SkPath clip_path(u64 path_identity, Gfx::PathData const&);
// Drops the clip paths that were not used since the last call.
void drop_clip_paths_not_used_since_last_frame();
diff --git a/Services/Compositor/SkiaUtils.cpp b/Services/Compositor/SkiaUtils.cpp
index 0f517485fbb..d7da78a055d 100644
--- a/Services/Compositor/SkiaUtils.cpp
+++ b/Services/Compositor/SkiaUtils.cpp
@@ -40,7 +40,11 @@ static_assert(sizeof(FloatPoint) == sizeof(SkPoint));
SkPath to_skia_path(Path const& path)
{
- auto data = path.data();
+ return to_skia_path(path.data());
+}
+
+SkPath to_skia_path(PathData const& data)
+{
return SkPath::Raw(
{ reinterpret_cast<SkPoint const*>(data.points.data()), data.points.size() },
{ reinterpret_cast<SkPathVerb const*>(data.verbs.data()), data.verbs.size() },
diff --git a/Services/Compositor/SkiaUtils.h b/Services/Compositor/SkiaUtils.h
index 7a32da403bc..71ca0e157fa 100644
--- a/Services/Compositor/SkiaUtils.h
+++ b/Services/Compositor/SkiaUtils.h
@@ -138,6 +138,7 @@ constexpr SkSamplingOptions to_skia_sampling_options(ScalingMode scaling_mode)
VERIFY_NOT_REACHED();
}
+SkPath to_skia_path(PathData const&);
SkPath to_skia_path(Path const& path);
// Builds the SkImageFilter tree a serialized filter graph describes; `image_frame` supplies the
// frames the graph names by id.