ui_controls

module pkg::alloy::drawsource

A retained 2D renderer over pkg::alloy::gpu (PLAN.md, stage F3): a canvas holds a tree of nodes, each with a transform, a clip for its children and a paint, the primitives it draws in its own coordinates. A frame redraws only what changed since the last one, into a texture the canvas keeps, then copies that to the frame. Coordinates are physical pixels, y down; colors are sRGB with straight alpha, blended as browsers blend them.

var canvas = Canvas::create(&var device, surface.format);
const card = canvas.add(canvas.root());
canvas.set_transform(card, Transform::translate(40.0, 40.0));
var paint = Paint::empty();
paint.rounded_rect(Rect::sized(300.0, 200.0), Radii::all(12.0), Fill::Color(Color::white()));
canvas.paint(card, move paint);

// on Redraw
if (canvas.damaged()) {
    if (device.acquire(surface, window.size()) is ::Some |frame|) {
        var encoder = Encoder::empty();
        canvas.render(&var device, &var encoder, frame);
        device.submit(move encoder);
        device.present(surface, &var window);
    }
}

## How it was built

Notes kept from each stage as it was finished, the sizes and numbers of their day; what changed since is in REVIEW.md and the history.

**F3 as built** (2026-10-12):

| | lavapipe | GTX 1660 | RTX 4070 Ti | |---|---|---|---| | encoding 10,000 primitives on 1000 nodes | 3.8 | 3.2 | 2.1 | | encoding one node changed among them | 0.07 | 0.06 | 0.03 | | a whole 1920x1080 frame of them, to its fence | 38.5 | 3.0 | 1.3 | | a frame redrawing one node | 1.7 | 0.35 | 0.14 |

(Encoding is CPU time, on each machine's CPU.) The startup program (12 cards in a window) presents its first frame 64 ms after main under headless weston on lavapipe, which compiling its pipelines dominates, peaking at 85 MB resident (lavapipe's LLVM); it is 0.77 MB natively and 0.55 MB of wasm. The plan's 50 ms and 20 MB targets are for a hello world on a GPU driver, to measure with F5.

**F10d as built** (2026-10-15; decisions/F10.md, "Color emoji"):

**F11c as built** (2026-10-15; decisions/F11.md, "What window and draw gain"): canvas.set_hidden(node, hidden) and is_hidden: a hidden node covers nothing (its reach empty, so frames gather nothing under it) and is skipped with its descendants, which keep their paint; hiding damages where the subtree drew, showing marks it moved, so it is measured and drawn again. ui hides the views a switch or a navigator keeps.

**F11e as built** (2026-10-15; decisions/F11.md, question 12A): canvas.set_opacity(node, opacity) and opacity: the alpha of every primitive under the node multiplied by the opacities above it as a frame encodes it (gradient stops' too, as they go into the frame's), so overlapping parts show through while fading; a node at 0 is skipped with all under it; a change damages where the subtree reaches. Color::mix(a, b, t).

