API Reference

The API documentation is automatically generated from the C++ source code using Doxygen and Breathe.

Full API Index

class Canvas
#include <canvas.hpp>

User-facing drawing API.

Canvas provides high-level drawing commands that are recorded through a Device. Use save()/restore() for state management and clipRect() for clipping.

Public Functions

explicit Canvas(Device *device)

Construct a Canvas that records into a device.

Parameters:

device – The device to record drawing commands into.

void clipRect(Rect r)

Intersect the current clip with a rectangle.

Parameters:

r – The clipping rectangle.

void drawImage(std::shared_ptr<Image> image, f32 x, f32 y)

Draw an image at the given position (alpha-blended).

Parameters:
  • image – Shared pointer to the image.

  • x – X position.

  • y – Y position.

void drawLine(Point p1, Point p2, Color c, f32 width = 1.0f)

Draw a line between two points.

Parameters:
  • p1 – Start point.

  • p2 – End point.

  • c – Line color.

  • width – Line width (default 1.0).

void drawPolyline(const Point *pts, i32 count, Color c, f32 width = 1.0f)

Draw a connected series of line segments.

Parameters:
  • pts – Array of vertices.

  • count – Number of points.

  • c – Line color.

  • width – Line width (default 1.0).

void drawText(Point p, std::string_view text, Color c)

Draw text at the given position.

Parameters:
  • p – Position of the text baseline.

  • text – UTF-8 text to draw.

  • c – Text color.

void fillRect(Rect r, Color c)

Fill a rectangle with a solid color.

Parameters:
  • r – The rectangle to fill.

  • c – Fill color.

void restore()

Restore the most recently saved clip state.

void save()

Save the current clip state onto the stack.

void strokeRect(Rect r, Color c, f32 width = 1.0f)

Stroke a rectangle outline.

Parameters:
  • r – The rectangle to stroke.

  • c – Stroke color.

  • width – Stroke line width (default 1.0).

Private Functions

void applyClip()

Private Members

State current_
Device *device_ = nullptr
std::vector<State> stack_
struct Color
#include <types.hpp>

An RGBA color with 8-bit components.

Public Members

u8 a = 255

Alpha component (0–255), default opaque.

u8 b = 0

Blue component (0–255).

u8 g = 0

Green component (0–255).

u8 r = 0

Red component (0–255).

struct CompactDrawOp
#include <recording.hpp>

Compact draw operation structure (28 bytes).

Public Members

Color color

Draw color (4 bytes).

union ink::CompactDrawOp::Data data

Per-operation payload (16 bytes).

u8 padding[3]

Alignment padding (3 bytes).

DrawOp::Type type

Operation type (1 byte).

f32 width

Line/stroke width (4 bytes).

union Data
#include <recording.hpp>

Union of per-operation data variants.

Public Functions

inline Data()

Public Members

struct ink::CompactDrawOp::Data::[anonymous] clip

SetClip data.

u32 count
struct ink::CompactDrawOp::Data::[anonymous] fill

FillRect data.

struct ink::CompactDrawOp::Data::[anonymous] image

DrawImage data.

u32 imageIndex
u32 len
struct ink::CompactDrawOp::Data::[anonymous] line

Line data.

u32 offset
Point p1
Point p2
struct ink::CompactDrawOp::Data::[anonymous] polyline

Polyline data (arena offset + count).

Point pos
Rect rect
struct ink::CompactDrawOp::Data::[anonymous] stroke

StrokeRect data.

struct ink::CompactDrawOp::Data::[anonymous] text

Text data (position + arena offset + length).

f32 x
f32 y
class Device
#include <device.hpp>

The single recording device.

Device converts Canvas high-level commands into low-level Recording ops. All Surfaces use the same Device — it always records, never draws directly. The actual rendering is done by a Backend that consumes the Recording.

Drawing commands

void fillRect(Rect r, Color c)

Fill a rectangle with a solid color.

Parameters:
  • r – The rectangle to fill.

  • c – Fill color.

void strokeRect(Rect r, Color c, f32 width = 1.0f)

Stroke a rectangle outline.

Parameters:
  • r – The rectangle to stroke.

  • c – Stroke color.

  • width – Stroke line width (default 1.0).

void drawLine(Point p1, Point p2, Color c, f32 width = 1.0f)

Draw a line between two points.

Parameters:
  • p1 – Start point.

  • p2 – End point.

  • c – Line color.

  • width – Line width (default 1.0).

void drawPolyline(const Point *pts, i32 count, Color c, f32 width = 1.0f)

Draw a connected series of line segments.

Parameters:
  • pts – Array of vertices.

  • count – Number of points.

  • c – Line color.

  • width – Line width (default 1.0).

void drawText(Point p, std::string_view text, Color c)

Draw text at the given position.

Parameters:
  • p – Position of the text baseline.

  • text – UTF-8 text to draw.

  • c – Text color.

void drawImage(std::shared_ptr<Image> image, f32 x, f32 y)

Draw an image at the given position (alpha-blended).

Parameters:
  • image – Shared pointer to the image.

  • x – X position.

  • y – Y position.

Public Functions

