renderhost

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 19 Imported by: 1 module, 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 renderhost runs the core module of the Rust renderer (renderer/, SDD-013) from Go with wazero: pure Go, no cgo, no import for the module to reach anything through. It is the only place that knows the exports of SDD-013 section 4.2.

Index

Constants

View Source
const (
	DefaultMaxTextureBytes = 512 << 20
	DefaultMaxBitmapBytes  = 128 << 20
)

Default budgets of vr_init (SDD-013 section 7.2).

View Source
const APIVersion = 3

APIVersion is the vr_api_version this host knows.

View Source
const RenderInputVersion = 1

RenderInputVersion is the version of the render input this host writes (SDD-000 section 12).

Variables

View Source
var ErrClosed = errors.New("renderhost: the instance is closed")

ErrClosed is returned by a host that was closed or whose instance trapped: a trapped instance is discarded and its stored textures are gone.

Functions

func ModuleSHA256

func ModuleSHA256() string

ModuleSHA256 is the digest of the embedded module, as lower-case hex.

Types

type Config

type Config struct {
	MaxTextureBytes uint32
	MaxBitmapBytes  uint32
	// Uncancellable runs the instance without the check that stops a call when its context ends, which is
	// about five times faster. Text requests use it: they are bounded by construction (SDD-013 section 4.6).
	Uncancellable bool
}

Config is what New needs; a zero value is the defaults.

type Error

type Error struct {
	Reason Reason
	Detail string
}

Error is a failure of the renderer. Detail names an item, a key or a limit, never a pixel or a path.

func (*Error) Error

func (e *Error) Error() string

type Host

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

Host runs one instance of the core module. It is not safe for concurrent use.

func New

func New(ctx context.Context, cfg Config) (*Host, error)

New instantiates the embedded module, compiled once per process. The module is refused unless its digest matches, it has no import, and its vr_api_version is APIVersion.

func (*Host) Close

func (h *Host) Close(ctx context.Context) error

Close frees the instance.

func (*Host) MemoryBytes added in v0.46.0

func (h *Host) MemoryBytes() int64

MemoryBytes is the memory the instance holds; 0 once it is closed.

func (*Host) Prepare added in v0.46.0

func (h *Host) Prepare(ctx context.Context, in RenderInput) (Prepared, error)

Prepare runs the preparation of the one layer of the plan (SDD-013 section 4.2.2) at opacity 1 and without its clip: its texture or bitmap, and the tables it names, must be stored. A layer that draws nothing gives an empty Prepared.

func (*Host) PutBitmap

func (h *Host) PutBitmap(ctx context.Context, key string, bm bitmap.Bitmap) error

PutBitmap uploads a bitmap of the rasterizer under its key (plan.BitmapSpec.Key). The module premultiplies it.

func (*Host) PutFont

func (h *Host) PutFont(ctx context.Context, hash string, data []byte) error

PutFont stores the bytes of a user font under its lower-case hex SHA-256 for the requests that name it; empty data records that the file is not stored. The caller has checked that the bytes hash to the digest.

func (*Host) PutLUT

func (h *Host) PutLUT(ctx context.Context, asset string, t *lut.Table) error

PutLUT uploads the lookup table of a lut asset under the asset id, replacing the table of that id (SDD-004 section 3.6): the entries are the little-endian 16-bit values of the table (lut.Table.Bytes).

func (*Host) PutPrepared added in v0.46.0

func (h *Host) PutPrepared(ctx context.Context, item string, p Prepared) error

PutPrepared stores a prepared layer as the texture of the item id: a media layer of that item is then drawn with no look (SDD-013 section 4.2.2). The module copies the block.

func (*Host) PutTexture

func (h *Host) PutTexture(ctx context.Context, item string, w, ht int, rgba []byte, opaque bool) error

PutTexture uploads the premultiplied RGBA pixels of a media layer under its item id, replacing the texture of that id. The module copies the pixels, so the caller reuses rgba at once.

func (*Host) Release

func (h *Host) Release(ctx context.Context, id string) error

Release forgets the texture, bitmap or lookup table with that id or key; an unknown one is not an error.

func (*Host) Render

func (h *Host) Render(ctx context.Context, in RenderInput, dst *image.RGBA) error

Render draws the frame into dst: RGBA, no stride padding, exactly Output.Width by Output.Height. With a band (Output.Y1 > Output.Y0) only those rows of dst are written. A band may also be drawn into a view of its rows alone (dst.SubImage of the rectangle from (0, Y0) to (Width, Y1) of the frame): the instance then holds only those rows, not the frame (SDD-013 section 4.2).

func (*Host) Reset added in v0.46.0

func (h *Host) Reset(ctx context.Context) error

Reset forgets every texture, bitmap and lookup table the instance holds, as vr_init does, and keeps the memory the instance has grown to.

func (*Host) RunText

func (h *Host) RunText(ctx context.Context, request []byte) ([]byte, error)

RunText draws or measures a shape, a text or a caption (the request format is in SDD-013 section 4.2) and returns the encoded result. A failure that has a structured detail is a *TextError.

func (*Host) ValidateFont

func (h *Host) ValidateFont(ctx context.Context, data []byte) error

ValidateFont checks a user font file; a refusal is a *TextError of reason font_invalid.

type Output

type Output struct {
	Width, Height int
	Scale         float64
	Y0, Y1        int
}

Output is the frame to write (SDD-013 section 2.1). Width and Height are whole pixels from 1 to 4096; Scale is the output scale of SDD-004 section 6, which multiplies every position and size of the plan. When Y1 is greater than Y0 the module draws only the rows [Y0, Y1) of the frame, which is how a band of the export is rendered by its own instance.

type Pool added in v0.46.0

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

Pool keeps idle instances of the module for the next render. An instance that has drawn a large picture owns the memory it grew to, and a fresh one pays for every page of it again (the pages are faulted in one at a time, and the module's memory is grown by copying): the render of an image document uses up to eight instances at once, so the next one is faster on instances that are already as large as it needs.

A pool bounds what it keeps: at most maxIdle instances and maxIdleBytes of their memory, and it closes them all when none was taken for ttl. An instance that failed is never kept. It is safe for concurrent use; every instance it gives out is the caller's alone until it comes back.

func NewPool added in v0.46.0

func NewPool(cfg Config, maxIdle int, maxIdleBytes int64, ttl time.Duration) *Pool

NewPool makes a pool of instances made with cfg.

func (*Pool) Drain added in v0.46.0

func (p *Pool) Drain()

Drain closes every idle instance.

func (*Pool) Get added in v0.46.0

func (p *Pool) Get(ctx context.Context) (*Host, error)

Get gives an idle instance, with nothing stored in it, or a new one.

func (*Pool) Idle added in v0.46.0

func (p *Pool) Idle() int

Idle is how many instances the pool holds.

func (*Pool) Put added in v0.46.0

func (p *Pool) Put(h *Host)

Put gives an instance back. It is cleared first; it is closed instead of kept when it failed, when the pool is full, or when it would hold more memory than the pool allows.

type Prepared added in v0.46.0

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

Prepared is a layer after its looks, the block `vr_prepare` gives (SDD-013 section 4.2.2): its pixels, the size of the rectangle that draws them and what places it again. It is a plain value: the host that made it and any other instance of the module read it the same way, so it can be kept and uploaded again.

func (Prepared) Bytes added in v0.46.0

func (p Prepared) Bytes() int

Bytes is the memory the prepared layer takes.

func (Prepared) Empty added in v0.46.0

func (p Prepared) Empty() bool

Empty reports that the layer draws nothing.

func (Prepared) Size added in v0.46.0

func (p Prepared) Size() PreparedSize

Size is the geometry of a prepared layer; the zero value for an empty one.

type PreparedSize added in v0.46.0

type PreparedSize struct {
	// W and H are the pixels of the prepared picture.
	W, H int
	// GrownW and GrownH are the size, in output pixels, of the rectangle that draws it, centred where the layer
	// is (a blur makes it larger than the layer).
	GrownW, GrownH float64
	// OffsetX and OffsetY are what a bitmap's metrics move its centre by; zero for a picture.
	OffsetX, OffsetY float64
	// Scale is the output scale it was prepared at.
	Scale float64
}

PreparedSize is the pixels of a prepared layer and the rectangle that draws them.

type Reason

type Reason string

Reason is a reason of SDD-013 section 7.1.

const (
	InvalidPlan        Reason = "invalid_plan"
	UnsupportedVersion Reason = "unsupported_version"
	UnsupportedLayer   Reason = "unsupported_layer"
	MissingTexture     Reason = "missing_texture"
	LimitExceeded      Reason = "limit_exceeded"
	OutOfMemory        Reason = "out_of_memory"
	InvalidArgument    Reason = "invalid_argument"
	InvalidSpec        Reason = "invalid_spec"
	FontUnknown        Reason = "font_unknown"
	FontInvalid        Reason = "font_invalid"
	FontNeeded         Reason = "font_needed"
	TextFailed         Reason = "text_failed"
	RenderTrap         Reason = "render_trap"
)

The reasons a call can fail with; RenderTrap has no status, the call itself failed.

type RenderInput

type RenderInput struct {
	Output Output
	Plan   plan.FramePlan
}

RenderInput is what one render call reads: the frame to write and the frame plan to draw.

func (RenderInput) MarshalJSON

func (in RenderInput) MarshalJSON() ([]byte, error)

MarshalJSON writes the render input of SDD-013 section 2.1: the plan keeps the JSON names of core/plan.

type TextError

type TextError struct {
	Reason Reason
	Detail string
	Data   []byte
}

TextError is a failure of a text request that carries the structured detail the module encoded: for font_unknown the family, weight, weights and digest; for font_invalid the reason and message.

func (*TextError) Error

func (e *TextError) Error() string

Jump to

Keyboard shortcuts

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