**Rounded clips nested** (2026-10-17; decisions/showcase.md, question 3A, ui's clip_radii): a rectangle clipping inside a rounded clip, both upright, no longer drops the rounded one for the scissor: when their intersection is one rounded rectangle (each corner the rounded clip's own or clear of its curve, as a picture across a card's top) that is tested per pixel, else the rounded clip is and the rectangle cuts by the scissor (child_state in encoding.alloy). A rounded clip inside another still keeps only the inner one's corners. Golden clips.png gained a band across a rounded card's top.

**F14c as built** (2026-10-20; decisions/F14.md, "Glyph memory", questions 9B and 10A):

type Rectsource

exp type Rect = struct

A rectangle from its top left corner.

Fields

exp x: f32
exp y: f32
exp width: f32
exp height: f32

Functions

fn Rect::atsource

exp fn Rect::at(x: f32, y: f32, width: f32, height: f32) -> Rect

fn Rect::sizedsource

exp fn Rect::sized(width: f32, height: f32) -> Rect

A rectangle at the origin.

fn Rect::emptysource

exp fn Rect::empty() -> Rect

fn bottomsource

exp fn bottom(self r: &Rect) -> f32

fn is_emptysource

exp fn is_empty(self r: &Rect) -> bool

fn intersectionsource

exp fn intersection(self r: &Rect, other: &Rect) -> Rect

What both cover, empty when they do not meet.

fn unionsource

exp fn union(self r: &Rect, other: &Rect) -> Rect

The smallest rectangle holding both, an empty one counting for nothing.

fn intersectssource

exp fn intersects(self r: &Rect, other: &Rect) -> bool

fn inflatedsource

exp fn inflated(self r: &Rect, by: f32) -> Rect

The rectangle grown by by on every side.

fn areasource

exp fn area(self r: &Rect) -> f32

type Radiisource

exp type Radii = struct

A rounded rectangle's corner radii.

Fields

exp top_left: f32
exp top_right: f32
exp bottom_right: f32
exp bottom_left: f32

Functions

fn Radii::allsource

exp fn Radii::all(radius: f32) -> Radii

fn Radii::nonesource

exp fn Radii::none() -> Radii

type Transformsource

exp type Transform = struct

A 2D affine transform: the point (x, y) goes to (a x + c y + tx, b x + d y + ty).

Fields

exp a: f32
exp b: f32
exp c: f32
exp d: f32
exp tx: f32
exp ty: f32

Functions

fn Transform::identitysource

exp fn Transform::identity() -> Transform

fn Transform::translatesource

exp fn Transform::translate(x: f32, y: f32) -> Transform

fn Transform::scalesource

exp fn Transform::scale(x: f32, y: f32) -> Transform

fn Transform::rotatesource

exp fn Transform::rotate(radians: f32) -> Transform

Turns by the angle, clockwise on the screen, about the origin.

fn thensource

exp fn then(self t: &Transform, next: &Transform) -> Transform

This transform, then the next.

fn applysource

exp fn apply(self t: &Transform, p: Point) -> Point

fn inversesource

exp fn inverse(self t: &Transform) -> Transform

The transform undoing this one; a flat one has none, and gives the identity.

fn map_rectsource

exp fn map_rect(self t: &Transform, r: &Rect) -> Rect

The bounds of the rectangle once transformed.

type Stopsource

exp type Stop = struct

A gradient's color at an offset from 0 to 1 along it.

Fields

exp offset: f32
exp color: Color

type Gradientsource

exp type Gradient = struct

Two points: a linear gradient's start and end, or a radial one's center and a point on its circle.

Fields

exp start: Point
exp end: Point
exp stops: Vector<Stop>

type Fillsource

exp type Fill = enum

Variants

Color: Color
Linear: Gradient
Radial: Gradient

Functions

const MAX_STOPSsource

exp const MAX_STOPS: u64 = 8;

At most this many stops a gradient.

fn Fill::linearsource

exp fn Fill::linear(start: Point, end: Point, colors: &[Color]) -> Fill

A linear gradient through the colors, evenly spaced.

fn Fill::linear_stopssource

exp fn Fill::linear_stops(start: Point, end: Point, stops: &[Stop]) -> Fill

fn Fill::radialsource

exp fn Fill::radial(center: Point, radius: f32, colors: &[Color]) -> Fill

A radial gradient through the colors, evenly spaced, from the center out to the radius.

fn Fill::radial_stopssource

exp fn Fill::radial_stops(center: Point, radius: f32, stops: &[Stop]) -> Fill

fn Color::mixsource

exp fn Color::mix(a: Color, b: Color, t: f32) -> Color

The color a fraction t of the way from a to b, each component and the alpha mixed straight: 0 gives a, 1 gives b.

type Shadowsource

exp type Shadow = struct

A box shadow, as CSS's: the shape moved by the offset, grown by the spread and blurred over the blur radius (twice the Gaussian's sigma).

Fields

exp offset: Point
exp blur: f32
exp spread: f32
exp color: Color

Functions

fn Shadow::softsource

exp fn Shadow::soft(x: f32, y: f32, blur: f32, color: Color) -> Shadow

type Filtersource

exp type Filter = enum

How an image is sampled between its pixels.

Variants

Linear
Nearest

type Picturesource

exp type Picture = struct

A region of a texture to draw images or masks from: one the canvas packed (Canvas::picture, Canvas::mask) or one of the program's.

Fields

exp texture: Texture
exp region: Rect

in the texture's pixels

Functions

fn Picture::wholesource

exp fn Picture::whole(texture: Texture) -> Picture

A whole texture of the program's, which it keeps alive while drawn.

type Paintsource

exp type Paint = struct

What a node draws: primitives in order, in its own coordinates.

