Skip to content

Truly independent web browser

LibGfx: Let Rust build on a path in place

Andreas Kling7fd1fbfc6ef8
diff --git a/Libraries/LibGfx/Path.cpp b/Libraries/LibGfx/Path.cpp
index 951106e6d92..8cbd24cece9 100644
--- a/Libraries/LibGfx/Path.cpp
+++ b/Libraries/LibGfx/Path.cpp
@@ -26,6 +26,11 @@ void ladybird_gfx_path_bounding_box(void const*, float*);
void ladybird_gfx_path_serialize(void const*, void (*)(void*, u8 const*, size_t), void*);
float ladybird_gfx_path_length(void const*);
void* ladybird_gfx_path_create_from_ops(u8 const* kinds, float const* values, size_t count);
+void ladybird_gfx_path_append_ops(void*, u8 const* kinds, float const* values, size_t count);
+void ladybird_gfx_path_append_path(void*, void const* other);
+bool ladybird_gfx_path_is_empty(void const*);
+void ladybird_gfx_path_last_point(void const*, float* out_x_y);
+void* ladybird_gfx_path_clone(void const*);
void* ladybird_gfx_path_create_from_serialized_bytes(u8 const* bytes, size_t count);
void* ladybird_gfx_path_copy_transformed(void const*, float const* affine_values);
bool ladybird_gfx_path_contains(void const*, float, float, int);
@@ -118,6 +123,13 @@ enum class PathBuilderOpKind : u8 {
extern "C" void* ladybird_gfx_path_create_from_ops(u8 const* kinds, float const* values, size_t count)
{
auto* path = new Gfx::Path;
+ ladybird_gfx_path_append_ops(path, kinds, values, count);
+ return path;
+}
+
+extern "C" void ladybird_gfx_path_append_ops(void* raw_path, u8 const* kinds, float const* values, size_t count)
+{
+ auto* path = static_cast<Gfx::Path*>(raw_path);
for (size_t i = 0; i < count; ++i) {
auto const* v = values + i * 7;
switch (static_cast<PathBuilderOpKind>(kinds[i])) {
@@ -146,7 +158,28 @@ extern "C" void* ladybird_gfx_path_create_from_ops(u8 const* kinds, float const*
VERIFY_NOT_REACHED();
}
}
- return path;
+}
+
+extern "C" void ladybird_gfx_path_append_path(void* path, void const* other)
+{
+ static_cast<Gfx::Path*>(path)->append_path(*static_cast<Gfx::Path const*>(other));
+}
+
+extern "C" bool ladybird_gfx_path_is_empty(void const* path)
+{
+ return static_cast<Gfx::Path const*>(path)->is_empty();
+}
+
+extern "C" void ladybird_gfx_path_last_point(void const* path, float* out_x_y)
+{
+ auto point = static_cast<Gfx::Path const*>(path)->last_point();
+ out_x_y[0] = point.x();
+ out_x_y[1] = point.y();
+}
+
+extern "C" void* ladybird_gfx_path_clone(void const* path)
+{
+ return new Gfx::Path(static_cast<Gfx::Path const*>(path)->clone());
}
static Vector<NonnullRefPtr<Gfx::GlyphRun>> adopt_glyph_runs(LadybirdGfxGlyphRun const* runs, size_t run_count)
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rs
index d1c95dbc7a5..1edb46cfc7a 100644
--- a/Libraries/LibGfx/Rust/src/path.rs
+++ b/Libraries/LibGfx/Rust/src/path.rs
@@ -5,9 +5,12 @@
*/
use std::ffi::c_void;
+use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ptr::NonNull;
+use crate::geometry::{FloatPoint, FloatSize};
+
unsafe extern "C" {
fn ladybird_gfx_process_next_path_identity() -> u64;
fn ladybird_gfx_path_destroy(path: *mut c_void);
@@ -27,6 +30,11 @@ unsafe extern "C" {
fn ladybird_gfx_path_contains(path: *const c_void, x: f32, y: f32, winding_rule: i32) -> bool;
fn ladybird_gfx_path_length(path: *const c_void) -> f32;
fn ladybird_gfx_path_create_from_ops(kinds: *const u8, values: *const f32, count: usize) -> *mut c_void;
+ fn ladybird_gfx_path_append_ops(path: *mut c_void, kinds: *const u8, values: *const f32, count: usize);
+ fn ladybird_gfx_path_append_path(path: *mut c_void, other: *const c_void);
+ fn ladybird_gfx_path_is_empty(path: *const c_void) -> bool;
+ fn ladybird_gfx_path_last_point(path: *const c_void, out_x_y: *mut f32);
+ fn ladybird_gfx_path_clone(path: *const c_void) -> *mut c_void;
fn ladybird_gfx_path_create_from_serialized_bytes(bytes: *const u8, count: usize) -> *mut c_void;
fn ladybird_gfx_path_copy_transformed(path: *const c_void, affine_values: *const f32) -> *mut c_void;
fn ladybird_gfx_path_create_from_glyph_runs(runs: *const FfiGlyphRun, run_count: usize) -> *mut c_void;
@@ -169,6 +177,119 @@ impl PathBuilder {
}
}
+/// A `Gfx::Path` that is built on where it is: one that an [`OwnedPath`] or C++ owns, borrowed mutably for `'a`.
+/// Each operation is that of the path, with its rules for a path that has no current point.
+pub struct PathMut<'a> {
+ raw: NonNull<c_void>,
+ path: PhantomData<&'a mut c_void>,
+}
+
+impl PathMut<'_> {
+ /// Borrows a `Gfx::Path` that C++ owns.
+ ///
+ /// # Safety
+ ///
+ /// `raw` must be a live `Gfx::Path`, which nothing else uses while the value lives.
+ pub unsafe fn from_raw(raw: *mut c_void) -> Self {
+ Self {
+ raw: NonNull::new(raw).expect("Gfx::Path pointer must not be null"),
+ path: PhantomData,
+ }
+ }
+
+ fn apply(&mut self, kind: PathBuilderOpKind, values: [f32; 7]) {
+ let kind = kind as u8;
+ // SAFETY: The path is live and borrowed mutably, and the op is live for the call.
+ unsafe { ladybird_gfx_path_append_ops(self.raw.as_ptr(), &raw const kind, values.as_ptr(), 1) };
+ }
+
+ /// Whether the path has no current point, which Gfx::Path::is_empty() calls empty.
+ pub fn is_empty(&self) -> bool {
+ // SAFETY: The path is live.
+ unsafe { ladybird_gfx_path_is_empty(self.raw.as_ptr()) }
+ }
+
+ pub fn last_point(&self) -> FloatPoint {
+ last_point_of(self.raw)
+ }
+
+ pub fn move_to(&mut self, point: FloatPoint) {
+ self.apply(PathBuilderOpKind::MoveTo, [point.x, point.y, 0.0, 0.0, 0.0, 0.0, 0.0]);
+ }
+
+ pub fn line_to(&mut self, point: FloatPoint) {
+ self.apply(PathBuilderOpKind::LineTo, [point.x, point.y, 0.0, 0.0, 0.0, 0.0, 0.0]);
+ }
+
+ pub fn quadratic_bezier_curve_to(&mut self, through: FloatPoint, point: FloatPoint) {
+ self.apply(
+ PathBuilderOpKind::QuadraticBezierCurveTo,
+ [through.x, through.y, point.x, point.y, 0.0, 0.0, 0.0],
+ );
+ }
+
+ pub fn cubic_bezier_curve_to(&mut self, c1: FloatPoint, c2: FloatPoint, point: FloatPoint) {
+ self.apply(
+ PathBuilderOpKind::CubicBezierCurveTo,
+ [c1.x, c1.y, c2.x, c2.y, point.x, point.y, 0.0],
+ );
+ }
+
+ pub fn elliptical_arc_to(
+ &mut self,
+ point: FloatPoint,
+ radii: FloatSize,
+ x_axis_rotation: f32,
+ large_arc: bool,
+ sweep: bool,
+ ) {
+ self.apply(
+ PathBuilderOpKind::EllipticalArcTo,
+ [
+ point.x,
+ point.y,
+ radii.width,
+ radii.height,
+ x_axis_rotation,
+ if large_arc { 1.0 } else { 0.0 },
+ if sweep { 1.0 } else { 0.0 },
+ ],
+ );
+ }
+
+ pub fn arc_to(&mut self, point: FloatPoint, radius: f32, large_arc: bool, sweep: bool) {
+ self.apply(
+ PathBuilderOpKind::ArcTo,
+ [
+ point.x,
+ point.y,
+ radius,
+ if large_arc { 1.0 } else { 0.0 },
+ if sweep { 1.0 } else { 0.0 },
+ 0.0,
+ 0.0,
+ ],
+ );
+ }
+
+ pub fn close(&mut self) {
+ self.apply(PathBuilderOpKind::Close, [0.0; 7]);
+ }
+
+ /// Adds all the subpaths of the other path to this one.
+ pub fn append_path(&mut self, other: &OwnedPath) {
+ // SAFETY: Both paths are live, and this one is borrowed mutably.
+ unsafe { ladybird_gfx_path_append_path(self.raw.as_ptr(), other.raw.as_ptr()) };
+ }
+}
+
+fn last_point_of(path: NonNull<c_void>) -> FloatPoint {
+ let mut x_y = [0.0f32; 2];
+ // SAFETY: The path is live for the call; the out-array holds two floats.
+ unsafe { ladybird_gfx_path_last_point(path.as_ptr(), x_y.as_mut_ptr()) };
+ FloatPoint { x: x_y[0], y: x_y[1] }
+}
+
unsafe extern "C" fn append_to_vec(context: *mut c_void, bytes: *const u8, length: usize) {
if length == 0 {
return;
@@ -244,6 +365,27 @@ impl OwnedPath {
this.raw.as_ptr()
}
+ /// The path, borrowed to be built on. The path gets a new identity, as it may no longer be the
+ /// path that a snapshot holding the old identity copied.
+ pub fn as_path_mut(&mut self) -> PathMut<'_> {
+ // SAFETY: The counter is an atomic on the other side of the boundary.
+ self.identity = unsafe { ladybird_gfx_process_next_path_identity() };
+ PathMut {
+ raw: self.raw,
+ path: PhantomData,
+ }
+ }
+
+ /// Whether the path has no current point, which Gfx::Path::is_empty() calls empty.
+ pub fn is_empty(&self) -> bool {
+ // SAFETY: The path is live for the duration of the call.
+ unsafe { ladybird_gfx_path_is_empty(self.raw.as_ptr()) }
+ }
+
+ pub fn last_point(&self) -> FloatPoint {
+ last_point_of(self.raw)
+ }
+
/// A process-unique, never-reused identity for this path allocation, so
/// consumers holding a copied snapshot can recognize an unchanged path
/// without comparing contents.
@@ -337,6 +479,14 @@ impl OwnedPath {
}
}
+/// A copy of the path, which has an identity of its own.
+impl Clone for OwnedPath {
+ fn clone(&self) -> Self {
+ // SAFETY: The path is live for the call; the result is a fresh heap-allocated Gfx::Path.
+ unsafe { OwnedPath::adopt(ladybird_gfx_path_clone(self.raw.as_ptr())) }
+ }
+}
+
impl PartialEq for OwnedPath {
fn eq(&self, other: &Self) -> bool {
self.identity == other.identity