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

View File

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

View File

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

View File

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

View File

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