♻️ Share WASM modifier bindings (#13016)

- Extract modifier encoding and FFI operations into common bindings
  for renderer benchmarks and application callers.
- Keep readiness checks and render scheduling in the frontend API.
- Release propagation output after decoding failures.
- Cover payload ownership, heap growth, snapping and interactive
  lifecycle behaviour with shared fixtures and integration tests.

AI-assisted-by: gpt-6.1-sol, deepseek-flash
This commit is contained in:
Miguel de Benito Delgado 2026-11-17 13:28:33 +01:00 committed by GitHub
parent 94b13d08fe
commit 6043edb95a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 576 additions and 136 deletions

View File

@ -5,12 +5,12 @@ layouts, memory helpers and serializers that turn Penpot shapes into the buffers
`render-wasm` consumes.
The workspace drives it from `app.render-wasm.*`, the headless exporter from
`app.wasm.*` — same code underneath, so the two cannot drift.
`app.wasm.*`, the renderer benchmarking code from `benches.render-wasm.*`.
Font knowledge is *not* here even though both hosts need it for rendering: it is
not specific to the wasm backend, so the google fonts catalog (baked from
`common/resources/fonts/gfonts.*.json`), the bundled builtin family and the
emoji/script fallback tables live in `app.common.fonts`. Likewise the image-id
emoji/script fallback tables live in `app.common.fonts`. Likewise, the image-id
enumeration lives in `app.common.types.shape.images`.
`shared.js` is not here: it is a per-build artifact, so each host compiles

View File

@ -0,0 +1,171 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns app.common.render-wasm.api.modifiers
"Modifier operations for an initialized WASM renderer.
Shared by the frontend and the renderer benchmarking code.
Calls share one payload slot and must run without overlap. The caller owns
renderer initialization and render scheduling."
(:require
[app.common.data :as d]
[app.common.geom.matrix :as gmt]
[app.common.render-wasm.helpers :as h]
[app.common.render-wasm.mem :as mem]
[app.common.render-wasm.mem.heap32 :as mem.h32]
[app.common.render-wasm.serializers :as sr]
[app.common.render-wasm.wasm :as wasm]
[app.common.uuid :as uuid]))
(def ^:private ^:const MODIFIER-U8-SIZE 40)
(def ^:private ^:const MODIFIER-U32-SIZE (/ MODIFIER-U8-SIZE 4))
(def ^:private ^:const INPUT-MODIFIER-U8-SIZE 44)
(def ^:private ^:const STRUCTURE-MODIFIER-U8-SIZE 44)
(def ^:private pixel-precision
{:disabled 0
:both 1
:only-x 2
:only-y 3})
(defn- pixel-precision-mode
[snap-pixel? snap-ignore-axis]
(pixel-precision
(cond
(not snap-pixel?) :disabled
(= :x snap-ignore-axis) :only-y
(= :y snap-ignore-axis) :only-x
:else :both)))
(defn- get-modifier-entry
[heapu32 heapf32 offset]
(let [id1 (aget heapu32 (+ offset 0))
id2 (aget heapu32 (+ offset 1))
id3 (aget heapu32 (+ offset 2))
id4 (aget heapu32 (+ offset 3))
a (aget heapf32 (+ offset 4))
b (aget heapf32 (+ offset 5))
c (aget heapf32 (+ offset 6))
d (aget heapf32 (+ offset 7))
e (aget heapf32 (+ offset 8))
f (aget heapf32 (+ offset 9))]
(d/vec2 (uuid/from-unsigned-parts id1 id2 id3 id4)
(gmt/matrix a b c d e f))))
(defn- get-modifiers
[pointer]
(let [offset (mem/->offset-32 pointer)
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)
length (aget heapu32 offset)
start (inc offset)
end (+ start (* length MODIFIER-U32-SIZE))]
(mapv (partial get-modifier-entry heapu32 heapf32)
(range start end MODIFIER-U32-SIZE))))
(defn propagate-modifiers
"Propagates geometry transforms through the shape tree.
Rounds geometry to the pixel grid when snapping is enabled, except on the
ignored axis. Requires an initialized renderer and enum bindings.
Args:
entries: Counted collection of [uuid {:transform matrix :kind kind}],
where kind is :parent or :child.
snap-pixel?: Whether to round geometry to the pixel grid.
snap-ignore-axis: Axis to leave unsnapped, :x, :y or nil.
Returns:
A vector of [uuid matrix] transforms, or [] for empty entries."
[entries snap-pixel? snap-ignore-axis]
(if (empty? entries)
[]
(let [size (mem/get-alloc-size entries INPUT-MODIFIER-U8-SIZE)
offset (mem/alloc->offset-32 size)
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)]
(reduce (fn [offset [id {:keys [transform kind]}]]
(-> offset
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-matrix heapf32 transform)
(mem.h32/write-u32 heapu32 (sr/translate-transform-entry-kind kind))))
offset
entries)
(let [precision (pixel-precision-mode snap-pixel? snap-ignore-axis)
pointer (h/call wasm/internal-module "_propagate_modifiers" precision)]
(try
(get-modifiers pointer)
(finally
(mem/free)))))))
(defn set-modifiers
"Sets temporary geometry transforms for rendering.
Empty modifiers leave the current preview unchanged.
Args:
modifiers: Vector of [uuid matrix] transforms."
[modifiers]
(when (seq modifiers)
(let [size (mem/get-alloc-size modifiers MODIFIER-U8-SIZE)
offset (mem/alloc->offset-32 size)
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
(reduce (fn [offset [id transform]]
(-> offset
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-matrix heapf32 transform)))
offset
modifiers)
(h/call wasm/internal-module "_set_modifiers")))
nil)
(defn set-structure-modifiers
"Sets temporary changes to shape structure and content scaling.
Empty entries leave the current structure modifiers unchanged.
Args:
entries: Counted collection of maps with :type, :parent, :id, :index
and :value. A missing index defaults to zero."
[entries]
(when (seq entries)
(let [size (mem/get-alloc-size entries STRUCTURE-MODIFIER-U8-SIZE)
offset (mem/alloc->offset-32 size)
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
(reduce (fn [offset {:keys [type parent id index value]}]
(-> offset
(mem.h32/write-u32 heapu32 (sr/translate-structure-modifier-type type))
(mem.h32/write-u32 heapu32 (d/nilv index 0))
(mem.h32/write-uuid heapu32 parent)
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-f32 heapf32 value)))
offset
entries)
(h/call wasm/internal-module "_set_structure_modifiers")))
nil)
(defn set-modifiers-start
"Enables interactive rendering for shape transforms."
[]
(h/call wasm/internal-module "_set_modifiers_start")
nil)
(defn set-modifiers-end
"Ends interactive rendering for shape transforms.
A subsequent render produces the final full-quality image."
[]
(h/call wasm/internal-module "_set_modifiers_end")
nil)
(defn clean-modifiers
"Clears temporary geometry, structure and content-scaling modifiers."
[]
(h/call wasm/internal-module "_clean_modifiers")
nil)

