offscreen

package
v1.17.7 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: BSD-3-Clause Imports: 19 Imported by: 0 modules, 1 package

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 offscreen draws scenes into pixels in the app's own process, with no window and no GPU: a software ui.Rasterizer, a Detacher that makes a scene safe to draw on any goroutine, and a Surface that keeps the pixels of one view across frames and redraws only what changed (SPEC §9.9, D28). It is plain Go over the software renderer and builds for every platform.

Flutter: no counterpart

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Detacher

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

Detacher copies scenes, on the UI thread, into scenes that hold nothing the framework changes afterwards, so that any goroutine can draw the copy while the UI thread goes on with the next frame. A scene is not safe to draw on another goroutine for these reasons:

  • a paragraph draws by asking the text engine that laid it out, whose caches have no lock, and the framework lays the same paragraph out again in a later frame;
  • the framework disposes the handle of an image when it lets go of it, and the renderer refuses an image that is disposed.

The copy replaces each paragraph by the drawing the paragraph makes now, and each image by a handle of its own on the same pixels. Everything else a scene holds (layers, pictures without a paragraph or an image, paths, paints, gradients, filters) is immutable once it is recorded, and is shared with the source.

The copy of a picture is reused when the next scene has the same picture, and a layer whose pictures were all kept is the same layer in both scenes, so that the damage tracker of a Surface still sees what did not change. A Detacher keeps the copies of the last scene only, and is used by one goroutine at a time (the UI thread).

Flutter: no counterpart

func NewDetacher

func NewDetacher() *Detacher

NewDetacher returns a detacher that has seen no scene.

Flutter: no counterpart

func (*Detacher) Detach

func (d *Detacher) Detach(scene *ui.Scene) *ui.Scene

Detach returns a scene that draws the same pixels as the scene, and that any goroutine can draw while the framework goes on. Call it on the UI thread, after the scene is built and before the framework changes what the scene refers to (lays a paragraph out again, disposes an image). A nil scene gives nil.

Flutter: no counterpart

type Frame

type Frame struct {
	// Image is the whole view, in premultiplied RGBA. It is the view's own and is valid
	// only during the call of the callback: the next frame draws into it, on the same
	// goroutine, as soon as the callback returns. A callback that sends the pixels
	// elsewhere copies them (the rectangles of Damage are what changed since the last
	// frame the callback saw).
	Image *image.RGBA
	// Damage are the rectangles, in pixels, that this frame redrew. They are not empty,
	// and the callback may keep the slice. The first frame, and the first after SetSize
	// changed the size in pixels, damage the whole image.
	Damage []image.Rectangle
	// Number is the number of the scene the framework made for the view, counting from 1.
	// It skips the scenes that a newer one replaced while the goroutine was drawing, and
	// those that changed no pixel, so a gap tells that frames were dropped.
	Number int
}

Frame is a frame of a View that changed some pixels.

Flutter: no counterpart

type Glyph

type Glyph struct {
	// Cluster is the text of the cluster, as it was asked.
	Cluster string
	// Rect is the part of the image that has the pixels of the cluster; it is empty for a cluster that
	// draws nothing (a space). The pixels around it, up to the next rectangle, are transparent.
	Rect ui.Rect
	// Offset is the vector from the pen position, on the baseline, to the top left corner of Rect.
	Offset ui.Offset
	// Advance is how far the pen moves right after the cluster: the width of a paragraph of the cluster,
	// times the device pixel ratio.
	Advance float64
	// Colored says that the cluster is drawn with colors of its own (a color emoji), which the pixels
	// keep, so that tinting multiplies them. Other clusters are white, with the coverage as alpha.
	Colored bool
}

Glyph is one grapheme cluster of a GlyphSheet: where its pixels are in the image and how to place them. All the lengths are in the pixels of the image.

To draw the cluster with its pen (its origin on the baseline) at (x, y) of a canvas, draw the part Rect of the image with its top left corner at (x+Offset.Dx, y+Offset.Dy), and then move the pen right by Advance. DrawAtlas does that with an RSTransform whose translation is that point and whose scale is 1.