Device() = default
~Device() = default
void beginFrame()

Begin recording a new frame (resets the recorder).

void endFrame()

End the current frame recording.

std::unique_ptr<Recording> finishRecording()

Finish recording and return the immutable Recording.

Returns:

Unique pointer to the completed Recording.

void resetClip()

Clear the current clip rectangle.

void setClipRect(Rect r)

Set the current clip rectangle.

Parameters:

r – The clipping rectangle.

Private Members

Recorder recorder_
std::unique_ptr<Recording> recording_
struct DrawOp
#include <recording.hpp>

Draw operation type tag.

Public Types

enum class Type : u8

Operation type enumeration.

Values:

enumerator FillRect

Fill a rectangle.

enumerator StrokeRect

Stroke a rectangle outline.

enumerator Line

Draw a line segment.

enumerator Polyline

Draw connected line segments.

enumerator Text

Draw text.

enumerator DrawImage

Draw an image.

enumerator SetClip

Set a clip rectangle.

enumerator ClearClip

Clear the clip rectangle.

class DrawOpArena
#include <recording.hpp>

Arena allocator for variable-length DrawOp data (strings, point arrays).

Public Functions

explicit DrawOpArena(size_t initialCapacity = 4096)

Construct an arena with the given initial capacity.

Parameters:

initialCapacity – Initial byte capacity (default 4096).

u32 allocate(size_t bytes)

Allocate raw storage.

Parameters:

bytes – Number of bytes to allocate.

Returns:

Byte offset into the arena.

const Point *getPoints(u32 offset) const

Retrieve stored points by offset.

Parameters:

offset – Byte offset returned by storePoints().

Returns:

Pointer to the first Point.

const char *getString(u32 offset) const

Retrieve a stored string by offset.

Parameters:

offset – Byte offset returned by storeString().

Returns:

Pointer to the null-terminated string.

void reset()

Reset the arena, discarding all stored data.

u32 storePoints(const Point *pts, i32 count)

Store a points array in the arena.

Parameters:
  • pts – Pointer to the points array.

  • count – Number of points.

Returns:

Byte offset to the stored points.

u32 storeString(std::string_view str)

Store a string in the arena.

Parameters:

str – The string to store.

Returns:

Byte offset to the stored string.

Private Members

std::vector<u8> data_
class DrawOpVisitor
#include <draw_op_visitor.hpp>

Visitor interface for traversing recorded draw operations.

Implement this interface to process drawing commands dispatched by Recording::accept() or Recording::dispatch().

Public Functions

virtual ~DrawOpVisitor() = default
virtual void visitClearClip() = 0

Visit a clear-clip command (removes current clip).

virtual void visitDrawImage(const Image *image, f32 x, f32 y) = 0

Visit an image drawing command.

Parameters:
  • image – Pointer to the image to draw.

  • x – X position to draw at.

  • y – Y position to draw at.

virtual void visitFillRect(Rect rect, Color color) = 0

Visit a filled rectangle command.

Parameters:
  • rect – The rectangle to fill.

  • color – Fill color.

virtual void visitLine(Point p1, Point p2, Color color, f32 width) = 0

Visit a line segment command.

Parameters:
  • p1 – Start point.

  • p2 – End point.

  • color – Line color.

  • width – Line width.

virtual void visitPolyline(const Point *points, i32 count, Color color, f32 width) = 0

Visit a polyline command.

Parameters:
  • points – Array of vertices.

  • count – Number of points.

  • color – Line color.

  • width – Line width.

virtual void visitSetClip(Rect rect) = 0

Visit a clip rectangle command.

Parameters:

rect – The clipping rectangle.

virtual void visitStrokeRect(Rect rect, Color color, f32 width) = 0

Visit a stroked rectangle command.

Parameters:
  • rect – The rectangle to stroke.

  • color – Stroke color.

  • width – Stroke line width.

virtual void visitText(Point pos, const char *text, u32 len, Color color) = 0

Visit a text drawing command.

Parameters:
  • pos – Position of the text baseline.

  • text – UTF-8 text string.

  • len – Length of the text in bytes.

  • color – Text color.

class DrawPass
#include <draw_pass.hpp>

Sorts recorded draw operations for optimal execution order.

DrawPass reorders operations by clip group, type, and color to minimize state changes in the rendering backend.

Public Functions

inline const std::vector<u32> &sortedIndices() const

Get the sorted operation indices.

Returns:

Indices into Recording::ops() in sorted execution order.

Public Static Functions

static DrawPass create(const Recording &recording)

Create a DrawPass that sorts the operations in a recording.

Parameters:

recording – The recording to sort.

Returns:

A new DrawPass with sorted indices.

Private Members

std::vector<u32> sortedIndices_
class GlyphCache
#include <glyph_cache.hpp>

Font rasterization cache backed by a texture atlas.

Rasterizes glyphs on demand using stb_truetype and stores them in a greyscale atlas that can be uploaded to the GPU.

Public Functions

~GlyphCache()
inline i32 ascent() const

Get the font ascent.

Returns:

Ascent in pixels.

inline const u8 *atlasData() const

Get the raw atlas pixel data.

Returns:

