compositor

package
v0.1.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 7 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

Package compositor stitches the chrome layer, the page texture and an optional alpha overlay into one RGBA frame.

Layers (bottom to top):

  1. page: a tall RGBA texture (the whole document, or a capped prefix) painted once per page change, shown in the viewport rectangle. Scrolling just moves a window over it, so scrolling never calls the engine and costs one memcpy. Outside the viewport (and below a short page) the background colour shows.
  2. chrome: window-sized premultiplied RGBA painted from the chrome page with a transparent canvas. Where the chrome draws nothing (the `#viewport` hole) the page shows through; anything the chrome draws over the hole (omnibox dropdown, find bar, toasts, popovers) is alpha-blended on top of the page.
  3. overlay: a premultiplied-alpha surface (the agent overlay) blended over the result with a global opacity for fades.

Damage tracking is exact: SetChrome/SetPage diff the new pixels against the old ones and only the bounding box of real changes is recomposed and handed to Present. Compose allocates nothing in steady state.

All coordinates are device pixels. Callers convert from logical pixels with the window scale.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EaseDuration

func EaseDuration(dist float64) float64

EaseDuration is how long an eased move over dist px takes (seconds).

Types

type Compositor

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

Compositor is safe for concurrent use; every method takes one mutex, held only for pixel copies.

func New

func New(w, h int, scale float64) *Compositor

New returns a compositor for a w x h device-pixel window.

func (*Compositor) ChromeAlphaAt

func (c *Compositor) ChromeAlphaAt(x, y int) uint8

ChromeAlphaAt returns the chrome layer's opacity at a device pixel (0 where the chrome draws nothing, so the page is visible and gets the pointer).

func (*Compositor) Compose

func (c *Compositor) Compose() (*image.RGBA, image.Rectangle)

Compose redraws the damaged region of the frame and returns the frame and the damage rectangle (empty when nothing changed). The frame is only valid until the next call that mutates the compositor, so Present must copy it (shell.Window.Present is documented not to retain it).

func (*Compositor) CopyFrame

func (c *Compositor) CopyFrame() *image.RGBA

CopyFrame returns a copy of the current frame.

func (*Compositor) Dirty

func (c *Compositor) Dirty() bool

Dirty reports whether Compose has work to do.

func (*Compositor) Frame

func (c *Compositor) Frame() *image.RGBA

Frame returns the last composed frame (for screenshots).

func (*Compositor) Gen

func (c *Compositor) Gen() int

Gen counts the frames composed with real damage. A capture source compares it with the value it saw last and resends its previous frame when nothing changed instead of converting identical pixels again.

func (*Compositor) Invalidate

func (c *Compositor) Invalidate(r image.Rectangle)

Invalidate marks a region (or everything, when empty) as needing recompose.

func (*Compositor) PageBand

func (c *Compositor) PageBand() (y0, y1 int)

PageBand returns the document rows (device px) the page texture covers: [y0, y1).

func (*Compositor) PageHeight

func (c *Compositor) PageHeight() int

PageHeight is the document row just below the last painted row, in device px (0 without a page).

func (*Compositor) RecordPaint

func (c *Compositor) RecordPaint(kind string, d time.Duration)

RecordPaint stores how long an external paint (kind "chrome" or "page") took, so Stats can report it next to the compose time.

func (*Compositor) Resize

func (c *Compositor) Resize(w, h int, scale float64)

Resize changes the window size (device px) and scale; everything is damaged and the chrome must be repainted by the caller.

func (*Compositor) Scroll

func (c *Compositor) Scroll() (pos, max int)

Scroll returns the scroll offset and its maximum (device px).

func (*Compositor) ScrollTo

func (c *Compositor) ScrollTo(y int) bool

ScrollTo sets the scroll offset; it reports whether the view moved.

func (*Compositor) SetBackground

func (c *Compositor) SetBackground(col color.RGBA)

SetBackground sets the colour shown below the end of a short page.

func (*Compositor) SetChrome

func (c *Compositor) SetChrome(src *image.RGBA) (spare *image.RGBA)

SetChrome installs new chrome pixels (premultiplied RGBA, transparent where the chrome draws nothing). The compositor takes ownership of src and returns its previous buffer for reuse (nil on the first call or a size change). Damage becomes the bounding box of the pixels that actually changed.

func (*Compositor) SetDocHeight

func (c *Compositor) SetDocHeight(h int)

SetDocHeight tells the compositor the full document height (device px). The page texture may be shorter (it is painted ahead of the scroll position); the scroll range follows the document.

func (*Compositor) SetOverlay

func (c *Compositor) SetOverlay(img *image.RGBA, at image.Point, alpha uint8)

SetOverlay shows img at position at with global opacity alpha (0..255). img must be premultiplied RGBA. A nil img or alpha 0 hides the overlay. The surface is referenced, not copied; call it again after mutating it.

func (*Compositor) SetPage

func (c *Compositor) SetPage(src *image.RGBA) (spare *image.RGBA)

SetPage installs new page pixels (the whole document or a prefix). The compositor takes ownership and returns the previous texture for reuse. Damage is the changed part of the visible window.

func (*Compositor) SetPageAt

func (c *Compositor) SetPageAt(src *image.RGBA, originY int) (spare *image.RGBA)

SetPageAt installs src as the band of the document that starts at document row originY (device px). Rows outside the band show the background until a band that covers them is installed; scrolling never needs the band to start at the top of the page.

func (*Compositor) SetScrollbar

func (c *Compositor) SetScrollbar(a uint8)

SetScrollbar sets the overlay scrollbar opacity (0 hides it).