View File

@ -0,0 +1,131 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns app.common.test-helpers.render-wasm
(:require
[app.common.geom.matrix :as gmt]
[app.common.uuid :as uuid]))
(def serializers
#js {"transform-entry-kind" #js {"parent" 0 "child" 1}})
(defn- get-uuid
[view offset]
(uuid/from-unsigned-parts
(.getUint32 view offset true)
(.getUint32 view (+ offset 4) true)
(.getUint32 view (+ offset 8) true)
(.getUint32 view (+ offset 12) true)))
(defn- get-matrix
[view offset]
(apply gmt/matrix
(mapv #(.getFloat32 view (+ offset (* 4 %)) true) (range 6))))
(defn- write-transform
[view offset [id matrix]]
(doseq [[index value] (map-indexed vector (uuid/get-u32 id))]
(.setUint32 view (+ offset (* 4 index)) value true))
(doseq [[index key] (map-indexed vector [:a :b :c :d :e :f])]
(.setFloat32 view (+ offset 16 (* 4 index)) (get matrix key) true)))
(defn create-module
"Creates a fake module implementing modifier payload exchanges.
Args:
options: Map with :output transforms, :grow-on-alloc, :grow-on-propagate,
:propagate-error or :set-error code, and :invalid-output-pointer.
Returns:
A map with :module and an atom :state containing decoded native inputs,
the active payload slot, previews and interactive mode."
[{:keys [output grow-on-alloc grow-on-propagate propagate-error set-error invalid-output-pointer]
:or {output []}}]
(let [module #js {}
state (atom {:slot nil :propagated nil :preview [] :structure []
:interactive false :error-code 0})
resize (fn [size]
(let [buffer (js/ArrayBuffer. size)]
(set! (.-HEAPU8 module) (js/Uint8Array. buffer))
(set! (.-HEAPU32 module) (js/Uint32Array. buffer))
(set! (.-HEAPF32 module) (js/Float32Array. buffer))))
alloc (fn [size]
(when (or (:slot @state) (not (pos? size)))
(throw (js/Error. "Payload slot is unavailable")))
(when grow-on-alloc
(resize (* 2 (.-byteLength (.-HEAPU8 module)))))
(swap! state assoc :slot {:offset 64 :size size})
64)
take-input
(fn [entry-size decode]
(let [{:keys [offset size]} (:slot @state)]
(when (or (nil? offset) (not (zero? (mod size entry-size))))
(throw (js/Error. "Invalid modifier payload")))
(swap! state assoc :slot nil :error-code 0)
(let [view (js/DataView. (.-buffer (.-HEAPU8 module)))]
(mapv #(decode view (+ offset %)) (range 0 size entry-size)))))
get-transform
(fn [view offset]
[(get-uuid view offset) (get-matrix view (+ offset 16))])]
(resize 4096)
(set! (.-_alloc_bytes module) alloc)
(set! (.-_free_bytes module)
(fn []
(swap! state assoc :slot nil :error-code 0)
js/undefined))
(set! (.-_read_error_code module) (fn [] (:error-code @state)))
(set! (.-_propagate_modifiers module)
(fn [precision]
(let [entries (take-input
44
(fn [view offset]
[(get-uuid view offset)
{:transform (get-matrix view (+ offset 16))
:kind (case (.getUint32 view (+ offset 40) true)
0 :parent
1 :child)}]))]
(swap! state assoc :propagated {:precision precision :entries entries})
(when propagate-error
(swap! state assoc :error-code propagate-error)
(throw (js/Error. "Native propagation failed")))
(when grow-on-propagate
(resize (* 2 (.-byteLength (.-HEAPU8 module)))))
(let [pointer (alloc (+ 4 (* 40 (count output))))
view (js/DataView. (.-buffer (.-HEAPU8 module)))]
(.setUint32 view pointer (count output) true)
(doseq [[index entry] (map-indexed vector output)]
(write-transform view (+ pointer 4 (* 40 index)) entry))
(if invalid-output-pointer
#js {"valueOf" (fn [] (throw (js/Error. "Invalid output pointer")))}
pointer)))))
(set! (.-_set_modifiers module)
(fn []
(let [transforms (take-input 40 get-transform)]
(when set-error
(swap! state assoc :error-code set-error)
(throw (js/Error. "Native preview failed")))
(swap! state assoc :preview transforms))))
(set! (.-_set_structure_modifiers module)
(fn []
(swap! state assoc :structure
(take-input
44
(fn [view offset]
{:type (case (.getUint32 view offset true)
1 :remove-children
2 :add-children
3 :scale-content)
:index (.getUint32 view (+ offset 4) true)
:parent (get-uuid view (+ offset 8))
:id (get-uuid view (+ offset 24))
:value (.getFloat32 view (+ offset 40) true)})))))
(set! (.-_set_modifiers_start module)
(fn [] (swap! state assoc :interactive true)))
(set! (.-_set_modifiers_end module)
(fn [] (swap! state assoc :interactive false)))
(set! (.-_clean_modifiers module)
(fn [] (swap! state assoc :preview [] :structure [])))
{:module module :state state}))

View File

@ -0,0 +1,111 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns common-tests.render-wasm.modifiers-test
(:require
[app.common.geom.matrix :as gmt]
[app.common.render-wasm.api.modifiers :as modifiers]
[app.common.render-wasm.wasm :as wasm]
[app.common.test-helpers.render-wasm :as thw]
[clojure.test :as t]))
(def id-a #uuid "f1234567-89ab-cdef-fedc-ba9876543210")
(def id-b #uuid "12345678-abcd-ef01-2345-6789abcdef01")
(def matrix-a (gmt/matrix 0.1 -0.2 3.75 4.125 120.5 -80.25))
(def matrix-b (gmt/matrix -1 0.5 0.25 2 32 -64))
(defn- f32-matrix
[matrix]
(gmt/matrix (js/Math.fround (:a matrix))
(js/Math.fround (:b matrix))
(js/Math.fround (:c matrix))
(js/Math.fround (:d matrix))
(js/Math.fround (:e matrix))
(js/Math.fround (:f matrix))))
(def requested [[id-a {:transform matrix-a :kind :child}]
[id-b {:transform matrix-b :kind :parent}]])
(t/deftest propagation-results-can-be-applied-after-memory-growth
(let [output [[id-b matrix-a] [id-a matrix-b]]
{:keys [module state]} (thw/create-module {:output output
:grow-on-propagate true
:grow-on-alloc true})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(let [result (modifiers/propagate-modifiers requested false nil)
expected (mapv (fn [[id matrix]] [id (f32-matrix matrix)]) output)
inputs (mapv (fn [[id data]] [id (update data :transform f32-matrix)])
requested)]
(t/is (= expected result))
(t/is (= {:precision 0 :entries inputs} (:propagated @state)))
(modifiers/set-modifiers result)
(t/is (= expected (:preview @state)))))))
(t/deftest pixel-snapping-respects-the-ignored-axis
(doseq [[snap ignore-axis precision] [[false nil 0]
[false :x 0]
[true nil 1]
[true :x 3]
[true :y 2]]]
(let [{:keys [module state]} (thw/create-module {})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(modifiers/propagate-modifiers requested snap ignore-axis)
(t/is (= precision (get-in @state [:propagated :precision])))))))
(t/deftest empty-propagation-result-releases-the-payload
(let [{:keys [module state]} (thw/create-module {})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(t/is (= [] (modifiers/propagate-modifiers requested false nil)))
(modifiers/set-modifiers [[id-a matrix-a]])
(t/is (= [[id-a (f32-matrix matrix-a)]] (:preview @state))))))
(t/deftest structure-entries-preserve-order-and-default-index
(let [entries [{:type :remove-children :parent id-a :id id-b :value 0}
{:type :add-children :index 7 :parent id-b :id id-a :value 2.5}
{:type :scale-content :index 2 :parent id-a :id id-b :value 0.1}]
{:keys [module state]} (thw/create-module {:grow-on-alloc true})]
(with-redefs [wasm/internal-module module]
(modifiers/set-structure-modifiers entries)
(t/is (= (-> entries
(assoc-in [0 :index] 0)
(update-in [2 :value] js/Math.fround))
(:structure @state))))))
(t/deftest empty-inputs-preserve-an-existing-preview
(let [{:keys [module state]} (thw/create-module {})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(modifiers/set-modifiers [[id-a matrix-a]])
(modifiers/set-structure-modifiers
[{:type :add-children :index 1 :parent id-a :id id-b :value 0}])
(let [before @state]
(t/is (= [] (modifiers/propagate-modifiers [] true nil)))
(modifiers/set-modifiers [])
(modifiers/set-structure-modifiers [])
(t/is (= before @state))))))
(t/deftest native-propagation-errors-preserve-diagnostics-and-release-input
(let [{:keys [module state]} (thw/create-module {:propagate-error 1})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(let [error (try
(modifiers/propagate-modifiers requested false nil)
(catch :default cause cause))]
(t/is (= {:type :wasm-error :code :non-blocking :fn "_propagate_modifiers"}
(select-keys (ex-data error) [:type :code :fn]))))
(modifiers/set-modifiers [[id-b matrix-b]])
(t/is (= [[id-b matrix-b]] (:preview @state))))))
(t/deftest undecodable-output-releases-the-payload
(let [{:keys [module state]} (thw/create-module {:output [[id-a matrix-a]]
:invalid-output-pointer true})]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers]
(t/is (thrown? js/Error (modifiers/propagate-modifiers requested false nil)))
(modifiers/set-modifiers [[id-b matrix-b]])
(t/is (= [[id-b matrix-b]] (:preview @state))))))

View File

@ -11,6 +11,7 @@
#?(:cljs [clojure.string :as str])
#?(:cljs [clojure.tools.cli :refer [parse-opts]])
#?(:cljs [common-tests.fonts-test])
#?(:cljs [common-tests.render-wasm.modifiers-test])
#?(:cljs [goog.object :as gobj])
[clojure.test :as t]
[common-tests.attrs-test]
@ -138,6 +139,7 @@
'common-tests.exceptions-test
'common-tests.files-builder-test
#?(:cljs 'common-tests.fonts-test)
#?(:cljs 'common-tests.render-wasm.modifiers-test)
'common-tests.files-changes-test
'common-tests.files-migrations-0025-test
'common-tests.files-migrations-0026-test

View File

@ -16,6 +16,7 @@
[app.common.fonts :as cfnt]
[app.common.logging :as log]
[app.common.math :as mth]
[app.common.render-wasm.api.modifiers :as modifiers]
[app.common.render-wasm.api.props :as props]
[app.common.render-wasm.api.select :as wselect]
[app.common.render-wasm.helpers :as h]
@ -229,13 +230,6 @@
(def ^:const UUID-U8-SIZE 16)
(def ^:const UUID-U32-SIZE (/ UUID-U8-SIZE 4))
;; FIXME: Migrate this as we adjust the DTO structure in wasm
(def ^:const MODIFIER-U8-SIZE 40)
(def ^:const MODIFIER-U32-SIZE (/ MODIFIER-U8-SIZE 4))
(def ^:const MODIFIER-TRANSFORM-U8-OFFSET-SIZE 16)
(def ^:const INPUT-MODIFIER-U8-SIZE 44)
(def ^:const INPUT-MODIFIER-U32-SIZE (/ INPUT-MODIFIER-U8-SIZE 4))
(def ^:const GRID-LAYOUT-ROW-U8-SIZE 8)
(def ^:const GRID-LAYOUT-COLUMN-U8-SIZE 8)
(def ^:const GRID-LAYOUT-CELL-U8-SIZE 36)
@ -2101,87 +2095,25 @@
(defn set-structure-modifiers
[entries]
(when (and (initialized?) (not ^boolean (empty? entries)))
(let [size (mem/get-alloc-size entries 44)
offset (mem/alloc->offset-32 size)
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
(reduce (fn [offset {:keys [type parent id index value]}]
(-> offset
(mem.h32/write-u32 heapu32 (sr/translate-structure-modifier-type type))
(mem.h32/write-u32 heapu32 (d/nilv index 0))
(mem.h32/write-uuid heapu32 parent)
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-f32 heapf32 value)))
offset
entries)
(h/call wasm/internal-module "_set_structure_modifiers"))))
(when (initialized?)
(modifiers/set-structure-modifiers entries)))
;; Axes the pixel grid rounds, as `propagate_modifiers` expects them.
(def ^:private pixel-precision
{:disabled 0
:both 1
:only-x 2
:only-y 3})
(defn- pixel-precision-mode
"Encodes the pixel grid snapping for the renderer. `snap-ignore-axis`
names the axis to leave alone (`:x`, `:y` or nil)."
[snap-pixel? snap-ignore-axis]
(pixel-precision
(cond
(not snap-pixel?) :disabled
(= :x snap-ignore-axis) :only-y
(= :y snap-ignore-axis) :only-x
:else :both)))
(defn propagate-modifiers
"Propagates geometry modifiers through the WASM shape tree.
"Propagates geometry transforms when the renderer is ready.
Rounds the resulting geometry to the pixel grid when `snap-pixel?` is set,
skipping the axis named by `snap-ignore-axis` (`:x`, `:y` or nil).
Args:
entries: Collection of [uuid {:transform matrix :kind kind}].
snap-pixel?: Whether to round geometry to the pixel grid.
snap-ignore-axis: Axis to leave unsnapped, :x, :y or nil.
Always returns a vector. When the context is not ready (lost / mid-reload)
or `entries` is empty, returns `[]` so callers never receive `nil` (which
would trip `set-modifiers`' vector assert)."
Returns:
A vector of [uuid matrix], or [] for an unavailable renderer or empty
entries."
[entries snap-pixel? snap-ignore-axis]
(if-not (and (initialized?) (not ^boolean (empty? entries)))
[]
(let [size (mem/get-alloc-size entries INPUT-MODIFIER-U8-SIZE)
offset (mem/alloc->offset-32 size)
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)]
(reduce (fn [offset [id data]]
(let [transform (:transform data)
kind (:kind data)]
(-> offset
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-matrix heapf32 transform)
(mem.h32/write-u32 heapu32 (sr/translate-transform-entry-kind kind)))))
offset
entries)
(let [precision (pixel-precision-mode snap-pixel? snap-ignore-axis)
offset (-> (h/call wasm/internal-module "_propagate_modifiers" precision)
(mem/->offset-32))
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)
length (aget heapu32 offset)
max-offset (+ offset 1 (* length MODIFIER-U32-SIZE))
result (loop [result (transient [])
offset (inc offset)]
(if (< offset max-offset)
(let [entry (dr/read-modifier-entry heapu32 heapf32 offset)]
(recur (conj! result entry)
(+ offset MODIFIER-U32-SIZE)))
(persistent! result)))]
(mem/free)
result))))
(if (initialized?)
(modifiers/propagate-modifiers entries snap-pixel? snap-ignore-axis)
[]))
(defn get-selection-rect
[entries]
@ -2216,48 +2148,29 @@
(defn clean-modifiers
[]
(when (initialized?)
(h/call wasm/internal-module "_clean_modifiers")))
(modifiers/clean-modifiers)))
(defn set-modifiers-start
"Enter interactive transform mode (drag / resize / rotate). Enables
fast-mode effect skipping in the renderer and activates an atlas
backdrop so tiles do not appear sequentially or flicker while the
gesture is in progress."
"Enables interactive rendering for shape transforms when the renderer is ready."
[]
(when (initialized?)
(h/call wasm/internal-module "_set_modifiers_start")))
(modifiers/set-modifiers-start)))
(defn set-modifiers-end
"Leave interactive transform mode. Cancels any pending async render
scheduled under it; the caller is expected to trigger a full-quality
render (via `request-render`) once the gesture is committed."
"Ends interactive rendering for shape transforms when the renderer is ready.
A subsequent render produces the final full-quality image."
[]
(when (initialized?)
(h/call wasm/internal-module "_set_modifiers_end")))
(modifiers/set-modifiers-end)))
(defn set-modifiers
[modifiers]
[entries]
(when (initialized?)
;; Assert only when we would touch WASM; callers may still build
;; modifiers while the context is unavailable.
(assert (vector? modifiers) "expected a vector for `set-modifiers`")
(let [length (count modifiers)]
(when (pos? length)
(let [offset (mem/alloc->offset-32 (* MODIFIER-U8-SIZE length))
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
(reduce (fn [offset [id transform]]
(-> offset
(mem.h32/write-uuid heapu32 id)
(mem.h32/write-matrix heapf32 transform)))
offset
modifiers)
(h/call wasm/internal-module "_set_modifiers")
(request-render "set-modifiers"))))))
(assert (vector? entries) "expected a vector for `set-modifiers`")
(when (seq entries)
(modifiers/set-modifiers entries)
(request-render "set-modifiers"))))
(defn initialize-viewport
[base-objects zoom vbox &

View File

@ -5,28 +5,8 @@
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns app.render-wasm.deserializers
(:require
[app.common.data :as d]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.uuid :as uuid]))
(defn read-modifier-entry
[heapu32 heapf32 offset]
(let [id1 (aget heapu32 (+ offset 0))
id2 (aget heapu32 (+ offset 1))
id3 (aget heapu32 (+ offset 2))
id4 (aget heapu32 (+ offset 3))
a (aget heapf32 (+ offset 4))
b (aget heapf32 (+ offset 5))
c (aget heapf32 (+ offset 6))
d (aget heapf32 (+ offset 7))
e (aget heapf32 (+ offset 8))
f (aget heapf32 (+ offset 9))]
(d/vec2 (uuid/from-unsigned-parts id1 id2 id3 id4)
(gmt/matrix a b c d e f))))
[app.common.geom.point :as gpt]))
(defn read-selection-rect
[heapf32 offset]

View File

@ -0,0 +1,130 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.render-wasm.modifiers-test
(:require
[app.common.geom.matrix :as gmt]
[app.common.render-wasm.api.modifiers :as modifiers]
[app.common.render-wasm.wasm :as wasm]
[app.common.test-helpers.render-wasm :as thw]
[app.render-wasm.api :as api]
[app.util.timers :as timers]
[cljs.test :as t :include-macros true]))
(def id-a #uuid "f1234567-89ab-cdef-fedc-ba9876543210")
(def id-b #uuid "12345678-abcd-ef01-2345-6789abcdef01")
(def transform (gmt/matrix 1 0 0 1 24 -16))
(defn- with-renderer
[options f]
(let [{:keys [module] :as env} (thw/create-module options)
frames (atom [])]
(with-redefs [wasm/internal-module module
wasm/serializers thw/serializers
wasm/context-initialized? true
wasm/context-lost? (atom false)
wasm/reloading? (atom false)
wasm/disable-request-render? (atom false)
wasm/internal-frame-id nil
timers/raf (fn [callback]
(swap! frames conj callback)
(count @frames))]
(try
(f (assoc env :frames frames))
(finally
(with-redefs [wasm/context-initialized? false]
(doseq [callback @frames]
(callback 0))))))))
(t/deftest unavailable-renderers-preserve-existing-modifiers
(doseq [[initialized lost reloading] [[false false false]
[true true false]
[true false true]]
interactive [false true]]
(with-renderer
{}
(fn [{:keys [state frames]}]
(modifiers/set-modifiers [[id-a transform]])
(modifiers/set-structure-modifiers
[{:type :add-children :parent id-a :id id-b :value 0}])
(when interactive
(modifiers/set-modifiers-start))
(let [before @state]
(with-redefs [wasm/context-initialized? initialized
wasm/context-lost? (atom lost)
wasm/reloading? (atom reloading)]
(t/is (= [] (api/propagate-modifiers
[[id-a {:transform transform :kind :child}]] true nil)))
(api/set-modifiers nil)
(api/set-structure-modifiers
[{:type :add-children :parent id-b :id id-a :index 9 :value 0}])
(api/clean-modifiers)
(api/set-modifiers-start)
(api/set-modifiers-end)
(t/is (= before @state))
(t/is (empty? @frames))))))))
(t/deftest applying-a-preview-schedules-a-render
(with-renderer
{}
(fn [{:keys [state frames]}]
(api/set-modifiers [])
(t/is (empty? @frames))
(t/is (thrown? js/Error (api/set-modifiers nil)))
(api/set-modifiers [[id-a transform] [id-b transform]])
(t/is (= [[id-a transform] [id-b transform]] (:preview @state)))
(t/is (= 1 (count @frames))))))
(t/deftest propagation-and-structure-updates-leave-rendering-explicit
(let [output [[id-b transform]]]
(with-renderer
{:output output}
(fn [{:keys [state frames]}]
(let [entries [[id-a {:transform transform :kind :child}]]]
(t/is (= output (api/propagate-modifiers entries true :x)))
(t/is (= {:precision 3 :entries entries} (:propagated @state))))
(api/set-structure-modifiers
[{:type :add-children :parent id-a :id id-b :index 2 :value 0}])
(t/is (= [{:type :add-children :parent id-a :id id-b :index 2 :value 0}]
(:structure @state)))
(t/is (empty? @frames))))))
(t/deftest interactive-mode-and-preview-cleanup-have-independent-effects
(let [first-preview [[id-a transform]]
first-structure [{:type :add-children :parent id-a :id id-b :index 2 :value 0}]
second-preview [[id-b (gmt/matrix 1 0 0 1 -48 32)]]
second-structure [{:type :remove-children :parent id-b :id id-a :index 1 :value 0}]
state-keys [:interactive :preview :structure]]
(with-renderer
{}
(fn [{:keys [state frames]}]
(api/set-modifiers-start)
(modifiers/set-modifiers first-preview)
(modifiers/set-structure-modifiers first-structure)
(t/is (= {:interactive true :preview first-preview :structure first-structure}
(select-keys @state state-keys)))
(api/clean-modifiers)
(t/is (= {:interactive true :preview [] :structure []}
(select-keys @state state-keys)))
(modifiers/set-modifiers second-preview)
(modifiers/set-structure-modifiers second-structure)
(api/set-modifiers-end)
(t/is (= {:interactive false :preview second-preview :structure second-structure}
(select-keys @state state-keys)))
(t/is (empty? @frames))))))
(t/deftest a-failed-preview-does-not-schedule-a-render
(with-renderer
{:set-error 2}
(fn [{:keys [frames]}]
(let [error (try
(api/set-modifiers [[id-a transform]])
(catch :default cause cause))]
(t/is (= "_set_modifiers" (:fn (ex-data error))))
(t/is (= :panic (:code (ex-data error))))
(t/is (empty? @frames))))))

View File

@ -118,6 +118,7 @@
[frontend-tests.plugins.value-objects-test]
[frontend-tests.render-dimensions-test]
[frontend-tests.render-wasm.call-test]
[frontend-tests.render-wasm.modifiers-test]
[frontend-tests.render-wasm.process-objects-test]
[frontend-tests.render-wasm.serialization-test]
[frontend-tests.render-wasm.shape-proxy-test]
@ -341,6 +342,7 @@
'frontend-tests.plugins.utils-test
'frontend-tests.plugins.value-objects-test
'frontend-tests.render-wasm.call-test
'frontend-tests.render-wasm.modifiers-test
'frontend-tests.render-wasm.process-objects-test
'frontend-tests.render-wasm.serialization-test
'frontend-tests.render-wasm.shape-proxy-test