Pointer to the greyscale atlas buffer.

inline bool atlasDirty() const

Check if the atlas has been modified since last upload.

Returns:

True if dirty.

inline i32 atlasHeight() const

Get the atlas height.

Returns:

Height in pixels.

inline i32 atlasWidth() const

Get the atlas width.

Returns:

Width in pixels.

void drawText(u32 *pixels, i32 stride, i32 bufH, i32 x, i32 y, std::string_view text, Color c)

Rasterize text directly into a pixel buffer (CPU path).

Parameters:
  • pixels – Destination pixel buffer (RGBA u32).

  • stride – Row stride of the destination buffer in pixels.

  • bufH – Height of the destination buffer in pixels.

  • x – X position to start drawing.

  • y – Y position to start drawing.

  • text – UTF-8 text to rasterize.

  • c – Text color.

const GlyphMetrics *getGlyph(char ch)

Get (or rasterize) metrics for a character.

Parameters:

ch – The character to look up.

Returns:

Pointer to the glyph metrics, or nullptr on failure.

bool init(const char *fontPath, f32 fontSize)

Initialize with a TrueType font file and pixel size.

Parameters:
  • fontPath – Path to the .ttf font file.

  • fontSize – Desired font size in pixels.

Returns:

True on success, false if the font could not be loaded.

inline i32 lineHeight() const

Get the line height for the loaded font.

Returns:

Line height in pixels.

inline void markClean()

Mark the atlas as clean (after uploading to GPU).

i32 measureText(std::string_view text)

Measure the horizontal advance width of a text string.

Parameters:

text – UTF-8 text to measure.

Returns:

Width in pixels.

void release()

Release all resources.

Private Functions

bool growAtlas()
bool rasterizeGlyph(char ch)

Private Members

i32 ascent_ = 0
std::vector<u8> atlas_
i32 atlasH_ = 256
i32 atlasW_ = 512
i32 cursorX_ = 1
i32 cursorY_ = 1
i32 descent_ = 0
bool dirty_ = true
std::vector<u8> fontData_
void *fontInfo_ = nullptr
std::unordered_map<char, GlyphMetrics> glyphs_
i32 lineGap_ = 0
i32 lineHeight_ = 0
i32 rowHeight_ = 0
f32 scale_ = 0
struct GlyphMetrics
#include <glyph_cache.hpp>

Metrics for a single rasterized glyph.

Public Members

i32 advance

Horizontal advance width.

f32 u0
f32 u1
f32 v0
f32 v1

Texture atlas UV coordinates.

i32 x0
i32 x1
i32 y0
i32 y1

Bounding box in pixels.

class GpuContext : public ink::Renderer
#include <gpu_context.hpp>

GPU rendering context.

Implements the Renderer interface for GPU rendering. Create via backend-specific factory functions:

Renderer interface

virtual void beginFrame(Color clearColor = {0, 0, 0, 255}) override

Begin a new frame, clearing the render target.

Parameters:

clearColor – The color to clear the render target to (default: opaque black).

virtual void endFrame() override

End the current frame.

virtual void execute(const Recording &recording, const DrawPass &pass) override

Execute recorded drawing commands.

Parameters:
  • recording – The recorded drawing commands.

  • pass – The draw pass defining execution order.

virtual void resize(i32 w, i32 h) override

Resize the render target.

Parameters:
  • w – New width in pixels.

  • h – New height in pixels.

virtual std::shared_ptr<Image> makeSnapshot() const override

Create an immutable snapshot of the current render target.

Returns:

Shared pointer to the snapshot Image.

virtual void setGlyphCache(GlyphCache *cache) override

Set the glyph cache used for text rendering.

Parameters:

cache – Pointer to the glyph cache.

GPU-specific operations

void readPixels(void *dst, i32 x, i32 y, i32 w, i32 h) const

Read pixels from the GPU framebuffer into a buffer.

Parameters:
  • dst – Destination buffer (must be large enough for w×h×4 bytes).

  • x – X offset in the framebuffer.

  • y – Y offset in the framebuffer.

  • w – Width of the region to read.

  • h – Height of the region to read.

unsigned int textureId() const

Get the GL texture ID of the offscreen color attachment.

Returns:

OpenGL texture name.

unsigned int fboId() const

Get the GL framebuffer object ID.

Returns:

OpenGL FBO name.

Public Functions

~GpuContext() override
bool valid() const

Check if the underlying GPU implementation is valid.

Returns:

True if the GPU backend was initialized successfully.

Private Functions

explicit GpuContext(std::shared_ptr<GpuImpl> impl)
u64 resolveImageTexture(const Image *image)

Private Members

std::shared_ptr<GpuImpl> impl_

Friends

friend std::shared_ptr<GpuContext> MakeGpuContextFromImpl(std::shared_ptr<GpuImpl>)
friend class Surface
class Image
#include <image.hpp>

An immutable snapshot of pixel data.

Images are created from Surfaces via makeSnapshot() and can be drawn onto any Surface via canvas->drawImage(). This is the mechanism for compositing multiple surfaces together.

Public Types

enum class StorageType : u8

Storage backend for the image data.

Values:

enumerator CpuPixmap

CPU-side pixel buffer.

enumerator GpuTexture

GPU texture handle.

Public Functions

inline u64 backendTextureHandle() const

Get the opaque backend texture handle.

Returns:

Texture handle for GPU-backed images; 0 otherwise.

inline PixelFormat format() const

Get pixel format.

Returns:

The pixel format of this image.

inline u32 glTextureId() const

Get the GL texture ID.

Returns:

OpenGL texture name (truncated from u64 to u32).

inline i32 height() const

Get image height.

Returns:

Height in pixels.

inline const PixmapInfo &info() const

Get full pixmap info.

Returns:

Reference to the PixmapInfo descriptor.

inline bool isCpuBacked() const

Check if image is CPU-backed.

Returns:

True if stored as a CPU pixmap.

inline bool isGpuBacked() const

Check if image is GPU-backed.

Returns:

True if stored as a GPU texture.

inline const void *pixels() const

Get raw pixel pointer (CPU-backed images only).

Returns:

Pointer to pixel data, or nullptr for GPU-backed images.

inline const u32 *pixels32() const

Get pixel data as 32-bit words.

Returns:

Pointer to pixel data cast to u32*.

inline StorageType storageType() const

Get the storage type.

Returns:

StorageType::CpuPixmap or StorageType::GpuTexture.

inline i32 stride() const

Get row stride in bytes.

Returns:

Bytes per row.

inline u64 uniqueId() const

Get a stable unique identity for backend caches.

Returns:

Unique image ID.

inline bool valid() const

Check if the image contains valid data.

Returns:

True if dimensions are positive and backing storage is present.

inline i32 width() const

Get image width.

Returns:

Width in pixels.

Public Static Functions

static std::shared_ptr<Image> MakeFromBackendTexture(u64 textureHandle, i32 width, i32 height, PixelFormat fmt = PixelFormat::RGBA8888, std::shared_ptr<void> lifetimeToken = nullptr)

Create an image from a backend-specific GPU texture handle.

Parameters:
  • textureHandle – Opaque handle (e.g. GLuint for GL, VkImage for Vulkan).

  • width – Texture width in pixels.

  • height – Texture height in pixels.

  • fmt – Pixel format (default RGBA8888).

  • lifetimeToken – Optional shared_ptr whose destructor releases the texture.

Returns:

Shared pointer to the new Image.

static std::shared_ptr<Image> MakeFromGLTexture(u32 textureId, i32 width, i32 height, PixelFormat fmt = PixelFormat::RGBA8888, std::shared_ptr<void> lifetimeToken = nullptr)

Convenience: create from a GL texture ID.

Parameters:
  • textureId – OpenGL texture name.

  • width – Texture width in pixels.

  • height – Texture height in pixels.

  • fmt – Pixel format (default RGBA8888).

  • lifetimeToken – Optional shared_ptr whose destructor releases the texture.

Returns:

Shared pointer to the new Image.

static std::shared_ptr<Image> MakeFromPixmap(const Pixmap &src)

Create an image by copying pixel data from a Pixmap.

Parameters:

src – Source pixmap to copy from.

Returns:

Shared pointer to the new Image.

static std::shared_ptr<Image> MakeFromPixmapNoCopy(const Pixmap &src)

Create an image wrapping existing pixel data (caller must keep data alive).

Parameters:

src – Source pixmap (data is not copied).

Returns:

Shared pointer to the new Image.

Private Functions

Image(const PixmapInfo &info, const void *pixels, std::unique_ptr<Pixmap> owned)
Image(const PixmapInfo &info, u64 textureHandle, std::shared_ptr<void> lifetimeToken)

Private Members

u64 backendTexture_ = 0
std::shared_ptr<void> gpuLifetimeToken_
u64 id_ = 0
PixmapInfo info_
std::unique_ptr<Pixmap> ownedPixmap_
const void *pixels_ = nullptr
StorageType storageType_ = StorageType::CpuPixmap

Private Static Functions

static u64 nextImageId()
class Pipeline
#include <pipeline.hpp>

Abstract base class for rendering pipeline stages.

Each pipeline handles a specific type of draw operations and manages its own vertex accumulation and GPU state. This decouples the rendering backend from specific draw operation types, making it easier to add new operations or backends.

Pipelines are expected to:

  1. Accumulate draw commands via encode()

  2. Submit accumulated work via flush()

  3. Reset state via reset() for the next frame

Public Functions

virtual ~Pipeline() = default
virtual void encode(const CompactDrawOp &op, const DrawOpArena &arena) = 0

Encode a draw operation into this pipeline’s internal buffers.

The pipeline should determine if the operation belongs to it and either accumulate it or ignore it.

Parameters:
  • op – The compact draw operation to encode.

  • arena – The arena holding variable-length operation data.

virtual void flush() = 0

Flush all accumulated draw operations to the GPU.

Called when switching pipelines or at frame end.

virtual void reset() = 0

Reset pipeline state for the next frame.

Clears vertex buffers and any cached state.

struct PixelData
#include <pixel_data.hpp>

Non-owning pixel data descriptor for host integration.

