📚 Separate WASM FFI and rendering guidance

Keep buffer ownership and tile rendering in focused notes. Point build
instructions to the README and retain the shared target safety rules.

AI-assisted-by: gpt-6.1-terra
This commit is contained in:
Andrey Antukh 2026-11-23 22:08:45 +00:00
parent d939e196e5
commit 4b61d6f4b1
4 changed files with 18 additions and 90 deletions

View File

@ -1,43 +1,7 @@
# render-wasm Architecture and Workflow
# render-wasm
`render-wasm/`: Rust crate compiled to WebAssembly via Emscripten/Skia; frontend loads generated JS/WASM renderer. FFI/memory/tile behavior: `mem:render-wasm/ffi-rendering-subtleties`.
## Stable Architecture
- Exported functions live around `src/main.rs` / `src/wapi.rs` and are called from ClojureScript bridge namespaces under `frontend/src/app/render_wasm*`.
- Updates are two-phase: ClojureScript calls exported setters to push shape data, then `render_frame()` performs Skia drawing.
- Rendering is tile-based and shape data is stored separately from hierarchy.
## Source Areas
- `src/state*`: renderer state structures.
- `src/render/` and `src/render.rs`: tile/surface render pipeline.
- `src/shapes/` and `src/shapes.rs`: shape data and Skia drawing.
- `src/wasm/`, `src/wasm.rs`, `src/mem.rs`: JS/WASM memory and interop helpers.
- `src/math/` and `src/view.rs`: geometry and viewport helpers.
## Build Environment
`./build` sources `_build_env`, which sets the Emscripten paths and `EMCC_CFLAGS`. The WASM heap starts at 256 MB and uses geometric growth.
- Linux builds require `flock` (util-linux).
- `build`, target cleanup, and each watch rebuild share `render-wasm/.render-wasm-build.lock` per checkout. The dispatcher for both targets does not hold the lock in its parent process.
- The lock covers setup, Cargo build, and artifact copy. The watch process releases it while waiting for changes. The operating system releases it on success, error, or signal; the file remains.
- The lock only coordinates repository scripts. Manual Cargo commands do not take it.
- Set `RENDER_WASM_LOCK_FILE` to the same path only when separate checkouts intentionally share a `CARGO_TARGET_DIR`.
- Build output is gitignored and stays untracked everywhere it lands: `frontend/resources/public/js/render-wasm.{js,wasm}` and `frontend/src/app/render_wasm/api/shared.js` from `./build`, plus `exporter/resources/wasm/render-wasm.{js,wasm}` and `exporter/src/app/wasm/shared.js`. Never commit generated JS/WASM; rebuild instead.
## Commands
From `render-wasm/`:
- Build/copy frontend artifacts: `./build [frontend|export]`; no target builds frontend, then export.
- Clean one target: `./clean [frontend|export]`; never run root `cargo clean` on the shared `target/`.
- Watch rebuild: `./watch [frontend|export]`; its initial and change-triggered builds use `./build`.
- Rust tests: `./test` or `cargo test <name>`.
- Rust coverage: `RENDER_WASM_COVERAGE=1 ./test` runs the same tests under `cargo llvm-cov` (needs `cargo-llvm-cov` on PATH; the script adds `llvm-tools-preview`) and writes `target/coverage/` (`lcov.info` with absolute paths, `summary.json` llvm export totals, `html/`). Native build only: code behind wasm-only `cfg` is not measured. `tests-wasm.yml` downloads a pinned, checksum-verified `cargo-llvm-cov` release because the devenv image lacks it.
- Cross-cutting testing principles and anti-patterns: `mem:testing`.
- Lint: `./lint`.
- Format: `./fmt`; check only: `./fmt --check`.
- `./build`, `./test`, `./lint` and `./fmt` all run `setup`, which selects the Rust version pinned in `_build_env` (skip with `PENPOT_WASM_PREPARED=1`).
Do not change exported WASM function signatures without updating the corresponding frontend bridge and verifying the frontend renderer path.
- `render-wasm/` compiles the Rust/Skia renderer to WebAssembly. Setters called from the CLJS bridge update shape state; `render_frame` draws it. Change exported signatures together with the frontend bridge.
- State, error codes and byte-buffer ownership: `mem:render-wasm/ffi`. Shape loading, tile invalidation and rendering: `mem:render-wasm/rendering`.
- Build commands, artifact locations and lock behavior: `render-wasm/README.md`. Generated JS/WASM and each target's `shared.js` are gitignored; rebuild, never edit or commit them.
- Build and cleanup scripts share a checkout lock, but manual Cargo commands do not. Checkouts that intentionally share a `CARGO_TARGET_DIR` must use the same `RENDER_WASM_LOCK_FILE`. Never run root `cargo clean` on the shared `target/`; use target-specific cleanup.
- For lint, native tests and formatting see `mem:scripts/ci`; native coverage excludes wasm-only `cfg` code. Cross-cutting test design: `mem:testing`.

View File

