Donner SVG 0.8.0-pre
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::svg::Renderer Class Reference

Backend-agnostic renderer that resolves to the active build backend (Skia or tiny-skia). More...

#include "donner/svg/renderer/Renderer.h"

Inheritance diagram for donner::svg::Renderer:
[legend]

Public Member Functions

 Renderer (bool verbose=false)
 Creates a renderer for the active backend.
 Renderer (std::shared_ptr< geode::GeodeDevice > device, bool verbose=false)
 Creates a renderer for the active backend using an externally-owned Geode device when the Geode backend is selected.
 ~Renderer () override
 Destructor.
 Renderer (Renderer &&) noexcept
 Move constructor.
Rendereroperator= (Renderer &&) noexcept
 Move assignment operator.
 Renderer (const Renderer &)=delete
Rendereroperator= (const Renderer &)=delete
void draw (SVGDocument &document) override
 Draws the SVG document using the active backend.
RendererImage renderElement (SVGElement element, Vector2i sizePx)
 Rasterize a single SVGElement and its descendant subtree to an image fitted into a target box.
void beginFrameResourceScope () override
 Begins one aggregate resource-budget scope around a controller-managed frame.
void endFrameResourceScope () override
 Completes a scope opened by beginFrameResourceScope.
void beginFrame (const RenderViewport &viewport) override
 Begins a render pass for the given viewport.
void endFrame () override
 Completes the current render pass.
void setPreserveTargetOnBeginFrame (bool preserve) override
 Preserve the current backend target for an append pass when supported.
void setTransform (const Transform2d &transform) override
 Sets the absolute transform, replacing the current matrix.
void pushTransform (const Transform2d &transform) override
 Pushes a relative transform.
void popTransform () override
 Pops the most recent transform.
void pushClip (const ResolvedClip &clip) override
 Pushes a clip rect or clip path.
void popClip () override
 Pops the most recent clip.
void pushIsolatedLayer (double opacity, MixBlendMode blendMode) override
 Pushes an isolated compositing layer.
void popIsolatedLayer () override
 Pops the most recent isolated layer.
void pushFilterLayer (const components::FilterGraph &filterGraph, const std::optional< Box2d > &filterRegion) override
 Pushes a filter layer.
void popFilterLayer () override
 Pops the most recent filter layer.
void pushMask (const std::optional< Box2d > &maskBounds) override
 Begins mask rendering.
void pushMask (const std::optional< Box2d > &maskBounds, MaskType maskType) override
 Begins mask rendering with an explicit luminance or alpha coverage mode.
void transitionMaskToContent () override
 Switches from mask rendering to masked content rendering.
void popMask () override
 Pops the current mask and composites the masked content.
bool beginPatternTile (const Box2d &tileRect, const Transform2d &targetFromPattern) override
 Begins pattern tile recording.
void endPatternTile (bool forStroke) override
 Ends pattern recording and stores the resulting pattern paint.
void setPaint (const PaintParams &paint) override
 Sets the active paint parameters for subsequent draw calls.
void drawPath (const PathShape &path, const StrokeParams &stroke) override
 Draws an arbitrary path.
void drawRect (const Box2d &rect, const StrokeParams &stroke) override
 Draws an axis-aligned rectangle.
void drawEllipse (const Box2d &bounds, const StrokeParams &stroke) override
 Draws an ellipse.
void drawImage (const ImageResource &image, const ImageParams &params) override
 Draws an image resource.
void drawBitmap (const RendererBitmap &bitmap, const ImageParams &params) override
 Draws a CPU-readable bitmap into the given target rectangle.
bool drawTextureSnapshot (const RendererTextureSnapshot &texture, const Box2d &targetRect, double opacity=1.0, bool pixelated=false) override
 Draws a backend-owned texture snapshot when the active backend supports it.
void drawText (Registry &registry, const components::ComputedTextComponent &text, const TextParams &params) override
 Draws shaped text.
RendererBitmap takeSnapshot () const override
 Captures a CPU-readable snapshot of the current frame.
RendererBitmap takeSnapshotInterruptibly (const std::function< bool()> &shouldCancel) const override
 Forwards cancellation-aware CPU snapshot capture to the active backend.
RendererReadbackStats consumeReadbackStats () override
 Consume readback diagnostics from the active backend.
std::shared_ptr< const RendererTextureSnapshottakeTextureSnapshot () override
 Captures a backend-owned GPU texture snapshot when the active backend supports it.
const RendererTextureSnapshotborrowTextureSnapshot () UTILS_LIFETIME_BOUND override
 Borrows the current backend-owned GPU texture for a synchronous presentation operation.
bool requiresTextureSnapshotPresentation () const override
 Returns true when this backend requires direct texture presentation.
RendererFilterPreparationBudgetfilterPreparationBudget () override
 Shared driver-side filter preparation budget for this renderer family, when supported.
std::unique_ptr< RendererInterfacecreateOffscreenInstance () const override
 Creates an offscreen renderer of the active backend type.
void setOffscreenCreationHookForTesting (std::function< void()> hook) override
 Forwards the test-only in-constructor hook to the active backend.
std::uint64_t filterBudgetChunksForTesting () const override
 Forwards filter-budget chunk diagnostics from the active backend.
void setDebugGeometryOverlay (bool enabled) override
 Forwards RendererInterface::setDebugGeometryOverlay to the active backend (no-op for backends without a debug overlay).
bool debugGeometryOverlay () const override
 Whether the active backend's geometry debug overlay is enabled.
bool save (const char *filename)
 Saves the last rendered frame to a PNG file.
int width () const override
 Returns the rendered width in pixels.
int height () const override
 Returns the rendered height in pixels.
Public Member Functions inherited from donner::svg::RendererInterface
virtual RendererResourceStats resourceStats () const
 Return resource-admission diagnostics for the current frame.
virtual void injectFilterLocalRasterAllocationFailureForTesting ()
 Cause the next local filter raster allocation to fail in a boundary test.

Detailed Description

Backend-agnostic renderer that resolves to the active build backend (Skia or tiny-skia).

Typical usage:

Renderer renderer;
renderer.draw(document);
renderer.save("output.png");
bool save(const char *filename)
Saves the last rendered frame to a PNG file.
Renderer(bool verbose=false)
Creates a renderer for the active backend.
void draw(SVGDocument &document) override
Draws the SVG document using the active backend.

The draw() method handles the full rendering pipeline internally: computing styles, building the render tree, and rasterizing. For frame-based rendering (e.g., in a viewer), use beginFrame() / endFrame() directly via the RendererInterface API.

Examples
svg_filter_interaction.cc, svg_text_interaction.cc, svg_to_png.cc, and svg_viewer.cc.

Constructor & Destructor Documentation

◆ Renderer() [1/2]

donner::svg::Renderer::Renderer ( bool verbose = false)
explicit

Creates a renderer for the active backend.

Parameters
verboseIf true, enables backend-specific verbose logging.

◆ Renderer() [2/2]

donner::svg::Renderer::Renderer ( std::shared_ptr< geode::GeodeDevice > device,
bool verbose = false )
explicit

Creates a renderer for the active backend using an externally-owned Geode device when the Geode backend is selected.

Non-Geode backends ignore device and construct their normal backend.

Parameters
deviceShared Geode device.
verboseIf true, enables backend-specific verbose logging.

Member Function Documentation

◆ beginFrame()

void donner::svg::Renderer::beginFrame ( const RenderViewport & viewport)
overridevirtual

Begins a render pass for the given viewport.

Parameters
viewportThe viewport dimensions for the render pass.

Implements donner::svg::RendererInterface.

◆ beginFrameResourceScope()

void donner::svg::Renderer::beginFrameResourceScope ( )
overridevirtual

Begins one aggregate resource-budget scope around a controller-managed frame.

A controller may render several offscreen passes before the root render pass. Backends that share frame budgets with their offscreen instances override this pair so every pass consumes one budget epoch instead of resetting independently. Ordinary single-pass callers may omit the scope and continue to rely on beginFrame.

Reimplemented from donner::svg::RendererInterface.

◆ beginPatternTile()

bool donner::svg::Renderer::beginPatternTile ( const Box2d & tileRect,
const Transform2d & targetFromPattern )
nodiscardoverridevirtual

Begins pattern tile recording.

Parameters
tileRectTile bounds in pattern space.
targetFromPatternTransform from pattern tile space to target space.

Implements donner::svg::RendererInterface.

