Skip to content
简体中文

Retained Native Scene Protocol

For high-density canvases, scrollable GPU documents, or 60 FPS scenes with hundreds of moving elements, use egfx-scene-apply instead of rebuilding a full draw list every frame.

egfx-scene-apply packs group, shape, shadow, text, image, and removal operations into a compact binary stream in a single Elisp pass and submits them in one FFI call:

(egfx-scene-apply
cv
`((:bg "#0f172a")
(:group 1 :tx 0.0 :ty -120.0 :scale 1.0 :opacity 1.0)
(:box 101 :group 1 :z 1 :x 40 :y 40 :w 220 :h 72
:radius 10 :fill "#1e293b" :stroke "#38bdf8" :line-width 1.5)
(:text 102 :group 1 :z 2 :x 58 :y 64 :size 15.0
:color "#e2e8f0" :text "Native CoreText / DirectWrite / Pango")))

Supported ops: :clear, :bg, :group, :remove-group, :box, :shadow, :circle, :line, :curve, :triangle, :arc, :pie, :text, :image, and :remove.

The native scene graph maintains O(1) ID-addressed hash tables for groups and items and separates updates into three cost tiers:

Tier Trigger Cost
Transform-only (xform_gen) Updating :group :tx, :ty, :scale, :opacity, or :clip O(groups) — updates GPU group transform buffer with zero item traversal or sorting
In-place patch (patch_gen) Updating an existing item keeping the same (group, z, order, kind) O(patches) — patches only that item’s GPU slot (gl_idx) with zero rebuild
Structural rebuild (structure_gen) Inserting, removing, or changing Z-layer / group membership Sorts visible items into contiguous Z-layer slices and reuses capacity-doubled GPU buffers

Performance Best Practice: Fixed-ID Object Pools

Section titled “Performance Best Practice: Fixed-ID Object Pools”

For 60 FPS particles, projectiles, or live telemetry bars, pre-allocate fixed integer IDs and hide inactive objects with :opacity 0 (or move them outside the viewport) instead of calling :remove and re-inserting every frame. This keeps every frame inside the O(patches) fast path with zero qsort spikes.

  • egfx-measure-text: Queries the exact platform GPU font engine (CoreText, Pango/Cairo, or DirectWrite) and returns cached (:width W :height H :ascent A :descent D) in logical pixels:
    (egfx-measure-text "GPU 渲染" :size 15.0 :weight 'semibold)
  • egfx-set-cursor: Switches the native OS cursor (default, hand, text, ew-resize, ns-resize, move) on a live canvas.
  • egfx-scene-stats: Returns (:items N :groups G :gen K) for profiling and diagnostics.