Does not own the pixel data — the caller must ensure the pixels remain valid for the lifetime of this descriptor.

Public Functions

PixelData() = default
inline PixelData(const void *data_, i32 w, i32 h, i32 stride, PixelFormat fmt)

Construct a PixelData descriptor.

Parameters:
  • data_ – Pointer to the raw pixel buffer.

  • w – Width in pixels.

  • h – Height in pixels.

  • stride – Bytes per row.

  • fmt – Pixel format.

inline bool isValid() const

Check if the descriptor points to valid pixel data.

Returns:

True if data is non-null and dimensions are positive.

inline std::int64_t sizeBytes() const

Compute the total size of the pixel data in bytes.

Returns:

Total byte count (height × rowBytes).

Public Members

const void *data = nullptr

Pointer to pixel data.

PixelFormat format = PixelFormat::BGRA8888

Pixel format.

i32 height = 0

Height in pixels.

i32 rowBytes = 0

Bytes per row (stride)

i32 width = 0

Width in pixels.

class Pixmap
#include <pixmap.hpp>

Owning or non-owning pixel buffer.

Use Alloc() to create an owned buffer, or Wrap() to reference external memory.

Public Functions

Pixmap() = default
Pixmap(const Pixmap&) = delete
Pixmap(Pixmap &&other) noexcept
~Pixmap()
inline void *addr()

Get raw pixel pointer (mutable).

inline const void *addr() const

Get raw pixel pointer (const).

inline u32 *addr32()

Get pixel pointer as u32* (mutable).

inline const u32 *addr32() const

Get pixel pointer as u32* (const).

inline u8 *addr8()

Get pixel pointer as u8* (mutable).

inline const u8 *addr8() const

Get pixel pointer as u8* (const).

void clear(Color c)

Fill the entire buffer with a color.

Parameters:

c – The fill color.

inline PixelFormat format() const

Get pixel format.

inline i32 height() const

Get height in pixels.

inline const PixmapInfo &info() const

Get the pixmap info descriptor.

Pixmap &operator=(const Pixmap&) = delete
Pixmap &operator=(Pixmap &&other) noexcept
void reallocate(const PixmapInfo &info)

Reallocate the buffer with new dimensions/format.

Parameters:

info – New pixmap descriptor.

void reset()

Release pixel data and reset to empty state.

inline void *rowAddr(i32 y)

Get pointer to the start of a specific row (mutable).

Parameters:

y – Row index.

inline const void *rowAddr(i32 y) const

Get pointer to the start of a specific row (const).

Parameters:

y – Row index.

inline i32 stride() const

Get row stride in bytes.

inline bool valid() const

Check if the pixmap has valid pixel data.

Returns:

True if pixels are non-null and dimensions are positive.

inline i32 width() const

Get width in pixels.

Public Static Functions

static Pixmap Alloc(const PixmapInfo &info)

Allocate a new pixel buffer described by info.

Parameters:

info – Dimensions, stride, and format of the buffer.

Returns:

A new Pixmap that owns its pixel data.

static Pixmap Wrap(const PixmapInfo &info, void *pixels)

Wrap existing pixel memory (caller keeps ownership).

Parameters:
  • info – Dimensions, stride, and format of the buffer.

  • pixels – Pointer to the external pixel data.

Returns:

A non-owning Pixmap.

Private Functions

Pixmap(const PixmapInfo &info, void *pixels, bool ownsPixels)

Private Members

PixmapInfo info_
bool ownsPixels_ = false
void *pixels_ = nullptr
struct PixmapInfo
#include <pixmap.hpp>

Descriptor for pixel buffer dimensions, stride, and format.

Public Functions

inline i32 bytesPerPixel() const

Get bytes per pixel (always 4 for current formats).

Returns:

Bytes per pixel.

inline i32 computeByteSize() const

Compute total byte size of the pixel buffer.

Returns:

stride × height.

Public Members

PixelFormat format = PixelFormat::RGBA8888

Pixel format.

i32 height = 0

Height in pixels.

i32 stride = 0

Bytes per row.

i32 width = 0

Width in pixels.

Public Static Functions

static inline PixmapInfo Make(i32 w, i32 h, PixelFormat fmt)

Create a PixmapInfo with the given dimensions and format.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

  • fmt – Pixel format.

Returns:

A new PixmapInfo.

static inline PixmapInfo MakeBGRA(i32 w, i32 h)

Create a PixmapInfo with BGRA8888 format.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

Returns:

A new PixmapInfo with BGRA8888 format.

static inline PixmapInfo MakeRGBA(i32 w, i32 h)

Create a PixmapInfo with RGBA8888 format.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

Returns:

A new PixmapInfo with RGBA8888 format.

struct Point
#include <types.hpp>

A 2D point with floating-point coordinates.

Public Members

f32 x = 0

X coordinate.

f32 y = 0

Y coordinate.

class Recorder
#include <recording.hpp>

Records draw operations into a compact command buffer.

Call drawing methods to accumulate operations, then finish() to produce an immutable Recording.

Public Functions

void clearClip()

Record a clear-clip operation.

void drawImage(std::shared_ptr<Image> image, f32 x, f32 y)

