buffer

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Arena: one wl_shm pool per session. A single anonymous file backs the whole session's buffers; each buffer is a sub-allocation (a byte range) of the shared mapping. Growth goes through file truncate + wl_shm_pool.resize + remap, so a session holds exactly one fd and one mapping no matter how many windows resize how often - the memfd churn and per-buffer mappings this package used to leak are gone.

The one shm pixel format lys-ui submits: the constant every buffer the session arena hands out is created with, and the diagnostics line the doctor block reports. It lives here so the doctor quotes the wire truth instead of restating it.

Package buffer owns the wl_shm buffer lifecycle for a session: one shared shm pool (Arena) whose sub-allocations rotate through per-surface pools, so the shell never draws into a buffer the compositor is still reading from and no memfd is ever created twice.

Index

Constants

Format is the wl_shm pixel format every buffer this package creates advertises to the compositor: ARGB8888, premultiplied. XRGB is deliberately never used - a translucent surface (an app background with alpha < 255, the default panel shape) would lose its alpha channel and composite as garbage. In memory the format is little-endian, so a pixel sits as bytes B, G, R, A - the layout render.ColorFromBytes decodes and format_test.go pins.

Variables

View Source
var ErrBusy = errors.New("buffer: all pool buffers busy")

ErrBusy reports that every buffer in the pool is held by the compositor. The caller skips the frame and retries after the next release event or frame callback.

View Source
var ErrClosed = errors.New("buffer: pool closed")

ErrClosed reports the pool was closed for its surface's teardown; it hands out nothing anymore.

View Source
var FormatName = "ARGB8888 premultiplied (wl_shm format 0x" +
	strconv.FormatUint(uint64(wl.ShmFormatArgb8888), 16) + ")"

FormatName describes the shm pixel format every lys-ui buffer uses: ARGB8888 premultiplied, byte order B, G, R, A - the format wl_shm.format advertises as wl.ShmFormatArgb8888 and the only one the session requires (see wlsession.Connect).

Functions

func Cancel

func Cancel(b *Buffer)

Cancel gives up a buffer whose commit never went out: the compositor never saw it, no release can arrive, and the slot returns to the arena immediately. Committed one-shot buffers need no call at all: their release event retires them.

func CloseArenas

func CloseArenas(shm *wl.Shm)

CloseArenas tears down the arena bound to shm, if any: it destroys the pool proxy, unmaps, and closes the session's fd. Wire it to session close so a reconnecting client does not hold a dead pool.

func CopyRect

func CopyRect(dst, src *Buffer, r render.Rect)

CopyRect copies r, in buffer pixels, from src's pixels into dst's: two shared-memory buffers of one size. Both views follow the arena's current mapping first: a buffer that sat out frames can still point at the one a growth unmapped.

func MoveRect added in v0.2.5

func MoveRect(dst, src *Buffer, r render.Rect, dx, dy int)

MoveRect puts src's pixels of r, in buffer pixels, into dst moved by dx, dy, whatever falls outside r dropped. dst and src may be one buffer: the rows and pixels copy in the order the overlap needs.

func MovedArea added in v0.2.5

func MovedArea(r render.Rect, dx, dy int) render.Rect

MovedArea is the part of r that moving r's pixels by dx, dy writes.

func Reclaimable added in v0.2.5

func Reclaimable() bool

Reclaimable reports whether a session arena holds freed slots a TrimArenas would give back.

func TrimArenas added in v0.2.5

func TrimArenas()

TrimArenas gives every session arena's freed slots back to the kernel.

Types

type Allocate added in v0.2.5

type Allocate func(bufW, bufH int, done func(*gpu.Target, error))

Allocate makes a bufW x bufH GPU target off the loop and calls done with it on the loop.

type Arena

type Arena struct {
	// contains filtered or unexported fields
}

Arena is one session's shared wl_shm pool. It is not safe for concurrent use: like the rest of the buffer lifecycle it belongs to the event loop that draws the frames. A nil shm is allowed for wire-free tests; the arena then runs without the wl_shm pool proxy and hands out buffers with a nil WL.

func NewArena

func NewArena(shm *wl.Shm) *Arena

NewArena returns an empty arena over shm.

func (*Arena) Acquire

func (a *Arena) Acquire(bufW, bufH, scale int) (*Buffer, error)

