💄 Add naming and docstring consistency fixes to wasm api

This commit is contained in:
Andrey Antukh 2025-08-11 12:49:01 +02:00
parent c39a8d84ac
commit 73a72ec1c7
5 changed files with 121 additions and 129 deletions

View File

@ -20,15 +20,15 @@
(def ^:const MAX-GRADIENT-STOPS 16)
(def ^:const MAX-FILLS 8)
(def ^:const GRADIENT-STOP-SIZE 8)
(def ^:const GRADIENT-BYTE-SIZE 156)
(def ^:const SOLID-BYTE-SIZE 4)
(def ^:const IMAGE-BYTE-SIZE 36)
(def ^:const METADATA-BYTE-SIZE 36)
(def ^:const FILL-BYTE-SIZE
(+ 4 (mth/max GRADIENT-BYTE-SIZE
IMAGE-BYTE-SIZE
SOLID-BYTE-SIZE)))
(def ^:const GRADIENT-STOP-U8-SIZE 8)
(def ^:const GRADIENT-U8-SIZE 156)
(def ^:const SOLID-U8-SIZE 4)
(def ^:const IMAGE-U8-SIZE 36)
(def ^:const METADATA-U8-SIZE 36)
(def ^:const FILL-U8-SIZE
(+ 4 (mth/max GRADIENT-U8-SIZE
IMAGE-U8-SIZE
SOLID-U8-SIZE)))
(def ^:private xf:take-stops
(take MAX-GRADIENT-STOPS))
@ -78,7 +78,7 @@
(buf/write-int buffer (+ offset 4)
(-> (hex->rgb color)
(rgb->rgba opacity)))
(+ offset FILL-BYTE-SIZE))
(+ offset FILL-U8-SIZE))
(defn write-gradient-fill
[offset buffer opacity gradient]
@ -114,8 +114,8 @@
(buf/write-int buffer (+ offset' 0) color)
(buf/write-float buffer (+ offset' 4) (:offset stop))
(recur (rest stops)
(+ offset' GRADIENT-STOP-SIZE)))
(+ offset FILL-BYTE-SIZE)))))
(+ offset' GRADIENT-STOP-U8-SIZE)))
(+ offset FILL-U8-SIZE)))))
(defn write-image-fill
[offset buffer opacity image]
@ -132,7 +132,7 @@
(buf/write-short buffer (+ offset 22) 0) ;; 2-byte padding (reserved for future use)
(buf/write-int buffer (+ offset 24) image-width)
(buf/write-int buffer (+ offset 28) image-height)
(+ offset FILL-BYTE-SIZE)))
(+ offset FILL-U8-SIZE)))
(defn- write-metadata
[offset buffer fill]
@ -169,8 +169,8 @@
(defn- read-fill
"Read segment from binary buffer at specified index"
[dbuffer mbuffer index]
(let [doffset (+ 4 (* index FILL-BYTE-SIZE))
moffset (* index METADATA-BYTE-SIZE)
(let [doffset (+ 4 (* index FILL-U8-SIZE))
moffset (* index METADATA-U8-SIZE)
type (buf/read-byte dbuffer doffset)
refs? (buf/read-bool mbuffer (+ moffset 0))
fill (case type
@ -195,7 +195,7 @@
result []]
(if (< index stops)
(recur (inc index)
(conj result (read-stop dbuffer (+ doffset 32 (* GRADIENT-STOP-SIZE index)))))
(conj result (read-stop dbuffer (+ doffset 32 (* GRADIENT-STOP-U8-SIZE index)))))
result))]
{:fill-opacity opacity
@ -410,8 +410,8 @@
[fills]
(let [fills (into [] xf:take-fills fills)
total (count fills)
dbuffer (buf/allocate (+ 4 (* MAX-FILLS FILL-BYTE-SIZE)))
mbuffer (buf/allocate (* total METADATA-BYTE-SIZE))]
dbuffer (buf/allocate (+ 4 (* MAX-FILLS FILL-U8-SIZE)))
mbuffer (buf/allocate (* total METADATA-U8-SIZE))]
(buf/write-byte dbuffer 0 total)
@ -419,8 +419,8 @@
image-ids #{}]
(if (< index total)
(let [fill (nth fills index)
doffset (+ 4 (* index FILL-BYTE-SIZE))
moffset (* index METADATA-BYTE-SIZE)
doffset (+ 4 (* index FILL-U8-SIZE))
moffset (* index METADATA-U8-SIZE)
opacity (get fill :fill-opacity 1)]
(if-let [color (get fill :fill-color)]

View File

@ -28,7 +28,7 @@
#?(:clj (set! *warn-on-reflection* true))
(def ^:const SEGMENT-BYTE-SIZE 28)
(def ^:const SEGMENT-U8-SIZE 28)
(defprotocol IPathData
(-write-to [_ buffer offset] "write the content to the specified buffer")
@ -107,7 +107,7 @@
f (dm/get-prop m :f)]
(loop [index 0]
(when (< index size)
(let [offset (* index SEGMENT-BYTE-SIZE)]
(let [offset (* index SEGMENT-U8-SIZE)]
(impl-transform-segment buffer offset a b c d e f)
(recur (inc index)))))))
@ -116,7 +116,7 @@
(loop [index 0
result (transient initial)]
(if (< index size)
(let [offset (* index SEGMENT-BYTE-SIZE)
(let [offset (* index SEGMENT-U8-SIZE)
type (buf/read-short buffer offset)
c1x (buf/read-float buffer (+ offset 4))
c1y (buf/read-float buffer (+ offset 8))
@ -141,7 +141,7 @@
(loop [index 0
result initial]
(if (< index size)
(let [offset (* index SEGMENT-BYTE-SIZE)
(let [offset (* index SEGMENT-U8-SIZE)
type (buf/read-short buffer offset)
c1x (buf/read-float buffer (+ offset 4))
c1y (buf/read-float buffer (+ offset 8))
@ -162,7 +162,7 @@
(defn impl-lookup
[buffer index f]
(let [offset (* index SEGMENT-BYTE-SIZE)
(let [offset (* index SEGMENT-U8-SIZE)
type (buf/read-short buffer offset)
c1x (buf/read-float buffer (+ offset 4))
c1y (buf/read-float buffer (+ offset 8))
@ -225,7 +225,7 @@
:cljs (StringBuffer.))]
(loop [index 0]
(when (< index size)
(let [offset (* index SEGMENT-BYTE-SIZE)
(let [offset (* index SEGMENT-U8-SIZE)
type (buf/read-short buffer offset)]
(to-string-segment* buffer offset type builder)
(recur (inc index)))))
@ -235,7 +235,7 @@
(defn- read-segment
"Read segment from binary buffer at specified index"
[buffer index]
(let [offset (* index SEGMENT-BYTE-SIZE)
(let [offset (* index SEGMENT-U8-SIZE)
type (buf/read-short buffer offset)]
(case (long type)
1 (let [x (buf/read-float buffer (+ offset 20))
@ -348,7 +348,7 @@
IPathData
(-get-byte-size [_]
(* size SEGMENT-BYTE-SIZE))
(* size SEGMENT-U8-SIZE))
(-write-to [_ _ _]
(throw (RuntimeException. "not implemented"))))
@ -576,13 +576,13 @@
(cond
(instance? ByteBuffer buffer)
(let [size (.capacity ^ByteBuffer buffer)
count (long (/ size SEGMENT-BYTE-SIZE))
count (long (/ size SEGMENT-U8-SIZE))
buffer (.order ^ByteBuffer buffer ByteOrder/LITTLE_ENDIAN)]
(PathData. count buffer nil))
(bytes? buffer)
(let [size (alength ^bytes buffer)
count (long (/ size SEGMENT-BYTE-SIZE))
count (long (/ size SEGMENT-U8-SIZE))
buffer (ByteBuffer/wrap buffer)]
(PathData. count
(.order buffer ByteOrder/LITTLE_ENDIAN)
@ -594,7 +594,7 @@
(cond
(instance? js/ArrayBuffer buffer)
(let [size (.-byteLength buffer)
count (long (/ size SEGMENT-BYTE-SIZE))]
count (long (/ size SEGMENT-U8-SIZE))]
(PathData. count
(js/DataView. buffer)
(weak-map/create)
@ -603,7 +603,7 @@
(instance? js/DataView buffer)
(let [buffer' (.-buffer ^js/DataView buffer)
size (.-byteLength ^js/ArrayBuffer buffer')
count (long (/ size SEGMENT-BYTE-SIZE))]
count (long (/ size SEGMENT-U8-SIZE))]
(PathData. count buffer (weak-map/create) nil))
(instance? js/Uint8Array buffer)
@ -627,11 +627,11 @@
(assert (check-plain-content segments))
(let [total (count segments)
buffer (buf/allocate (* total SEGMENT-BYTE-SIZE))]
buffer (buf/allocate (* total SEGMENT-U8-SIZE))]
(loop [index 0]
(when (< index total)
(let [segment (nth segments index)
offset (* index SEGMENT-BYTE-SIZE)]
offset (* index SEGMENT-U8-SIZE)]
(case (get segment :command)
:move-to
(let [params (get segment :params)

View File

@ -39,9 +39,7 @@
[promesa.core :as p]
[rumext.v2 :as mf]))
;; (defonce internal-frame-id nil)
;; (defonce wasm/internal-module #js {})
(defonce use-dpr? (contains? cf/flags :render-wasm-dpr))
(def use-dpr? (contains? cf/flags :render-wasm-dpr))
;;
;; List of common entry sizes.
@ -49,33 +47,32 @@
;; All of these entries are in bytes so we need to adjust
;; these values to work with TypedArrays of 32 bits.
;;
(def ^:const UUID-BYTE-SIZE 16)
(def ^:const UUID-U32-SIZE (/ UUID-BYTE-SIZE 4))
(def ^:const UUID-U8-SIZE 16)
(def ^:const UUID-U32-SIZE (/ UUID-U8-SIZE 4))
(def ^:const MODIFIER-U8-SIZE 40)
(def ^:const MODIFIER-TRANSFORM-U8-OFFSET-SIZE 16)
(def MODIFIER-ENTRY-SIZE 40)
(def MODIFIER-ENTRY-TRANSFORM-OFFSET 16)
(def GRID-LAYOUT-ROW-ENTRY-SIZE 5)
(def GRID-LAYOUT-COLUMN-ENTRY-SIZE 5)
(def GRID-LAYOUT-CELL-ENTRY-SIZE 37)
(def RAW-SEGMENT-SIZE 28)
(def ^:const GRID-LAYOUT-ROW-U8-SIZE 5)
(def ^:const GRID-LAYOUT-COLUMN-U8-SIZE 5)
(def ^:const GRID-LAYOUT-CELL-U8-SIZE 37)
(defn modifier-get-entries-size
"Returns the list of a modifier list in bytes"
[modifiers]
(mem/get-list-size modifiers MODIFIER-ENTRY-SIZE))
(mem/get-list-size modifiers MODIFIER-U8-SIZE))
(defn grid-layout-get-row-entries-size
[rows]
(mem/get-list-size rows GRID-LAYOUT-ROW-ENTRY-SIZE))
(mem/get-list-size rows GRID-LAYOUT-ROW-U8-SIZE))
(defn grid-layout-get-column-entries-size
[columns]
(mem/get-list-size columns GRID-LAYOUT-COLUMN-ENTRY-SIZE))
(mem/get-list-size columns GRID-LAYOUT-COLUMN-U8-SIZE))
(defn grid-layout-get-cell-entries-size
[cells]
(mem/get-list-size cells GRID-LAYOUT-CELL-ENTRY-SIZE))
(mem/get-list-size cells GRID-LAYOUT-CELL-U8-SIZE))
(def dpr
(if use-dpr? (if (exists? js/window) js/window.devicePixelRatio 1.0) 1.0))
@ -179,7 +176,7 @@
length (count children)]
(perf/begin-measure "set-shape-children")
(when (pos? length)
(let [offset (mem/alloc-bytes-32 (* UUID-BYTE-SIZE length))]
(let [offset (mem/alloc->offset-32 (* UUID-U8-SIZE length))]
(reduce (fn [offset id]
(sr/heapu32-set-uuid id heap offset)
(+ offset UUID-U32-SIZE))
@ -210,7 +207,7 @@
;; is possible (if image size modulo
;; permits it)
(let [size (.-byteLength image)
offset (mem/alloc-bytes size)
offset (mem/alloc size)
heap (mem/get-heap-u8)
data (js/Uint8Array. image)]
(.set heap data offset)
@ -257,7 +254,7 @@
(if (empty? fills)
(h/call wasm/internal-module "_clear_shape_fills")
(let [fills (types.fills/coerce fills)
offset (mem/alloc-bytes-32 (types.fills/get-byte-size fills))
offset (mem/alloc->offset-32 (types.fills/get-byte-size fills))
heap (mem/get-heap-u32)]
;; write fills to the heap
@ -292,7 +289,7 @@
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)
offset (mem/alloc-bytes types.fills.impl/FILL-BYTE-SIZE)
offset (mem/alloc types.fills.impl/FILL-U8-SIZE)
heap (mem/get-heap-u8)
dview (js/DataView. (.-buffer heap))]
(case align
@ -329,7 +326,7 @@
(merge style))
str (sr/serialize-path-attrs attrs)
size (count str)
offset (mem/alloc-bytes size)]
offset (mem/alloc size)]
(h/call wasm/internal-module "stringToUTF8" str offset size)
(h/call wasm/internal-module "_set_shape_path_attrs" (count attrs))))
@ -338,7 +335,7 @@
[content]
(let [pdata (path/content content)
size (path/get-byte-size content)
offset (mem/alloc-bytes size)
offset (mem/alloc size)
heap (mem/get-heap-u8)]
(path/write-to pdata (.-buffer heap) offset)
(h/call wasm/internal-module "_set_shape_path_content")))
@ -346,7 +343,7 @@
(defn set-shape-svg-raw-content
[content]
(let [size (get-string-length content)
offset (mem/alloc-bytes size)]
offset (mem/alloc size)]
(h/call wasm/internal-module "stringToUTF8" content offset size)
(h/call wasm/internal-module "_set_shape_svg_raw_content")))
@ -471,7 +468,7 @@
(defn set-grid-layout-rows
[entries]
(let [size (grid-layout-get-row-entries-size entries)
offset (mem/alloc-bytes size)
offset (mem/alloc size)
heap
(js/Uint8Array.
@ -484,13 +481,13 @@
(let [{:keys [type value]} (first entries)]
(.set heap (sr/u8 (sr/translate-grid-track-type type)) (+ current-offset 0))
(.set heap (sr/f32->u8 value) (+ current-offset 1))
(recur (rest entries) (+ current-offset GRID-LAYOUT-ROW-ENTRY-SIZE)))))
(recur (rest entries) (+ current-offset GRID-LAYOUT-ROW-U8-SIZE)))))
(h/call wasm/internal-module "_set_grid_rows")))
(defn set-grid-layout-columns
[entries]
(let [size (grid-layout-get-column-entries-size entries)
offset (mem/alloc-bytes size)
offset (mem/alloc size)
heap
(js/Uint8Array.
@ -503,14 +500,14 @@
(let [{:keys [type value]} (first entries)]
(.set heap (sr/u8 (sr/translate-grid-track-type type)) (+ current-offset 0))
(.set heap (sr/f32->u8 value) (+ current-offset 1))
(recur (rest entries) (+ current-offset GRID-LAYOUT-COLUMN-ENTRY-SIZE)))))
(recur (rest entries) (+ current-offset GRID-LAYOUT-COLUMN-U8-SIZE)))))
(h/call wasm/internal-module "_set_grid_columns")))
(defn set-grid-layout-cells
[cells]
(let [entries (vals cells)
size (grid-layout-get-cell-entries-size entries)
offset (mem/alloc-bytes size)
offset (mem/alloc size)
heap
(js/Uint8Array.
@ -556,7 +553,7 @@
;; shape_id_d: [u8; 4],
(.set heap (sr/uuid->u8 (or (-> cell :shapes first) uuid/zero)) (+ current-offset 21))
(recur (rest entries) (+ current-offset GRID-LAYOUT-CELL-ENTRY-SIZE)))))
(recur (rest entries) (+ current-offset GRID-LAYOUT-CELL-U8-SIZE)))))
(h/call wasm/internal-module "_set_grid_cells")))
@ -675,9 +672,9 @@
([]
(let [offset (h/call wasm/internal-module "_get_text_dimensions")
heapf32 (mem/get-heap-f32)
width (aget heapf32 (mem/ptr8->ptr32 offset))
height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4)))
max-width (aget heapf32 (mem/ptr8->ptr32 (+ offset 8)))]
width (aget heapf32 (mem/->offset-32 offset))
height (aget heapf32 (mem/->offset-32 (+ offset 4)))
max-width (aget heapf32 (mem/->offset-32 (+ offset 8)))]
(mem/free)
{:width width :height height :max-width max-width})))
@ -827,7 +824,7 @@
(defn set-focus-mode
[entries]
(let [offset (mem/alloc-bytes-32 (* (count entries) 16))
(let [offset (mem/alloc->offset-32 (* (count entries) 16))
heapu32 (mem/get-heap-u32)]
(loop [entries (seq entries)
@ -835,7 +832,7 @@
(when-not (empty? entries)
(let [id (first entries)]
(sr/heapu32-set-uuid id heapu32 current-offset)
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 16))))))
(recur (rest entries) (+ current-offset (mem/->offset-32 16))))))
(h/call wasm/internal-module "_set_focus_mode")
(clear-drawing-cache)
@ -844,7 +841,7 @@
(defn set-structure-modifiers
[entries]
(when-not (empty? entries)
(let [offset (mem/alloc-bytes-32 (mem/get-list-size entries 44))
(let [offset (mem/alloc->offset-32 (mem/get-list-size entries 44))
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
(loop [entries (seq entries)
@ -862,7 +859,7 @@
(defn propagate-modifiers
[entries pixel-precision]
(when (d/not-empty? entries)
(let [offset (mem/alloc-bytes-32 (modifier-get-entries-size entries))
(let [offset (mem/alloc->offset-32 (modifier-get-entries-size entries))
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)]
@ -871,16 +868,16 @@
(when-not (empty? entries)
(let [{:keys [id transform]} (first entries)]
(sr/heapu32-set-uuid id heapu32 current-offset)
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET)))
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE))))))
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/->offset-32 MODIFIER-TRANSFORM-U8-OFFSET-SIZE)))
(recur (rest entries) (+ current-offset (mem/->offset-32 MODIFIER-U8-SIZE))))))
(let [result-offset (h/call wasm/internal-module "_propagate_modifiers" pixel-precision)
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)
len (aget heapu32 (mem/ptr8->ptr32 result-offset))
len (aget heapu32 (mem/->offset-32 result-offset))
result
(->> (range 0 len)
(mapv #(dr/heap32->entry heapu32 heapf32 (mem/ptr8->ptr32 (+ result-offset 4 (* % MODIFIER-ENTRY-SIZE))))))]
(mapv #(dr/heap32->entry heapu32 heapf32 (mem/->offset-32 (+ result-offset 4 (* % MODIFIER-U8-SIZE))))))]
(mem/free)
result))))
@ -888,7 +885,7 @@
(defn propagate-apply
[entries pixel-precision]
(when (d/not-empty? entries)
(let [offset (mem/alloc-bytes-32 (modifier-get-entries-size entries))
(let [offset (mem/alloc->offset-32 (modifier-get-entries-size entries))
heapf32 (mem/get-heap-f32)
heapu32 (mem/get-heap-u32)]
@ -897,22 +894,22 @@
(when-not (empty? entries)
(let [{:keys [id transform]} (first entries)]
(sr/heapu32-set-uuid id heapu32 current-offset)
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET)))
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE))))))
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/->offset-32 MODIFIER-TRANSFORM-U8-OFFSET-SIZE)))
(recur (rest entries) (+ current-offset (mem/->offset-32 MODIFIER-U8-SIZE))))))
(let [offset (h/call wasm/internal-module "_propagate_apply" pixel-precision)
heapf32 (mem/get-heap-f32)
width (aget heapf32 (mem/ptr8->ptr32 (+ offset 0)))
height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4)))
cx (aget heapf32 (mem/ptr8->ptr32 (+ offset 8)))
cy (aget heapf32 (mem/ptr8->ptr32 (+ offset 12)))
width (aget heapf32 (mem/->offset-32 (+ offset 0)))
height (aget heapf32 (mem/->offset-32 (+ offset 4)))
cx (aget heapf32 (mem/->offset-32 (+ offset 8)))
cy (aget heapf32 (mem/->offset-32 (+ offset 12)))
a (aget heapf32 (mem/ptr8->ptr32 (+ offset 16)))
b (aget heapf32 (mem/ptr8->ptr32 (+ offset 20)))
c (aget heapf32 (mem/ptr8->ptr32 (+ offset 24)))
d (aget heapf32 (mem/ptr8->ptr32 (+ offset 28)))
e (aget heapf32 (mem/ptr8->ptr32 (+ offset 32)))
f (aget heapf32 (mem/ptr8->ptr32 (+ offset 36)))
a (aget heapf32 (mem/->offset-32 (+ offset 16)))
b (aget heapf32 (mem/->offset-32 (+ offset 20)))
c (aget heapf32 (mem/->offset-32 (+ offset 24)))
d (aget heapf32 (mem/->offset-32 (+ offset 28)))
e (aget heapf32 (mem/->offset-32 (+ offset 32)))
f (aget heapf32 (mem/->offset-32 (+ offset 36)))
transform (gmt/matrix a b c d e f)]
(mem/free)
@ -926,7 +923,7 @@
(defn get-selection-rect
[entries]
(when (d/not-empty? entries)
(let [offset (mem/alloc-bytes-32 (* (count entries) 16))
(let [offset (mem/alloc->offset-32 (* (count entries) 16))
heapu32 (mem/get-heap-u32)]
(loop [entries (seq entries)
@ -934,26 +931,27 @@
(when-not (empty? entries)
(let [id (first entries)]
(sr/heapu32-set-uuid id heapu32 current-offset)
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 16))))))
(recur (rest entries) (+ current-offset (mem/->offset-32 16))))))
(let [offset (h/call wasm/internal-module "_get_selection_rect")
heapf32 (mem/get-heap-f32)
width (aget heapf32 (mem/ptr8->ptr32 (+ offset 0)))
height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4)))
cx (aget heapf32 (mem/ptr8->ptr32 (+ offset 8)))
cy (aget heapf32 (mem/ptr8->ptr32 (+ offset 12)))
a (aget heapf32 (mem/ptr8->ptr32 (+ offset 16)))
b (aget heapf32 (mem/ptr8->ptr32 (+ offset 20)))
c (aget heapf32 (mem/ptr8->ptr32 (+ offset 24)))
d (aget heapf32 (mem/ptr8->ptr32 (+ offset 28)))
e (aget heapf32 (mem/ptr8->ptr32 (+ offset 32)))
f (aget heapf32 (mem/ptr8->ptr32 (+ offset 36)))
transform (gmt/matrix a b c d e f)]
heap (mem/get-heap-f32)
width (aget heap (mem/->offset-32 (+ offset 0)))
height (aget heap (mem/->offset-32 (+ offset 4)))
cx (aget heap (mem/->offset-32 (+ offset 8)))
cy (aget heap (mem/->offset-32 (+ offset 12)))
a (aget heap (mem/->offset-32 (+ offset 16)))
b (aget heap (mem/->offset-32 (+ offset 20)))
c (aget heap (mem/->offset-32 (+ offset 24)))
d (aget heap (mem/->offset-32 (+ offset 28)))
e (aget heap (mem/->offset-32 (+ offset 32)))
f (aget heap (mem/->offset-32 (+ offset 36)))]
(mem/free)
{:width width
:height height
:center (gpt/point cx cy)
:transform transform}))))
:transform (gmt/matrix a b c d e f)}))))
(defn set-canvas-background
[background]
@ -968,7 +966,7 @@
(defn set-modifiers
[modifiers]
(when-not (empty? modifiers)
(let [offset (mem/alloc-bytes-32 (* MODIFIER-ENTRY-SIZE (count modifiers)))
(let [offset (mem/alloc->offset-32 (* MODIFIER-U8-SIZE (count modifiers)))
heapu32 (mem/get-heap-u32)
heapf32 (mem/get-heap-f32)]
@ -977,8 +975,8 @@
(when-not (empty? entries)
(let [{:keys [id transform]} (first entries)]
(sr/heapu32-set-uuid id heapu32 current-offset)
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET)))
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE))))))
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/->offset-32 MODIFIER-TRANSFORM-U8-OFFSET-SIZE)))
(recur (rest entries) (+ current-offset (mem/->offset-32 MODIFIER-U8-SIZE))))))
(h/call wasm/internal-module "_set_modifiers")
@ -1056,8 +1054,8 @@
(get position :x)
(get position :y))
heapi32 (mem/get-heap-i32)
row (aget heapi32 (mem/ptr8->ptr32 (+ offset 0)))
column (aget heapi32 (mem/ptr8->ptr32 (+ offset 4)))]
row (aget heapi32 (mem/->offset-32 (+ offset 0)))
column (aget heapi32 (mem/->offset-32 (+ offset 4)))]
(mem/free)
[row column]))
@ -1065,13 +1063,13 @@
[id]
(use-shape id)
(let [offset (h/call wasm/internal-module "_current_to_path")
offset (mem/ptr8->ptr32 offset)
offset (mem/->offset-32 offset)
heapu32 (mem/get-heap-u32)
length (aget heapu32 offset)
data (mem/slice heapu32
(+ offset 1)
(+ offset 1 (* length (/ path.impl/SEGMENT-BYTE-SIZE 4))))
(+ offset 1 (* length (/ path.impl/SEGMENT-U8-SIZE 4))))
content (path/from-bytes data)]
(mem/free)
content))
@ -1079,7 +1077,7 @@
(defn calculate-bool
[bool-type ids]
(let [num-ids (count ids)
offset (mem/alloc-bytes-32 (* UUID-BYTE-SIZE num-ids))
offset (mem/alloc->offset-32 (* UUID-U8-SIZE num-ids))
heap (mem/get-heap-u32)]
(reduce (fn [offset id]
@ -1089,13 +1087,13 @@
(rseq ids)))
(let [offset (h/call wasm/internal-module "_calculate_bool" (sr/translate-bool-type bool-type))
offset (mem/ptr8->ptr32 offset)
offset (mem/->offset-32 offset)
heapu32 (mem/get-heap-u32)
length (aget heapu32 offset)
data (mem/slice heapu32
(+ offset 1)
(+ offset 1 (* length (/ path.impl/SEGMENT-BYTE-SIZE 4))))
(+ offset 1 (* length (/ path.impl/SEGMENT-U8-SIZE 4))))
content (path/from-bytes data)]
(mem/free)
content))