Record an image-drawing operation.

Parameters:
  • image – Shared pointer to the image.

  • x – X position.

  • y – Y position.

void drawLine(Point p1, Point p2, Color c, f32 width)

Record a line-drawing operation.

Parameters:
  • p1 – Start point.

  • p2 – End point.

  • c – Line color.

  • width – Line width.

void drawPolyline(const Point *pts, i32 count, Color c, f32 width)

Record a polyline-drawing operation.

Parameters:
  • pts – Array of vertices.

  • count – Number of points.

  • c – Line color.

  • width – Line width.

void drawText(Point p, std::string_view text, Color c)

Record a text-drawing operation.

Parameters:
  • p – Position of the text baseline.

  • text – UTF-8 text to draw.

  • c – Text color.

void fillRect(Rect r, Color c)

Record a fill-rectangle operation.

Parameters:
  • r – Rectangle to fill.

  • c – Fill color.

std::unique_ptr<Recording> finish()

Finish recording and produce an immutable Recording.

Returns:

Unique pointer to the completed Recording.

void reset()

Reset the recorder, discarding all accumulated operations.

void setClip(Rect r)

Record a set-clip operation.

Parameters:

r – The clipping rectangle.

void strokeRect(Rect r, Color c, f32 width)

Record a stroke-rectangle operation.

Parameters:
  • r – Rectangle to stroke.

  • c – Stroke color.

  • width – Stroke line width.

Private Members

DrawOpArena arena_
std::vector<std::shared_ptr<Image>> images_
std::vector<CompactDrawOp> ops_
class Recording
#include <recording.hpp>

Immutable command buffer containing recorded draw operations.

Created by Recorder::finish(). Operations can be traversed in original order via accept() or in sorted order via dispatch().

Public Functions

Recording(std::vector<CompactDrawOp> ops, DrawOpArena arena, std::vector<std::shared_ptr<Image>> images)

Construct a Recording from operations, arena, and images.

Parameters:
  • ops – Vector of compact draw operations.

  • arena – Arena holding variable-length data.

  • images – Vector of referenced images.

void accept(DrawOpVisitor &visitor) const

Traverse operations in original recording order.

Parameters:

visitor – The visitor to receive each operation.

inline const DrawOpArena &arena() const

Get the data arena.

void dispatch(DrawOpVisitor &visitor, const class DrawPass &pass) const

Dispatch operations in sorted order defined by a draw pass.

Parameters:
  • visitor – The visitor to receive each operation.

  • pass – The draw pass defining execution order.

const Image *getImage(u32 index) const

Get an image by index.

Parameters:

index – Index into the images list.

Returns:

Pointer to the Image, or nullptr if out of range.

inline const std::vector<std::shared_ptr<Image>> &images() const

Get the list of referenced images.

inline const std::vector<CompactDrawOp> &ops() const

Get the list of recorded operations.

Private Functions

void dispatchOp(const CompactDrawOp &op, DrawOpVisitor &visitor) const

Private Members

DrawOpArena arena_
std::vector<std::shared_ptr<Image>> images_
std::vector<CompactDrawOp> ops_
struct Rect
#include <types.hpp>

An axis-aligned rectangle defined by position and size.

Public Members

f32 h = 0

Height.

f32 w = 0

Width.

f32 x = 0

Left edge X coordinate.

f32 y = 0

Top edge Y coordinate.

class Renderer
#include <renderer.hpp>

Abstract rendering interface.

Implemented by CPU and GPU renderers to execute drawing commands. This abstraction allows Surface to work with any rendering backend through a unified interface.

Subclassed by ink::GpuContext

Public Functions

virtual ~Renderer() = default
virtual void beginFrame(Color clearColor = {0, 0, 0, 255}) = 0

Begin a new frame, clearing the render target.

Parameters:

clearColor – The color to clear the render target to (default: opaque black).

virtual void endFrame() = 0

End the current frame (no GPU submission; see flush()).

virtual void execute(const Recording &recording, const DrawPass &pass) = 0

Execute recorded drawing commands in the order defined by a draw pass.

Parameters:
  • recording – The recorded drawing commands.

  • pass – The draw pass defining execution order.

virtual std::shared_ptr<Image> makeSnapshot() const = 0

Create an immutable snapshot of the current render target contents.

Returns:

Shared pointer to the snapshot Image.

virtual void resize(i32 w, i32 h) = 0

Resize the render target.

Parameters:
  • w – New width in pixels.

  • h – New height in pixels.

inline virtual void setGlyphCache(GlyphCache *cache)

Set the glyph cache used for text rendering.

Parameters:

cache – Pointer to the glyph cache (may be null to disable text).

struct SortKey
#include <draw_pass.hpp>

64-bit sort key for DrawOp ordering.

Layout: [63:48] clip_id, [47:40] op_type, [39:8] color_hash, [7:0] sequence.

Public Functions

inline bool operator<(const SortKey &o) const

Compare sort keys for ordering.

Public Members

u64 key

The packed 64-bit sort key.

u32 opIndex

Index into the Recording’s operation list.

Public Static Functions

static inline SortKey make(u16 clipId, DrawOp::Type type, Color c, u8 seq, u32 idx)

Construct a sort key from draw operation attributes.

Parameters:
  • clipId – Current clip group identifier.

  • type – The draw operation type.

  • c – The draw color (used for batching).

  • seq – Sequence number for ordering within a batch.

  • idx – Index of the operation in the Recording.

Returns:

A new SortKey.

struct State

Public Members

Rect clip = {}
bool hasClip = false
class Surface
#include <surface.hpp>

Top-level rendering target.

A Surface owns a Canvas, a Device, and a Renderer. The typical frame cycle is: beginFrame() → draw via canvas()endFrame()flush().

Note

The caller must ensure the GL context is current when using a GPU-backed surface.

Public Functions

~Surface()
void beginFrame(Color clearColor = {0, 0, 0, 255})

Begin a new frame, clearing to the specified color.

Parameters:

clearColor – The color to clear the render target to (default: opaque black).

inline Canvas *canvas() const

Get the Canvas used for drawing on this surface.

Returns:

Pointer to the Canvas.

void endFrame()

End the current frame (finishes recording).

void flush()

Execute recorded commands and submit to the backend.

PixelData getPixelData() const

Get a non-owning pixel data descriptor for host integration.

Returns:

A PixelData descriptor.

bool isGPU() const

Check if this is a GPU-backed surface.

Returns:

True if GPU-backed.

std::shared_ptr<Image> makeSnapshot() const

Create an immutable snapshot of current surface contents.

Returns:

Shared pointer to the snapshot Image.

Pixmap *peekPixels()

Direct pixel access (raster surfaces only).

Returns:

Pointer to the underlying Pixmap, or nullptr for GPU/recording surfaces.

const Pixmap *peekPixels() const

Direct pixel access (raster surfaces only).

Returns:

Pointer to the underlying Pixmap, or nullptr for GPU/recording surfaces.

void resize(i32 w, i32 h)

Resize the surface to new dimensions.

Parameters:
  • w – New width in pixels.

  • h – New height in pixels.

void setGlyphCache(GlyphCache *cache)

Set the glyph cache used for text rendering.

Parameters:

cache – Pointer to the glyph cache.

std::unique_ptr<Recording> takeRecording()

Take ownership of the current recording (for recording surfaces).

Returns:

Unique pointer to the Recording.

Public Static Functions

static std::unique_ptr<Surface> MakeGpu(const std::shared_ptr<GpuContext> &context, i32 w, i32 h, PixelFormat fmt = PixelFormat::BGRA8888)

Create a GPU surface.

Falls back to CPU if context is null or invalid.

Parameters:
  • context – Shared pointer to the GPU context.

  • w – Width in pixels.

  • h – Height in pixels.

  • fmt – Pixel format (default BGRA8888).

Returns:

Unique pointer to the new Surface.

static std::unique_ptr<Surface> MakeRaster(i32 w, i32 h, PixelFormat fmt = PixelFormat::BGRA8888)

Create a CPU raster surface that allocates its own pixel buffer.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

  • fmt – Pixel format (default BGRA8888).

Returns:

Unique pointer to the new Surface.

static std::unique_ptr<Surface> MakeRasterDirect(const PixmapInfo &info, void *pixels)

Create a CPU raster surface wrapping a caller-owned pixel buffer (zero-copy).

Parameters:
  • infoPixmap descriptor for the external buffer.

  • pixels – Pointer to the caller-owned pixel data.

Returns:

Unique pointer to the new Surface.

static std::unique_ptr<Surface> MakeRecording(i32 w, i32 h)

Create a recording-only surface (no rendering, just captures commands).

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

Returns:

Unique pointer to the new Surface.

Private Functions

Surface(std::shared_ptr<Renderer> renderer, std::unique_ptr<Pixmap> pixmap)

Private Members

std::unique_ptr<Canvas> canvas_
Device device_
GlyphCache *glyphCache_ = nullptr
std::unique_ptr<Pixmap> pixmap_
std::shared_ptr<Renderer> renderer_
namespace ink

Typedefs

using f32 = float

32-bit floating point.

using f64 = double

64-bit floating point.

using i32 = int32_t

Signed 32-bit integer.

using u16 = uint16_t

Unsigned 16-bit integer.

using u32 = uint32_t

Unsigned 32-bit integer.

using u64 = uint64_t

Unsigned 64-bit integer.

using u8 = uint8_t

Unsigned 8-bit integer.

Enums

enum class PixelFormat

Pixel format enumeration.

Values:

enumerator RGBA8888

Red-Green-Blue-Alpha, 8 bits each.

enumerator BGRA8888

Blue-Green-Red-Alpha, 8 bits each (native on many platforms).

Functions

inline const char *version()

Return the library version string (e.g.

“0.1.1”).

Returns:

Null-terminated version string in “major.minor.patch” format.

inline int versionMajor()

Return the major version number.

Returns:

Major version as an integer.

inline int versionMinor()

Return the minor version number.

Returns:

Minor version as an integer.

inline int versionPatch()

Return the patch version number.

Returns:

Patch version as an integer.

namespace GpuContexts

GL-specific GpuContext factory functions.

Vulkan-specific GpuContext factory functions.

Metal-specific GpuContext factory functions.

Usage: #include <ink/gpu/gl/gl_context.hpp> auto ctx = GpuContexts::MakeGL();

Usage: #include <ink/gpu/metal/metal_context.hpp> auto ctx = GpuContexts::MakeMetal();

Usage: #include <ink/gpu/vk/vk_context.hpp> auto ctx = GpuContexts::MakeVulkan();

Functions

std::shared_ptr<GpuContext> MakeGL()

Create a GpuContext bound to the currently active OpenGL context.

Host must have created and made current a GL context before calling. Returns nullptr if no GL context is current or GL init fails.

std::shared_ptr<GpuContext> MakeMetal()

Create a GpuContext using the default Metal device.

Returns nullptr if no Metal-capable GPU is available or init fails.

std::shared_ptr<GpuContext> MakeVulkan()

Create a GpuContext for Vulkan.

Host must have initialized Vulkan before calling. Returns nullptr if Vulkan init fails.

TODO: Implement Vulkan backend

namespace std

STL namespace.

file canvas.hpp
#include “ink/types.hpp
#include <string_view>
#include <vector>
#include <memory>

User-facing drawing API with state management.

file device.hpp
#include “ink/types.hpp
#include “ink/recording.hpp
#include <memory>

Recording device that converts Canvas commands into low-level draw operations.

file draw_op_visitor.hpp
#include “ink/types.hpp
#include <cstdint>
#include <memory>

Visitor interface for traversing recorded draw operations.

file draw_pass.hpp
#include “ink/types.hpp
#include “ink/recording.hpp
#include <vector>
#include <algorithm>

Sort key and draw pass for optimizing draw operation execution order.

file glyph_cache.hpp
#include “ink/types.hpp
#include <string_view>
#include <unordered_map>
#include <vector>
#include <string>

Font rasterization cache with texture atlas for text rendering.

file gl_context.hpp
#include “ink/gpu/gpu_context.hpp
file gpu_context.hpp
#include “ink/renderer.hpp
#include <memory>

GPU rendering context implementing the Renderer interface.

file metal_context.hpp
#include “ink/gpu/gpu_context.hpp
file vk_context.hpp
#include “ink/gpu/gpu_context.hpp
file image.hpp
#include “ink/types.hpp
#include “ink/pixmap.hpp
#include <memory>

Immutable image snapshot for compositing and drawing.

file ink.hpp
#include “ink/version.hpp
#include “ink/types.hpp
#include “ink/pixmap.hpp
#include “ink/pixel_data.hpp
#include “ink/image.hpp
#include “ink/recording.hpp
#include “ink/draw_op_visitor.hpp
#include “ink/draw_pass.hpp
#include “ink/device.hpp
#include “ink/canvas.hpp
#include “ink/gpu/gpu_context.hpp
#include “ink/pipeline.hpp
#include “ink/surface.hpp
#include “ink/glyph_cache.hpp

Convenience header that includes the entire ink public API.

Ink - A lightweight 2D rendering library

Usage:

// CPU rendering #include <ink/ink.hpp> auto surface = ink::Surface::MakeRaster(800, 600); surface->canvas()->fillRect({0, 0, 100, 100}, {255, 0, 0, 255}); surface->flush();

// GPU rendering (requires #include <ink/gpu/gl/gl_context.hpp>) auto ctx = ink::GpuContexts::MakeGL(); auto surface = ink::Surface::MakeGpu(ctx, 800, 600); surface->canvas()->fillRect({0, 0, 100, 100}, {255, 0, 0, 255}); surface->flush();

file pipeline.hpp
#include “ink/types.hpp
#include “ink/recording.hpp

Abstract base class for rendering pipeline stages.

file pixel_data.hpp
#include “ink/types.hpp
#include “ink/pixmap.hpp
#include <cstdint>

Non-owning pixel data descriptor for host integration.

file pixmap.hpp
#include “ink/types.hpp
#include <cstdlib>
#include <cstring>

Pixel format, pixel buffer descriptor, and owning/non-owning pixel buffer.

file recording.hpp
#include “ink/types.hpp
#include <string_view>
#include <vector>
#include <memory>
#include <cstring>

Draw operation types, arena allocator, recording, and recorder.

file renderer.hpp
#include “ink/types.hpp
#include <memory>

Abstract rendering interface for CPU and GPU backends.

file surface.hpp
#include “ink/types.hpp
#include “ink/pixmap.hpp
#include “ink/canvas.hpp
#include “ink/device.hpp
#include “ink/pixel_data.hpp
#include “ink/image.hpp
#include “ink/renderer.hpp
#include <memory>

Top-level rendering target (CPU raster, GPU, or recording-only).

file types.hpp
#include <cstdint>

Core type aliases and basic geometric/color types for the ink library.

file version.hpp

Library version information.

Defines

INK_VERSION_MAJOR
INK_VERSION_MINOR
INK_VERSION_PATCH
dir include/ink/gpu/gl
dir include/ink/gpu
dir include
dir include/ink
dir include/ink/gpu/metal
dir include/ink/gpu/vk