Acquire sub-allocates a bufW x bufH buffer from the pool, growing the pool when no free slot fits. A fresh buffer is fully stale: reused slots carry whatever the last buffer at that offset drew, and the full staleness is what forces the repaint that overwrites it.

func (*Arena) Close

func (a *Arena) Close() error

Close destroys the pool proxy, unmaps, and closes the fd. Buffers still referencing the arena are detached; their slots are gone with the mapping.

func (*Arena) Live

func (a *Arena) Live() int

Live reports how many sub-allocations are handed out and not yet retired. Zero means every slot is back in the free list.

func (*Arena) Size

func (a *Arena) Size() int

Size reports the pool's current byte size: the high-water mark of the session's buffer demand.

func (*Arena) Trim added in v0.2.5

func (a *Arena) Trim()

Trim gives the pages of every slot freed since the last Trim back to the kernel. A resize frees and retakes slots each step, so retire leaves that to this idle-paced call: one fallocate per free range, however many buffers came and went.

type Buffer

type Buffer struct {
	// WL is the wayland buffer object; nil only in tests of pool
	// bookkeeping.
	WL *wl.Buffer

	// Data is the mmap view of the buffer: Height rows of Stride bytes,
	// ARGB8888 premultiplied, byte order B, G, R, A. Nil for a dmabuf
	// buffer.
	Data []byte

	Timeline *Timeline

	// Target is a dmabuf buffer's GPU target (NewDmabuf), which a frame
	// renders into; nil for a shm buffer.
	Target *gpu.Target

	// Width and Height are in buffer pixels (output scale already
	// applied).
	Width, Height int
	Stride        int
	Scale         int

	// Stale is the region where this buffer's content lags what is on
	// screen: the damage painted into other buffers since this one was
	// last presented. A frame drawn here must repaint Stale plus the
	// frame's own damage; a fresh buffer starts fully stale.
	Stale Region
	// contains filtered or unexported fields
}

Buffer is one wl_shm-backed pixel buffer: in production a sub-allocation of the session arena's shared pool, sharing its fd and mapping.

func NewDmabuf

func NewDmabuf(allocate Allocate, mgr *wlr.ZwpDmabufV1, bufW, bufH, scale int, rejected func(error)) (*Buffer, error)

NewDmabuf returns a bufW x bufH buffer the GPU renders into, pending: its target is allocated through allocate, so the loop waits on no driver, and then imported through mgr as a wl_buffer. Its Data is nil; a frame renders into Target.

The import is linux-dmabuf's asynchronous create, whose refusal is an event: create_immed's is the fatal invalid_wl_buffer error, which would end the whole connection. The buffer is pending until the compositor answers, and a pool counts it busy meanwhile. A failed allocation or a refusal drops the buffer from its pool and reports the error to rejected. A pool avoids the frame's wait for both through Pool.Prepare.

func NewFile

func NewFile(shm *wl.Shm, bufW, bufH, scale int) (*Buffer, error)

NewFile allocates a bufW x bufH buffer of buffer pixels at the given integer output scale. The signature predates the session pool and is kept for its callers: the buffer is no longer a fresh memfd plus a throwaway wl_shm pool but a sub-allocation of shm's session arena, so N windows and resizes still mean exactly one fd and one mapping.

shm may be nil in wire-free tests: the arena then runs without the wl_shm pool proxy and hands out buffers with a nil WL.

func (*Buffer) Busy

func (b *Buffer) Busy() bool

Busy reports whether the compositor still holds the buffer.

func (*Buffer) Release

func (b *Buffer) Release()

Release marks the buffer free for the next frame. Only the wayland release event handler should call it; calling it earlier allows drawing into a buffer the compositor may still read.

A pool buffer goes back to its rotation; a buffer no pool owns - a one-shot surface's frame - is done after its release: the slot returns to the session arena right here. And a buffer its owner already dropped (resize, close, Cancel) has its deferred slot return now.

type FrameOutcome added in v0.2.5

type FrameOutcome uint8

FrameOutcome is what became of a frame handed to a GPU renderer.

