♻️ Switch to a f32 offset for gradient stops

This commit is contained in:
Belén Albeza 2025-04-15 11:04:36 +02:00
parent abcd050c69
commit 5765d1c56c
4 changed files with 41 additions and 42 deletions

View file

@ -51,7 +51,7 @@
(def GRID-LAYOUT-COLUMN-ENTRY-SIZE 5)
(def GRID-LAYOUT-CELL-ENTRY-SIZE 37)
;; FIXME: use `gradient-byte-size` instead
(defn gradient-stop-get-entries-size
[stops]
(mem/get-list-size stops sr-fills/GRADIENT-STOP-SIZE))
@ -238,7 +238,7 @@
:linear
(let [size (gradient-byte-size gradient)
offset (mem/alloc-bytes size)
heap (mem/get-heap-u8)]
heap (mem/get-heap-u32)]
(sr-fills/serialize-linear-fill gradient opacity heap offset)
(h/call wasm/internal-module "_add_shape_linear_fill"))
:radial
@ -297,11 +297,10 @@
(cond
(some? gradient)
(let [stops (:stops gradient)
size (gradient-stop-get-entries-size stops)
(let [stops (:stops gradient)
size (gradient-stop-get-entries-size stops)
offset (mem/alloc-bytes size)
heap (mem/get-heap-u8)
mem (js/Uint8Array. (.-buffer heap) offset size)]
heap (mem/get-heap-u8)]
(if (= (:type gradient) :linear)
(h/call wasm/internal-module "_add_shape_stroke_linear_fill"
(:start-x gradient)
@ -316,11 +315,7 @@
(: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)))))
(sr-fills/serialize-gradient-stops stops heap offset)
(h/call wasm/internal-module "_add_shape_stroke_stops"))
(some? image)

View file

@ -3,12 +3,26 @@
[app.common.data.macros :as dm]
[app.render-wasm.serializers.color :as clr]))
(def GRADIENT-STOP-SIZE 5)
(def GRADIENT-STOP-SIZE 8)
(def GRADIENT-BASE-SIZE 24)
(defn serialize-gradient-stops
[stops heap offset]
(let [dview (js/DataView. (.-buffer heap))]
(loop [stops (seq stops) offset offset]
(when-not (empty? stops)
(let [stop (first stops)
hex-color (dm/get-prop stop :color)
opacity (dm/get-prop stop :opacity)
argb (clr/hex->u32argb hex-color opacity)
stop-offset (dm/get-prop stop :offset)]
(.setUint32 dview offset argb true)
(.setFloat32 dview (+ offset 4) stop-offset true)
(recur (rest stops) (+ offset GRADIENT-STOP-SIZE)))))))
(defn serialize-linear-fill
[gradient opacity heap-u8 offset]
(let [dview (js/DataView. (.-buffer heap-u8))
[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)
@ -21,14 +35,4 @@
(.setFloat32 dview (+ offset 12) end-y true)
(.setFloat32 dview (+ offset 16) opacity true)
(.setFloat32 dview (+ offset 20) width true)
(loop [stops (seq stops) idx 0]
(when-not (empty? stops)
(let [stop (first stops)
hex-color (dm/get-prop stop :color)
opacity (dm/get-prop stop :opacity)
rgba (clr/hex->u32argb hex-color opacity)
stop-offset (* 100 (dm/get-prop stop :offset))
dview-offset (+ (* idx 5) offset 24)]
(.setUint32 dview dview-offset rgba true)
(.setUint8 dview (+ dview-offset 4) stop-offset)
(recur (rest stops) (+ idx 1)))))))
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))

View file

@ -306,7 +306,10 @@ pub extern "C" fn add_shape_fill_stops() {
let entries: Vec<_> = bytes
.chunks(size_of::<shapes::RawStopData>())
.map(|data| shapes::RawStopData::from_bytes(data.try_into().unwrap()))
.map(|data| {
let raw_stop_bytes: [u8; RAW_STOP_DATA_SIZE] = data.try_into().unwrap();
shapes::RawStopData::from(raw_stop_bytes)
})
.collect();
with_current_shape!(state, |shape: &mut Shape| {
@ -523,7 +526,10 @@ pub extern "C" fn add_shape_stroke_stops() {
let entries: Vec<_> = bytes
.chunks(size_of::<shapes::RawStopData>())
.map(|data| shapes::RawStopData::from_bytes(data.try_into().unwrap()))
.map(|data| {
let raw_stop_bytes: [u8; RAW_STOP_DATA_SIZE] = data.try_into().unwrap();
shapes::RawStopData::from(raw_stop_bytes)
})
.collect();
with_current_shape!(state, |shape: &mut Shape| {

View file

@ -43,13 +43,13 @@ impl RawGradientData {
}
}
pub const RAW_STOP_DATA_SIZE: usize = 5;
pub const RAW_STOP_DATA_SIZE: usize = 8;
#[derive(Debug)]
#[repr(C)]
pub struct RawStopData {
color: u32,
offset: u8,
offset: f32,
}
impl RawStopData {
@ -58,22 +58,16 @@ impl RawStopData {
}
pub fn offset(&self) -> f32 {
self.offset as f32 / 100.0
}
pub fn from_bytes(bytes: [u8; 5]) -> Self {
let color_bytes: [u8; 4] = bytes[0..4].try_into().unwrap();
Self {
color: u32::from_le_bytes(color_bytes),
offset: bytes[4],
}
self.offset
}
}
impl From<[u8; 5]> for RawStopData {
// TODO: remove from_bytes and copy its implementation here
fn from(bytes: [u8; 5]) -> Self {
Self::from_bytes(bytes)
impl From<[u8; RAW_STOP_DATA_SIZE]> for RawStopData {
fn from(bytes: [u8; RAW_STOP_DATA_SIZE]) -> Self {
Self {
color: u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
offset: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
}
}
}