@ -1,48 +0,0 @@
# render-wasm FFI and Rendering Subtleties
## FFI state and errors
- The renderer uses one unsafe global `STATE`; the `with_state*` macros currently panic on invalid state pointer. Treat state pointer validity as critical, not recoverable.
- `#[wasm_error]` clears the error code on entry. Recoverable errors set code `0x01`, critical errors/panics set `0x02`, free the byte buffer, then panic so the CLJS bridge can catch and inspect `_read_error_code`.
- The frontend bridge maps `0x01` to `:non-blocking` and `0x02` to `:panic` in ex-data (`:type :wasm-error`). Check actual bridge code if changing names; older comments/docs may use different labels.
- WASM byte transfer is a single global slot. A caller that receives a pointer result must read and free it before another byte payload is written; errors free the slot via `#[wasm_error]`.
## Shape pool and loading
- Shapes are UUID-indexed, and hierarchy/structure is tracked separately. `ShapesPool::get` may return a cached modified clone when modifiers, structure, scale-content, or bool handling apply; `get_raw` bypasses those derived values.
- Bulk loading uses a `loading` flag. `touch_current` / `touch_shape` avoid tile invalidation while loading; text layouts and final view setup must happen after loading ends.
- Many setters mutate only the current shape selected by `use_shape` / current-shape APIs. If no current shape is selected, some mutation blocks are skipped silently.
- `set_parent_for_current_shape` only sets parent metadata and invalidates parent geometry; children must be updated separately to avoid duplicate children.
- Child deletion marks descendants deleted and removes them from all indexed tiles, preserving undo/redo while avoiding stale pixels after panning.
## Tile/render behavior
- Raster `Fill::Image`: skip `save_layer` unless the shape has an image filter; plain
Rect/Frame (no corners) also skip the container clip (`draw_image_fill` in fills.rs).
- `can_render_directly` paints onto Current (no Fills/Strokes blit) for plain geometry and
for stroke-free text (SrcOver, no blur/shadows). Multi-style text is fine: span styles
live in Paragraph `TextStyle`s. Text skips the `nested_fills` guard (fills are on spans).
`draw_text` only `save_layer`s when stroke-group opacity is set; plain fill paint is direct.
- Plain text fill paint reuses `TextContent.layout` paragraphs when
`has_usable_paint_layout` (paragraphs present + version match; during
interactive transforms rotation/move skips width check via
`modifier_changes_text_layout`, resize falls back to `layout_width` vs
`get_width(selrect.width())`), via `text::try_paint_from_layout_cache`.
The walker computes `text_layout_cache_rotation_only` from `tree` and
passes it into `render_shape`; stroke/shadow paths pass `false`.
- `TextContentLayout` paragraphs are `Rc`-shared on `Clone` so modifier clones
(rotate/pan) keep the paint cache; `needs_update` is paragraphs-empty only.
Decorations are skipped when no span requests underline/strike.
- Zoom settle: visible tiles present via `FrameType::ViewportReady` before interest-ring
work; crop-cache rebuild is deferred to the later `Full` so the soft→sharp snap is
compose+present only.
- Interactive transforms are distinct from viewport fast mode. `set_modifiers_start` enables fast mode and interactive transform; interactive transform still flushes each animation frame.
- During interactive transform, modifier tile invalidation is deferred to `render()` once per rAF. Outside interactive transform, `set_modifiers` rebuilds modifier tiles immediately.
- `set_modifiers_end` disables fast/interactive state and cancels pending async render; the caller must request the final full-quality render.
- Plain viewport fast mode (`options.is_viewport_interaction()`) renders from cache and does not flush target output inside `process_animation_frame`; interactive transforms do flush.
- Zoom settle wipes the tile texture cache in `set_view_end`. Mid-zoom overlays
key tiles by scale; shape edits must `invalidate_cached_tiles_intersecting`
the old∪new extrect so those overlays do not keep pre-edit pixels.
- Pending tile priority is intentionally reversed by pop order; check the queue construction before changing tile scheduling.
- Frames with a fill may use `render_frame_container_drop_shadow` (direct rrect +
blur saveLayer on `DropShadows`) when `uses_direct_container_drop_shadow` is true.

View File

@ -0,0 +1,5 @@
# render-wasm FFI
- For shape loading, tile invalidation and rendering see `mem:render-wasm/rendering`.
- The unsafe global `STATE` must be valid before calling the `with_state*` macros; invalid state is a panic, not a recoverable error. Inspect `#[wasm_error]` and the frontend bridge together when changing error handling: the macro clears the code on entry, frees the byte slot on failure and distinguishes recoverable `0x01` from critical `0x02`; the bridge maps them to `:non-blocking` and `:panic`.
- WASM byte transfer uses one global slot. Read and free a returned pointer before another payload is written, or the first payload is lost. Keep this ownership rule consistent in the frontend and exporter bridges.

View File

@ -0,0 +1,7 @@
# render-wasm shape and tile rendering
- `ShapesPool::get` can return a modified clone; `get_raw` bypasses derived modifiers and structure. Hierarchy is separate from shape data. `set_parent_for_current_shape` updates parent metadata, not children; keep both sides consistent without adding duplicates.
- Setters may silently skip mutation without a current shape. During bulk loading, touch operations skip tile invalidation; finish loading before text layout and final view setup. Deleting children marks descendants deleted and removes them from indexed tiles so undo/redo and panning do not show stale pixels.
- Interactive transforms and viewport fast mode have different flush behavior. Interactive modifier invalidation is deferred until render, while non-interactive updates invalidate immediately. `set_modifiers_end` cancels pending async render; its caller must request the final full-quality render. See `set_modifiers_start`, `set_modifiers_end` and `process_animation_frame`.
- Zoom overlays cache tiles by scale. Invalidate cached tiles intersecting both old and new extents on edits, not only the current-scale tiles. At zoom settle, visible tiles present before interest-ring work; defer the crop-cache rebuild to `Full`. Check pending tile pop order before changing priorities.
- Direct painting, image layers, text layout reuse and frame shadows depend on rendering guards. Consult `can_render_directly`, `draw_image_fill`, `has_usable_paint_layout` and `uses_direct_container_drop_shadow` with their tests before broadening an optimization: modifiers, styles or effects can require a different surface or fresh layout.