From e4a1c373bb4af228f0598541cc63e9750a3ee341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Wed, 21 May 2025 14:17:37 +0200 Subject: [PATCH] :sparkles: Only take N amount of fills --- common/src/app/common/types/shape.cljc | 5 ++- frontend/src/app/render_wasm/api.cljs | 42 ++++++++----------- .../app/render_wasm/serializers/fills.cljs | 22 +++++++++- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/common/src/app/common/types/shape.cljc b/common/src/app/common/types/shape.cljc index 4f8f1f313b..e46b009cf0 100644 --- a/common/src/app/common/types/shape.cljc +++ b/common/src/app/common/types/shape.cljc @@ -762,4 +762,7 @@ (cond-> (cfh/text-shape? shape) (patch-text-props props)) (cond-> (cfh/frame-shape? shape) (patch-layout-props props))))) -(def MAX-GRADIENT-STOPS 16) \ No newline at end of file +;; FIXME: Get these from the wasm module, and tweak the values +;; (we'd probably want 12 stops at most) +(def MAX-GRADIENT-STOPS 16) +(def MAX-FILLS 8) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 8a28f19130..6e1a1fa7ae 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -13,6 +13,7 @@ [app.common.geom.matrix :as gmt] [app.common.geom.point :as gpt] [app.common.types.path :as path] + [app.common.types.shape :as shp] [app.common.types.shape.layout :as ctl] [app.common.uuid :as uuid] [app.config :as cf] @@ -241,33 +242,24 @@ [fills] (h/call wasm/internal-module "_clear_shape_fills") (keep (fn [fill] - (let [opacity (or (:fill-opacity fill) 1.0) - color (:fill-color fill) - gradient (:fill-color-gradient fill) - image (:fill-image fill) - offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE) + (let [offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE) heap (mem/get-heap-u8) - dview (js/DataView. (.-buffer heap))] - (cond - (some? color) - (do - (sr-fills/write-solid-fill! offset dview (sr-clr/hex->u32argb color opacity)) - (h/call wasm/internal-module "_add_shape_fill")) - - (some? gradient) - (do - (sr-fills/write-gradient-fill! offset dview gradient opacity) - (h/call wasm/internal-module "_add_shape_fill")) - - (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))] - (sr-fills/write-image-fill! offset dview id opacity (dm/get-prop image :width) (dm/get-prop image :height)) - (h/call wasm/internal-module "_add_shape_fill") - (when (== cached-image? 0) + 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)))))) - fills)) + (take shp/MAX-FILLS fills))) (defn set-shape-strokes [strokes] diff --git a/frontend/src/app/render_wasm/serializers/fills.cljs b/frontend/src/app/render_wasm/serializers/fills.cljs index 4d8257e626..404e29e22f 100644 --- a/frontend/src/app/render_wasm/serializers/fills.cljs +++ b/frontend/src/app/render_wasm/serializers/fills.cljs @@ -1,12 +1,12 @@ (ns app.render-wasm.serializers.fills (:require + [app.common.data.macros :as dm] [app.common.types.shape :as shp] [app.common.uuid :as uuid] [app.render-wasm.serializers.color :as clr])) (def ^:private GRADIENT-STOP-SIZE 8) - (def GRADIENT-BYTE-SIZE 156) (def SOLID-BYTE-SIZE 4) (def IMAGE-BYTE-SIZE 28) @@ -14,6 +14,7 @@ ;; FIXME: get it from the wasm module (def FILL-BYTE-SIZE (+ 4 (max GRADIENT-BYTE-SIZE IMAGE-BYTE-SIZE SOLID-BYTE-SIZE))) + (defn write-solid-fill! [offset dview argb] (.setUint8 dview offset 0x00 true) @@ -60,4 +61,21 @@ stop-offset (:offset stop)] (.setUint32 dview loop-offset argb true) (.setFloat32 dview (+ loop-offset 4) stop-offset true) - (recur (rest stops) (+ loop-offset GRADIENT-STOP-SIZE))))))) \ No newline at end of file + (recur (rest stops) (+ loop-offset GRADIENT-STOP-SIZE))))))) + +(defn write-fill! + [offset dview fill] + (let [opacity (or (:fill-opacity fill) 1.0) + color (:fill-color fill) + gradient (:fill-color-gradient fill) + image (:fill-image fill)] + (cond + (some? color) + (write-solid-fill! offset dview (clr/hex->u32argb color opacity)) + + (some? gradient) + (write-gradient-fill! offset dview gradient opacity) + + (some? image) + (let [id (dm/get-prop image :id)] + (write-image-fill! offset dview id opacity (dm/get-prop image :width) (dm/get-prop image :height)))))) \ No newline at end of file