🐛 Fix problem with thumbnails in parallel

This commit is contained in:
alonso.torres
2025-11-25 17:56:00 +01:00
parent aaca2c41d8
commit 8880f07a6a
5 changed files with 143 additions and 120 deletions

View File

@@ -60,10 +60,10 @@
(defn render-thumbnail (defn render-thumbnail
[file-id revn] [file-id revn]
(if (features/active-feature? @st/state "render-wasm/v1") (if (features/active-feature? @st/state "render-wasm/v1")
(->> (mw/ask! {:cmd :thumbnails/generate-for-file-wasm (mw/ask! {:cmd :thumbnails/generate-for-file-wasm
:revn revn :revn revn
:file-id file-id :file-id file-id
:width thumbnail-width})) :width thumbnail-width})
(->> (mw/ask! {:cmd :thumbnails/generate-for-file (->> (mw/ask! {:cmd :thumbnails/generate-for-file
:revn revn :revn revn
:file-id file-id :file-id file-id

View File

@@ -128,11 +128,12 @@
(defn update-text-rect! (defn update-text-rect!
[id] [id]
(when wasm/context-initialized?
(mw/emit! (mw/emit!
{:cmd :index/update-text-rect {:cmd :index/update-text-rect
:page-id (:current-page-id @st/state) :page-id (:current-page-id @st/state)
:shape-id id :shape-id id
:dimensions (get-text-dimensions id)})) :dimensions (get-text-dimensions id)})))
(defn- ensure-text-content (defn- ensure-text-content
@@ -198,6 +199,7 @@
(defn set-shape-children (defn set-shape-children
[children] [children]
(perf/begin-measure "set-shape-children") (perf/begin-measure "set-shape-children")
(let [children (into [] (filter uuid?) children)]
(case (count children) (case (count children)
0 0
(h/call wasm/internal-module "_set_children_0") (h/call wasm/internal-module "_set_children_0")
@@ -261,7 +263,7 @@
(mem.h32/write-uuid offset heap id)) (mem.h32/write-uuid offset heap id))
offset offset
children) children)
(h/call wasm/internal-module "_set_children"))) (h/call wasm/internal-module "_set_children"))))
(perf/end-measure "set-shape-children") (perf/end-measure "set-shape-children")
nil) nil)
@@ -1031,8 +1033,9 @@
(into full-acc full))) (into full-acc full)))
{:thumbnails thumbnails-acc :full full-acc}))] {:thumbnails thumbnails-acc :full full-acc}))]
(perf/end-measure "set-objects") (perf/end-measure "set-objects")
(process-pending shapes thumbnails full render-callback (process-pending shapes thumbnails full noop-fn
(fn [] (fn []
(when render-callback (render-callback))
(ug/dispatch! (ug/event "penpot:wasm:set-objects"))))))) (ug/dispatch! (ug/event "penpot:wasm:set-objects")))))))
(defn clear-focus-mode (defn clear-focus-mode

View File

@@ -83,12 +83,13 @@
(defn update-text-layout (defn update-text-layout
[id] [id]
(when wasm/context-initialized?
(let [shape-id-buffer (uuid/get-u32 id)] (let [shape-id-buffer (uuid/get-u32 id)]
(h/call wasm/internal-module "_update_shape_text_layout_for" (h/call wasm/internal-module "_update_shape_text_layout_for"
(aget shape-id-buffer 0) (aget shape-id-buffer 0)
(aget shape-id-buffer 1) (aget shape-id-buffer 1)
(aget shape-id-buffer 2) (aget shape-id-buffer 2)
(aget shape-id-buffer 3)))) (aget shape-id-buffer 3)))))
;; IMPORTANT: Only TTF fonts can be stored. ;; IMPORTANT: Only TTF fonts can be stored.
(defn- store-font-buffer (defn- store-font-buffer

View File

@@ -13,6 +13,7 @@
[app.common.logging :as log] [app.common.logging :as log]
[app.common.types.color :as cc] [app.common.types.color :as cc]
[app.common.uri :as u] [app.common.uri :as u]
[app.common.uuid :as uuid]
[app.config :as cf] [app.config :as cf]
[app.main.fonts :as fonts] [app.main.fonts :as fonts]
[app.main.render :as render] [app.main.render :as render]
@@ -125,16 +126,27 @@
(def thumbnail-aspect-ratio (/ 2 3)) (def thumbnail-aspect-ratio (/ 2 3))
(defmethod impl/handler :thumbnails/generate-for-file-wasm (defn render-canvas-blob
[{:keys [file-id revn width] :as message} _] [canvas width height background-color]
(-> (.convertToBlob canvas)
(p/then
(fn [blob]
(rds/renderToStaticMarkup
(mf/element
svg-wrapper
#js {:data-uri (blob->uri blob)
:width width
:height height
:background background-color}))))))
(defn process-wasm-thumbnail
[{:keys [id file-id revn width] :as message}]
(->> (rx/from @init-wasm) (->> (rx/from @init-wasm)
(rx/mapcat #(request-data-for-thumbnail file-id revn false)) (rx/mapcat #(request-data-for-thumbnail file-id revn false))
(rx/mapcat (rx/mapcat
(fn [{:keys [page] :as file}] (fn [{:keys [page] :as file}]
(rx/create (rx/create
(fn [subs] (fn [subs]
(try
(let [background-color (or (:background page) cc/canvas) (let [background-color (or (:background page) cc/canvas)
height (* width thumbnail-aspect-ratio) height (* width thumbnail-aspect-ratio)
canvas (js/OffscreenCanvas. width height) canvas (js/OffscreenCanvas. width height)
@@ -155,28 +167,34 @@
(wasm.api/render-sync-shape (:id frame)) (wasm.api/render-sync-shape (:id frame))
(wasm.api/render-sync)) (wasm.api/render-sync))
(-> (.convertToBlob canvas) (-> (render-canvas-blob canvas width height background-color)
(p/then (p/then #(rx/push! subs {:id id :data % :file-id file-id :revn revn}))
(fn [blob] (p/catch #(rx/error! subs %))
(let [data
(rds/renderToStaticMarkup
(mf/element
svg-wrapper
#js {:data-uri (blob->uri blob)
:width width
:height height
:background background-color}))]
(rx/push! subs {:data data :file-id file-id :revn revn}))))
(p/catch #(do (.error js/console %)
(rx/error! subs %)))
(p/finally #(rx/end! subs)))))) (p/finally #(rx/end! subs))))))
(do (rx/error! subs "Error loading webgl context") (rx/end! subs))
(rx/end! subs)))
nil) nil)))))))
(catch :default err (defonce thumbs-subject (rx/subject))
(.error js/console err)
(rx/error! subs err) (defonce thumbs-stream
(rx/end! subs))))))))) (->> thumbs-subject
(rx/mapcat process-wasm-thumbnail)
(rx/share)))
(defmethod impl/handler :thumbnails/generate-for-file-wasm
[message _]
(rx/create
(fn [subs]
(let [id (uuid/next)
sid
(->> thumbs-stream
(rx/filter #(= id (:id %)))
(rx/subs!
#(do
(rx/push! subs %)
(rx/end! subs))))]
(rx/push! thumbs-subject (assoc message :id id))
#(rx/dispose! sid)))))

View File

@@ -291,9 +291,10 @@ pub extern "C" fn set_shape_text_content() {
let bytes = mem::bytes(); let bytes = mem::bytes();
with_current_shape_mut!(state, |shape: &mut Shape| { with_current_shape_mut!(state, |shape: &mut Shape| {
let raw_text_data = RawParagraph::try_from(&bytes).unwrap(); let raw_text_data = RawParagraph::try_from(&bytes).unwrap();
shape
.add_paragraph(raw_text_data.into()) if let Err(_) = shape.add_paragraph(raw_text_data.into()) {
.expect("Failed to add paragraph"); println!("Error with set_shape_text_content on {:?}", shape.id);
}
}); });
mem::free_bytes(); mem::free_bytes();
} }