Merge pull request #6379 from penpot/ladybenko-10753-fills-serialization

🎉 Serialize as bytes all fill kinds
This commit is contained in:
Elena Torró 2025-05-07 18:03:42 +02:00 committed by GitHub
commit 46709fb02e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 358 additions and 329 deletions

View file

@ -214,35 +214,26 @@
(let [opacity (or (:fill-opacity fill) 1.0)
color (:fill-color fill)
gradient (:fill-color-gradient fill)
image (:fill-image fill)]
image (:fill-image fill)
offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE)
heap (mem/get-heap-u32)]
(cond
(some? color)
(let [rgba (sr-clr/hex->u32argb color opacity)]
(h/call wasm/internal-module "_add_shape_solid_fill" rgba))
(let [argb (sr-clr/hex->u32argb color opacity)]
(sr-fills/write-solid-fill! offset heap argb)
(h/call wasm/internal-module "_add_shape_fill"))
(some? gradient)
(let [size sr-fills/GRADIENT-BYTE-SIZE
offset (mem/alloc-bytes size)
heap (mem/get-heap-u32)]
(do
(sr-fills/write-gradient-fill! offset heap gradient opacity)
(case (:type gradient)
:linear
(h/call wasm/internal-module "_add_shape_linear_fill")
:radial
(h/call wasm/internal-module "_add_shape_radial_fill")))
(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))]
(h/call wasm/internal-module "_add_shape_image_fill"
(aget buffer 0)
(aget buffer 1)
(aget buffer 2)
(aget buffer 3)
opacity
(dm/get-prop image :width)
(dm/get-prop image :height))
(sr-fills/write-image-fill! offset heap 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))))))
fills))
@ -259,7 +250,9 @@
align (:stroke-alignment stroke)
style (-> stroke :stroke-style sr/translate-stroke-style)
cap-start (-> stroke :stroke-cap-start sr/translate-stroke-cap)
cap-end (-> stroke :stroke-cap-end sr/translate-stroke-cap)]
cap-end (-> stroke :stroke-cap-end sr/translate-stroke-cap)
offset (mem/alloc-bytes sr-fills/FILL-BYTE-SIZE)
heap (mem/get-heap-u32)]
(case align
:inner (h/call wasm/internal-module "_add_shape_inner_stroke" width style cap-start cap-end)
:outer (h/call wasm/internal-module "_add_shape_outer_stroke" width style cap-start cap-end)
@ -267,34 +260,23 @@
(cond
(some? gradient)
(let [size sr-fills/GRADIENT-BYTE-SIZE
offset (mem/alloc-bytes size)
heap (mem/get-heap-u32)]
(let [_ nil]
(sr-fills/write-gradient-fill! offset heap gradient opacity)
(case (:type gradient)
:linear
(h/call wasm/internal-module "_add_shape_stroke_linear_fill")
:radial
(h/call wasm/internal-module "_add_shape_stroke_radial_fill")))
(h/call wasm/internal-module "_add_shape_stroke_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))]
(h/call wasm/internal-module "_add_shape_image_stroke"
(aget buffer 0)
(aget buffer 1)
(aget buffer 2)
(aget buffer 3)
opacity
(dm/get-prop image :width)
(dm/get-prop image :height))
(sr-fills/write-image-fill! offset heap id opacity (dm/get-prop image :width) (dm/get-prop image :height))
(h/call wasm/internal-module "_add_shape_stroke_fill")
(when (== cached-image? 0)
(store-image id)))
(some? color)
(let [rgba (sr-clr/hex->u32argb color opacity)]
(h/call wasm/internal-module "_add_shape_stroke_solid_fill" rgba)))))
(let [argb (sr-clr/hex->u32argb color opacity)]
(sr-fills/write-solid-fill! offset heap argb)
(h/call wasm/internal-module "_add_shape_stroke_fill")))))
strokes))
(defn set-shape-path-attrs

View file

@ -1,5 +1,6 @@
(ns app.render-wasm.serializers.fills
(:require
[app.common.uuid :as uuid]
[app.render-wasm.serializers.color :as clr]))
(def ^:private GRADIENT-STOP-SIZE 8)
@ -10,30 +11,61 @@
(def GRADIENT-BYTE-SIZE
(+ GRADIENT-BASE-SIZE (* MAX-GRADIENT-STOPS GRADIENT-STOP-SIZE)))
(def SOLID-BYTE-SIZE 4)
(def IMAGE-BYTE-SIZE 28)
;; 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 heap-u32 argb]
(let [dview (js/DataView. (.-buffer heap-u32))]
(.setUint8 dview offset 0x00 true)
(.setUint32 dview (+ offset 4) argb true)
(+ offset FILL-BYTE-SIZE)))
(defn write-image-fill!
[offset heap-u32 id opacity width height]
(let [dview (js/DataView. (.-buffer heap-u32))
uuid-buffer (uuid/get-u32 id)]
(.setUint8 dview offset 0x03 true)
(.setUint32 dview (+ offset 4) (aget uuid-buffer 0) true)
(.setUint32 dview (+ offset 8) (aget uuid-buffer 1) true)
(.setUint32 dview (+ offset 12) (aget uuid-buffer 2) true)
(.setUint32 dview (+ offset 16) (aget uuid-buffer 3) true)
(.setFloat32 dview (+ offset 20) opacity true)
(.setInt32 dview (+ offset 24) width true)
(.setInt32 dview (+ offset 28) height true)
(+ offset FILL-BYTE-SIZE)))
(defn write-gradient-fill!
[offset heap gradient opacity]
(let [dview (js/DataView. (.-buffer heap))
[offset heap-u32 gradient opacity]
(let [dview (js/DataView. (.-buffer heap-u32))
start-x (:start-x gradient)
start-y (:start-y gradient)
end-x (:end-x gradient)
end-y (:end-y gradient)
width (or (:width gradient) 0)
stops (take MAX-GRADIENT-STOPS (:stops gradient))]
(.setFloat32 dview offset start-x true)
(.setFloat32 dview (+ offset 4) start-y true)
(.setFloat32 dview (+ offset 8) end-x true)
(.setFloat32 dview (+ offset 12) end-y true)
(.setFloat32 dview (+ offset 16) opacity true)
(.setFloat32 dview (+ offset 20) width true)
(.setUint32 dview (+ offset 24) (count stops) true)
(loop [stops (seq stops) offset (+ offset GRADIENT-BASE-SIZE)]
stops (take MAX-GRADIENT-STOPS (:stops gradient))
type (if (= (:type gradient) :linear) 0x01 0x02)]
(.setUint8 dview offset type true)
(.setFloat32 dview (+ offset 4) start-x true)
(.setFloat32 dview (+ offset 8) start-y true)
(.setFloat32 dview (+ offset 12) end-x true)
(.setFloat32 dview (+ offset 16) end-y true)
(.setFloat32 dview (+ offset 20) opacity true)
(.setFloat32 dview (+ offset 24) width true)
(.setUint8 dview (+ offset 28) (count stops) true)
(loop [stops (seq stops) loop-offset (+ offset 32)]
(if (empty? stops)
offset
(+ offset FILL-BYTE-SIZE)
(let [stop (first stops)
hex-color (:color stop)
opacity (:opacity stop)
argb (clr/hex->u32argb hex-color opacity)
stop-offset (:offset stop)]
(.setUint32 dview offset argb true)
(.setFloat32 dview (+ offset 4) stop-offset true)
(recur (rest stops) (+ offset GRADIENT-STOP-SIZE)))))))
(.setUint32 dview loop-offset argb true)
(.setFloat32 dview (+ loop-offset 4) stop-offset true)
(recur (rest stops) (+ loop-offset GRADIENT-STOP-SIZE)))))))