buffer

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 21 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.

Types

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.

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

	// 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 union of 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 render.Rect
	// 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(dev *gpu.Device, mgr *wlr.ZwpDmabufV1, bufW, bufH, scale int, modifiers []fourcc.Modifier, rejected func(error)) (*Buffer, error)

NewDmabuf allocates a bufW x bufH buffer the GPU renders into: a target of dev laid out in one of modifiers (ones the compositor imports), 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 refusal drops the buffer from its pool and reports the error to rejected.

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 Copier

type Copier interface {
	Copy(dst, src *Buffer, r render.Rect) (bool, error)
}

Copier copies rects between two GPU targets' buffers: a surface's GPU renderer. A copy it cannot make reports false, with the error when the GPU failed.

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) CopyForward

func (p *Pool) CopyForward(b *Buffer, gpu Copier) error

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 on the CPU, GPU targets through gpu (nil when the surface has no renderer). A copy that cannot be made leaves b stale, for the frame to paint; the GPU's error comes back.

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) Presented

func (p *Pool) Presented(b *Buffer, changed render.Rect)

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 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.

func (ReleaseHandler) HandleBufferRelease

func (h ReleaseHandler) HandleBufferRelease(wl.BufferReleaseEvent)

HandleBufferRelease implements wl.BufferReleaseHandler.

Jump to

Keyboard shortcuts

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