Skip to content

Draw and capture input

Choose the host according to how your application receives and presents input:

Host Input and presentation
InkDrawingSurface Compose primary-pointer input and frame clock; one active gesture; path renderer by default
InkLowLatencySurface Compose wrapper around the native panel; simultaneous pointers, prediction and direct Skia painting; mesh renderer by default
InkLowLatencyPanel The same native authoring in a Swing application; configurable predictor factory
InkAuthoringController / InkAuthoringSession Application-managed events and rendering; one pointer / simultaneous pointers

Compose surface

InkDrawingSurface handles primary mouse/touch/stylus gestures and frame updates. Render finished strokes in drawContent; retain the new stroke in onStrokeFinished so it stays visible after release.

package wiki

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.ink.geometry.ImmutableAffineTransform
import com.vivenotes.byteink.compose.InkDrawingSurface
import com.vivenotes.byteink.compose.rememberInkMeshRenderer
import com.vivenotes.byteink.compose.drawInk
import com.vivenotes.byteink.compose.rememberInkAuthoringController
import com.vivenotes.byteink.kit.AuthoredViveStroke
import com.vivenotes.byteink.kit.StoredInkStroke
import com.vivenotes.byteink.kit.ViveInkTool
import java.util.UUID

@Composable
fun DrawingExample(onRowReady: (StoredInkStroke) -> Unit) {
    val tool = remember { ViveInkTool(sizeDp = 3f) }
    val controller = rememberInkAuthoringController()
    val renderer = rememberInkMeshRenderer()
    val completed = remember { mutableStateListOf<AuthoredViveStroke>() }
    val density = LocalDensity.current.density
    val pageToPixels = remember(density) {
        ImmutableAffineTransform(density, 0f, 0f, 0f, density, 0f)
    }
    DisposableEffect(controller) {
        onDispose { controller.close() }
    }
    InkDrawingSurface(
        controller = controller,
        brush = tool.brush,
        modifier = Modifier.fillMaxSize(),
        strokeToView = pageToPixels,
        renderer = renderer,
        onStrokeFinished = { finished ->
            val authored = tool.complete(
                stroke = finished,
                id = UUID.randomUUID().toString(),
                pageId = "page-1",
                seq = completed.size + 1, // Demo sequence; a repository allocates real values.
                createdAt = System.currentTimeMillis(),
            )
            completed.add(authored)
            onRowReady(authored.row)
        },
        drawContent = {
            completed.forEach { drawInk(renderer, it.canonicalStroke, pageToPixels) }
        },
    )
}

The example renders page dp at device density, serializes each completed stroke, and displays its canonical geometry. It returns new rows through onRowReady; your repository persists them.

Surface parameter Use
controller One controller per attached surface
brush Native brush captured at pointer down
modifier Layout size, background and other Compose modifiers
strokeToView Page-to-local-pixel transform; include density and zoom
renderer Defaults to path rendering; pass rememberInkMeshRenderer() for mesh effects, textures and prediction shading
enabled false cancels and disables authoring
inputSource Optional serial input adapter; default Compose input when null
onStrokeFinished Captured completion callback; retain/store its Stroke
drawContent Finished ink/paper drawn before the wet stroke

Changing a tool or transform during a gesture does not alter its captured brush/transform. Explicitly call cancel() on a page/tool switch if the old gesture must be discarded. Removing or disabling the surface cancels it automatically. rememberInkAuthoringController() remembers a controller; the owner still closes it.

InkDrawingSurface keeps the first active pointer and ignores other pointers until that gesture ends. It forwards explicit predictions from an input source but does not create a predictor or expire a forecast automatically. Use a session or the direct surfaces below for simultaneous pointers and managed prediction expiry.

Native low-latency surfaces

InkLowLatencySurface and InkLowLatencyPanel acquire native input when inputSource = null. Real device history reaches an InkAuthoringSession, followed by a coalesced direct Skia render request on the AWT event dispatch thread (EDT). Windows/X11 use a dedicated double-buffered Skia layer with vsync throttling disabled; native Wayland uses immediate software Swing painting. Timed brush behavior and forecast expiry schedule additional updates; settled ink does not require a continuous polling paint loop.

This example embeds the native surface in a Swing-hosted Compose application and retains completed ink synchronously:

package wiki

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.awt.RenderSettings
import androidx.ink.geometry.AffineTransform
import androidx.ink.strokes.Stroke
import com.vivenotes.byteink.compose.InkLowLatencySurface
import com.vivenotes.byteink.compose.InkMeshRenderer
import com.vivenotes.byteink.compose.InkRenderer
import com.vivenotes.byteink.core.InkRuntime
import com.vivenotes.byteink.kit.StoredInkStroke
import com.vivenotes.byteink.kit.ViveInkTool
import java.awt.Toolkit
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.util.UUID
import javax.swing.JFrame
import javax.swing.SwingUtilities
import javax.swing.WindowConstants

/** The host owns [finishedRenderer]; the surface owns its separate default wet mesh renderer. */
@Composable
fun NativeDrawingExample(
    finishedRenderer: InkRenderer,
    onRowReady: (StoredInkStroke) -> Unit,
) {
    val tool = remember { ViveInkTool(sizeDp = 3f) }
    val finished = remember { mutableStateListOf<Stroke>() }
    // Native panels use AWT logical units. Skia applies device scaling itself; do not multiply
    // this transform by LocalDensity as the regular Compose Canvas example does.
    val pageToAwtLogical = AffineTransform.IDENTITY
    InkLowLatencySurface(
        brush = tool.brush,
        modifier = Modifier.fillMaxSize(),
        strokeToView = pageToAwtLogical,
        // Omitted renderer/inputSource select the owned mesh renderer and built-in native capture.
        onStrokeFinished = { _, realStroke ->
            val authored = tool.complete(
                stroke = realStroke,
                id = UUID.randomUUID().toString(),
                pageId = "page-1",
                seq = finished.size + 1, // Demo order; a repository allocates real values.
                createdAt = System.currentTimeMillis(),
            )
            // Keep save/reload geometry visible synchronously before the immediate next draw.
            finished.add(authored.canonicalStroke)
            onRowReady(authored.row)
        },
        drawContent = { canvas, _, _ ->
            finished.forEach { finishedRenderer.render(canvas, it, pageToAwtLogical) }
        },
    )
}

/**
 * Call from a desktop entry point, never the headless smoke runner. Start JBR 25 with
 * -Dawt.toolkit.name=WLToolkit, --add-opens=java.desktop/sun.awt.wl=ALL-UNNAMED and
 * --enable-native-access=ALL-UNNAMED before any AWT initialization.
 */
@OptIn(ExperimentalComposeUiApi::class)
fun showNativeWaylandExample(onRowReady: (StoredInkStroke) -> Unit = {}) {
    SwingUtilities.invokeLater {
        check(Toolkit.getDefaultToolkit().javaClass.name == "sun.awt.wl.WLToolkit") {
            "Launch on JBR 25 with WLToolkit and the native Wayland JVM flags"
        }
        InkRuntime.load()
        val finishedRenderer = InkMeshRenderer()
        val content = ComposePanel(renderSettings = RenderSettings.SwingGraphics())
        // The host disposes detached content before closing its borrowed finished renderer.
        content.isDisposeOnRemove = false
        content.setContent { NativeDrawingExample(finishedRenderer, onRowReady) }
        val frame = JFrame("ByteInk native Wayland")
        frame.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        frame.addWindowListener(object : WindowAdapter() {
            override fun windowClosed(event: WindowEvent) {
                try { content.dispose() } finally { finishedRenderer.close() }
            }
        })
        frame.contentPane.add(content)
        frame.setSize(900, 650)
        frame.setLocationRelativeTo(null)
        frame.isVisible = true
    }
}
Direct surface parameter Use
brush, strokeToView, onStrokeFinished Captured independently at each pointer's Begin
strokeToView Maps stroke/page units to AWT component-local logical units; the panel applies device scale
renderer null creates an owned InkMeshRenderer; a supplied renderer remains caller-owned
inputSource null acquires native capture; a supplied source overrides it
onStrokeFinished Receives (pointerId, Stroke) with only real canonical inputs
drawContent (Canvas, width, height) callback before live ink; dimensions and canvas coordinates are AWT logical units
centimetersPerNativePixel Optional measured physical calibration; leave null when unknown

Do not copy the density multiplication from the regular Compose example into this surface: native coordinates are already divided by the panel's device scale, and its canvas applies that scale during painting. Include page zoom/scroll in strokeToView, while keeping input and finished content in the same logical coordinate space.

Construct, update and close a panel on the EDT. Add completed strokes to finished content inside the captured completion callback, then enqueue database work on a worker thread. When updating finished content outside an input callback, call panel.requestInkRender(). The Compose wrapper requests redraws as it updates its panel.

Native Wayland retains finished content and a composed physical frame. Triangle mesh paints restore damaged pixels from the background and redraw overlapping live strokes in their original order. Filled outlines (InkPathRenderer and mesh-renderer DISCARD coats, including highlighters) restore the full old/current wet extent on geometry changes. Damage also expands across intersecting outlined strokes: antialiasing can change prefix coverage outside native mesh damage or through a partial clip. Prediction shrink/expiry and cancellation erase old pixels; completion refreshes the finished background. An OS repaint presents the existing frame. Keep drawContent stable until an explicit change: call requestInkRender() after modifying a scene, partial erase/exclusion, view, texture or animation outside completion, even when the callback object stays the same. Changing drawContent or clearColorArgb invalidates the background; request a render to show the change immediately.

panel.retainedPixelBudgetBytes defaults to 64 MiB for both physical N32 rasters combined (8 × physical width × physical height). A zero budget disables retention. Oversized viewports and custom renderers use full redraw; the built-in InkMeshRenderer and InkPathRenderer support retention. Detach, disable and close release the buffers. Hidden or disabled panels still paint finished content without retaining pixels; showing or reenabling restores the configured budget. Windows/X11 keep their existing presentation path.

retainedPixelBytes reports retained raster storage. retainedBackgroundBuildCount, retainedFullRedrawCount and retainedDirtyRedrawCount count raster rebuilds; retainedLastRedrawnPixelCount reports restored/rasterized physical pixels in the last paint. These counts exclude the full-window Skiko presentation copies. The panel consumes and resets its session's native updated-region accumulator after successful painting; custom consumers may read borrowed live engines but must leave their mutation and damage reset to the panel.

Mesh preparation separately reuses verified unchanged wet vertices, triangle chunks and texture-free shaders. It compares full owned snapshots and invalidates chunks whose indices or referenced attributes changed, including prediction rollback and earlier vertex mutations. Linear transforms, color and atlas changes invalidate preparation keys. This reduces preparation/allocation within dirty paints; full native export and whole-window presentation copies remain. See mesh cache behavior for byte accounting and disposal.

The built-in panel retires live renderer caches on completion/cancellation and reconnects native capture across attachment and Wayland hide/show. Closing it also closes its session and presentation resources. It closes only its own default mesh renderer; close a supplied mesh renderer yourself, or use rememberInkMeshRenderer() for automatic composition disposal. See renderer ownership.

InkLowLatencyPanel accepts predictorFactory = null to disable its automatic forecasting or a factory to replace it. InkLowLatencySurface uses the built-in predictor and does not expose that constructor option.

lastInputToRenderNanos measures packet handling through Skia recording. It excludes time before the handler, pixel transfer, compositor presentation and physical pen-to-photon latency. renderedFrameCount includes OS repaint requests; processedPacketCount counts packets rather than individual observations inside a Batch.

Native platform setup

Platform/backend Requirements
Windows (x86_64, arm64) / WINDOWS_POINTER Java 25+, WM_POINTER pen/history, touch and primary mouse
Linux X11 or XWayland / LINUX_XINPUT2 Java 25+, libX11.so.6, libXi.so.6, libc.so.6
Linux native Wayland / LINUX_WAYLAND_TABLET Tested JBR 25 with WLToolkit, libwayland-client.so.0, libc.so.6, a live compositor; tablet-v2 for pen axes, wl_touch for touch; libwayland-cursor.so.0 for pen cursors where the compositor lacks cursor-shape-v1

All native capture requires --enable-native-access=ALL-UNNAMED. For native Wayland, launch JBR 25 with these additional arguments before AWT initializes:

-Dawt.toolkit.name=WLToolkit
--add-opens=java.desktop/sun.awt.wl=ALL-UNNAMED
--enable-native-access=ALL-UNNAMED

Connect to a reachable compositor: use its WAYLAND_DISPLAY, or the default $XDG_RUNTIME_DIR/wayland-0 socket when that variable is unset. Backend selection follows the actual AWT toolkit: running an ordinary Linux JVM on a Wayland desktop can still use X11/XWayland. Missing tablet-v2 leaves touch and primary mouse available without fabricating pen pressure. The toolkit flag follows JetBrains' Wayland startup guidance and JBR 25 toolkit selection; the package-opening and native-access arguments are ByteInk's additional requirements.

For native Wayland Compose embedding, use the example's JFrame with ComposePanel(renderSettings = RenderSettings.SwingGraphics()). The tested Compose/Skiko heavyweight host assumes X11; use this Swing graphics host or a direct Swing InkLowLatencyPanel for WLToolkit. SwingGraphics is experimental off-screen presentation and adds a copy whose cost grows with panel size; ByteInk separately selects software painting for its Wayland ink panel. Compose's Swing interoperability guide describes this host option. Ordinary Compose windows can continue to use the regular surface under X11/XWayland.

Wayland GPU investigation

The native panel continues to use retained software Skia rendering. The pinned Skiko 0.150.1 supports an isolated offscreen EGL renderer through its public GLAssembledInterface and makeGLWithInterface APIs, but presenting those pixels through Swing requires GPU readback and full-frame image transfers. That experiment is kept in the verification/benchmark sources. It does not replace the panel's backend or swap buffers on JBR's borrowed wl_surface.

JBR 25 separately supports an optional Vulkan Java2D destination, selected at application startup with -Dsun.java2d.vulkan=true. This preserves the toolkit's window ownership and can change image blits and submission; ByteInk's retained Skia rasters still run on CPU. Set the property before AWT initializes and inspect a visible panel's actual configuration:

val configuration = panel.graphicsConfiguration
println("${configuration.javaClass.name}: $configuration")

sun.java2d.vulkan.WLVKGraphicsConfig identifies the Vulkan destination; sun.awt.wl.WLSMGraphicsConfig identifies shared-memory presentation. A requested Vulkan pipeline can fall back to shared memory when initialization is unavailable. A Vulkan configuration backed by a CPU device, or an EGL renderer named llvmpipe, does not establish hardware acceleration. ByteInk does not force this process-wide startup choice.

Keep the normal ancestor Swing buffering for immediate Wayland paints: its buffered paint path asks JBR to commit the owning window. A custom host that globally disables Swing buffering must arrange a supported toolkit commit; Toolkit.sync() alone only flushes an unbuffered paint. The benchmark's explicit internal peer call is confined to verification.

The Step 3 investigation report records the pinned source audit, lifecycle/fidelity checks and client paint/submission request measurements. JBR can coalesce several requests into a later buffer submission; deferred completion, compositor scanout and physical pen-to-photon latency are outside those timings.

NativeInkInputSource is the lower-level adapter for custom hosts. Subscribe and close on the EDT, use a displayable component and its matching live native handle, and allow one subscription per source. Windows/X11 use the drawing component's HWND/XID; Wayland requires its visible, configured top-level wl_surface. Recreate custom Wayland sources after hiding/showing the window, since the native surface changes. The built-in panel handles that lifecycle and waits for configuration. Native acquisition failures are explicit; runtime failures cancel gestures, close capture and invoke onFailure.

Pen cursor and hover on native Wayland

Binding tablet-v2 takes the pen away from the toolkit: the compositor stops moving the mouse pointer for it and shows no cursor for it over the application until the application sets one. Mutter then draws nothing at all. Each Wayland source therefore gives every tool over its window the AWT cursor of the component under the tool, the cursor a mouse would show there:

  • With cursor-shape-v1 (GNOME 48+, Plasma 6, wlroots 0.17+), a wp_cursor_shape_device_v1 per tool and set_shape with the latest proximity_in serial.
  • Otherwise, zwp_tablet_tool_v2.set_cursor with a themed surface of the tool's own, from libwayland-cursor (XCURSOR_THEME, XCURSOR_SIZE) at the window's integer scale. -Dbyteink.wayland.cursorShape=false forces this path.

AWT's predefined cursors map as JBR maps them for its own pointer. A custom AWT cursor named after a CSS cursor keyword (grabbing, nwse-resize, …) takes that shape, one named none hides the tool's cursor, and any other shows the arrow. Set the component's cursor to choose the pen's cursor, as for the mouse. A tool over a window that no source serves gets the arrow.

Set source.onHover to receive the tool's hover on EDT, in order with the subscription's events. InkHoverEvent.Move carries the position in component units, tool, time and tilt, never pressure; InkHoverEvent.Exit follows proximity loss, leaving the component or tool removal. A contact ends hover without Exit, and Move resumes after the lift. Hover never reaches the authoring listener. X11 and Windows move the system pointer with the pen and report its hover as mouse motion, so onHover is only called on native Wayland. A Compose host can forward hover to its UI as AWT mouse motion so that pointerHoverIcon, hover states and tooltips follow the pen; the source reads the resulting cursor after that host's events.

Native Wayland discovery runs on the reader thread. subscribe returns while setup is pending; read source.isReady on EDT, or panel.isInputReady when using the built-in panel. Registry and device-binding sync replies must both arrive before readiness is published on EDT. Contacts that begin earlier are ignored through their remaining moves and release. Readiness indicates initialized capture; a physical tablet and tablet-v2 support are separate from AWT mouse capture. Windows/X11 subscriptions become ready before subscribe returns. Supplied custom panel sources are assumed ready when their subscription returns.

Closing a Wayland subscription clears readiness and invalidates queued delivery immediately. Native cleanup finishes on its reader, so EDT does not join a reader or wait for a compositor reply. Setup can be cancelled during either discovery stage, and a new subscription can start while the old reader releases its resources. Generation checks discard old readiness, input and failure packets. Wayland setup errors use the existing EDT onFailure callback after cancellation and close. The private compositor tests cover delayed replies, disconnects during setup, rapid resubscription, hide/show and hotplug at both 1× and 2× scaling. The queue and wrapper ownership follow the libwayland client API.

The direct panel and Compose surface can capture bounded, opt-in software latency diagnostics: queue wait, normalization, handling, scheduled engine advances, drawing, Swing transfer/drawing and immediate request return. Use InkLatencyDiagnostics on EDT and export its immutable snapshot off-thread. Unknown boundaries stay null; physical pen/display latency and deferred compositor completion remain unmeasured.

Controller lifecycle

For application-managed input, use the same controller directly:

package wiki

import androidx.ink.geometry.ImmutableAffineTransform
import androidx.ink.strokes.Stroke
import com.vivenotes.byteink.compose.InkAuthoringController
import com.vivenotes.byteink.compose.InkPointerSample
import com.vivenotes.byteink.kit.ViveInkTool

fun pointerStroke(): Stroke = InkAuthoringController().use { controller ->
    val tool = ViveInkTool(sizeDp = 3f)
    controller.begin(
        brush = tool.brush,
        sample = InkPointerSample(x = 20f, y = 100f, uptimeMillis = 1_000L),
        strokeToView = ImmutableAffineTransform(2f, 0f, 0f, 0f, 2f, 0f),
    )
    controller.append(InkPointerSample(x = 80f, y = 100f, uptimeMillis = 1_016L))
    if (controller.isUpdateNeeded()) controller.advance(uptimeMillis = 1_016L)
    requireNotNull(controller.finish(
        InkPointerSample(x = 180f, y = 100f, uptimeMillis = 1_032L),
    ))
}
Call/state Behavior
begin(brush, sample, strokeToView) Starts a gesture and makes the first dot visible
append(sample) Buffers input; returns false for idle, wrong-tool, older or duplicate observations
setPredictedInputs(samples) Replaces the speculative tail; an empty list clears it
hasPendingInputs Schedule a frame even when native shape state has not changed yet
isUpdateNeeded() Buffered input or timed brush behavior needs a frame
advance(uptimeMillis) Processes buffered input once; returns whether geometry needed updating
revision Read in the draw block to observe processed geometry changes
liveStroke Current wet stroke; read it on the controller's thread and leave mutation to the controller
strokeToView Frozen transform for the active gesture
finish(sample?) Settles inputs and returns independent canonical Stroke, or null when idle
cancel() Discards active ink; safe while idle
close() Discards input and releases references; controller cannot restart

Custom frame loops must observe both isDrawing and hasPendingInputs, then use isUpdateNeeded(). append() alone does not advance geometry. Keep event and frame times in the same monotonic millisecond clock; passing epoch milliseconds to advance is incorrect.

If you supply predictions directly to a controller, retract them when their horizon expires and schedule an advance. Real input and completion also clear the previous speculative tail. Retire the controller's renderer cache before clearing or recycling its live engine.

Simultaneous pointers and prediction

An InkInputSource emits events serially on the UI thread; native sources deliver on the EDT. Return a subscription that prevents callbacks after closing. Preserve original timestamps, all accepted real history and measured device axes.

Event Behavior
Begin(sample, pointerId = 0L) Captures a new gesture; a session replaces any old gesture with that ID
Move(sample, pointerId = 0L) Buffers a real observation for that pointer
Batch(samples, predictedSamples = null, pointerId = 0L) Buffers real history; a session generates a forecast when predictions are null, or uses the supplied replacement
Predict(samples, pointerId = 0L) Replaces the forecast without adding real observations
Finish(sample = null, pointerId = 0L) Clears predictions and completes the real-input stroke
CancelPointer(pointerId) Discards only that pointer
Cancel Discards all active pointers

An empty prediction list clears the speculative tail. Predictions never become saved inputs, even when the pointer lifts before a forecast expires. The regular InkDrawingSurface forwards only explicitly supplied forecasts; it does not generate the session forecast described in the table.

When updating an existing adapter, add Batch, Predict and CancelPointer branches to exhaustive when expressions. Calls that omit the new sample axes and pointer IDs retain their source defaults. Recompile JVM consumers against the updated library: the data-class constructor signatures have changed.

This example advances two pointers independently, replaces predictions and completes both strokes from real inputs:

package wiki

import androidx.ink.brush.InputToolType
import androidx.ink.geometry.ImmutableAffineTransform
import androidx.ink.strokes.StrokeInput
import com.vivenotes.byteink.compose.InkAuthoringSession
import com.vivenotes.byteink.compose.InkInputEvent
import com.vivenotes.byteink.compose.InkPointerSample
import com.vivenotes.byteink.kit.AuthoredViveStroke
import com.vivenotes.byteink.kit.ViveInkTool

/** Device-packet example; no window or renderer is needed to author and save real inputs. */
fun simultaneousPointerStrokes(): Map<Long, AuthoredViveStroke> {
    val tool = ViveInkTool(sizeDp = 3f)
    val completed = linkedMapOf<Long, AuthoredViveStroke>()
    val pageToSurfacePixels = ImmutableAffineTransform(2f, 0f, 0f, 0f, 2f, 0f)
    fun pen(x: Float, time: Long) = InkPointerSample(
        x, 40f, time, InputToolType.STYLUS, pressure = .5f,
        tiltRadians = .3f, orientationRadians = .5f,
    )
    fun touch(x: Float, time: Long) = InkPointerSample(x, 100f, time, InputToolType.TOUCH)

    // Disable automatic forecasting here so each replacement comes from the sample device stream.
    InkAuthoringSession(predictorFactory = null).use { session ->
        fun deliver(event: InkInputEvent) = session.handle(
            event = event,
            brush = tool.brush,
            strokeToView = pageToSurfacePixels,
            onStrokeFinished = { pointerId, realStroke ->
                completed[pointerId] = tool.complete(
                    stroke = realStroke,
                    id = "pointer-$pointerId", // Demo IDs/order; the application's repository allocates these.
                    pageId = "page-1",
                    seq = completed.size + 1,
                    createdAt = 42L,
                )
            },
        )

        deliver(InkInputEvent.Begin(pen(20f, 1_000L), pointerId = 11L))
        deliver(InkInputEvent.Begin(touch(20f, 1_000L), pointerId = 22L))
        deliver(InkInputEvent.Batch(
            samples = listOf(pen(40f, 1_008L), pen(60f, 1_016L)),
            predictedSamples = listOf(pen(200f, 1_028L)),
            pointerId = 11L,
        ))
        deliver(InkInputEvent.Batch(
            samples = listOf(touch(40f, 1_008L)),
            predictedSamples = emptyList(), // Explicitly retract predictions for this pointer.
            pointerId = 22L,
        ))
        session.advance(uptimeMillis = 1_016L)
        check(session.activePointerIds == setOf(11L, 22L)) { "Both pointers must remain active independently" }
        val wetPen = session.liveStrokes.single { it.pointerId == 11L }.stroke
        check(wetPen.getRealInputCount() == 3 && wetPen.getPredictedInputCount() == 1) {
            "Initial wet pen: expected 3 real/1 predicted, got ${wetPen.getRealInputCount()}/${wetPen.getPredictedInputCount()}"
        }

        // A replacement changes the wet tail without becoming a real observation.
        deliver(InkInputEvent.Predict(listOf(pen(80f, 1_024L)), pointerId = 11L))
        session.advance(uptimeMillis = 1_017L)
        check(wetPen.getPredictedInputCount() == 1) { "Prediction replacement must retain exactly one speculative sample" }
        val replacementX = wetPen.populateInput(StrokeInput(), 3).x
        check(replacementX == 40f) { "Replacement prediction: expected stroke-space x=40, got $replacementX" }

        deliver(InkInputEvent.Batch(
            samples = listOf(pen(70f, 1_018L)), predictedSamples = emptyList(), pointerId = 11L,
        ))
        session.advance(uptimeMillis = 1_018L)
        check(wetPen.getPredictedInputCount() == 0) { "Empty prediction must retract the wet tail" }
        deliver(InkInputEvent.Predict(listOf(pen(200f, 1_030L)), pointerId = 11L))
        session.advance(uptimeMillis = 1_019L)
        check(wetPen.getPredictedInputCount() == 1) { "Finish fixture must contain a visible prediction" }

        // Finish removes even a visible prediction before producing the saved canonical stroke.
        deliver(InkInputEvent.Finish(pen(90f, 1_020L), pointerId = 11L))
        check(session.activePointerIds == setOf(22L)) { "Finishing the pen must preserve the active touch" }
        deliver(InkInputEvent.Finish(touch(60f, 1_020L), pointerId = 22L))
        check(session.activePointerIds.isEmpty()) { "Both finished pointers must be retired" }
    }
    return completed.toMap()
}

InkAuthoringSession creates one controller/predictor per gesture. Its defaults are a 12 ms horizon and InkLinearPredictor, whose horizon/distance limits default to 24 ms and 32 input-coordinate units. The predictor holds measured optional axes and bounds position extrapolation; it resets motion prediction after reversals, stationary observations, tool changes or long gaps. Pass predictorFactory = null or predictionMillis = 0L to disable automatic forecasting; explicitly supplied predictions still work.

Custom session loops call advance(uptimeMillis) on the event clock or advanceNow() on the mapped monotonic wall clock. Schedule while needsAnimationTick() is true, including when isUpdateNeeded() is false but a forecast must expire. Render each borrowed liveStrokes entry with its captured strokeToView; never mutate or retain its engine across completion/cancellation/reuse. Retire cached live geometry before those lifecycle operations. Closing a session is terminal.

Pressure and device adapters

InkPointerSample uses the input surface's local units: pixels on the regular Compose canvas, AWT logical units on direct panels. Coordinates must be finite and monotonic time nonnegative. Preserve an axis only when the device reports it; primary mouse/default touch omit synthetic pressure. Some native touch devices can report measured pressure.

Optional sample field Valid value
pressure Finite [0, 1], or null
tiltRadians Finite [0, π/2] from the surface normal, or null
orientationRadians Finite [0, 2π) shaft azimuth from local +x toward +y, or null; distinct from barrel twist
strokeUnitLengthCm Calibrated finite positive centimeters per input-surface unit, or null

Optional-axis availability and tool type are frozen at begin. An absent axis stays absent for that gesture; later missing values for an initially present axis hold the last measured value. Calligraphy uses speed when pressure is unavailable. The controller maps shaft orientation through the inverse captured transform.

Native adapters derive strokeUnitLengthCm from supplied centimetersPerNativePixel and device scale. Logical DPI is insufficient for physical calibration. The controller converts that scale into stroke units for uniform/rotated transforms, and omits it for anisotropic or sheared transforms rather than inventing a scalar.

Brush choices

val marker = ViveInkTool(familyId = ViveBrushes.MARKER, sizeDp = 3f)
val dashed = ViveInkTool(familyId = ViveBrushes.DASHED_LINE, sizeDp = 3f)
val nib = ViveInkTool(
    familyId = ViveBrushes.calligraphy(pressure = 3),
    stabilization = 2,
    sizeDp = 6f,
)
val highlight = ViveInkTool(
    familyId = ViveBrushes.HIGHLIGHTER,
    stabilization = 0,
    colorArgb = 0x80ffff00.toInt(),
    sizeDp = 18f,
    colorFollowsTheme = false,
)
Stabilization Model
0 Passthrough
1, 2, 3, 4, 5 Sliding windows of 20, 40, 60, 90, 120 ms at 180 Hz

Highlighter uses its pinned stock input model and stores 0 as “not applicable.” ViveInkTool validates levels 0..5 and known authoring families; ViveBrushes lookup helpers clamp levels and provide legacy fallbacks.

Complete input/surface parameters · Complete brush/tool parameters