From 093fa18839c0936d6da380ca311851c768753cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Tue, 29 Apr 2025 16:20:45 +0200 Subject: [PATCH] :sparkles: Serialize solid fills as bytes (wasm) --- frontend/src/app/render_wasm/api.cljs | 8 +++- .../app/render_wasm/serializers/fills.cljs | 12 +++++- render-wasm/src/shapes.rs | 7 +++- render-wasm/src/shapes/fills.rs | 9 ++-- render-wasm/src/shapes/strokes.rs | 17 ++++---- render-wasm/src/wasm/fills.rs | 42 +++++++++++++++++-- render-wasm/src/wasm/strokes.rs | 2 +- 7 files changed, 73 insertions(+), 24 deletions(-) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index caff9874e4..1cc55da7ad 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -217,8 +217,12 @@ image (:fill-image fill)] (cond (some? color) - (let [rgba (sr-clr/hex->u32argb color opacity)] - (h/call wasm/internal-module "_add_shape_solid_fill" rgba)) + (let [size sr-fills/SOLID-BYTE-SIZE + offset (mem/alloc-bytes size) + heap (mem/get-heap-u32) + argb (sr-clr/hex->u32argb color opacity)] + (sr-fills/write-solid-fill! offset heap argb) + (h/call wasm/internal-module "_add_shape_solid_fill")) (some? gradient) (let [size sr-fills/GRADIENT-BYTE-SIZE diff --git a/frontend/src/app/render_wasm/serializers/fills.cljs b/frontend/src/app/render_wasm/serializers/fills.cljs index bef0c35220..685030e558 100644 --- a/frontend/src/app/render_wasm/serializers/fills.cljs +++ b/frontend/src/app/render_wasm/serializers/fills.cljs @@ -2,6 +2,14 @@ (:require [app.render-wasm.serializers.color :as clr])) +(def SOLID-BYTE-SIZE 4) + +(defn write-solid-fill! + [offset heap-u32 argb] + (let [dview (js/DataView. (.-buffer heap-u32))] + (.setUint32 dview offset argb true) + (+ offset 4))) + (def ^:private GRADIENT-STOP-SIZE 8) (def ^:private GRADIENT-BASE-SIZE 28) ;; TODO: Define in shape model @@ -11,8 +19,8 @@ (+ GRADIENT-BASE-SIZE (* MAX-GRADIENT-STOPS GRADIENT-STOP-SIZE))) (defn write-gradient-fill! - [offset heap gradient opacity] - (let [dview (js/DataView. (.-buffer heap)) + [offset heap-u32 gradient opacity] + (let [dview (js/DataView. (.-buffer heap-u32)) start-x (:start-x gradient) start-y (:start-y gradient) end-x (:end-x gradient) diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index caf9056c82..3931eca380 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -860,8 +860,11 @@ mod tests { let mut shape = any_shape(); assert_eq!(shape.fills.len(), 0); - shape.add_fill(Fill::Solid(Color::TRANSPARENT)); - assert_eq!(shape.fills.get(0), Some(&Fill::Solid(Color::TRANSPARENT))) + shape.add_fill(Fill::Solid(SolidColor(Color::TRANSPARENT))); + assert_eq!( + shape.fills.get(0), + Some(&Fill::Solid(SolidColor(Color::TRANSPARENT))) + ) } #[test] diff --git a/render-wasm/src/shapes/fills.rs b/render-wasm/src/shapes/fills.rs index fd87f08042..948b962728 100644 --- a/render-wasm/src/shapes/fills.rs +++ b/render-wasm/src/shapes/fills.rs @@ -1,6 +1,6 @@ use skia_safe::{self as skia, Rect}; -use super::Color; +pub use super::Color; use crate::uuid::Uuid; #[derive(Debug, Clone, PartialEq)] @@ -112,9 +112,12 @@ impl ImageFill { } } +#[derive(Debug, Clone, PartialEq, Copy)] +pub struct SolidColor(pub Color); + #[derive(Debug, Clone, PartialEq)] pub enum Fill { - Solid(Color), + Solid(SolidColor), LinearGradient(Gradient), RadialGradient(Gradient), Image(ImageFill), @@ -132,7 +135,7 @@ impl Fill { pub fn to_paint(&self, rect: &Rect, anti_alias: bool) -> skia::Paint { match self { - Self::Solid(color) => { + Self::Solid(SolidColor(color)) => { let mut p = skia::Paint::default(); p.set_color(*color); p.set_style(skia::PaintStyle::Fill); diff --git a/render-wasm/src/shapes/strokes.rs b/render-wasm/src/shapes/strokes.rs index c1372d224c..ffa1e40621 100644 --- a/render-wasm/src/shapes/strokes.rs +++ b/render-wasm/src/shapes/strokes.rs @@ -1,4 +1,4 @@ -use crate::shapes::fills::Fill; +use crate::shapes::fills::{Fill, SolidColor}; use skia_safe::{self as skia, Rect}; use std::collections::HashMap; @@ -78,10 +78,9 @@ impl Stroke { } pub fn new_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self { - let transparent = skia::Color::from_argb(0, 0, 0, 0); Stroke { - fill: Fill::Solid(transparent), - width: width, + fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)), + width, style: StrokeStyle::from(style), cap_end: StrokeCap::from(cap_end), cap_start: StrokeCap::from(cap_start), @@ -90,10 +89,9 @@ impl Stroke { } pub fn new_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self { - let transparent = skia::Color::from_argb(0, 0, 0, 0); Stroke { - fill: Fill::Solid(transparent), - width: width, + fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)), + width, style: StrokeStyle::from(style), cap_end: StrokeCap::from(cap_end), cap_start: StrokeCap::from(cap_start), @@ -102,10 +100,9 @@ impl Stroke { } pub fn new_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self { - let transparent = skia::Color::from_argb(0, 0, 0, 0); Stroke { - fill: Fill::Solid(transparent), - width: width, + fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)), + width, style: StrokeStyle::from(style), cap_end: StrokeCap::from(cap_end), cap_start: StrokeCap::from(cap_start), diff --git a/render-wasm/src/wasm/fills.rs b/render-wasm/src/wasm/fills.rs index a2642bfc16..50521d6ca3 100644 --- a/render-wasm/src/wasm/fills.rs +++ b/render-wasm/src/wasm/fills.rs @@ -2,16 +2,18 @@ use skia_safe as skia; use crate::mem; use crate::shapes; -use crate::shapes::Gradient; +use crate::shapes::{Gradient, SolidColor}; use crate::utils::uuid_from_u32_quartet; use crate::with_current_shape; use crate::STATE; #[no_mangle] -pub extern "C" fn add_shape_solid_fill(raw_color: u32) { +pub extern "C" fn add_shape_solid_fill() { with_current_shape!(state, |shape: &mut Shape| { - let color = skia::Color::new(raw_color); - shape.add_fill(shapes::Fill::Solid(color)); + let bytes = mem::bytes(); + let solid_color = SolidColor::try_from(&bytes[..]).expect("Invalid solid color data"); + + shape.add_fill(shapes::Fill::Solid(solid_color)); }); } @@ -60,6 +62,38 @@ pub extern "C" fn clear_shape_fills() { }); } +#[repr(C)] +pub struct RawSolidData { + color: u32, +} + +impl From<[u8; 4]> for RawSolidData { + fn from(value: [u8; 4]) -> Self { + Self { + color: u32::from_le_bytes(value), + } + } +} + +impl From for SolidColor { + fn from(value: RawSolidData) -> Self { + Self(skia::Color::new(value.color)) + } +} + +impl TryFrom<&[u8]> for SolidColor { + type Error = String; + + fn try_from(bytes: &[u8]) -> Result { + let raw_solid_bytes: [u8; 4] = bytes[0..4] + .try_into() + .map_err(|_| "Invalid solid fill data".to_string())?; + let color = RawSolidData::from(raw_solid_bytes).into(); + + Ok(color) + } +} + const MAX_GRADIENT_STOPS: usize = 16; const BASE_GRADIENT_DATA_SIZE: usize = 28; const RAW_GRADIENT_DATA_SIZE: usize = diff --git a/render-wasm/src/wasm/strokes.rs b/render-wasm/src/wasm/strokes.rs index 5aea834cfb..2912b9a37e 100644 --- a/render-wasm/src/wasm/strokes.rs +++ b/render-wasm/src/wasm/strokes.rs @@ -38,7 +38,7 @@ pub extern "C" fn add_shape_stroke_solid_fill(raw_color: u32) { with_current_shape!(state, |shape: &mut Shape| { let color = skia::Color::new(raw_color); shape - .set_stroke_fill(shapes::Fill::Solid(color)) + .set_stroke_fill(shapes::Fill::Solid(shapes::SolidColor(color))) .expect("could not add stroke solid fill"); }); }