Flutter: no counterpart

type GlyphSheet

type GlyphSheet struct {
	// Image has the pixels of every cluster: premultiplied RGBA, white where an outline glyph covers
	// the pixel and the coverage as the alpha, transparent elsewhere. The sheet gives it to the
	// caller, who disposes it.
	Image *ui.Image
	// DevicePixelRatio is the ratio the clusters were drawn at: the font size in pixels is the size
	// of the style times it.
	DevicePixelRatio float64
	// Glyphs has one entry for each cluster that NewGlyphSheet was given, in its order. A cluster that
	// was given twice shares its Rect.
	Glyphs []Glyph
}

GlyphSheet is an image of many grapheme clusters drawn in one text style, with the place of each (NewGlyphSheet). It is what a grid of text draws from, with Canvas.DrawAtlas: one draw tints thousands of cells.

Flutter: no counterpart

func NewGlyphSheet

func NewGlyphSheet(engine ui.ParagraphFactory, style *ui.TextStyle, devicePixelRatio float64, clusters []string) (*GlyphSheet, error)

NewGlyphSheet shapes each cluster with the text engine, with the same fonts, fallback fonts and glyph outlines that a paragraph in the style uses, and draws them all into one image with the software renderer. A cluster is drawn the way a paragraph draws it with its origin on a whole pixel: the same paths, quarter-pixel positions and coverage, in white (the style's color, shadows, decorations and background are left out); a color glyph keeps its colors. No paragraph is drawn or recorded for each glyph, and nothing is kept after the call: the engine's own caches are the only state. devicePixelRatio is how many pixels a logical pixel is, so the glyphs are drawn at size*ratio. A nil style is the default style, and a cluster that is empty or draws nothing gets an empty Rect and its advance. It is for the UI thread, which owns the text engine. It fails with GlyphEngineMissing when engine is nil, GlyphEngineUnsupported when the engine's paragraphs cannot paint themselves, GlyphClustersMissing for an empty list, GlyphPixelRatioInvalid for a ratio that is not a positive finite number, and GlyphSheetTooLarge when a glyph or the sheet would not fit in memory.

Flutter: no counterpart

type Rasterizer

type Rasterizer struct{}

Rasterizer is a ui.Rasterizer that draws on the CPU with Glutter's software renderer: apps give it to Scene.ToImage, Picture.ToImage and RenderRepaintBoundary.ToImage (ToImageOpts.Rasterizer) to turn what they draw into pixels, with no window and no GPU, on any platform. It keeps nothing after RasterizeScene returns, so one Rasterizer may be used by many goroutines at once. A scene made by the framework is drawn on the UI thread; to draw it on another goroutine, detach it first (Detacher).

Flutter: no counterpart

func NewRasterizer

func NewRasterizer() *Rasterizer

NewRasterizer returns a software rasterizer.

Flutter: no counterpart

func (*Rasterizer) RasterizeScene

func (*Rasterizer) RasterizeScene(scene *ui.Scene, width, height int) (*ui.Image, error)

RasterizeScene draws the scene into a new image of width x height pixels, in premultiplied RGBA with a transparent background. The errors are those of internal/gpu/soft: a size that is not valid, a nil scene, a layer or an operation the software renderer cannot draw, an image that was disposed.

Flutter: no counterpart

type Surface

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

Surface holds the pixels of one view across frames: Draw redraws only what changed since the last frame over the pixels of the frame before, and tells which rectangles it redrew. The pixels are those of a drawing of the whole scene. It keeps the mipmap chains of the images it scales down from one frame to the next, as a window's renderer keeps the textures of images, for as long as they are drawn. A Surface is used by one goroutine at a time; draw detached scenes (Detacher) when that is not the UI thread.

Flutter: no counterpart

func NewSurface

func NewSurface(width, height int) *Surface

NewSurface returns a surface of width x height pixels, transparent until the first Draw. It panics with the tag SurfaceSizeInvalid for a size that is not between 1 and 32768.

Flutter: no counterpart

func (*Surface) Draw

func (s *Surface) Draw(scene *ui.Scene) ([]image.Rectangle, error)

Draw makes the pixels of the surface those of the scene, and returns the rectangles it redrew, in pixels.

  • The first frame, the first after Resize or after an error, and a scene with something the damage tracker cannot bound (an image filter, a backdrop filter) redraw everything: the damage is the whole surface.
  • Otherwise each damaged rectangle is cleared and the scene is drawn into it, clipped to it, so the pixels equal those of a drawing of the whole scene. The damage is empty for a scene that changes nothing, and nothing is drawn. The scene the surface already shows, given again, changes nothing: this differs from the damage tracker of a window (SPEC §9.1), which treats the same scene again as a request to draw all of it, since the pixels of a surface are never lost.

After an error from the software renderer (a layer or an operation it cannot draw, an image that was disposed) the pixels are not defined, and the next Draw redraws everything.

Flutter: no counterpart

func (*Surface) Height

func (s *Surface) Height() int

Height is the height of the surface in pixels.

Flutter: no counterpart

func (*Surface) Image

func (s *Surface) Image() *image.RGBA

Image returns the pixels of the last frame, in premultiplied RGBA. The image is the surface's own: it is valid until the next Draw or Resize, and the caller does not change it.

Flutter: no counterpart

func (*Surface) Resize

func (s *Surface) Resize(width, height int)

Resize makes the surface width x height pixels. The pixels are cleared, and the next Draw draws the whole scene. A surface of the same size is cleared too. It panics with the tag SurfaceSizeInvalid for a size that is not valid.

Flutter: no counterpart

func (*Surface) Width

func (s *Surface) Width() int

Width is the width of the surface in pixels.

Flutter: no counterpart

type View

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

View runs a widget tree in a view of its own, on a widgets binding of its own that shares the UI thread of the app's binding, and hands the pixels of each frame to a callback on a goroutine of its own (SPEC §9.9, D28).

The framework runs on the UI thread of the parent binding: it builds, lays out and paints the tree there, and the scene of each frame is detached (Detacher) and given to the goroutine of the view, which draws it with a Surface and calls the callback with the pixels and what changed. A newer scene replaces one that waits, so a slow callback makes the view drop frames, never queue them. The view asks for a frame at most MaxFrameRate times a second, whatever the app's windows do: its frame rate does not depend on them.

Flutter: no counterpart

func NewView

func NewView(parent *widgets.WidgetsBinding, size ui.Size, root widgets.Widget, onFrame func(Frame), o ...ViewOpts) (*View, error)

NewView runs the widget tree root in a view of size logical pixels, on a widgets binding of its own, and calls onFrame with the pixels of each frame that changed some. Call it on the UI thread of parent (the binding of the app); every other method of the view is called there too.

  • The view's dispatcher has one view, which root fills, wrapped in a widgets.View as RunWidget's WrapWithDefaultView does, so MediaQuery and the rest work. It shares the text engine of the parent's dispatcher (fonts the app loaded work), and the binding shares the parent's UI thread, error reporter, ErrorWidgetBuilder, debug flags, asset bundle and HTTP client. The view starts with a copy of the platform configuration of the parent's dispatcher (locale, brightness, text scale, accessibility features, fonts, caret blink time, the rest), before the tree is mounted, and follows its changes until Close; with ViewOpts.PlatformConfiguration it has that one and ignores the parent's. ViewOpts.Window is the window of its configuration.
  • onFrame runs on the goroutine of the view, one call at a time. A panic in it is recovered and reported to the parent's error reporter (CallbackPanicked), and the view goes on. It must not call Close or wait for the UI thread: Close waits for it.
  • A nil parent, root or onFrame, and a size that is not positive, are errors.

Flutter: no counterpart

func (*View) Binding

func (v *View) Binding() *widgets.WidgetsBinding

Binding is the widgets binding of the view. The app sends input through its dispatcher, on the UI thread: DispatchPointerDataPacket (positions in pixels of the image, ViewID ui.ImplicitViewID), DispatchKeyData and DispatchTextInput; it can change the platform configuration with UpdatePlatformConfiguration, and reach the tree with WidgetsBindingOf from a widget of the view.

Flutter: no counterpart

func (*View) Close

func (v *View) Close()

Close ends the view: the widget tree is unmounted (every State is disposed), the frame timer stops, the goroutine of the view ends and Close waits for it, including a callback that is running. After Close nothing is posted to the UI thread, no frame is made and the callback is not called; what was posted before is dropped when it runs. Calling Close again does nothing. Call it on the UI thread, and never from the callback.

Flutter: no counterpart

func (*View) SetSize

func (v *View) SetSize(size ui.Size) error

SetSize changes the size of the view, in logical pixels, and nothing else of its configuration: the window, the padding, the insets and the display features that the app set with UpdateViewConfiguration stay. The framework hears of it as a change of the view's metrics and lays out again; the next frame has the new size in pixels and damages all of it. It fails for a size that is not positive (tag ViewSizeInvalid) and for a view that was closed (tag ViewClosed).

Flutter: no counterpart

func (*View) SetWindow

func (v *View) SetWindow(info ui.WindowInfo) error

SetWindow changes the window of the view's configuration (ViewOpts.Window) and nothing else of it. The framework hears of it as a change of the view's metrics, as it does when a platform changes the state of a window: a WindowDecoration builds again. It fails for a view that was closed (tag ViewClosed).

Flutter: no counterpart

type ViewOpts

type ViewOpts struct {
	// DevicePixelRatio is the number of pixels of the image for each logical pixel; the
	// default is 1.
	DevicePixelRatio float64
	// MaxFrameRate is the most frames the view makes in a second, counting the time from
	// the start of one to the start of the next; the default is 30.
	MaxFrameRate int
	// Clipboard is the clipboard of the view. When it is nil, the view uses the clipboard of
	// the app (the parent's), as before: copy, cut and paste in the view reach the desktop.
	// When it is set, every clipboard call of the view's tree (copy, cut, paste, the check
	// that decides whether Paste is offered, the primary selection that a selection fills and
	// a middle click pastes, rich content) goes to it, and none reaches the app's clipboard or
	// the desktop's.
	//
	// SetClipboardText and GetClipboardText go to the host. If it also implements
	// ui.RichClipboardHost, SetClipboard and GetClipboard go to it, for every kind, the
	// primary selection included. If it does not, a rich call for the clipboard kind and the
	// plain text format goes to its text methods (a content with a text sets that text, and
	// its other formats are dropped), and every other rich call (the primary selection, an
	// image, a list of URIs) does nothing: it completes with an empty content and no error.
	//
	// The host's callbacks (the done functions it is given) run on the UI thread of the
	// parent: the host calls them there, or posts them with the parent binding's Post, and
	// never before the method that took them has returned. A done function that the host
	// calls after Close does nothing: the view drops it.
	//
	// Flutter: no counterpart
	Clipboard ui.ClipboardHost
	// PlatformConfiguration is a fixed platform configuration for the view (locales, text
	// scale and its scaler, brightness, accessibility features, accent color, fonts, caret
	// blink time, the rest of the struct). When it is set, the view has it from the start and
	// keeps it: the parent's later changes are ignored. The app may still change it through
	// the view's dispatcher, with Binding().PlatformDispatcher().UpdatePlatformConfiguration.
	// When it is not set, the view starts with the parent's configuration and follows its
	// changes until Close (SPEC §9.9).
	//
	// Flutter: no counterpart
	PlatformConfiguration foundation.Opt[ui.PlatformConfiguration]
	// Window is the window of the view's configuration from the start: whether the app draws
	// the title bar (ui.DecorationApp), whether the window is activated, maximized, and the
	// rest of ui.WindowInfo. The zero value is a window with no decoration, not activated. The
	// view has no window of its own: the value is what a WindowDecoration or a title bar in
	// the tree reads. Change it later with SetWindow.
	//
	// Flutter: no counterpart
	Window ui.WindowInfo
}

ViewOpts are the optional parameters of NewView.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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