const (
	// FrameDrawn went to the GPU: the buffer may be committed.
	FrameDrawn FrameOutcome = iota
	// FrameRefused did nothing, the GPU being too far behind: the buffer
	// goes back and the frame stays owed, stale rects and damage alike,
	// for the retry the renderer armed.
	FrameRefused
	// FrameIncomplete went to the GPU without something still on its way
	// there, a large image: the buffer may be committed, and what the frame
	// painted stays owed for the next one, which the commit's frame callback
	// brings.
	FrameIncomplete
	// FrameLost failed: the surface turns to the CPU.
	FrameLost
)

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is a fixed-capacity set of buffers for one surface, backed by the session arena through create.

func New

func New(create func() (*Buffer, error), capacity int) *Pool

New returns a pool holding at most capacity buffers, allocated on demand through create. A capacity below 1 is treated as 1.

func (*Pool) Acquire

func (p *Pool) Acquire() (*Buffer, error)

Acquire returns a buffer to draw the next frame into and marks it busy. Free buffers are reused before new ones are allocated; a new buffer still pending its import joins the pool busy, and the call reports ErrBusy until the compositor's answer frees it. ErrBusy means every buffer is held by the compositor: skip the frame and retry after a release event - the pool never grows past capacity to dodge it, and pending buffers never stack beyond the cap. Allocation failures are returned verbatim.

func (*Pool) Close

func (p *Pool) Close()

Close drops the pool for a closing surface: free buffers return to the arena now, held ones survive until the compositor releases them.

func (*Pool) CopiedPixels added in v0.2.5

func (p *Pool) CopiedPixels() int

CopiedPixels counts the pixels CopyForward copied on the CPU so far.

func (*Pool) CopyForward

func (p *Pool) CopyForward(b *Buffer) (carryFrom *Buffer)

CopyForward brings b up to the screen where it is stale by copying that region from the front buffer, which shows what the screen does, instead of painting it: a buffer that sat out frames (the third, taken while frames outpace releases) can owe most of the surface. Shared-memory buffers copy here. A GPU target copies inside the submission that draws it, so the front buffer comes back for the frame to carry b.Stale from, and b stays stale until the frame is Presented: a refused frame carried nothing. A nil result with b still stale leaves the frame to paint it.

func (*Pool) Deferrals added in v0.2.5

func (p *Pool) Deferrals() int

Deferrals counts the Acquires that reported ErrBusy, each a frame that could not go out when it wanted to.

func (*Pool) Footprint

func (p *Pool) Footprint() (n, bytes int)

Footprint is how many buffers the pool holds, rotation and retired, and their pixel bytes.

func (*Pool) Front

func (p *Pool) Front() *Buffer

Front is the buffer presented last, whose content is what the screen shows: a reused buffer can copy what it owes from it (CopyRect) rather than paint it again. Nil before the first present and after a resize.

func (*Pool) Pending

func (p *Pool) Pending() int

Pending reports how many buffers the pool still tracks: the rotation plus buffers waiting for the compositor's release. Zero after Close means every slot came back.

func (*Pool) Prepare added in v0.2.5

func (p *Pool) Prepare() error

Prepare allocates the second buffer ahead of the frame that needs it: the buffer a frame just committed stays the compositor's until the next commit, so a following frame finds nothing free. A dmabuf import answers before that frame then, rather than holding it back. Call it after a commit while frames are coming; a pool that holds two already, or none, does nothing.

func (*Pool) Presented

func (p *Pool) Presented(b *Buffer, changed *Region)