View File

@ -35,7 +35,7 @@
(some? image)
(types.fills.impl/write-image-fill offset dview opacity image))
(+ offset types.fills.impl/FILL-BYTE-SIZE)))
(+ offset types.fills.impl/FILL-U8-SIZE)))
current-offset
fills))
@ -51,7 +51,7 @@
num-leaves (count leaves)
paragraph-attr-size 48
total-fills (total-fills-count leaves)
total-fills-size (* types.fills.impl/FILL-BYTE-SIZE total-fills)
total-fills-size (* types.fills.impl/FILL-U8-SIZE total-fills)
leaf-attr-size 56
metadata-size (+ paragraph-attr-size (* num-leaves leaf-attr-size) total-fills-size)
text-buffer (utf8->buffer text)
@ -137,7 +137,7 @@
;; Allocate memory and set buffer
(let [total-size (.-byteLength buffer)
metadata-offset (mem/alloc-bytes total-size)
metadata-offset (mem/alloc total-size)
heap (mem/get-heap-u8)]
(.set heap (js/Uint8Array. buffer) metadata-offset)))

View File

@ -9,36 +9,30 @@
[app.render-wasm.helpers :as h]
[app.render-wasm.wasm :as wasm]))
(defn ptr8->ptr32
"Returns a 32-bit (4-byte aligned) pointer of an 8-bit pointer"
(defn ->offset-32
"Convert a 8-bit (1 byte) offset to a 32-bit (4 bytes) offset"
[value]
;; Divides the value by 4
(bit-shift-right value 2))
(defn ptr32->ptr8
"Returns a 8-bit pointer of a 32-bit (4-byte aligned) pointer"
[value]
;; Multiplies by 4
(bit-shift-left value 2))
(defn get-list-size
"Returns the size of a list in bytes"
[list list-item-size]
(* list-item-size (count list)))
(defn alloc-bytes
"Allocates an arbitrary amount of bytes"
(defn alloc
"Allocates an arbitrary amount of bytes (aligned to 4 bytes).
Returns an offset of 8 bits (1 byte) size."
[size]
(when (= size 0)
(js/console.trace "Tried to allocate 0 bytes"))
(h/call wasm/internal-module "_alloc_bytes" size))
(defn alloc-bytes-32
"Allocates a 4-byte aligned amount of bytes"
(defn alloc->offset-32
"Allocates an arbitrary amount of bytes (aligned to 4 bytes).
Returns an offset of 32 bits (4 bytes) size."
[size]
(when (= size 0)
(js/console.trace "Tried to allocate 0 bytes"))
(ptr8->ptr32 (h/call wasm/internal-module "_alloc_bytes" size)))
(-> (alloc size) (->offset-32)))
(defn get-heap-u8
"Returns a Uint8Array view of the heap"