🎉 Serialize radial fills in one go

This commit is contained in:
Belén Albeza 2025-04-15 11:53:09 +02:00
parent 5765d1c56c
commit dc3d802d3d
4 changed files with 65 additions and 33 deletions

View file

@ -242,24 +242,11 @@
(sr-fills/serialize-linear-fill gradient opacity heap offset) (sr-fills/serialize-linear-fill gradient opacity heap offset)
(h/call wasm/internal-module "_add_shape_linear_fill")) (h/call wasm/internal-module "_add_shape_linear_fill"))
:radial :radial
(let [stops (:stops gradient) (let [size (gradient-byte-size gradient)
size (gradient-stop-get-entries-size stops)
offset (mem/alloc-bytes size) offset (mem/alloc-bytes size)
heap (mem/get-heap-u8) heap (mem/get-heap-u32)]
mem (js/Uint8Array. (.-buffer heap) offset size)] (sr-fills/serialize-radial-fill gradient opacity heap offset)
(h/call wasm/internal-module "_add_shape_radial_fill" (h/call wasm/internal-module "_add_shape_radial_fill")))
(:start-x gradient)
(:start-y gradient)
(:end-x gradient)
(:end-y gradient)
opacity
(:width gradient))
(.set mem (js/Uint8Array. (clj->js (flatten (map (fn [stop]
(let [[r g b a] (sr-clr/rgba-bytes-from-hex (:color stop) (:opacity stop))
offset (:offset stop)]
[r g b a (* 100 offset)]))
stops)))))
(h/call wasm/internal-module "_add_shape_fill_stops")))
(some? image) (some? image)
(let [id (dm/get-prop image :id) (let [id (dm/get-prop image :id)
@ -301,13 +288,15 @@
size (gradient-stop-get-entries-size stops) size (gradient-stop-get-entries-size stops)
offset (mem/alloc-bytes size) offset (mem/alloc-bytes size)
heap (mem/get-heap-u8)] heap (mem/get-heap-u8)]
(if (= (:type gradient) :linear) (case (:type gradient)
:linear
(h/call wasm/internal-module "_add_shape_stroke_linear_fill" (h/call wasm/internal-module "_add_shape_stroke_linear_fill"
(:start-x gradient) (:start-x gradient)
(:start-y gradient) (:start-y gradient)
(:end-x gradient) (:end-x gradient)
(:end-y gradient) (:end-y gradient)
opacity) opacity)
:radial
(h/call wasm/internal-module "_add_shape_stroke_radial_fill" (h/call wasm/internal-module "_add_shape_stroke_radial_fill"
(:start-x gradient) (:start-x gradient)
(:start-y gradient) (:start-y gradient)

View file

@ -35,4 +35,21 @@
(.setFloat32 dview (+ offset 12) end-y true) (.setFloat32 dview (+ offset 12) end-y true)
(.setFloat32 dview (+ offset 16) opacity true) (.setFloat32 dview (+ offset 16) opacity true)
(.setFloat32 dview (+ offset 20) width true) (.setFloat32 dview (+ offset 20) width true)
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))
(defn serialize-radial-fill
[gradient opacity heap offset]
(let [dview (js/DataView. (.-buffer heap))
start-x (dm/get-prop gradient :start-x)
start-y (dm/get-prop gradient :start-y)
end-x (dm/get-prop gradient :end-x)
end-y (dm/get-prop gradient :end-y)
stops (dm/get-prop gradient :stops)
width (dm/get-prop gradient :width)]
(.setFloat32 dview offset start-x true)
(.setFloat32 dview (+ offset 4) start-y true)
(.setFloat32 dview (+ offset 8) end-x true)
(.setFloat32 dview (+ offset 12) end-y true)
(.setFloat32 dview (+ offset 16) opacity true)
(.setFloat32 dview (+ offset 20) width true)
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE)))) (serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))

View file

@ -282,20 +282,26 @@ pub extern "C" fn add_shape_linear_fill() {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn add_shape_radial_fill( pub extern "C" fn add_shape_radial_fill() {
start_x: f32,
start_y: f32,
end_x: f32,
end_y: f32,
opacity: f32,
width: f32,
) {
with_current_shape!(state, |shape: &mut Shape| { with_current_shape!(state, |shape: &mut Shape| {
shape.add_fill(shapes::Fill::new_radial_gradient( let bytes = mem::bytes();
(start_x, start_y), let raw_gradient_bytes: [u8; RAW_FILL_DATA_SIZE] =
(end_x, end_y), bytes[0..RAW_FILL_DATA_SIZE].try_into().unwrap();
opacity, let raw_gradient = shapes::RawGradientData::from(raw_gradient_bytes);
width, let stops: Vec<shapes::RawStopData> = bytes[RAW_FILL_DATA_SIZE..]
.chunks(RAW_STOP_DATA_SIZE)
.map(|chunk| {
let data: [u8; RAW_STOP_DATA_SIZE] = chunk.try_into().unwrap();
shapes::RawStopData::from(data)
})
.collect();
shape.add_fill(shapes::Fill::new_radial_gradient_with_stops(
raw_gradient.start(),
raw_gradient.end(),
raw_gradient.opacity(),
raw_gradient.width(),
stops,
)); ));
}); });
} }

View file

@ -41,6 +41,10 @@ impl RawGradientData {
pub fn opacity(&self) -> f32 { pub fn opacity(&self) -> f32 {
self.opacity self.opacity
} }
pub fn width(&self) -> f32 {
self.width
}
} }
pub const RAW_STOP_DATA_SIZE: usize = 8; pub const RAW_STOP_DATA_SIZE: usize = 8;
@ -199,14 +203,30 @@ impl Fill {
opacity: f32, opacity: f32,
width: f32, width: f32,
) -> Self { ) -> Self {
Self::RadialGradient(Gradient { Self::new_radial_gradient_with_stops(start, end, opacity, width, vec![])
}
pub fn new_radial_gradient_with_stops(
start: (f32, f32),
end: (f32, f32),
opacity: f32,
width: f32,
stops: Vec<RawStopData>,
) -> Self {
let mut gradient = Gradient {
start, start,
end, end,
opacity, opacity,
colors: vec![], colors: vec![],
offsets: vec![], offsets: vec![],
width, width,
}) };
for stop in stops {
gradient.add_stop(stop.color(), stop.offset());
}
Self::RadialGradient(gradient)
} }
pub fn new_image_fill(id: Uuid, opacity: u8, (width, height): (i32, i32)) -> Self { pub fn new_image_fill(id: Uuid, opacity: u8, (width, height): (i32, i32)) -> Self {