From 7105e49ac295b05071caba346f11efcd3f8268af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Mon, 2 Dec 2024 17:04:44 +0100 Subject: [PATCH] :recycle: Refactor adding gradient stops --- render-wasm/src/main.rs | 35 +++++++++++---------------------- render-wasm/src/shapes.rs | 6 ++++-- render-wasm/src/shapes/fills.rs | 17 ++++++++++++++++ 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 639607598c..964103ad67 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -1,6 +1,6 @@ mod debug; mod math; -pub mod mem; +mod mem; mod render; mod shapes; mod state; @@ -177,33 +177,20 @@ pub extern "C" fn add_shape_linear_fill( } } -#[derive(Debug)] -pub struct RawStopData { - color: [u8; 4], - offset: u8, -} - #[no_mangle] -pub extern "C" fn add_shape_fill_stops(ptr: *mut RawStopData, n_stops: i32) { +pub extern "C" fn add_shape_fill_stops(ptr: *mut shapes::RawStopData, n_stops: u32) { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); + if let Some(shape) = state.current_shape() { + let len = n_stops as usize; + let buffer_size = std::mem::size_of::() * len; + unsafe { - let buf = Vec::::from_raw_parts(ptr, n_stops as usize, n_stops as usize); - for raw_stop in buf.iter() { - let color = skia::Color::from_argb( - raw_stop.color[3], - raw_stop.color[0], - raw_stop.color[1], - raw_stop.color[2], - ); - shape - .add_gradient_stop(color, (raw_stop.offset as f32) / 100.) - .expect("got no fill or an invalid one"); - } - mem::free( - ptr as *mut u8, - n_stops as usize * std::mem::size_of::(), - ); + let buffer = Vec::::from_raw_parts(ptr, len, len); + shape + .add_gradient_stops(buffer) + .expect("could not add gradient stops"); + mem::free(ptr as *mut u8, buffer_size); } } } diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index c967a4bfe1..b60712d8ac 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -92,14 +92,16 @@ impl Shape { self.fills.clear(); } - pub fn add_gradient_stop(&mut self, color: skia::Color, offset: f32) -> Result<(), String> { + pub fn add_gradient_stops(&mut self, buffer: Vec) -> Result<(), String> { let fill = self.fills.last_mut().ok_or("Shape has no fills")?; let gradient = match fill { Fill::LinearGradient(g) => Ok(g), _ => Err("Active fill is not a gradient"), }?; - gradient.add_stop(color, offset); + for stop in buffer.into_iter() { + gradient.add_stop(stop.color(), stop.offset()); + } Ok(()) } diff --git a/render-wasm/src/shapes/fills.rs b/render-wasm/src/shapes/fills.rs index 0641979e88..fea83a8cc9 100644 --- a/render-wasm/src/shapes/fills.rs +++ b/render-wasm/src/shapes/fills.rs @@ -4,6 +4,23 @@ use super::Color; use crate::math; use uuid::Uuid; +#[derive(Debug)] +#[repr(C)] +pub struct RawStopData { + color: [u8; 4], + offset: u8, +} + +impl RawStopData { + pub fn color(&self) -> skia::Color { + skia::Color::from_argb(self.color[3], self.color[0], self.color[1], self.color[2]) + } + + pub fn offset(&self) -> f32 { + self.offset as f32 / 100.0 + } +} + #[derive(Debug, Clone, PartialEq)] pub struct Gradient { colors: Vec,