diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 83060faba..a4e02bee9 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -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] diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 9e1cd2aa2..b719718c5 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -495,6 +495,10 @@ impl Shape { self.fills.iter() } + pub fn set_fills(&mut self, fills: Vec) { + self.fills = fills; + } + pub fn add_fill(&mut self, f: Fill) { self.fills.push(f); } diff --git a/render-wasm/src/wasm/fills.rs b/render-wasm/src/wasm/fills.rs index 6978b2360..9b8d2588d 100644 --- a/render-wasm/src/wasm/fills.rs +++ b/render-wasm/src/wasm/fills.rs @@ -63,6 +63,15 @@ pub fn parse_fills_from_bytes(buffer: &[u8], num_fills: usize) -> Vec