func (*Compositor) SetViewport

func (c *Compositor) SetViewport(r image.Rectangle)

SetViewport sets the hole rectangle (device px).

func (*Compositor) Size

func (c *Compositor) Size() (w, h int, scale float64)

Size returns the device-pixel size.

func (*Compositor) SnapshotPage

func (c *Compositor) SnapshotPage(dst *image.RGBA) (*image.RGBA, int)

SnapshotPage copies what the viewport shows of the page texture, at the current scroll position, without the chrome and its popovers drawn over it. It is the "this tab" source: a permission prompt or the site-info popover never ends up in a shared tab.

func (*Compositor) SnapshotWindow

func (c *Compositor) SnapshotWindow(dst *image.RGBA) (*image.RGBA, int)

SnapshotWindow copies the last composed frame (chrome, page and overlay: the whole browser window) into dst, allocating when dst is nil or the wrong size, and returns it with the generation it shows.

func (*Compositor) Stats

func (c *Compositor) Stats() Stats

Stats returns timing over the last 256 samples of each series.

func (*Compositor) Viewport

func (c *Compositor) Viewport() image.Rectangle

Viewport returns the current hole.

type Percentiles

type Percentiles struct {
	N             int
	P50, P99, Max time.Duration
}

Percentiles summarises a set of durations.

type Scroller

type Scroller struct {
	Pos, Target float64 // device px
	Max         float64
	// Tau is the time constant in seconds of the spring (settle time is about 4 Tau; smaller is snappier).
	Tau float64
	// FlingTau is the decay time constant of a fling in seconds.
	FlingTau float64
	// Smooth false jumps straight to the target and never flings.
	Smooth bool
	// contains filtered or unexported fields
}

Scroller turns scroll input into smooth, pixel-based motion. It only needs ticking while Animating() is true, so a still page costs no CPU. Everything is in device pixels and time is passed in (seconds on any monotonic clock), so the physics is deterministic and testable.

Four kinds of motion, chosen by the input that starts them:

  • Spring (a mouse-wheel notch, an arrow key): the target moves by the delta and the position follows it with a critically damped spring, so quick repeated input accumulates, keeps its speed from one notch to the next and never overshoots.
  • Direct (a precision touchpad: precise pixel deltas): the position follows the fingers exactly. When the deltas stop and the last ones were fast, the gesture ends in a fling.
  • Fling: the speed measured at the end of a direct gesture decays exponentially (Tau) until the view stops or reaches an edge.
  • Ease (PageUp/PageDown/Space, Home/End, programmatic smooth scrolling, anchor jumps): a timed move along a cubic-bezier ease curve whose duration grows with the distance.

func NewScroller

func NewScroller() *Scroller

NewScroller returns a smooth scroller.

func (*Scroller) Animating

func (s *Scroller) Animating() bool

Animating reports whether Tick still has movement to do.

func (*Scroller) EaseBy

func (s *Scroller) EaseBy(dy float64)

EaseBy eases by dy from where the view is going (the target of a running ease or spring), so repeated page keys add up instead of restarting.

func (*Scroller) EaseByAt

func (s *Scroller) EaseByAt(dy, now float64)

EaseByAt is EaseBy with an explicit time.

func (*Scroller) EaseTo

func (s *Scroller) EaseTo(y float64)

EaseTo starts a timed move from the current position to y (clamped) along the standard ease curve. A move that is already running is retargeted from where it is.

func (*Scroller) EaseToAt

func (s *Scroller) EaseToAt(y, now float64)

EaseToAt is EaseTo with an explicit time.

func (*Scroller) Precise

func (s *Scroller) Precise(dy float64)

Precise applies a pixel-accurate delta from a precision touchpad: the view follows the fingers, and when they lift at speed the gesture continues as a fling.

func (*Scroller) PreciseAt

func (s *Scroller) PreciseAt(dy, now float64)

PreciseAt is Precise with an explicit time.

func (*Scroller) Reset

func (s *Scroller) Reset(p float64)

Reset puts both position and target at p and stops all motion.

func (*Scroller) ScrollTo

func (s *Scroller) ScrollTo(y float64, instant bool)

ScrollTo sets the target absolutely (scrollbar drag, restore, programmatic jump). instant moves there at once; otherwise the move is a short eased one.

func (*Scroller) SetClock

func (s *Scroller) SetClock(f func() float64)

SetClock replaces the time source (tests).

func (*Scroller) SetMax

func (s *Scroller) SetMax(m float64)

SetMax updates the scroll range and clamps.

func (*Scroller) Stop

func (s *Scroller) Stop()

Stop halts any motion where it is (a click, a key press, a touch down stops a fling).

func (*Scroller) Tick

func (s *Scroller) Tick(dt float64) int

Tick advances a virtual clock by dt seconds and returns the integer position: the call for fixed-step callers and tests. Callers with real timestamps use TickAt.

func (*Scroller) TickAt

func (s *Scroller) TickAt(now float64) int

TickAt advances to time now and returns the integer position.

func (*Scroller) TickNow

func (s *Scroller) TickNow() int

TickNow advances to the current time (the scroller's clock) and returns the integer position.

func (*Scroller) Wheel

func (s *Scroller) Wheel(dy float64)

Wheel adds a pixel delta (positive = scroll down) as a spring move: a mouse-wheel notch or an arrow key.

type Stats

type Stats struct {
	Frames  int
	Blank   int // frames that showed document rows the page texture did not cover yet
	Compose Percentiles
	Paint   map[string]Percentiles
}

Stats is the frame timing report (`sweef gui --stats`).

Jump to

Keyboard shortcuts

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