Functions

fn Paint::emptysource

exp fn Paint::empty() -> Paint

fn textsource

exp fn text(self paint: &var Paint, layout: &Layout, origin: Point, color: Color)

The layout's text with its pen at origin, its top left, in the color, but where a span gives its own; underlines and strikethroughs under it. The layout's fonts are the canvas's (canvas.fonts()).

fn shrinksource

exp fn shrink(self paint: &var Paint)

Gives back the room past what the paint holds, for a paint kept.

fn lengthsource

exp fn length(self paint: &Paint) -> u64

How many primitives it holds.

fn rectsource

exp fn rect(self paint: &var Paint, area: Rect, fill: Fill)

fn rounded_rectsource

exp fn rounded_rect(self paint: &var Paint, area: Rect, radii: Radii, fill: Fill)

fn bordersource

exp fn border(self paint: &var Paint, area: Rect, radii: Radii, width: f32, color: Color)

A border of the width inside the rounded rectangle's edge.

fn shadowsource

exp fn shadow(self paint: &var Paint, area: Rect, radii: Radii, shadow: Shadow)

The rounded rectangle's shadow, drawn under what follows. One radius shapes it, the top left one.

fn imagesource

exp fn image(self paint: &var Paint, area: Rect, picture: Picture)

The picture stretched over the area, filtered linearly.

fn imagesource

exp fn image(self paint: &var Paint, area: Rect, picture: Picture, filter: Filter)

fn masksource

exp fn mask(self paint: &var Paint, area: Rect, picture: Picture, color: Color)

The color where the picture, a coverage mask, covers the area.

type NodeIdsource

exp type NodeId = struct

A node of a canvas, valid until it is removed.

type Clipsource

exp type Clip = struct

A clip: its children draw only inside the rounded rectangle, in the node's coordinates.

Fields

exp area: Rect
exp radii: Radii

Functions

fn Clip::rectsource

exp fn Clip::rect(area: Rect) -> Clip

fn Clip::roundedsource

exp fn Clip::rounded(area: Rect, radius: f32) -> Clip

type FrameStatssource

exp type FrameStats = struct

What the last frame drew, for tests and benchmarks.

Fields

exp rects: Vector<Rect>

the damaged rectangles redrawn, whole pixels

exp full: bool

the whole frame redrawn

exp instances: u32

the primitives drawn, a clear for each rectangle included

exp draws: u32

type Canvassource

exp type Canvas = struct

Functions

fn Canvas::createsource

exp fn Canvas::create(device: &var Device, format: TextureFormat) -> Canvas

A canvas drawing into textures of the format: a surface's, or one the program reads back.

fn fontssource

exp fn fonts(self canvas: &var Canvas) -> &var Fonts

The fonts the canvas draws text in, the bundled family first, for the layouts it draws.

fn add_fontsource

exp fn add_font(self canvas: &var Canvas, bytes: Vector<u8>) -> Option<Family>

