Only take N amount of fills

This commit is contained in:
Belén Albeza 2025-05-21 14:17:37 +02:00
parent be13704934
commit e4a1c373bb
3 changed files with 41 additions and 28 deletions

View file

@ -762,4 +762,7 @@
(cond-> (cfh/text-shape? shape) (patch-text-props props)) (cond-> (cfh/text-shape? shape) (patch-text-props props))
(cond-> (cfh/frame-shape? shape) (patch-layout-props props))))) (cond-> (cfh/frame-shape? shape) (patch-layout-props props)))))
(def MAX-GRADIENT-STOPS 16) ;; 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)

View file

@ -13,6 +13,7 @@
[app.common.geom.matrix :as gmt] [app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.types.path :as path] [app.common.types.path :as path]
[app.common.types.shape :as shp]
[app.common.types.shape.layout :as ctl] [app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.config :as cf] [app.config :as cf]
@ -241,33 +242,24 @@
[fills] [fills]
(h/call wasm/internal-module "_clear_shape_fills") (h/call wasm/internal-module "_clear_shape_fills")
(keep (fn [fill] (keep (fn [fill]
(let [opacity (or (:fill-opacity fill) 1.0) (let [offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE)
color (:fill-color fill)
gradient (:fill-color-gradient fill)
image (:fill-image fill)
offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE)
heap (mem/get-heap-u8) heap (mem/get-heap-u8)
dview (js/DataView. (.-buffer heap))] dview (js/DataView. (.-buffer heap))
(cond image (:fill-image fill)]
(some? color) (sr-fills/write-fill! offset dview fill)
(do (h/call wasm/internal-module "_add_shape_fill")
(sr-fills/write-solid-fill! offset dview (sr-clr/hex->u32argb color opacity)) ;; store image for image fills if not cached
(h/call wasm/internal-module "_add_shape_fill")) (when (some? image)
(let [id (dm/get-prop image :id)
(some? gradient) buffer (uuid/get-u32 id)
(do cached-image? (h/call wasm/internal-module "_is_image_cached"
(sr-fills/write-gradient-fill! offset dview gradient opacity) (aget buffer 0)
(h/call wasm/internal-module "_add_shape_fill")) (aget buffer 1)
(aget buffer 2)
(some? image) (aget buffer 3))]
(let [id (dm/get-prop image :id) (when (zero? cached-image?)
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)
(store-image id)))))) (store-image id))))))
fills)) (take shp/MAX-FILLS fills)))
(defn set-shape-strokes (defn set-shape-strokes
[strokes] [strokes]

View file

@ -1,12 +1,12 @@
(ns app.render-wasm.serializers.fills (ns app.render-wasm.serializers.fills
(:require (:require
[app.common.data.macros :as dm]
[app.common.types.shape :as shp] [app.common.types.shape :as shp]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.render-wasm.serializers.color :as clr])) [app.render-wasm.serializers.color :as clr]))
(def ^:private GRADIENT-STOP-SIZE 8) (def ^:private GRADIENT-STOP-SIZE 8)
(def GRADIENT-BYTE-SIZE 156) (def GRADIENT-BYTE-SIZE 156)
(def SOLID-BYTE-SIZE 4) (def SOLID-BYTE-SIZE 4)
(def IMAGE-BYTE-SIZE 28) (def IMAGE-BYTE-SIZE 28)
@ -14,6 +14,7 @@
;; FIXME: get it from the wasm module ;; FIXME: get it from the wasm module
(def FILL-BYTE-SIZE (+ 4 (max GRADIENT-BYTE-SIZE IMAGE-BYTE-SIZE SOLID-BYTE-SIZE))) (def FILL-BYTE-SIZE (+ 4 (max GRADIENT-BYTE-SIZE IMAGE-BYTE-SIZE SOLID-BYTE-SIZE)))
(defn write-solid-fill! (defn write-solid-fill!
[offset dview argb] [offset dview argb]
(.setUint8 dview offset 0x00 true) (.setUint8 dview offset 0x00 true)
@ -60,4 +61,21 @@
stop-offset (:offset stop)] stop-offset (:offset stop)]
(.setUint32 dview loop-offset argb true) (.setUint32 dview loop-offset argb true)
(.setFloat32 dview (+ loop-offset 4) stop-offset true) (.setFloat32 dview (+ loop-offset 4) stop-offset true)
(recur (rest stops) (+ loop-offset GRADIENT-STOP-SIZE))))))) (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))))))