phaselock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 5 Imported by: ? 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 phaselock lines the host's frames up with the client display's scan-out. The client's Estimator measures, from the refresh times its pictures reached the screen, how long before the next refresh each picture was ready and whether the system scanned it out directly or composited it first. The host's Controller moves the phase of the frames it captures so that pictures are ready a small target before a refresh: a picture ready just after one waits nearly a whole refresh.

Index

Constants

View Source
const (
	LockTolerance = int64(500 * time.Microsecond)
)

Variables

This section is empty.

Functions

func Harmonic

func Harmonic(period, nominal, tolerance int64) (int64, bool)

Harmonic brings a refresh period measured from screen times to the display's stated one, nominal: a period a whole number of times nominal is that many refreshes, as a 60 fps stream on a 120 Hz display shows every other one. It reports false for a period shorter than nominal, which no display refreshes at. With nominal 0 it keeps period.

func Wrap

func Wrap(d, period int64) int64

Wrap brings d into [-period/2, period/2): a phase error, which is the same one a whole refresh earlier or later.

Types

type Controller

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

Controller turns the client's reports into small moves of the host's capture phase. Update and Advance may be called from different goroutines.

func NewController

func NewController(cfg ControllerConfig) *Controller

func (*Controller) Advance

func (c *Controller) Advance(frame uint32) int64

Advance is called once before each frame is captured, with its ID, and returns how much later than its cadence to capture it, earlier when below 0. Frames after it carry the move too.

func (*Controller) State

func (c *Controller) State() State

State reports what the Controller is doing.

func (*Controller) Update

func (c *Controller) Update(r Report)

Update takes a client report.

type ControllerConfig

type ControllerConfig struct {
	// FramePeriod is the stream's frame period.
	FramePeriod int64
	// Steerable is set when the source paces its own frames and can move
	// them. A display capture runs on the display's refresh, which the
	// host cannot move: capturing later only moves the wait from the
	// client to the host.
	Steerable bool
}

ControllerConfig is what the host knows of the stream it steers.

type Estimator

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

Estimator measures the phase of pictures against the display's refreshes. Safe for concurrent use.

func (*Estimator) Observe

func (e *Estimator) Observe(s Sample, g Grid)

Observe takes a picture that reached the screen and the display's refreshes as known then: a zero Grid where they are not.

func (*Estimator) Report

func (e *Estimator) Report() Report

Report summarizes the last pictures. Each call also moves the margin with the share of late pictures, so call it at a steady rate.

func (*Estimator) Reset

func (e *Estimator) Reset()

Reset forgets every picture measured and the mode, as when the pictures start reaching the screen another way, such as a surface going direct: those of the old path would hold its mode for a whole modeSpan. The nominal period and whether the surface asked for direct scan-out stay.

func (*Estimator) SetDirectSurface

func (e *Estimator) SetDirectSurface(on bool)

SetDirectSurface tells whether the surface asked the system to scan its pictures out directly (see directSurfaceBelow).

func (*Estimator) SetNominalPeriod

func (e *Estimator) SetNominalPeriod(ns int64)

SetNominalPeriod gives the display's shortest refresh period as the system states it. The mode is read against it, and a grid of a shorter period, or of a whole multiple of it, is taken for a harmonic of the refresh.

type Grid

type Grid struct{ Period, Anchor int64 }

Grid is a display's refreshes: Anchor + k*Period, on the client's screen clock.

func (Grid) Next

func (g Grid) Next(t int64) int64

Next is the first refresh after t.

type Mode

type Mode uint8

Mode is how the system shows the pictures.

const (
	ModeUnknown Mode = iota
	// ModeDirect: the display scans each picture out at the first refresh
	// after the GPU finished it.
	ModeDirect
	// ModeComposited: a compositor draws the picture into its own frame
	// first, a refresh or more later.
	ModeComposited
)

func (Mode) String

func (m Mode) String() string

type Report

type Report struct {
	// Valid is set once the refresh is known and enough pictures came.
	Valid bool
	// Locked is set while the phase error stays within LockTolerance.
	Locked bool
	Mode   Mode
	// PeriodNs is the display's refresh period.
	PeriodNs int64
	// SlackNs is the median time from a picture being ready to the next
	// refresh, TargetNs the slack aimed at, and ErrorNs their difference,
	// wrapped: above 0 pictures are ready too early and wait, so the host
	// should capture later; below 0 they risk missing their refresh.
	SlackNs, TargetNs, ErrorNs int64
	// JitterNs is half the spread of the slack between its 10th and 90th
	// percentiles.
	JitterNs int64
	// LatePct is the share of pictures that missed the refresh they were
	// ready for, CompositedPct the share that took longer from the GPU
	// finishing them to the screen than direct scan-out does, in percent.
	LatePct, CompositedPct float64
	// Oldest and Newest are the frames the slack was measured over.
	Oldest, Newest uint32
	Samples        int
}

Report is the Estimator's view of the last pictures.

func (Report) RefreshHz

func (r Report) RefreshHz() float64

RefreshHz is the display's refresh rate, 0 before it is known.

type Sample

type Sample struct {
	Frame uint32
	// Ready is when the decoded picture was ready to present; GPUDone when
	// the GPU finished drawing it into the drawable, 0 where the platform
	// does not tell; Glass when it reached the screen.
	Ready, GPUDone, Glass int64
}

Sample is one picture that reached the screen, on the screen clock.

type State

type State struct {
	Active bool
	// Reason says why it is not active, or what it waits for.
	Reason string
	// ErrorNs is the phase error of the last report it acted on.
	ErrorNs int64
	// MovedNs is how far it moved the capture phase in all, later
	// positive; TrimNs the rate trim it adds to every frame.
	MovedNs, TrimNs int64
	Corrections     int
}

State is what the Controller is doing.

Jump to

Keyboard shortcuts

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