# Native Rendering

How the native host turns retained elements into layout, drawing commands, and incrementally rendered pixels.

<a id="pipeline"></a>

## A retained host and a replaceable renderer

`refract-native` owns the window, input, layout, and presentation lifecycle. Core sends semantic patches keyed by `ElementId`; the host applies them to retained elements, then commits the prepared update. Drawing happens separately. A frame can change because of scrolling, hover, or a caret without reevaluating an application view.

The default renderer uses Vello and wgpu. `run_with_renderer` can supply another `Renderer` while retaining the same host, reconciliation, layout, and text shaping. Renderers receive positioned drawing commands rather than views or graph state. See [Architecture](/docs/architecture) for the surrounding update cycle.

| Stage | Responsibility |
| --- | --- |
| Winit event loop | Translate input, drain scheduled work, flush core updates, and request redraws. |
| Retained host | Keep elements, child order, layout nodes, text, images, and interaction state. |
| Layout and painting | Resolve logical geometry and build a complete `Frame` in painter’s order. |
| Renderer | Rasterize commands into pixels and present them through the window surface. |

<a id="resources"></a>

## Layout, text, and images survive updates

Each retained element has a Taffy layout node. Patches update those nodes and invalidate layout when geometry, content measurements, or topology changes; viewport changes also require layout. Paint-only updates can reuse the existing layout. Taffy’s own caches can skip unchanged nodes within a layout pass.

Parley shapes text into cached glyph runs. Color changes do not reshape text, and wrapping can reflow existing shaping; truncated labels cache glyphs for their current width and alignment. Images share resources for equal sources and load through a bounded worker queue. Decoded pixels return to the UI thread, where completion can invalidate measurement and request a frame.

Editable fields retain a Parley-backed `TextEditor` alongside their host element. It manages selection, composition, local history, and editor scrolling, then emits glyphs and decoration commands into the frame. See [Text Editing](/docs/text-editing#native-editor) for synchronization and the ranged-style extension in Refract's Parley fork.

<a id="commands"></a>

## The drawing contract

A `Frame` contains a background color, scale factor, and complete ordered list of `DrawCommand` values. Geometry uses logical pixels with a top-left origin; the renderer converts it to physical pixels. Commands describe fills, gradients, borders, shadows, images, positioned glyphs, and balanced layer pushes and pops. Glyph runs and image pixels are shared immutable resources.

Host painting walks retained elements, resolves scrolling and clipping, and emits commands without evaluating views or reading application state. Clips and group opacity apply to composed subtrees. A custom renderer owns its device, surface, and caches and implements `resize` and `render`; it may render complete frames or use `DamageTracker` to retain pixels.

<a id="damage"></a>

## Retain pixels and repaint damage

Vello retains an offscreen target between frames. `DamageTracker` compares commands with the last successfully rasterized frame, trims equal prefixes and suffixes, and combines old and new bounds for changed commands. Damage is a single physical-pixel rectangle with antialiasing coverage. Selecting intersecting commands preserves painter’s order and layer delimiters, so overlapping content is repainted correctly.

A regional update renders from the background into a region-sized scratch texture, then copies it into the retained target. Unchanged frames skip rasterization. First frames, resize, scale or background changes, changed layer boundaries, unknown changed bounds, and changed frames containing filter effects require full rendering. Vello also renders fully when damage covers at least three quarters of the target.

> Incremental rasterization still receives a complete frame and compares its command list. It reduces encoding and pixel work for local changes; view evaluation, layout, frame construction, and presentation remain separate costs.

<a id="effects"></a>

## Layers, blur, and backdrops

`PushEffects` isolates a subtree for content blur, backdrop blur, and group opacity. The Vello encoder captures earlier painted content for a backdrop, renders filtered groups into intermediate textures, applies GPU Gaussian passes, and composites the result. Backdrop snapshots include ancestor clipping and earlier siblings, never later paint.

Filters can sample outside a changed rectangle, and backdrop filters depend on earlier content. Until those dependencies are tracked regionally, any changed frame containing `PushEffects` takes the full-render path. The renderer retains reusable image and shadow resources alongside its effect machinery; custom backends must preserve the same layer and filter semantics.

<a id="presentation"></a>

## Scheduling and surface presentation

The Winit loop coalesces wakeups, flushes pending work, and sleeps until an event or the next timer, scrollbar, or retry deadline. Drawing samples the latest window size for both layout and surface allocation. Zero-sized and occluded windows skip drawing. `Presentation::Retry` keeps the frame pending and schedules a delayed attempt instead of spinning.

Presentation copies the retained target into the acquired surface texture, even when rasterization found no damage: swapchain images need not preserve previous pixels. Surface loss recreates the renderer’s retained resources. `Presentation::Presented` means the frame was submitted and handed to the surface, not that the display has scanned it out.

<a id="source"></a>

## Diagnostics and implementation

`NativeOptions::diagnostics` reports core commits and host work. `VelloRenderer::stats()` reports submitted command and pixel counts, excluding presentation. `RenderMode::Full` provides a comparison path; GPU tests compare its pixels with incremental rendering. The optional `debug-damage` feature outlines repainted regions at presentation time without changing retained scene pixels.

| Source | Responsibility |
| --- | --- |
| `crates/native/src/app/mod.rs` | Window lifecycle, flushes, redraws, and presentation retries. |
| `crates/native/src/host/mod.rs` and `host/paint.rs` | Retained elements, patch application, layout, and frame construction. |
| `crates/native/src/layout.rs`, `text.rs`, and `image.rs` | Taffy styles, Parley shaping caches, and image loading. |
| `crates/native/src/renderer/mod.rs` and `damage.rs` | Drawing contract and renderer-independent damage planning. |
| `crates/native/src/renderer/vello.rs` and `vello_painter.rs` | Surface presentation, retained targets, and regional rasterization. |
| `crates/native/src/renderer/vello_scene.rs` and `vello_effects.rs` | Scene encoding, resource reuse, and GPU filtering. |
| `crates/native/src/renderer/vello_incremental_tests.rs` | Full-versus-incremental pixel checks and profiling. |
