Rendering and GPU
render
render is the 2D canvas. It needs neither Wayland nor a GPU, so it is the fallback when gpu.Open fails.
A Canvas paints into a caller-owned buffer: rows of Stride(width) bytes of premultiplied ARGB8888 in wl_shm byte order. Color uses the same layout, 0xAARRGGBB; RGB and RGBA build one, ParseHex reads one from CSS hex (#rgb, #rgba, #rrggbb, #rrggbbaa, straight alpha) and FormatHex writes it back.
package main
import (
"image/png"
"log"
"os"
"github.com/stubbedev/lys/lib/render"
)
func main() {
const width, height = 320, 120
stride := render.Stride(width)
pixels := make([]byte, stride*height)
cv := render.New(pixels, stride, width, height)
cv.Clear(cv.Rect(), render.RGB(0x1f, 0x21, 0x36))
data, err := os.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
face, err := render.LoadFont(data)
if err != nil {
log.Fatal(err)
}
face.DrawAligned(cv, "lys-lib", cv.Rect(), 32, render.RGB(0xff, 0xb4, 0x54), render.AlignCenter)
out, err := os.Create("out.png")
if err != nil {
log.Fatal(err)
}
defer out.Close()
if err := png.Encode(out, render.NRGBA(pixels, stride, width, height)); err != nil {
log.Fatal(err)
}
}| Area | API |
|---|---|
| Device scale | NewScaled takes a fractional num/denom scale |
| Shapes | Canvas.FillRect, FillPath, Arc, Line, RoundedBorder, BoxShadow; Path, ParsePathData |
| Paint | Gradient, the sixteen CSS BlendModes, Filters |
| Layers | Canvas.Layer, DrawLayer, Composite, CompositeProjective |
| Clip and opacity | PushClip, PushAlpha, PushBrightness |
| Images | LoadPNG, LoadJPEG, LoadSVG, IconFromImage make an Icon; DecodeImage also returns an Animation for GIF and APNG |
| Animated icons | LoadStatefulSVG makes an Icon whose Stateful() holds a GTK symbolic icon's states, transitions and animations; an IconPlayer draws its frames at explicit times (IsStatefulSVG tells such a file) |
| Text | LoadFont makes a Typeface; NewChain falls back across faces; Variation sets variable-font axes; WrapText, EllipsizeText |
| Damage | Rect.Subtract, Rect.Union, UnionAll |
- Fonts are shaped by typesetting; SVG uses oksvg and rasterx.
LoadSVGreturns an error for an unsupported element.LoadFontreads tables in place: keep its bytes valid and unchanged while the typeface is in use.
Recording
NewRecorder returns a canvas that records a List instead of painting, so one drawing routine serves both renderers:
gpu.Device.Renderdraws the list on the GPU.List.Paintrasterizes it onto a canvas, identical to painting directly.List.Playhands its calls to anyPlayer.
A list references its inputs without copying them; play it within the frame that recorded it.
text
Editing rules independent of any widget. Positions are rune indexes.
| Area | API |
|---|---|
| Words | WordStart, WordEnd, WordRun, WordBoundaries, IsWordRune |
| Grapheme clusters | NextCluster, PrevCluster, ClusterRun, ClusterStarts, SnapCluster, SnapClusterForward |
| Bidirectional text (UAX #9) | BidiRuns, VisualOrder, VisualStep, VisualWordStep, RTL |
A word is a run of same-class grapheme clusters, the class being letters, digits and underscore. This is not UAX #29 word segmentation: word motion across server.hostname.example skips the dots.
gpu
gpu renders through Vulkan into memory shared by file descriptor, without copies.
package main
import (
"errors"
"image/png"
"log"
"os"
"github.com/stubbedev/lys/lib/gpu"
"github.com/stubbedev/lys/lib/render"
)
func main() {
dev, err := gpu.Open()
if errors.Is(err, gpu.ErrNoVulkan) || errors.Is(err, gpu.ErrNoDevice) {
log.Fatalf("no GPU here, so draw on the CPU with render.New: %v", err)
}
if err != nil {
log.Fatal(err)
}
defer dev.Close()
target, err := dev.NewOffscreen(320, 120)
if err != nil {
log.Fatal(err)
}
defer target.Close()
cv := render.NewRecorder(320, 120, 1, 1)
cv.Clear(cv.Rect(), render.RGB(0x1f, 0x21, 0x36))
cv.FillRect(render.Rect{X: 40, Y: 40, W: 240, H: 40}, render.RGB(0xff, 0xb4, 0x54))
if err := dev.Render(cv.Recording(), target); err != nil {
log.Fatal(err)
}
img, err := target.ReadPixels()
if err != nil {
log.Fatal(err)
}
out, err := os.Create("out.png")
if err != nil {
log.Fatal(err)
}
defer out.Close()
if err := png.Encode(out, img); err != nil {
log.Fatal(err)
}
}- A
Deviceand everything made from it belong to one goroutine. Openpicks the first device with a graphics queue that exports dmabufs, else the first with a graphics queue.OpenNodeopens the device behind a DRM node.OpenCPUopens only a device of type CPU (lavapipe) that exports dmabufs, never a GPU, to render into buffers a card without a Vulkan driver allocates.Preparebuilds pipelines on a worker; a pass that meets an unbuilt pipeline skips that draw and reportsIncomplete.
| Error | Meaning |
|---|---|
ErrNoVulkan | No Vulkan loader |
ErrNoDevice | No device that draws |
ErrNoDmabuf | Device renders offscreen only; Exports reports it, NewTarget fails with it |
ErrNoSyncFile | No sync_file semaphores; SyncFiles reports it |
ErrNoFrameTime | No calibrated timestamps |
ErrNoVideoEncoder | See video encoding |
ErrBusy | Render refused a frame because too many are in flight; retry later |
ErrDeviceLost | Device lost; close it and open a new one. LossLimit losses within LossWindow is the recovery bound. A test arms one with Device.LoseForTesting: the device's next submission, a video decoder's or encoder's too, and everything after it fail with it, the GPU untouched |
ErrDeviceClosed | The device is closed |
Image kinds are distinct types:
| Type | Made by | Purpose |
|---|---|---|
Target | NewTarget, NewOffscreen, NewOffscreenFloat, ImportTarget | Rendered into: a window frame, a scanout buffer, offscreen pixels |
Texture | NewTexture, ImportTexture, ImportTextureYCbCr | Sampled across frames; CPU-uploaded or imported from a dmabuf |
Buffer | NewBuffer | A dmabuf others write, such as a screencast frame or the linear copy of a frame another card scans out; never drawn into, so non-renderable layouts are allowed. In any of TextureFormats, the 10-bit ones too |
Linear | ImportLinear | An imported linear dmabuf; CopyToLinear copies a target into it |
Drawing:
Renderdraws a recordedList.RenderExplicitdoes the same and returns async_fileinstead of waiting.Beginopens aPassclipped to damage:Clear,FillRect,FillGradient,DrawTexture,DrawFrame(a decoded video frame),DrawGlass,DrawFragmentShader,FillGoo,Kawase,SetCut,SetColor. The first failing call fails the pass;Endreports it and records nothing.Endrecords without submitting. A frame's passes go to the queue in one submission whenPublishhands their target over,SyncFileexports, a CPU read orFinishneeds them,FrameDoneasks, orFlushis called.Submissionscounts submissions.Publishhands a target the passes drew to its consumer, asRenderdoes for its own: it submits them and attaches their fence to the target's dmabuf, which linux-dmabuf's implicit synchronization has every reader wait on, and returns without waiting. Where no fence tells the consumer (NVIDIA's driver, which does not wait on one when a compositor samples a buffer; a device withoutsync_filesemaphores; a kernel that attaches none) it waits until the passes are done instead. A consumer that takes an explicit fence usesSyncFile.- Every use of another user's dmabuf (a target's, an imported texture's, a
Buffer's, aLinear's) waits on the GPU for the fences that user left on it: the submission that uses it waits on a semaphore holding the dmabuf'sDMA_BUF_IOCTL_EXPORT_SYNC_FILE. A draw or copy into it waits for every reader and writer, a sample or copy out of it for its writers, so a compositor drawing into an encoder'sBufferwaits for the conversion still reading it. A device withoutsync_filesemaphores, or a kernel without the ioctl (before Linux 6.0), polls the dmabuf here instead, within the wait limit. A wait that cannot be set up fails the submission it was for, and the next one runs without it. This wait is separate from the hand-over: on NVIDIA,RenderandPublishstill wait for their own frame after queuing it, because the compositor reading the buffer may ignore the fence, while the frame itself waits on the GPU for the fences already on the dmabuf.
Vulkan comes from the patched vk bindings. Shaders are embedded SPIR-V; just generate rebuilds them.
drm, fourcc, edid
drmissues DRM ioctls in pure Go: KMS resources, connectors, planes, properties and blobs, dumb buffers, framebuffers, atomic commits (Request,Commit), capabilities, CRTC vblank sequences (CRTCSequence), timeline syncobjs and sync files.fourccnames DRM pixel formats and modifiers.edidparses make, model and serial (Parse), colour and HDR data (ParseHDR) and the variable refresh range (ParseVRR).Vendornames a maker from the PNP ID table built into the package, generated from hwdata'spnp.ids(go generate ./edidin the dev shell; a test fails when it is stale).
dmabufs
dmabuf.Bufferdescribes a dmabuf: size, format, modifier, planes. Keep the plane fds open while the compositor-side buffer lives; importing does not dup them.BeginCPUWrite,EndCPUWriteandImportSyncFilehandle CPU access and implicit-sync fences;ExportSyncFiletakes the fences aReador aWriteof the dmabuf waits for as async_file, andWaitwaits for them here.dmabufwlbuildszwp_linux_buffer_params_v1objects (Params) and readszwp_linux_dmabuf_feedback_v1: the default feedback (DefaultFeedback) or a surface's (WatchSurfaceFeedback), parsed into tranches with target devices and scanout flags.Feedback.Preferredpicks the tranche to allocate from, scanout first.udmabufallocates linear, CPU-visible dmabufs from a sealed memfd through/dev/udmabuf, without a GPU library.dmabufprobepicks the modifier for a screencopy into GPU memory.Layoutsorders the compositor's import modifiers: linear, then device-renderable, then export-only.Firsttries each until a buffer allocates, imports and captures a frame;ErrNoLayoutmeans none is shared.
motion
Animation maths, bit-identical across architectures.
SpringStepandSpringPosition: closed-form damped harmonic oscillator from any position and velocity, so a retargeted spring keeps its motion.NewCubicBezier: the CSS timing function, clamping time controls as CSS does.