GPU render context
Give an in-process renderer the scene's Skia DirectContext and GPU device, so it draws without a second device or a per-frame copy.
TaoGpuRenderContext publishes the GPU context of the enclosing Compose surface: the Skia
DirectContext the scene draws with and the native device behind it. An in-process renderer,
such as a map engine, a 3D viewport or a charting library, allocates its render targets there,
and Compose samples them with no shared handle or copy. It comes with nucleus-application.
For a producer on its own device or in another process, use
TextureView.
Get the context
Call rememberTaoGpuRenderContext() inside a surface that owns a GPU context: DecoratedWindow
content, a native popup layer, a NativeView overlay or a tray panel. It returns null during
bring-up and teardown, and when no hardware context exists. Every call site on the same surface
gets the same instance, so composables can share state keyed on it, such as one 3D engine per
window.
@Composable
fun MapViewport() {
val renderContext = rememberTaoGpuRenderContext() ?: return
// A rebuilt context is a new instance: key renderer state on it.
val renderer = remember(renderContext) { MapEngineRenderer(renderContext) }
DisposableEffect(renderContext) {
onDispose { renderContext.runOnGpuThread { renderer.releaseGpuResources() } }
}
LaunchedEffect(renderer) {
while (true) {
withFrameNanos { renderer.renderFrame() } // GPU work inside the callback
}
}
}Pick the backend
| Type | Platforms | backend | Extra member |
|---|---|---|---|
TaoMetalRenderContext | macOS | TaoRenderBackend.METAL | metalDevicePtr: the id<MTLDevice> |
TaoOpenGlRenderContext | Linux, Windows | TaoRenderBackend.OPENGL | withContextCurrent { }: binds the surface's GL context |
Both expose skiaContext and runOnGpuThread { }. On Windows, OPENGL means ANGLE's GLES 3 on
Direct3D 11: resolve entry points through ANGLE's eglGetProcAddress.
Respect ownership and lifetime
- Every handle is borrowed: never retain, release or free it.
- The context is rebuilt on window detach, a Wayland hide and show, or popup and tray panel
teardown, and
rememberTaoGpuRenderContext()then returns a new instance ornull. Key state withremember(renderContext)and free GPU resources in aDisposableEffect(renderContext), insiderunOnGpuThreadorwithContextCurrent. - Call every member from the UI thread (the main thread on macOS).
Pace frames
Issue GPU work inside the withFrameNanos callback, or during composition, layout or draw. On
Linux, code that runs after the withFrameNanos suspension gets null from withContextCurrent
almost every vsync. withContextCurrent is safe to nest.
What's next
- TextureView: import GPU frames from an external producer.
- Native views: embed interactive native widgets.
- Threading: the UI thread these calls run on.
Last updated on