diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index e586dc3d1a..ee3f58b74c 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -31,6 +31,9 @@ [app.main.errors :as errors] [app.main.repo :as rp] [app.main.store :as st] + [app.render-wasm.helpers :as wasm.h] + [app.render-wasm.mem :as wasm.mem] + [app.render-wasm.wasm :as wasm] [app.util.debug :as dbg] [app.util.dom :as dom] [app.util.http :as http] @@ -117,6 +120,43 @@ (js/console.log str (json/->js val)) val)) +(defn- wasm-read-len-prefixed-utf8 + "Reads a `[u32 byte_len][utf8 bytes...]` buffer returned by WASM and frees it. + Returns a JS string (possibly empty)." + [ptr] + (when (and ptr (not (zero? ptr))) + (let [heap-u8 (wasm.mem/get-heap-u8) + heap-u32 (wasm.mem/get-heap-u32) + len (aget heap-u32 (wasm.mem/->offset-32 ptr)) + start (+ ptr 4) + end (+ start len) + decoder (js/TextDecoder. "utf-8") + text (.decode decoder (.subarray heap-u8 start end))] + (wasm.mem/free) + text))) + +(defn ^:export wasmCacheConsole + "Logs the current render-wasm cache surface as an image in the JS console." + [] + (let [module wasm/internal-module + f (when module (unchecked-get module "_debug_cache_console"))] + (if (fn? f) + (wasm.h/call module "_debug_cache_console") + (js/console.warn "[debug] render-wasm module not ready or missing _debug_cache_console")))) + +(defn ^:export wasmCacheBase64 + "Returns the cache surface PNG base64 (empty string if missing/empty)." + [] + (let [module wasm/internal-module + f (when module (unchecked-get module "_debug_cache_base64"))] + (if (fn? f) + (let [ptr (wasm.h/call module "_debug_cache_base64") + s (or (wasm-read-len-prefixed-utf8 ptr) "")] + s) + (do + (js/console.warn "[debug] render-wasm module not ready or missing _debug_cache_base64") + "")))) + (when (exists? js/window) (set! (.-dbg ^js js/window) json/->js) (set! (.-pp ^js js/window) pprint)) diff --git a/render-wasm/src/render/debug.rs b/render-wasm/src/render/debug.rs index 624cdaef78..47b739b484 100644 --- a/render-wasm/src/render/debug.rs +++ b/render-wasm/src/render/debug.rs @@ -1,4 +1,7 @@ use super::{tiles, RenderState, SurfaceId}; +use crate::with_state_mut; +use crate::STATE; +use macros::wasm_error; use skia_safe::{self as skia, Rect}; #[cfg(target_arch = "wasm32")] @@ -210,3 +213,13 @@ pub fn console_debug_surface_rect(render_state: &mut RenderState, id: SurfaceId, run_script!(format!("console.log('%c ', 'font-size: 1px; background: url(data:image/png;base64,{base64_image}) no-repeat; padding: 100px; background-size: contain;')")) } } + +#[no_mangle] +#[wasm_error] +#[cfg(target_arch = "wasm32")] +pub extern "C" fn debug_cache_console() -> Result<()> { + with_state_mut!(state, { + console_debug_surface(state.render_state_mut(), SurfaceId::Cache); + }); + Ok(()) +}