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 ¶
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 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 ¶
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. 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 ¶
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 ¶
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) Image ¶
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
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 platform configuration (locale, brightness, text scale) is the default one; change it with Binding().PlatformDispatcher().
- 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 ¶
SetSize changes the size of the view, in logical pixels. 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
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
}
ViewOpts are the optional parameters of NewView.
Flutter: no counterpart