Presented records a committed frame. changed is the region where the screen actually differs after this commit (the frame's own damage, not the buffer-initialization paint a fresh buffer also performs): b's content now matches the screen, while every other buffer in the pool lags by changed and must repaint it before its next reuse. This is what makes partial damage safe with a rotating pool: whichever buffer comes back next carries the accumulated damage of every frame missed since its content was on screen. Retired buffers are skipped: they were replaced by a resize and will never be shown again.

func (*Pool) Reclaim

func (p *Pool) Reclaim()

Reclaim ends the wait on the buffers a closed pool's surface still held. Call it once the compositor has processed the surface's destruction: a compositor need not release what a destroyed surface held (sway does not release the buffer current at the destroy), so a held buffer would otherwise keep its slot resident for the rest of the session. The compositor no longer reads them; each is destroyed and its slot returns to the arena.

func (*Pool) Resize

func (p *Pool) Resize(create func() (*Buffer, error))

Resize swaps the pool to a new buffer size. Free buffers are disposed on the spot; buffers the compositor still holds are preserved - their wl_buffer objects and arena slots stay alive until the release event lands, so a resize never paints over memory the compositor reads. The retired list is capped at the pool capacity: overflow drops the oldest frame's buffer instead of stacking unbounded pending buffers (destroying a busy wl_buffer is allowed; its release simply never arrives, and its slot stays reserved until the arena closes).

func (*Pool) Spares

func (p *Pool) Spares() bool

Spares reports whether the rotation holds a buffer beyond the one a frame needs: what Trim may free.

func (*Pool) Trim

func (p *Pool) Trim()

Trim disposes of the free buffers no Acquire handed out since the previous Trim, the front aside: a surface that stopped drawing, or draws slower than the compositor releases, keeps only what its frames use. Acquire allocates again on demand, and a new buffer repaints or copies forward what it lacks.

type Region added in v0.2.5

type Region struct {
	// contains filtered or unexported fields
}

Region is a bounded set of rects covering what was added, possibly more: disjoint rects stay apart unless the box around them is hardly bigger, so two thin bars at a window's far edges do not cost the window between them. The zero value is empty.

func RegionOf added in v0.2.5

func RegionOf(r render.Rect) Region

RegionOf is the region covering r.

func (*Region) Add added in v0.2.5

func (g *Region) Add(r render.Rect)

Add covers r too, merging it with any rect worth merging, and at the bound with whichever grows least.

func (*Region) AddRegion added in v0.2.5

func (g *Region) AddRegion(o *Region)

AddRegion covers o too.

func (*Region) Area added in v0.2.5

func (g *Region) Area() int

Area is the pixels of the region's rects.

func (*Region) Clear added in v0.2.5

func (g *Region) Clear()

Clear empties the region.

func (*Region) Empty added in v0.2.5

func (g *Region) Empty() bool

Empty reports whether the region covers nothing.

func (*Region) Rects added in v0.2.5

func (g *Region) Rects() []render.Rect

Rects is the region's rects, valid until the next change.

func (*Region) Remove added in v0.2.5

func (g *Region) Remove(i int)

Remove drops the i-th of Rects, moving the last into its place.

type ReleaseHandler

type ReleaseHandler struct {
	B *Buffer
}

ReleaseHandler adapts a Buffer to the wayland release event so the pool refills as the compositor returns buffers. The arena wires it once per buffer at allocation; test-created buffers without a wl object are released by hand. A buffer with a Timeline ignores it: its release point frees it.

func (ReleaseHandler) HandleBufferRelease

func (h ReleaseHandler) HandleBufferRelease(wl.BufferReleaseEvent)

HandleBufferRelease implements wl.BufferReleaseHandler.

type SurfaceSync added in v0.2.5

type SurfaceSync struct {
	// contains filtered or unexported fields
}

SurfaceSync is one surface's wp_linux_drm_syncobj_surface_v1, made with its first explicitly synchronized buffer and destroyed before the surface takes one that is not, as the compositor would refuse it then.

func (*SurfaceSync) Close added in v0.2.5

func (s *SurfaceSync) Close() error

Close destroys the surface's object, if it has one.

func (*SurfaceSync) Points added in v0.2.5

func (s *SurfaceSync) Points(mgr *wlr.WpDrmSyncobjManagerV1, surf *wl.Surface, b *Buffer) error

Points sets b's acquire and release points on surf for the commit that attaches b, as the protocol requires of every commit attaching a buffer.

type Timeline added in v0.2.5

type Timeline struct {
	WL               *wlr.WpDrmSyncobjTimelineV1
	Syncobj          drm.Syncobj
	Released         func()
	Acquire, Release uint64
	// contains filtered or unexported fields
}

Timeline is a dmabuf buffer's explicit synchronization: the compositor samples it once Acquire signals, and it is the client's again once Release does, wp_linux_drm_syncobj_surface_v1's points on one timeline of its own. wl_buffer.release means nothing for such a buffer: Release, through the renderer's watch, frees it.

func NewTimeline added in v0.2.5

func NewTimeline(wl *wlr.WpDrmSyncobjTimelineV1, s drm.Syncobj, close func()) *Timeline

NewTimeline is a timeline over wl, dropped by close when its buffer goes.

func (*Timeline) Next added in v0.2.5

func (t *Timeline) Next() (acquire, release uint64)

Next moves the timeline on to a frame's points and returns them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL