Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::svg::GeodeSurfaceRenderer Class Reference

Renders SVG documents with Geode directly into a window surface the host owns, keeping every frame on the GPU from rendering through presentation. More...

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

Public Types

enum class  FrameStatus {
  Presented ,
  Skipped ,
  ResizeNeeded ,
  Failed
}
 Outcome of one draw call. More...

Public Member Functions

 ~GeodeSurfaceRenderer ()
 Releases the surface, then the device. Native builds wait, bounded, for submitted frames to finish; WebAssembly builds do not wait.
 GeodeSurfaceRenderer (const GeodeSurfaceRenderer &)=delete
GeodeSurfaceRenderer & operator= (const GeodeSurfaceRenderer &)=delete
 GeodeSurfaceRenderer (GeodeSurfaceRenderer &&)=delete
GeodeSurfaceRenderer & operator= (GeodeSurfaceRenderer &&)=delete
bool resize (Vector2i sizePx, std::string *error=nullptr)
 Changes the drawable size, for example after the host view resizes or moves to a display with a different scale.
Vector2i size () const
 Current drawable size in pixels.
FrameStatus draw (SVGDocument &document)
 Draws document into the next surface frame and presents it.
bool isDeviceLost () const
 True once the GPU device has been lost; every later draw returns Failed.
void trimTexturePool (RendererGeodeTexturePoolTrim level)
 Frees pooled textures no frame is using, as RendererGeode::trimTexturePool does.

Static Public Member Functions

static std::unique_ptr< GeodeSurfaceRenderer > CreateForMetalLayer (void *metalLayer, Vector2i sizePx, std::string *error=nullptr)
 Creates a renderer that presents to a Core Animation Metal layer.
static std::unique_ptr< GeodeSurfaceRenderer > CreateForCanvas (std::string_view canvasSelector, Vector2i sizePx, std::string *error=nullptr)
 Creates a renderer that presents to a browser <canvas> element through WebGPU.

Detailed Description

Renders SVG documents with Geode directly into a window surface the host owns, keeping every frame on the GPU from rendering through presentation.

Available with Geode enabled (donner.configure(renderer = "geode")) on macOS, presenting to a CAMetalLayer (CreateForMetalLayer), and in WebAssembly builds with the browser backend (--@donner//donner/svg/renderer/geode:browser_backend=true), presenting to a <canvas> (CreateForCanvas).

This is the application-embedding entry point: it opens a GPU root, the Geode device and a runtime surface, and acquires, draws and presents one frame per draw.

Metal layer contract:

  • The layer outlives the renderer. Destroy the renderer first.
  • The drawable size passed to CreateForMetalLayer and resize is in pixels and must equal the layer's bounds.size multiplied by its contentsScale, rounded to the nearest pixel on each axis. Keep contentsScale equal to the display's backing scale; a layer-hosting view does not update it for you. When the layer's pixel extent no longer matches, draw returns FrameStatus::ResizeNeeded and presents nothing until resize is called with the new size. A layer with empty bounds (not laid out in a window) is never reported as resized.
  • The renderer configures the layer's device, pixelFormat (BGRA8), drawableSize, framebufferOnly and opaque properties. Do not change them yourself. Drawables allow GPU copies (framebufferOnly is off), which blend modes need to read their backdrop.
  • The layer is configured opaque, so transparent regions of a document composite as black. Give the document a background to control the color.
  • A frame target must fit the renderer's surface budget (4 bytes per pixel within RendererSurfaceBudget::kMaximumBytes); larger sizes are rejected. Documents whose layers, masks or patterns need more storage near that limit can still fail to draw.
  • Calls must come from one thread at a time.

Typical use from a macOS view that is already in a window:

CAMetalLayer* layer = [CAMetalLayer layer];
view.layer = layer;
view.wantsLayer = YES;
layer.contentsScale = view.window.backingScaleFactor;
const NSSize px = [view convertSizeToBacking:view.bounds.size];
const Vector2i sizePx(static_cast<int>(std::lround(px.width)),
static_cast<int>(std::lround(px.height)));
std::string error;
auto presenter =
GeodeSurfaceRenderer::CreateForMetalLayer((__bridge void*)layer, sizePx, &error);
// Each frame, after updating contentsScale and calling resize() if the view changed:
document.setCanvasSize(presenter->size().x, presenter->size().y);
presenter->draw(document);
static std::unique_ptr< GeodeSurfaceRenderer > CreateForMetalLayer(void *metalLayer, Vector2i sizePx, std::string *error=nullptr)
Creates a renderer that presents to a Core Animation Metal layer.
Vector2< int > Vector2i
Shorthand for Vector2<int>.
Definition Vector2.h:400

Canvas contract:

  • Link the module with -sASYNCIFY and with the browser bridge's JavaScript library: list @donner//donner/gpu/browser:library_donner_gpu.js in additional_linker_inputs and pass --js-library=$(execpath @donner//donner/gpu/browser:library_donner_gpu.js).
  • Create the renderer where the browser's asynchronous WebGPU device request can be awaited through Asyncify, such as main(), then draw from the per-frame loop.
  • Size the canvas with CSS only. Pass its drawing-buffer size in pixels (CSS size times devicePixelRatio); the renderer writes the canvas width and height attributes itself.
  • Track size and devicePixelRatio changes yourself and call resize: a canvas never reports FrameStatus::ResizeNeeded. The browser presents each drawn frame at the end of the current task.
  • The canvas is configured with the browser's preferred format (BGRA8 or RGBA8) and opaque alpha, so transparent regions composite as black.

Linux (Vulkan) window presentation has no public entry point yet.

Member Enumeration Documentation

◆ FrameStatus

Outcome of one draw call.

Enumerator
Presented 

The frame was drawn and handed to the surface for presentation.

Skipped 

No surface frame was available in time. Nothing was drawn; draw again next frame.

ResizeNeeded 

The surface's pixel extent differs from the configured size. Nothing was drawn; call resize with the current size (for a Metal layer, bounds.size * contentsScale), then draw again.

Failed 

The device was lost, or the surface refused a frame. Recreate the renderer to continue.

Member Function Documentation

◆ CreateForCanvas()

std::unique_ptr< GeodeSurfaceRenderer > donner::svg::GeodeSurfaceRenderer::CreateForCanvas ( std::string_view canvasSelector,
Vector2i sizePx,
std::string * error = nullptr )
static

Creates a renderer that presents to a browser <canvas> element through WebGPU.

Parameters
canvasSelectorCSS selector naming the canvas, such as "#canvas". In a worker, which has no document, the selector instead names an OffscreenCanvas registered through Emscripten's offscreen-canvas support; a leading # is stripped, so "#id" works there.
sizePxDrawing-buffer size in pixels. Both dimensions must be positive.
errorWhen non-null, receives a description of why creation failed.
Returns
The renderer, or nullptr when the selector is empty, the size is empty or exceeds the surface budget, the build has no browser backend, the browser offers no WebGPU device, or the canvas cannot present.

◆ CreateForMetalLayer()

std::unique_ptr< GeodeSurfaceRenderer > donner::svg::GeodeSurfaceRenderer::CreateForMetalLayer ( void * metalLayer,
Vector2i sizePx,
std::string * error = nullptr )
static

Creates a renderer that presents to a Core Animation Metal layer.

Parameters
metalLayerThe host's CAMetalLayer*, bridged to void*. It must outlive the renderer.
sizePxDrawable size in pixels (bounds.size * contentsScale). Both dimensions must be positive.
errorWhen non-null, receives a description of why creation failed.
Returns
The renderer, or nullptr when the layer is null or not a CAMetalLayer, the size is empty or exceeds the surface budget, Metal is unavailable, the surface cannot present, or the build is a browser-backend WebAssembly build (which has no Metal).

◆ draw()

FrameStatus donner::svg::GeodeSurfaceRenderer::draw ( SVGDocument & document)
nodiscard

Draws document into the next surface frame and presents it.

The document is drawn at its canvas size; set it to size() to fill the surface.

Parameters
documentDocument to draw.
Returns
Whether a frame was presented, and what the host should do otherwise.

◆ resize()

bool donner::svg::GeodeSurfaceRenderer::resize ( Vector2i sizePx,
std::string * error = nullptr )
nodiscard

Changes the drawable size, for example after the host view resizes or moves to a display with a different scale.

Resizing to the current size does nothing.

Parameters
sizePxNew size in pixels (for a Metal layer, bounds.size * contentsScale). Both dimensions must be positive.
errorWhen non-null, receives a description of why the resize failed.
Returns
False when the size is empty, exceeds the surface budget, or the surface cannot be reconfigured; the previous size remains in effect.

◆ trimTexturePool()

void donner::svg::GeodeSurfaceRenderer::trimTexturePool ( RendererGeodeTexturePoolTrim level)

Frees pooled textures no frame is using, as RendererGeode::trimTexturePool does.

Call it with RendererGeodeTexturePoolTrim::All when the host hides the surface, for example on a browser visibilitychange to hidden, and with ToFloor when drawing pauses.

Parameters
levelHow much to free.

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