Documentation
¶
Overview ¶
Package render provides the internal rendering implementation for gogpu/ui.
This package is INTERNAL and not intended for public use. It implements the widget.Canvas interface using gogpu/gg as the 2D drawing backend.
Architecture ¶
The render package provides:
- Canvas: Implementation of widget.Canvas that wraps gg.Context
- Renderer: Orchestrates render cycles (frame begin/end, surface management)
- Color conversion utilities for widget.Color to gg.RGBA
Canvas Implementation ¶
Canvas wraps a gg.Context and implements all drawing operations required by the widget system. It manages:
- Clip stack: PushClip/PopClip for hierarchical clipping regions
- Transform stack: PushTransform/PopTransform for coordinate translation
- Drawing primitives: Rectangles, rounded rectangles, circles, lines
Thread Safety ¶
Canvas is NOT thread-safe. All drawing operations must occur on the main/UI thread during the Draw phase. This matches the widget.Canvas contract.
Usage ¶
This package is used internally by the UI framework. Application code should use the widget.Canvas interface instead of directly using this package.
// Internal framework usage canvas := render.NewCanvas(ggContext, width, height) widget.Draw(ctx, canvas)
Index ¶
- func Clamp01Float32(v float32) float32
- func Clamp01Float64(v float64) float64
- func FromGGColor(c gg.RGBA) widget.Color
- func LerpColor(a, b gg.RGBA, t float64) gg.RGBA
- func ToGGColor(c widget.Color) gg.RGBA
- func ToGGColorPremultiplied(c widget.Color) gg.RGBA
- type Canvas
- func (c *Canvas) Clear(color widget.Color)
- func (c *Canvas) ClipBounds() geometry.Rect
- func (c *Canvas) ClipDepth() int
- func (c *Canvas) Context() *gg.Context
- func (c *Canvas) DrawCircle(center geometry.Point, radius float32, color widget.Color)
- func (c *Canvas) DrawImage(img image.Image, at geometry.Point)
- func (c *Canvas) DrawLine(from, to geometry.Point, color widget.Color, strokeWidth float32)
- func (c *Canvas) DrawRect(r geometry.Rect, color widget.Color)
- func (c *Canvas) DrawRoundRect(r geometry.Rect, color widget.Color, radius float32)
- func (c *Canvas) DrawText(s string, bounds geometry.Rect, fontSize float32, color widget.Color, ...)
- func (c *Canvas) Height() int
- func (c *Canvas) MeasureText(s string, fontSize float32, bold bool) float32
- func (c *Canvas) PopClip()
- func (c *Canvas) PopTransform()
- func (c *Canvas) PushClip(r geometry.Rect)
- func (c *Canvas) PushClipRoundRect(r geometry.Rect, radius float32)
- func (c *Canvas) PushTransform(offset geometry.Point)
- func (c *Canvas) Reset()
- func (c *Canvas) StrokeCircle(center geometry.Point, radius float32, color widget.Color, strokeWidth float32)
- func (c *Canvas) StrokeRect(r geometry.Rect, color widget.Color, strokeWidth float32)
- func (c *Canvas) StrokeRoundRect(r geometry.Rect, color widget.Color, radius float32, strokeWidth float32)
- func (c *Canvas) TransformDepth() int
- func (c *Canvas) TransformOffset() geometry.Point
- func (c *Canvas) Width() int
- type RenderConfig
- type RenderTarget
- type Renderer
- func (r *Renderer) BeginFrame(background widget.Color) *Canvas
- func (r *Renderer) Canvas() *Canvas
- func (r *Renderer) Close() error
- func (r *Renderer) Context() *gg.Context
- func (r *Renderer) EndFrame() *gg.Context
- func (r *Renderer) Height() int
- func (r *Renderer) InFrame() bool
- func (r *Renderer) Resize(width, height int)
- func (r *Renderer) Width() int
- type SceneCanvas
- func (c *SceneCanvas) Clear(color widget.Color)
- func (c *SceneCanvas) Close()
- func (c *SceneCanvas) DrawCircle(center geometry.Point, radius float32, color widget.Color)
- func (c *SceneCanvas) DrawImage(img image.Image, at geometry.Point)
- func (c *SceneCanvas) DrawLine(from, to geometry.Point, color widget.Color, strokeWidth float32)
- func (c *SceneCanvas) DrawRect(r geometry.Rect, color widget.Color)
- func (c *SceneCanvas) DrawRoundRect(r geometry.Rect, color widget.Color, radius float32)
- func (c *SceneCanvas) DrawText(s string, bounds geometry.Rect, fontSize float32, color widget.Color, ...)
- func (c *SceneCanvas) MeasureText(s string, fontSize float32, bold bool) float32
- func (c *SceneCanvas) PopClip()
- func (c *SceneCanvas) PopTransform()
- func (c *SceneCanvas) PushClip(r geometry.Rect)
- func (c *SceneCanvas) PushClipRoundRect(r geometry.Rect, radius float32)
- func (c *SceneCanvas) PushTransform(offset geometry.Point)
- func (c *SceneCanvas) Scene() *scene.Scene
- func (c *SceneCanvas) StrokeCircle(center geometry.Point, radius float32, color widget.Color, strokeWidth float32)
- func (c *SceneCanvas) StrokeRect(r geometry.Rect, color widget.Color, strokeWidth float32)
- func (c *SceneCanvas) StrokeRoundRect(r geometry.Rect, color widget.Color, radius float32, strokeWidth float32)
- func (c *SceneCanvas) TransformOffset() geometry.Point
- type SoftwareTarget
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp01Float32 ¶
Clamp01Float32 clamps a float32 value to the range [0, 1].
func Clamp01Float64 ¶
Clamp01Float64 clamps a float64 value to the range [0, 1].
func FromGGColor ¶
FromGGColor converts a gg.RGBA to widget.Color.
This is the inverse of ToGGColor.
func LerpColor ¶
LerpColor linearly interpolates between two colors.
t=0 returns a, t=1 returns b, values in between interpolate. This is a convenience function that operates on gg.RGBA directly.
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas implements widget.Canvas using gogpu/gg as the 2D drawing backend.
Canvas wraps a gg.Context and provides all drawing operations required by the widget system. It manages clip and transform stacks internally.
Canvas is NOT thread-safe. All drawing operations must occur on the main/UI thread during the Draw phase.
func NewCanvas ¶
NewCanvas creates a new Canvas wrapping the given gg.Context.
The width and height specify the canvas dimensions in logical pixels. The gg.Context should already be created with matching dimensions.
func (*Canvas) ClipBounds ¶
ClipBounds returns the current clip bounds.
This is useful for widgets that want to optimize drawing by skipping elements outside the visible area.
func (*Canvas) Context ¶
Context returns the underlying gg.Context.
This is provided for advanced use cases where direct access to gg functionality is needed. Use with caution as it bypasses Canvas state.
func (*Canvas) DrawCircle ¶
DrawCircle fills a circle with the given color.
func (*Canvas) DrawImage ¶
DrawImage draws an image at the specified position.
The image is composited using source-over blending via gg.DrawImage. The position is adjusted by the current transform offset and snapped to the pixel grid.
func (*Canvas) DrawRoundRect ¶
DrawRoundRect fills a rounded rectangle with the given color.
func (*Canvas) DrawText ¶
func (c *Canvas) DrawText(s string, bounds geometry.Rect, fontSize float32, color widget.Color, bold bool, align widget.TextAlign)
DrawText draws text within the given bounding rectangle.
func (*Canvas) MeasureText ¶
MeasureText returns the width in pixels of the given text string when rendered at the specified font size and weight.
func (*Canvas) PopClip ¶
func (c *Canvas) PopClip()
PopClip removes the most recently pushed clipping region.
Must be called for each PushClip or PushClipRoundRect call.
func (*Canvas) PopTransform ¶
func (c *Canvas) PopTransform()
PopTransform removes the most recently pushed transform.
Must be called for each PushTransform call.
func (*Canvas) PushClip ¶
PushClip pushes a clipping rectangle onto the clip stack.
All subsequent drawing operations will be clipped to this rectangle intersected with any parent clip rectangles.
func (*Canvas) PushClipRoundRect ¶
PushClipRoundRect pushes a rounded rectangle clipping region.
Uses gg.ClipRoundRect which activates GPU SDF-based clipping for rounded rectangles. All subsequent draw operations will be clipped to the rounded rect shape.
func (*Canvas) PushTransform ¶
PushTransform pushes a translation transform onto the transform stack.
All subsequent drawing operations will be offset by the given point.
func (*Canvas) Reset ¶
func (c *Canvas) Reset()
Reset clears the clip and transform stacks, restoring initial state.
func (*Canvas) StrokeCircle ¶
func (c *Canvas) StrokeCircle(center geometry.Point, radius float32, color widget.Color, strokeWidth float32)
StrokeCircle draws the outline of a circle.
func (*Canvas) StrokeRect ¶
StrokeRect draws the outline of a rectangle.
func (*Canvas) StrokeRoundRect ¶
func (c *Canvas) StrokeRoundRect(r geometry.Rect, color widget.Color, radius float32, strokeWidth float32)
StrokeRoundRect draws the outline of a rounded rectangle.
func (*Canvas) TransformDepth ¶
TransformDepth returns the current depth of the transform stack.
func (*Canvas) TransformOffset ¶
TransformOffset returns the current cumulative transform offset.
This is useful for converting between local and global coordinates.
type RenderConfig ¶
type RenderConfig struct {
// BackgroundColor is the color used to clear the canvas at frame start.
BackgroundColor widget.Color
// Scale is the display scale factor (1.0 for standard, 2.0 for HiDPI).
Scale float32
}
RenderConfig contains configuration options for rendering.
func DefaultRenderConfig ¶
func DefaultRenderConfig() RenderConfig
DefaultRenderConfig returns the default render configuration.
type RenderTarget ¶
type RenderTarget interface {
// Width returns the target width in pixels.
Width() int
// Height returns the target height in pixels.
Height() int
// Bounds returns the target bounds as a geometry.Rect.
Bounds() geometry.Rect
}
RenderTarget represents an abstract render target.
This interface allows the renderer to work with different output targets (software buffer, GPU texture, etc.).
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer orchestrates the render cycle for the UI framework.
Renderer manages:
- Frame begin/end lifecycle
- Canvas creation and management
- Surface abstraction for different backends
Renderer is NOT thread-safe. All operations must occur on the main/UI thread.
func NewRenderer ¶
NewRenderer creates a new Renderer with the given dimensions.
The dimensions specify the render target size in logical pixels.
func (*Renderer) BeginFrame ¶
BeginFrame starts a new render frame.
This must be called before any drawing operations. It clears the canvas with the given background color and resets the canvas state.
Returns the Canvas to use for drawing.
func (*Renderer) Canvas ¶
Canvas returns the current Canvas.
This is a convenience method for cases where the Canvas is needed outside of the BeginFrame/EndFrame cycle.
func (*Renderer) Close ¶
Close releases resources associated with the Renderer.
After Close, the Renderer should not be used.
func (*Renderer) Context ¶
Context returns the underlying gg.Context.
This is provided for advanced use cases where direct access is needed.
func (*Renderer) EndFrame ¶
EndFrame finishes the current render frame.
This should be called after all drawing operations are complete. It returns the gg.Context which can be used to extract the rendered image.
type SceneCanvas ¶
type SceneCanvas struct {
// contains filtered or unexported fields
}
SceneCanvas implements widget.Canvas by recording drawing commands into a scene.Scene. This is used inside RepaintBoundary for tile-parallel rendering of widget subtrees.
Shape drawing (rect, round rect, circle, line) is recorded directly into the scene's encoding as scene shapes. Text rendering is delegated to a temporary gg.Context to preserve MSDF text quality, then captured as an image and drawn into the scene.
SceneCanvas is NOT thread-safe. All drawing operations must occur on the main/UI thread during the Draw phase.
func NewSceneCanvas ¶
func NewSceneCanvas(sc *scene.Scene, width, height int) *SceneCanvas
NewSceneCanvas creates a new SceneCanvas that records drawing commands into the given scene. The width and height specify the canvas dimensions in logical pixels.
func (*SceneCanvas) Clear ¶
func (c *SceneCanvas) Clear(color widget.Color)
Clear fills the entire canvas with the given color.
func (*SceneCanvas) Close ¶
func (c *SceneCanvas) Close()
Close releases resources held by the SceneCanvas. Must be called after drawing is complete to free the temporary text context.
func (*SceneCanvas) DrawCircle ¶
DrawCircle fills a circle with the given color.
func (*SceneCanvas) DrawImage ¶
func (c *SceneCanvas) DrawImage(img image.Image, at geometry.Point)
DrawImage draws an image at the specified position.
func (*SceneCanvas) DrawRect ¶
func (c *SceneCanvas) DrawRect(r geometry.Rect, color widget.Color)
DrawRect fills a rectangle with the given color.
func (*SceneCanvas) DrawRoundRect ¶
DrawRoundRect fills a rounded rectangle with the given color.
func (*SceneCanvas) DrawText ¶
func (c *SceneCanvas) DrawText(s string, bounds geometry.Rect, fontSize float32, color widget.Color, bold bool, align widget.TextAlign)
DrawText draws text within the given bounding rectangle. Text is rendered via gg.Context (MSDF pipeline) and captured as an image to preserve high-quality text rendering.
func (*SceneCanvas) MeasureText ¶
func (c *SceneCanvas) MeasureText(s string, fontSize float32, bold bool) float32
MeasureText returns the width in pixels of the given text string when rendered at the specified font size and weight. SceneCanvas approximates using average character width since the text rendering context may not be initialized.
func (*SceneCanvas) PopClip ¶
func (c *SceneCanvas) PopClip()
PopClip removes the most recently pushed clipping region.
func (*SceneCanvas) PopTransform ¶
func (c *SceneCanvas) PopTransform()
PopTransform removes the most recently pushed transform.
func (*SceneCanvas) PushClip ¶
func (c *SceneCanvas) PushClip(r geometry.Rect)
PushClip pushes a clipping rectangle onto the clip stack.
func (*SceneCanvas) PushClipRoundRect ¶
func (c *SceneCanvas) PushClipRoundRect(r geometry.Rect, radius float32)
PushClipRoundRect pushes a rounded rectangle clipping region. SceneCanvas falls back to rectangular clip (scene.Scene does not yet support rounded clip shapes).
func (*SceneCanvas) PushTransform ¶
func (c *SceneCanvas) PushTransform(offset geometry.Point)
PushTransform pushes a translation transform onto the transform stack.
func (*SceneCanvas) Scene ¶
func (c *SceneCanvas) Scene() *scene.Scene
Scene returns the underlying scene.Scene.
func (*SceneCanvas) StrokeCircle ¶
func (c *SceneCanvas) StrokeCircle(center geometry.Point, radius float32, color widget.Color, strokeWidth float32)
StrokeCircle draws the outline of a circle.
func (*SceneCanvas) StrokeRect ¶
StrokeRect draws the outline of a rectangle.
func (*SceneCanvas) StrokeRoundRect ¶
func (c *SceneCanvas) StrokeRoundRect(r geometry.Rect, color widget.Color, radius float32, strokeWidth float32)
StrokeRoundRect draws the outline of a rounded rectangle.
func (*SceneCanvas) TransformOffset ¶
func (c *SceneCanvas) TransformOffset() geometry.Point
TransformOffset returns the current cumulative transform offset.
type SoftwareTarget ¶
type SoftwareTarget struct {
// contains filtered or unexported fields
}
SoftwareTarget is a RenderTarget backed by a software buffer (gg.Context).
func NewSoftwareTarget ¶
func NewSoftwareTarget(width, height int) *SoftwareTarget
NewSoftwareTarget creates a new software render target.
func (*SoftwareTarget) Bounds ¶
func (t *SoftwareTarget) Bounds() geometry.Rect
Bounds returns the target bounds.
func (*SoftwareTarget) Context ¶
func (t *SoftwareTarget) Context() *gg.Context
Context returns the underlying gg.Context.
func (*SoftwareTarget) Height ¶
func (t *SoftwareTarget) Height() int
Height returns the target height.