Merge pull request #6623 from penpot/ladybenko-11106-send-all-fills

 Send all fills of a shape in a single wasm call
This commit is contained in:
Elena Torró 2025-06-04 09:18:39 +02:00 committed by GitHub
commit bbac5d050e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 20 deletions

View file

@ -240,26 +240,36 @@
(defn set-shape-fills
[fills]
(h/call wasm/internal-module "_clear_shape_fills")
(keep (fn [fill]
(let [offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE)
heap (mem/get-heap-u8)
dview (js/DataView. (.-buffer heap))
image (:fill-image fill)]
(sr-fills/write-fill! offset dview fill)
(h/call wasm/internal-module "_add_shape_fill")
;; store image for image fills if not cached
(when (some? image)
(let [id (dm/get-prop image :id)
buffer (uuid/get-u32 id)
cached-image? (h/call wasm/internal-module "_is_image_cached"
(aget buffer 0)
(aget buffer 1)
(aget buffer 2)
(aget buffer 3))]
(when (zero? cached-image?)
(store-image id))))))
(take shp/MAX-FILLS fills)))
(let [fills (take shp/MAX-FILLS fills)
image-fills (filter :fill-image fills)
offset (mem/alloc-bytes (* (count fills) sr-fills/FILL-BYTE-SIZE))
heap (mem/get-heap-u8)
dview (js/DataView. (.-buffer heap))]
;; write fill data to heap
(loop [fills (seq fills)
current-offset 0]
(when-not (empty? fills)
(let [fill (first fills)]
(sr-fills/write-fill! offset dview fill)
(recur (rest fills) (+ current-offset sr-fills/FILL-BYTE-SIZE)))))
;; send fills to wasm
(h/call wasm/internal-module "_set_shape_fills")
;; load images for image fills if not cached
(keep (fn [fill]
(let [image (:fill-image fill)
id (dm/get-prop image :id)
buffer (uuid/get-u32 id)
cached-image? (h/call wasm/internal-module "_is_image_cached"
(aget buffer 0)
(aget buffer 1)
(aget buffer 2)
(aget buffer 3))]
(when (zero? cached-image?)
(store-image id))))
image-fills)))
(defn set-shape-strokes
[strokes]

View file

@ -495,6 +495,10 @@ impl Shape {
self.fills.iter()
}
pub fn set_fills(&mut self, fills: Vec<Fill>) {
self.fills = fills;
}
pub fn add_fill(&mut self, f: Fill) {
self.fills.push(f);
}

View file

@ -63,6 +63,15 @@ pub fn parse_fills_from_bytes(buffer: &[u8], num_fills: usize) -> Vec<shapes::Fi
.collect()
}
#[no_mangle]
pub extern "C" fn set_shape_fills() {
with_current_shape!(state, |shape: &mut Shape| {
let bytes = mem::bytes();
let fills = parse_fills_from_bytes(&bytes, bytes.len() / RAW_FILL_DATA_SIZE);
shape.set_fills(fills);
});
}
#[no_mangle]
pub extern "C" fn add_shape_fill() {
with_current_shape!(state, |shape: &mut Shape| {