A TrueType face as a family of its own (pkg::alloy::text's Fonts::add).

fn add_font_tosource

exp fn add_font_to(self canvas: &var Canvas, family: Family, bytes: Vector<u8>) -> bool

fn releasesource

exp fn release(self canvas: &var Canvas, device: &var Device)

Releases what the canvas made on the device: its pictures' textures among them.

fn rootsource

exp fn root(self canvas: &Canvas) -> NodeId

The node every other descends from, which the canvas never removes.

fn addsource

exp fn add(self canvas: &var Canvas, parent: NodeId) -> NodeId

A new node, the parent's last child: it draws after the parent's other children, over them.

fn removesource

exp fn remove(self canvas: &var Canvas, node: NodeId)

Removes the node and its descendants; what they drew is damaged.

fn containssource

exp fn contains(self canvas: &Canvas, node: NodeId) -> bool

Whether the node is still there: neither it nor an ancestor removed.

fn set_transformsource

exp fn set_transform(self canvas: &var Canvas, node: NodeId, transform: Transform)

Moves the node and its descendants: the transform takes its coordinates into its parent's.

fn transformsource

exp fn transform(self canvas: &Canvas, node: NodeId) -> Transform

fn set_clipsource

exp fn set_clip(self canvas: &var Canvas, node: NodeId, clip: Option<Clip>)

Clips the node's children, not its own paint, to the rounded rectangle; None draws them whole.

fn set_hiddensource

exp fn set_hidden(self canvas: &var Canvas, node: NodeId, hidden: bool)

Hides the node and its descendants, which keep what they draw, or shows them again; where they are is damaged.

fn set_opacitysource

exp fn set_opacity(self canvas: &var Canvas, node: NodeId, opacity: f32)

Fades the node and its descendants: the alpha of all they draw multiplied by the opacity, from 0 (nothing drawn) to 1, as each primitive is drawn, so where they overlap the lower shows through the upper. Where they draw is damaged.

fn opacitysource

exp fn opacity(self canvas: &Canvas, node: NodeId) -> f32

fn is_hiddensource

exp fn is_hidden(self canvas: &Canvas, node: NodeId) -> bool

Whether the node is hidden.

fn paintsource

exp fn paint(self canvas: &var Canvas, node: NodeId, paint: Paint)

Replaces what the node draws.

fn set_backgroundsource

exp fn set_background(self canvas: &var Canvas, color: Color)

The color under everything, white at first.

fn damagedsource

exp fn damaged(self canvas: &Canvas) -> bool

Whether a frame would change anything: false, the last one stands.

fn damage_allsource

exp fn damage_all(self canvas: &var Canvas)

Redraws the whole next frame, as after the window's size changed.

fn rendersource

exp fn render(
    self canvas: &var Canvas,
    device: &var Device,
    encoder: &var Encoder,
    frame: Texture
)

Records the frame's drawing into the encoder: what changed since the last frame redrawn into the texture the canvas keeps, which is then copied to the frame, a texture of the canvas's format that copies may write. A frame of another size than the last redraws everything.

fn render_oversource

exp fn render_over(
    self canvas: &var Canvas,
    device: &var Device,
    encoder: &var Encoder,
    frame: Texture
)

The same, but blending the kept texture over what the frame holds instead of copying it, its premultiplied alpha letting the frame show where the canvas draws nothing: a canvas whose background is transparent over another's drawing, as an engine's (pkg::alloy::ui's Hud). Every frame blends, whether anything changed or not.

fn texturesource

exp fn texture(self canvas: &Canvas) -> Option<Texture>

The texture the canvas keeps its drawing in, of the last frame's size, for a program placing it itself; None before the first frame.

fn statssource

exp fn stats(self canvas: &Canvas) -> &FrameStats

What the last frame drew.

type GlyphBudgetsource

exp type GlyphBudget = struct

How many pages of glyphs the canvas keeps of each format, each cut into 4 by 4 plots, which it clears the least recently used of when it needs room. A frame needing more than these gets pages past them, given back in the frames after it.

Fields

exp coverage: u32 = 4

pages of one-byte pixels: 4 MiB

exp color: u32 = 2

pages of RGBA: 8 MiB

exp page: u32 = 1024

a page's width and height in pixels, a multiple of 4

type GlyphStatssource

exp type GlyphStats = struct

What the glyph cache holds, and what its last frame did.

Fields

exp pages: u32

the glyphs' textures, a large glyph's own among them

exp past: u32

of them past the budget

exp plots: u32

holding glyphs

exp bytes: u64

the textures'

exp entries: u64

the glyphs the cache knows

exp rasterized: u32

by the last frame

exp cleared: u32

plots, by the last frame

fn set_glyph_budgetsource

exp fn set_glyph_budget(self canvas: &var Canvas, device: &var Device, budget: GlyphBudget)

Sets the glyph cache's budget, which pages past it follow from the next frame on; a change of the page's size empties the cache.

fn glyph_statssource

exp fn glyph_stats(self canvas: &Canvas) -> GlyphStats

fn picturesource

exp fn picture(
    self canvas: &var Canvas,
    device: &var Device,
    width: u32,
    height: u32,
    pixels: &[u8]
) -> Picture

Packs the image, RGBA rows with straight alpha, into a texture the canvas keeps: small ones share pages of an atlas.

fn release_picturesource

exp fn release_picture(self canvas: &var Canvas, device: &var Device, picture: Picture)

Gives back what a picture picture packed holds, for the next one; nothing may draw it afterwards.

fn masksource

exp fn mask(
    self canvas: &var Canvas,
    device: &var Device,
    width: u32,
    height: u32,
    coverage: &[u8]
) -> Picture

Packs a coverage mask, a byte a pixel, as glyphs are.