◆ borrowTextureSnapshot()

const RendererTextureSnapshot * donner::svg::Renderer::borrowTextureSnapshot ( )
nodiscardoverridevirtual

Borrows the current backend-owned GPU texture for a synchronous presentation operation.

Reimplemented from donner::svg::RendererInterface.

◆ consumeReadbackStats()

RendererReadbackStats donner::svg::Renderer::consumeReadbackStats ( )
nodiscardoverridevirtual

Consume readback diagnostics from the active backend.

Reimplemented from donner::svg::RendererInterface.

◆ createOffscreenInstance()

std::unique_ptr< RendererInterface > donner::svg::Renderer::createOffscreenInstance ( ) const
nodiscardoverridevirtual

Creates an offscreen renderer of the active backend type.

Reimplemented from donner::svg::RendererInterface.

◆ debugGeometryOverlay()

bool donner::svg::Renderer::debugGeometryOverlay ( ) const
nodiscardoverridevirtual

Whether the active backend's geometry debug overlay is enabled.

Reimplemented from donner::svg::RendererInterface.

◆ draw()

void donner::svg::Renderer::draw ( SVGDocument & document)
overridevirtual

Draws the SVG document using the active backend.

Parameters
documentThe SVG document to render.

Implements donner::svg::RendererInterface.

Examples
svg_filter_interaction.cc, svg_text_interaction.cc, svg_to_png.cc, and svg_viewer.cc.

◆ drawBitmap()

void donner::svg::Renderer::drawBitmap ( const RendererBitmap & bitmap,
const ImageParams & params )
overridevirtual

Draws a CPU-readable bitmap into the given target rectangle.

Parameters
bitmapThe bitmap to draw.
paramsImage placement parameters.

Reimplemented from donner::svg::RendererInterface.

◆ drawEllipse()

void donner::svg::Renderer::drawEllipse ( const Box2d & bounds,
const StrokeParams & stroke )
overridevirtual

Draws an ellipse.

Parameters
boundsThe ellipse bounds.
strokeStroke configuration for the ellipse.

Implements donner::svg::RendererInterface.

◆ drawImage()

void donner::svg::Renderer::drawImage ( const ImageResource & image,
const ImageParams & params )
overridevirtual

Draws an image resource.

Parameters
imageThe image resource to draw.
paramsImage placement parameters.

Implements donner::svg::RendererInterface.

◆ drawPath()

void donner::svg::Renderer::drawPath ( const PathShape & path,
const StrokeParams & stroke )
overridevirtual

Draws an arbitrary path.

Parameters
pathThe path to draw.
strokeStroke configuration for the path.

Implements donner::svg::RendererInterface.

◆ drawRect()

void donner::svg::Renderer::drawRect ( const Box2d & rect,
const StrokeParams & stroke )
overridevirtual

Draws an axis-aligned rectangle.

Parameters
rectThe rectangle bounds.
strokeStroke configuration for the rectangle.

Implements donner::svg::RendererInterface.

◆ drawText()

void donner::svg::Renderer::drawText ( Registry & registry,
const components::ComputedTextComponent & text,
const TextParams & params )
overridevirtual

Draws shaped text.

Parameters
registryECS registry used for resolving paint references.
textThe shaped text runs.
paramsText styling parameters.

Implements donner::svg::RendererInterface.

◆ drawTextureSnapshot()

bool donner::svg::Renderer::drawTextureSnapshot ( const RendererTextureSnapshot & texture,
const Box2d & targetRect,
double opacity = 1.0,
bool pixelated = false )
overridevirtual

Draws a backend-owned texture snapshot when the active backend supports it.

Reimplemented from donner::svg::RendererInterface.

◆ endFrame()

void donner::svg::Renderer::endFrame ( )
overridevirtual

Completes the current render pass.

Implements donner::svg::RendererInterface.

◆ endFrameResourceScope()

void donner::svg::Renderer::endFrameResourceScope ( )
overridevirtual

Completes a scope opened by beginFrameResourceScope.

Reimplemented from donner::svg::RendererInterface.

◆ endPatternTile()

void donner::svg::Renderer::endPatternTile ( bool forStroke)
overridevirtual

Ends pattern recording and stores the resulting pattern paint.

Parameters
forStrokeIf true, store as stroke paint, otherwise fill paint.

Implements donner::svg::RendererInterface.

◆ filterBudgetChunksForTesting()

std::uint64_t donner::svg::Renderer::filterBudgetChunksForTesting ( ) const
nodiscardoverridevirtual

Forwards filter-budget chunk diagnostics from the active backend.

Reimplemented from donner::svg::RendererInterface.

◆ filterPreparationBudget()

RendererFilterPreparationBudget * donner::svg::Renderer::filterPreparationBudget ( )
nodiscardoverridevirtual

Shared driver-side filter preparation budget for this renderer family, when supported.

Reimplemented from donner::svg::RendererInterface.

◆ height()

int donner::svg::Renderer::height ( ) const
nodiscardoverridevirtual

Returns the rendered height in pixels.

Returns
The rendered height.

Implements donner::svg::RendererInterface.

Examples
svg_filter_interaction.cc, svg_text_interaction.cc, and svg_to_png.cc.

◆ popClip()

void donner::svg::Renderer::popClip ( )
overridevirtual

Pops the most recent clip.

Implements donner::svg::RendererInterface.

◆ popFilterLayer()

void donner::svg::Renderer::popFilterLayer ( )
overridevirtual

Pops the most recent filter layer.

Implements donner::svg::RendererInterface.

◆ popIsolatedLayer()

void donner::svg::Renderer::popIsolatedLayer ( )
overridevirtual

Pops the most recent isolated layer.

Implements donner::svg::RendererInterface.

◆ popMask()

void donner::svg::Renderer::popMask ( )
overridevirtual

Pops the current mask and composites the masked content.

Implements donner::svg::RendererInterface.

◆ popTransform()

void donner::svg::Renderer::popTransform ( )
overridevirtual

Pops the most recent transform.

Implements donner::svg::RendererInterface.

◆ pushClip()

void donner::svg::Renderer::pushClip ( const ResolvedClip & clip)
overridevirtual

Pushes a clip rect or clip path.

Parameters
clipThe resolved clip to apply.

Implements donner::svg::RendererInterface.

◆ pushFilterLayer()

void donner::svg::Renderer::pushFilterLayer ( const components::FilterGraph & filterGraph,
const std::optional< Box2d > & filterRegion )
overridevirtual

Pushes a filter layer.

Parameters
filterGraphThe filter graph to apply when the layer is popped.
filterRegionOptional filter region bounds in user space.

Implements donner::svg::RendererInterface.

◆ pushIsolatedLayer()

void donner::svg::Renderer::pushIsolatedLayer ( double opacity,
MixBlendMode blendMode )
overridevirtual

Pushes an isolated compositing layer.

Parameters
opacityGroup opacity applied when the layer is composited back.
blendModeMix-blend-mode applied when the layer is composited back.

Implements donner::svg::RendererInterface.

◆ pushMask() [1/2]

void donner::svg::Renderer::pushMask ( const std::optional< Box2d > & maskBounds)
overridevirtual

Begins mask rendering.

Parameters
maskBoundsOptional mask bounds clip.
maskTypeWhether mask coverage comes from luminance or alpha.

Implements donner::svg::RendererInterface.

◆ pushMask() [2/2]

void donner::svg::Renderer::pushMask ( const std::optional< Box2d > & maskBounds,
MaskType maskType )
overridevirtual

Begins mask rendering with an explicit luminance or alpha coverage mode.

Reimplemented from donner::svg::RendererInterface.

◆ pushTransform()

void donner::svg::Renderer::pushTransform ( const Transform2d & transform)
overridevirtual

Pushes a relative transform.

Parameters
transformThe transform to compose with the current matrix.

Implements donner::svg::RendererInterface.

◆ renderElement()

RendererImage donner::svg::Renderer::renderElement ( SVGElement element,
Vector2i sizePx )
nodiscard

Rasterize a single SVGElement and its descendant subtree to an image fitted into a target box.

This renders just the given element + its descendants through an isolated offscreen instance of this renderer, scaled and centered into sizePx with a transparent background. The requested size is a maximum thumbnail extent: the returned image dimensions are computed from the element's nonzero- opacity painted geometry bounds and may be narrower or shorter than sizePx. Layers whose visible bounds cover the root SVG viewBox fit the canvas-visible bounds into the preview cell. The element's own paint and transform are honored. The bounds are clipped to the root SVG viewBox when present so thumbnails show the document-canvas-visible part of a layer; other ancestor clips and opacity are intentionally ignored so the preview shows the element's own artwork rather than how it happens to be clipped in context.

The element's owning document is prepared for rendering under write access for this isolated draw, then its pending render invalidation state is restored so previews do not consume dirty flags needed by another renderer.

A GPU backend keeps the result in a sampleable texture and a software backend returns pixels; RendererImage hides the difference, reading the texture back on demand for callers that need pixels.

Parameters
elementElement whose subtree should be rasterized.
sizePxMaximum image dimensions in device pixels. Both axes must be positive; otherwise an empty image is returned.
Returns
The rendered image with a transparent background, or an empty RendererImage when the element has no renderable geometry or the inputs are degenerate.

◆ requiresTextureSnapshotPresentation()

bool donner::svg::Renderer::requiresTextureSnapshotPresentation ( ) const
nodiscardoverridevirtual

Returns true when this backend requires direct texture presentation.

Reimplemented from donner::svg::RendererInterface.

◆ save()

bool donner::svg::Renderer::save ( const char * filename)

Saves the last rendered frame to a PNG file.

Parameters
filenameThe output PNG filename.
Returns
True if the file was written.
Examples
svg_filter_interaction.cc, svg_text_interaction.cc, and svg_to_png.cc.

◆ setDebugGeometryOverlay()

void donner::svg::Renderer::setDebugGeometryOverlay ( bool enabled)
overridevirtual

Forwards RendererInterface::setDebugGeometryOverlay to the active backend (no-op for backends without a debug overlay).

Reimplemented from donner::svg::RendererInterface.

◆ setOffscreenCreationHookForTesting()

void donner::svg::Renderer::setOffscreenCreationHookForTesting ( std::function< void()> hook)
overridevirtual

Forwards the test-only in-constructor hook to the active backend.

Reimplemented from donner::svg::RendererInterface.

◆ setPaint()

void donner::svg::Renderer::setPaint ( const PaintParams & paint)
overridevirtual

Sets the active paint parameters for subsequent draw calls.

Parameters
paintThe resolved paint state.

Implements donner::svg::RendererInterface.

◆ setPreserveTargetOnBeginFrame()

void donner::svg::Renderer::setPreserveTargetOnBeginFrame ( bool preserve)
overridevirtual

Preserve the current backend target for an append pass when supported.

Reimplemented from donner::svg::RendererInterface.

◆ setTransform()

void donner::svg::Renderer::setTransform ( const Transform2d & transform)
overridevirtual

Sets the absolute transform, replacing the current matrix.

Parameters
transformThe transform to apply.

Implements donner::svg::RendererInterface.

◆ takeSnapshot()

RendererBitmap donner::svg::Renderer::takeSnapshot ( ) const
nodiscardoverridevirtual

Captures a CPU-readable snapshot of the current frame.

Returns
A snapshot of the rendered frame.

Implements donner::svg::RendererInterface.

Examples
svg_viewer.cc.

◆ takeSnapshotInterruptibly()

RendererBitmap donner::svg::Renderer::takeSnapshotInterruptibly ( const std::function< bool()> & shouldCancel) const
nodiscardoverridevirtual

Forwards cancellation-aware CPU snapshot capture to the active backend.

Reimplemented from donner::svg::RendererInterface.

◆ takeTextureSnapshot()

std::shared_ptr< const RendererTextureSnapshot > donner::svg::Renderer::takeTextureSnapshot ( )
nodiscardoverridevirtual

Captures a backend-owned GPU texture snapshot when the active backend supports it.

Reimplemented from donner::svg::RendererInterface.

◆ transitionMaskToContent()

void donner::svg::Renderer::transitionMaskToContent ( )
overridevirtual

Switches from mask rendering to masked content rendering.

Implements donner::svg::RendererInterface.

◆ width()

int donner::svg::Renderer::width ( ) const
nodiscardoverridevirtual

Returns the rendered width in pixels.

Returns
The rendered width.

Implements donner::svg::RendererInterface.

Examples
svg_filter_interaction.cc, svg_text_interaction.cc, and svg_to_png.cc.

The documentation for this class was generated from the following file: