CommandQueue

A CommandQueue is the worker that runs Rive in a thread. It holds all of the state, including assets (images, audio, and fonts), Rive files, artboards, state machines, and view model instances.

Instances of the command queue are reference counted. At initialization, the command queue has a ref count of 1. Call acquire to increment the ref count, and release to decrement it. When the ref count reaches 0, the command queue is disposed, and its resources are released. Using a disposed command queue will throw an IllegalStateException. Any pending operations will be notified with a CancellationException.

Rive resources such as manually-created files, assets, and surfaces may acquire references to the command queue. Releasing the command queue's own reference is not enough to fully dispose the worker while those resources remain open; close each resource when you no longer need it.

The command queue normally is passed into rememberRiveFile or, alternatively, created by default if one is not supplied. This is then transitively supplied to other Rive elements, such as artboards, when the Rive file is passed in.

It is important to understand the threading model. Each command queue begins a system thread. The "command" part of the name indicates that "commands" are serialized to the CommandServer, all in the Rive C++ runtime. This design minimizes shared state. As a consequence, all operations are asynchronous.

If the operation creates a resource, it will be received as a handle, a monotonically increasing opaque identifier that can be translated to a pointer on the command server. A handle can be used immediately, since commands are ordered, ensuring validity at the time of any operation that uses it.

If instead the operation is a query, such as getting the names of artboards or view models, it is represented as a suspend function. In most cases, the result can be cached, as Rive files are largely immutable.

You may choose to have multiple command queues to run multiple Rive files in parallel. However, be aware that these instances cannot share memory. This means that each file and asset must be loaded into each command queue separately.

A command queue needs to be polled to receive messages from the command server. This is handled by the rememberRiveWorker composable or by calling beginPolling.

Parameters

renderContext

Render context owned by this command queue after native startup succeeds.

bridge

Native bridge used to create and communicate with the command queue. This is injectable so unit tests can mock JNI calls without loading native code.

tracingEnabled

Whether native draw/advance tracing should start enabled.

nativePointer

Allocated native command queue created by createNativeCommandQueueResources. Supplying it lets backend fallback finish native startup before ownership is transferred to this instance.

Constructors

Link copied to clipboard
constructor(renderBackend: RenderBackend = RenderBackend.OpenGL, tracingEnabled: Boolean = false)

Creates a command queue using the requested render backend.

Types

Link copied to clipboard
object Companion
Link copied to clipboard
data class PropertyUpdate<T>(val handle: ViewModelInstanceHandle, val propertyPath: String, val value: T)

Contains the data associated with a property update event.

Properties

Link copied to clipboard

Hot flow that emits when any boolean property is updated.

Link copied to clipboard

Hot flow that emits when any color property is updated.

Link copied to clipboard

Hot flow that emits when any enum property is updated.

Link copied to clipboard
open override val isDisposed: Boolean

Whether this object has been disposed due to the reference count reaching 0.

Link copied to clipboard

Hot flow that emits when any number property is updated.

Link copied to clipboard
open override val refCount: Int

The current reference count.

Link copied to clipboard

Flow that emits when a state machine has settled.

Link copied to clipboard

Hot flow that emits when any string property is updated.

Link copied to clipboard

Hot flow that emits when any trigger property is fired.

Functions

Link copied to clipboard
open override fun acquire(source: String)

Acquire a reference. Must be balanced with a call to release.

Link copied to clipboard
fun advanceStateMachine(stateMachineHandle: StateMachineHandle, deltaTime: Duration)

Advance the state machine by the given delta time in nanoseconds.

Link copied to clipboard
fun appendToList(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, itemHandle: ViewModelInstanceHandle)

Appends a view model instance to the end of a list property.

Link copied to clipboard
suspend fun beginPolling(lifecycle: Lifecycle, ticker: FrameTicker = ChoreographerFrameTicker)

Begin polling the CommandQueue for messages from the CommandServer. Without this, callbacks and errors will not be delivered. This should typically be called by tying to the lifecycle scope of the containing activity, fragment, or composable, e.g. with lifecycleScope.launch.

Link copied to clipboard
fun bindViewModelInstance(stateMachineHandle: StateMachineHandle, viewModelInstanceHandle: ViewModelInstanceHandle)

Bind a view model instance to a state machine. This establishes the data binding for the instance's properties.

Link copied to clipboard

Create an artboard by name for the given file. This is useful when the file has multiple artboards and you want to create a specific one.

Link copied to clipboard

Create the default artboard for the given file. This is the artboard marked "Default" in the Rive editor.

Link copied to clipboard

Create the default state machine for the given artboard. This is the state machine marked "Default" in the Rive editor.

Link copied to clipboard
fun createImageSurface(width: Int, height: Int): RiveSurface

Create an off-screen image surface for rendering to a buffer.

Link copied to clipboard

Create a Rive rendering surface for Rive to draw into.

Link copied to clipboard

Create a state machine by name for the given artboard. This is useful when the artboard has multiple state machines and you want to create a specific one.

Link copied to clipboard

Create a new view model instance based on the given source. The source is a combination of a view model source and an instance source, which multiply to capture all the possible ways to create an instance (+1 for referencing).

Link copied to clipboard
suspend fun decodeAudio(bytes: ByteArray): AudioHandle

Decode an audio file from the given bytes. The decoded audio is stored on the CommandServer.

Link copied to clipboard
suspend fun decodeFont(bytes: ByteArray): FontHandle

Decode a font file from the given bytes. The bytes are for a font file, such as TTF. The decoded font is stored on the CommandServer.

Link copied to clipboard
suspend fun decodeImage(bytes: ByteArray): ImageHandle

Decode an image file from the given bytes. The bytes are for a compressed image format such as PNG or JPEG. The decoded image is stored on the CommandServer.

Link copied to clipboard
fun deleteArtboard(artboardHandle: ArtboardHandle)

Delete an artboard and free its resources. This is useful when you no longer need the artboard and want to free up memory. Counterpart to createDefaultArtboard or createArtboardByName.

Link copied to clipboard
fun deleteAudio(audioHandle: AudioHandle)

Delete audio and free its resources. This is useful when you no longer need the audio and want to free up memory. Counterpart to decodeAudio.

Link copied to clipboard
fun deleteFile(fileHandle: FileHandle)

Counterpart to loadFile to free the resources associated to the file handle.

Link copied to clipboard
fun deleteFont(fontHandle: FontHandle)

Delete a font and free its resources. This is useful when you no longer need the font and want to free up memory. Counterpart to decodeFont.

Link copied to clipboard
fun deleteImage(imageHandle: ImageHandle)

Delete an image and free its resources. This is useful when you no longer need the image and want to free up memory. Counterpart to decodeImage.

Link copied to clipboard
fun deleteStateMachine(stateMachineHandle: StateMachineHandle)

Delete a state machine and free its resources. This is useful when you no longer need the state machine and want to free up memory. Counterpart to createDefaultStateMachine or createStateMachineByName.

Link copied to clipboard
fun deleteViewModelInstance(viewModelInstanceHandle: ViewModelInstanceHandle)

Delete a view model instance and free its resources. This is useful when you no longer need the view model instance and want to free up memory. Counterpart to createViewModelInstance.

Link copied to clipboard

Destroy a Rive rendering surface. The surface will not be valid for use after this call.

Link copied to clipboard
fun draw(artboardHandle: ArtboardHandle, stateMachineHandle: StateMachineHandle, surface: RiveSurface, fit: Fit, clearColor: Int = Color.TRANSPARENT)

Draw the artboard with the given state machine.

Link copied to clipboard
fun drawToBuffer(artboardHandle: ArtboardHandle, stateMachineHandle: StateMachineHandle, surface: RiveSurface, buffer: ByteArray, width: Int, height: Int, fit: Fit = Fit.Contain(), clearColor: Int = Color.TRANSPARENT)

Renders the current state of an artboard/state machine pair into an off-screen buffer that can be used for snapshot comparisons.

Link copied to clipboard
fun fireTriggerProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String)

Fire a trigger property on the view model instance.

Link copied to clipboard
suspend fun getArtboardNames(fileHandle: FileHandle): List<String>

Query the file for available artboard names. Returns on onArtboardsListed.

Link copied to clipboard
suspend fun getBooleanProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): Boolean

Get a boolean property's value on the view model instance.

Link copied to clipboard
suspend fun getColorProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): Int

Get a color property's value on the view model instance.

Link copied to clipboard
suspend fun getDefaultViewModelInfo(fileHandle: FileHandle, artboardHandle: ArtboardHandle): DefaultViewModelInfo

Query the default view model info for an artboard. Returns on onDefaultViewModelInfoReceived.

Link copied to clipboard
suspend fun getEnumProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): String

Get an enum property's value on the view model instance.

Link copied to clipboard
suspend fun getEnums(fileHandle: FileHandle): List<File.Enum>

Query the file for available enums. Returns on onEnumsListed.

Link copied to clipboard
suspend fun getListSize(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): Int

Gets the size of a list property on the view model instance.

Link copied to clipboard
suspend fun getNumberProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): Float

Get a number property's value on the view model instance.

Link copied to clipboard
suspend fun getStateMachineNames(artboardHandle: ArtboardHandle): List<String>

Query the artboard for available state machine names. Returns on onStateMachinesListed.

Link copied to clipboard
suspend fun getStringProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String): String

Get a string property's value on the view model instance.

Link copied to clipboard
suspend fun getViewModelInstanceNames(fileHandle: FileHandle, viewModelName: String): List<String>

Query the file for available view model instance names for the given view model. Returns on onViewModelInstancesListed.

Link copied to clipboard
suspend fun getViewModelNames(fileHandle: FileHandle): List<String>

Query the file for available view model names. Returns on onViewModelsListed.

Link copied to clipboard
suspend fun getViewModelProperties(fileHandle: FileHandle, viewModelName: String): List<ViewModel.Property>

Query the file for available view model properties for the given view model. Returns on onViewModelPropertiesListed.

Link copied to clipboard
fun insertToListAtIndex(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, index: Int, itemHandle: ViewModelInstanceHandle)

Inserts a view model instance into a list property at the specified index.

Link copied to clipboard
suspend fun loadFile(bytes: ByteArray): FileHandle

Load a Rive into the command queue. Returns the handle in either onFileLoaded or an error in onFileError.

Link copied to clipboard
fun pointerDown(stateMachineHandle: StateMachineHandle, fit: Fit, surfaceWidth: Float, surfaceHeight: Float, pointerID: Int, pointerX: Float, pointerY: Float)

Notify the state machine that the pointer (typically a user's touch) has touched down. This is used to interact with the state machine, triggering pointer events.

Link copied to clipboard
fun pointerExit(stateMachineHandle: StateMachineHandle, fit: Fit, surfaceWidth: Float, surfaceHeight: Float, pointerID: Int, pointerX: Float, pointerY: Float)

Notify the state machine that the pointer (typically a user's touch) has exited the surface. This is used to interact with the state machine, triggering pointer events.

Link copied to clipboard
fun pointerMove(stateMachineHandle: StateMachineHandle, fit: Fit, surfaceWidth: Float, surfaceHeight: Float, pointerID: Int, pointerX: Float, pointerY: Float)

Notify the state machine that the pointer (typically a user's touch) has moved. This is used to interact with the state machine, triggering pointer events.

Link copied to clipboard
fun pointerUp(stateMachineHandle: StateMachineHandle, fit: Fit, surfaceWidth: Float, surfaceHeight: Float, pointerID: Int, pointerX: Float, pointerY: Float)

Notify the state machine that the pointer (typically a user's touch) has lifted up. This is used to interact with the state machine, triggering pointer events.

Link copied to clipboard

Poll messages from the CommandServer to the CommandQueue. This is the channel that all callbacks and errors arrive on. Should be called every frame, regardless of whether there is any advancing or drawing.

Link copied to clipboard
fun registerAudio(name: String, audioHandle: AudioHandle)

Register audio as an asset with the given name. This allows the audio to be used to fulfill a referenced asset when loading a Rive file.

Link copied to clipboard
fun registerFont(name: String, fontHandle: FontHandle)

Register a font as an asset with the given name. This allows the font to be used to fulfill a referenced asset when loading a Rive file.

Link copied to clipboard
fun registerImage(name: String, imageHandle: ImageHandle)

Register an image as an asset with the given name. This allows the image to be used to fulfill a referenced asset when loading a Rive file.

Link copied to clipboard
open override fun release(source: String, reason: String)

Release a reference. When the last reference is released, the object is disposed.

Link copied to clipboard
fun removeFromList(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, itemHandle: ViewModelInstanceHandle)

Removes a view model instance from a list property.

Link copied to clipboard
fun removeFromListAtIndex(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, index: Int)

Removes an item from a list property at the specified index.

Link copied to clipboard
fun resetArtboardSize(artboardHandle: ArtboardHandle)

Resets an artboard to its original dimensions.

Link copied to clipboard
fun resizeArtboard(artboardHandle: ArtboardHandle, surface: RiveSurface, scaleFactor: Float = 1.0f)

Resizes an artboard to match the dimensions of the given surface.

Link copied to clipboard
fun setArtboardProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, artboardHandle: ArtboardHandle?)

Assign an artboard to a bindable artboard property on the view model instance, or clear the property if artboardHandle is null.

Link copied to clipboard
fun setBooleanProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, value: Boolean)

Set a boolean property's value on the view model instance.

Link copied to clipboard
fun setColorProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, @ColorInt value: Int)

Set a color property's value on the view model instance.

Link copied to clipboard
fun setEnumProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, value: String)

Set an enum property's value on the view model instance.

Link copied to clipboard
fun setImageProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, imageHandle: ImageHandle)

Assign an image to an image property on the view model instance.

Link copied to clipboard
fun setNumberProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, value: Float)

Update a number property's value on the view model instance.

Link copied to clipboard
fun setStringProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, value: String)

Set a string property's value on the view model instance.

Link copied to clipboard

Enables or disables native command server tracing for draw and advance work.

Link copied to clipboard
fun setViewModelInstanceProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, valueHandle: ViewModelInstanceHandle)

Assign a view model instance to a nested view model property on the view model instance.

Link copied to clipboard
fun subscribeToProperty(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, propertyType: ViewModel.PropertyDataType)

Subscribe to changes to a property on the view model instance. Updates will be emitted on the flow of the corresponding type, e.g. numberPropertyFlow for number properties.

Link copied to clipboard
fun swapListItems(viewModelInstanceHandle: ViewModelInstanceHandle, propertyPath: String, indexA: Int, indexB: Int)

Swaps two items in a list property by their indices.

Link copied to clipboard

Unregister audio that was previously registered with registerAudio. This will remove the reference to the audio from the CommandServer, allowing the memory to be freed if the audio handle was also deleted with deleteAudio.

Link copied to clipboard

Unregister a font that was previously registered with registerFont. This will remove the reference to the font from the CommandServer, allowing the memory to be freed if the font handle was also deleted with deleteFont.

Link copied to clipboard

Unregister an image that was previously registered with registerImage. This will remove the reference to the image from the CommandServer, allowing the memory to be freed if the image handle was also deleted with deleteImage.

Link copied to clipboard

Tie this CommandQueue's lifetime to a LifecycleOwner. This will call release when the owner's lifecycle reaches DESTROYED. Returns an AutoCloseable that can be used to manually release early; calling it is idempotent.