Skip to content

Truly independent web browser

LibWeb: Hand Rust-built paths to C++ as bytes

Aliaksandr Kalenik2510ca3e0995
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 7ba5ade7e2d..e7d8f269c69 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1026,21 +1026,24 @@ impl SvgShapeGeometry {
/// The geometry a shape element draws, in the user units of a viewport of `viewport_size`, from
/// the shape `geometry` names and the computed style in `payloads`. The DOM's geometry queries
/// measure this path. Only the `geometry_kind` and `line_*` fields of `geometry` are read.
-/// Returns a heap-allocated `Gfx::Path` that the caller owns.
+/// Calls `append` once with the byte form of the path, which `Gfx::Path` reads.
///
/// # Safety
///
/// `payloads` must point at `payload_count` live style group payloads, and `points` at
-/// `point_count` points.
+/// `point_count` points. `context` must be valid for `append` for the duration of this call.
+#[allow(clippy::too_many_arguments)]
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn svg_geometry_element_path(
+pub unsafe extern "C" fn svg_geometry_element_encode_path(
geometry: FfiSvgAttributeFacts,
payloads: *const *const c_void,
payload_count: usize,
points: *const FfiFloatPoint,
point_count: usize,
viewport_size: FfiCssPixelSize,
-) -> *mut c_void {
+ context: *mut c_void,
+ append: unsafe extern "C" fn(*mut c_void, *const u8, usize),
+) {
assert!(!payloads.is_null(), "style group payload array is null");
let mut style_payloads = FfiStylePayloads::default();
// SAFETY: Guaranteed by the caller.
@@ -1053,9 +1056,11 @@ pub unsafe extern "C" fn svg_geometry_element_path(
viewport_width: viewport_size.width,
viewport_height: viewport_size.height,
};
- shape_geometry
+ let bytes = shape_geometry
.path(geometry, StyleValues::new(&style_payloads), Some(points))
- .into_raw()
+ .serialize_to_bytes();
+ // SAFETY: Guaranteed by the caller.
+ unsafe { append(context, bytes.as_ptr(), bytes.len()) };
}
impl<'pass> SvgFormattingContext<'pass> {
diff --git a/Libraries/LibWeb/Rust/src/svg/path_parser.rs b/Libraries/LibWeb/Rust/src/svg/path_parser.rs
index 1723f44e9a3..9b916af46a5 100644
--- a/Libraries/LibWeb/Rust/src/svg/path_parser.rs
+++ b/Libraries/LibWeb/Rust/src/svg/path_parser.rs
@@ -1373,9 +1373,18 @@ pub unsafe extern "C" fn rust_svg_path_serialize(
unsafe { append(context, serialized.as_ptr(), serialized.len()) };
}
+/// Calls `append` once with the byte form of the path that `path` draws, which `Gfx::Path` reads.
+///
+/// # Safety
+/// `context` must be valid for `append` for the duration of this call.
#[unsafe(no_mangle)]
-pub extern "C" fn rust_svg_path_to_gfx_path(path: &ParsedPath) -> *mut c_void {
- path.to_gfx_path().into_raw()
+pub unsafe extern "C" fn rust_svg_path_encode_gfx_path(
+ path: &ParsedPath,
+ context: *mut c_void,
+ append: unsafe extern "C" fn(*mut c_void, *const u8, usize),
+) {
+ let bytes = path.to_gfx_path().serialize_to_bytes();
+ unsafe { append(context, bytes.as_ptr(), bytes.len()) };
}
#[cfg(test)]
diff --git a/Libraries/LibWeb/SVG/Path.cpp b/Libraries/LibWeb/SVG/Path.cpp
index bb4170682a0..56bce4bc723 100644
--- a/Libraries/LibWeb/SVG/Path.cpp
+++ b/Libraries/LibWeb/SVG/Path.cpp
@@ -74,10 +74,12 @@ Path& Path::operator=(Path&& other)
Gfx::Path Path::to_gfx_path() const
{
- auto* path = static_cast<Gfx::Path*>(RustFFI::rust_svg_path_to_gfx_path(m_rust_path));
- auto result = move(*path);
- delete path;
- return result;
+ Vector<u8> bytes;
+ auto append = [](void* context, u8 const* data, size_t length) {
+ static_cast<Vector<u8>*>(context)->append(data, length);
+ };
+ RustFFI::rust_svg_path_encode_gfx_path(m_rust_path, &bytes, append);
+ return Gfx::Path::from_serialized_bytes(bytes);
}
String Path::serialize() const
diff --git a/Libraries/LibWeb/SVG/SVGGeometryElement.cpp b/Libraries/LibWeb/SVG/SVGGeometryElement.cpp
index ddc9472f138..768f7f8bcf5 100644
--- a/Libraries/LibWeb/SVG/SVGGeometryElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGGeometryElement.cpp
@@ -115,16 +115,17 @@ static Gfx::Path geometry_path(SVGGeometryElement const& element, CSSPixelSize v
payloads[index] = computed_values.style_group_payload(static_cast<CSS::StyleGroupIndex>(index));
auto points = Layout::svg_shape_points(element);
static_assert(sizeof(Gfx::FloatPoint) == sizeof(Layout::RustFFI::FfiFloatPoint));
- auto* native_path = static_cast<Gfx::Path*>(Layout::RustFFI::svg_geometry_element_path(
+ Vector<u8> path_bytes;
+ Layout::RustFFI::svg_geometry_element_encode_path(
Layout::svg_geometry_facts(element),
payloads.data(),
payloads.size(),
reinterpret_cast<Layout::RustFFI::FfiFloatPoint const*>(points.data()),
points.size(),
- viewport_size));
- auto path = move(*native_path);
- delete native_path;
- return path;
+ viewport_size,
+ &path_bytes,
+ [](void* context, u8 const* data, size_t length) { static_cast<Vector<u8>*>(context)->append(data, length); });
+ return Gfx::Path::from_serialized_bytes(path_bytes);
}
Gfx::Path SVGGeometryElement::computed_path()