This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: kmp-compose-metal-vulkan-zero-copy-bridge

Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform

Enforces hardware-backed zero-copy texture sharing between platform-native rendering pipelines (AVFoundation/Metal on Apple, AHardwareBuffer/Vulkan on Android) and Compose Multiplatform's Skiko rasterization layer. Prohibits CPU heap allocation, readbacks, and synchronous GPU fence stalls to guarantee sustained 60/120 FPS performance.

Prohibited Anti-Pattern: Intermediate CPU Heap Readback and Synchronous GPU Stalls in Texture Render Loops

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform - Enforces hardware-backed zero-copy texture sharing between platform-native rendering pipelines (AVFoundation/Metal on Apple, AHardwareBuffer/Vulkan on Android) and Compose Multiplatform's Skiko rasterization layer. Prohibits CPU heap allocation, readbacks, and synchronous GPU fence stalls to guarantee sustained 60/120 FPS performance.
globs: **/src/**/rendering/**/*.kt, **/src/**/graphics/**/*.kt, **/src/**/platform/**/*.kt, **/src/**/ui/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/kmp-compose-metal-vulkan-zero-copy-bridge"
---

# Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Intermediate CPU Heap Readback and Synchronous GPU Stalls in Texture Render Loops

## System Constraints
You are a Principal Graphics and Mobile Runtime Engineer specializing in Kotlin Multiplatform and Skiko rendering internals. Enforce strict zero-copy invariants when bridging native textures into Compose Multiplatform. You must reject any code executing CPU readbacks (e.g., glReadPixels, CVPixelBufferLockBaseAddress with CPU access, Bitmap allocation) or synchronous GPU stalls (glFinish, waitUntilCompleted) across the rendering boundary. Mandate direct hardware-backed external texture importing via AHardwareBuffer on Vulkan/Android and CVMetalTextureCache on Metal/Apple.

## Strict Guidelines
- Engineers MUST ALWAYS wrap native GPU textures directly into Skia/Skiko backend texture structures (e.g., org.jetbrains.skia.Image.makeFromTexture or platform Metal/Vulkan equivalents) without copying raw pixel data to the JVM/Native heap.
- Engineers MUST NOT call CVPixelBufferLockBaseAddress on Apple platforms or ImageReader.acquireLatestImage() byte-buffer extractors on Android for high-framerate rendering pipelines.
- Code MUST use asynchronous GPU fence synchronization primitives (e.g., MTLEvent, VkSemaphore, or EGLSyncKHR) rather than blocking the main/render thread with glFinish() or commandBuffer.waitUntilCompleted().
- Services MUST manage lifetime and recycling of native texture caches (CVMetalTextureCacheRef, VkDeviceMemory bindings) to prevent memory leaks and Vulkan descriptor exhaustion.
- Components NEVER reallocate backing textures per-frame; textures MUST be acquired from a double/triple-buffered hardware swapchain or ring buffer pool.

## Bad vs Good Implementations
### Bridging iOS CVPixelBuffer to Skiko Compose Metal Context

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Reading pixel bytes back to the CPU via CVPixelBufferLockBaseAddress and copying them into a Kotlin ByteArray forces a round-trip from GPU memory to CPU heap. This induces excessive GC churn, high bus bandwidth consumption, and frame drops below 60/120 FPS.
```kotlin
// BAD: Allocating a CPU heap byte buffer and copying pixel data across the runtime
fun renderFrame(pixelBuffer: CVPixelBufferRef, canvas: Canvas) {
    CVPixelBufferLockBaseAddress(pixelBuffer, 0u)
    val width = CVPixelBufferGetWidth(pixelBuffer).toInt()
    val height = CVPixelBufferGetHeight(pixelBuffer).toInt()
    val bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer).toInt()
    val srcPtr = CVPixelBufferGetBaseAddress(pixelBuffer)
    
    // Allocating a massive ByteArray on the Kotlin heap every frame
    val byteArray = ByteArray(height * bytesPerRow)
    memcpy(byteArray.refTo(0), srcPtr, (height * bytesPerRow).toULong())
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0u)
    
    // CPU decoding / Skia raster allocation triggers garbage collection and frame stutter
    val skiaImage = Image.makeFromEncoded(byteArray)
    canvas.drawImage(skiaImage, 0f, 0f)
}
```

#### Production Standard (Correct)
> **Standard**: CVMetalTextureCacheCreateTextureFromImage provides direct access to the IOSurface/CVPixelBuffer GPU memory as an MTLTexture. Wrapping this into a Skia BackendTexture executes completely within GPU VRAM with zero heap memory allocation and zero CPU memory copies.
```kotlin
// GOOD: Zero-copy texture ingestion using CVMetalTextureCache and Skiko GrDirectContext
fun renderFrameZeroCopy(
    pixelBuffer: CVPixelBufferRef,
    textureCache: CVMetalTextureCacheRef,
    skikoGrContext: DirectContext,
    canvas: Canvas
) {
    val width = CVPixelBufferGetWidth(pixelBuffer).toULong()
    val height = CVPixelBufferGetHeight(pixelBuffer).toULong()
    val textureRef = memScoped { alloc<CVMetalTextureRefVar>() }
    
    // Map CVPixelBuffer directly into an MTLTexture without CPU copying
    val status = CVMetalTextureCacheCreateTextureFromImage(
        kCFAllocatorDefault,
        textureCache,
        pixelBuffer,
        null,
        MTLPixelFormatBGRA8Unorm,
        width,
        height,
        0u,
        textureRef.ptr
    )
    if (status != kCVReturnSuccess) return
    
    val metalTexture = CVMetalTextureGetTexture(textureRef.value) ?: return
    
    // Zero-copy wrap native Metal texture into Skia Image via backend texture bridge
    val backendTexture = BackendTexture.makeMetal(width.toInt(), height.toInt(), false, metalTexture)
    val skiaImage = Image.makeFromTexture(
        skikoGrContext,
        backendTexture,
        SurfaceOrigin.TOP_LEFT,
        ColorType.BGRA_8888,
        ColorAlphaType.PREMUL
    )
    
    if (skiaImage != null) {
        canvas.drawImage(skiaImage, 0f, 0f)
    }
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform

Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform defines an industrial-grade behavioral standard for mobile engineering. Enforces hardware-backed zero-copy texture sharing between platform-native rendering pipelines (AVFoundation/Metal on Apple, AHardwareBuffer/Vulkan on Android) and Compose Multiplatform's Skiko rasterization layer. Prohibits CPU heap allocation, readbacks, and synchronous GPU fence stalls to guarantee sustained 60/120 FPS performance. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Intermediate CPU Heap Readback and Synchronous GPU Stalls in Texture Render Loops Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftIntermediate CPU Heap Readback and Synchronous GPU Stalls in Texture Render LoopsCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS wrap native GPU textures directly into Skia/Skiko backend texture structures (e.g., org.jetbrains.skia.Image.makeFromTexture or platform Metal/Vulkan equivalents) without copying raw pixel data to the JVM/Native heap.

Engineers MUST ALWAYS wrap native GPU textures directly into Skia/Skiko backend texture structures (e.g., org.jetbrains.skia.Image.makeFromTexture or platform Metal/Vulkan equivalents) without copying raw pixel data to the JVM/Native heap.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT call CVPixelBufferLockBaseAddress on Apple platforms or ImageReader.acquireLatestImage() byte-buffer extractors on Android for high-framerate rendering pipelines.

Engineers MUST NOT call CVPixelBufferLockBaseAddress on Apple platforms or ImageReader.acquireLatestImage() byte-buffer extractors on Android for high-framerate rendering pipelines.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST use asynchronous GPU fence synchronization primitives (e.g., MTLEvent, VkSemaphore, or EGLSyncKHR) rather than blocking the main/render thread with glFinish() or commandBuffer.waitUntilCompleted().

Code MUST use asynchronous GPU fence synchronization primitives (e.g., MTLEvent, VkSemaphore, or EGLSyncKHR) rather than blocking the main/render thread with glFinish() or commandBuffer.waitUntilCompleted().

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST manage lifetime and recycling of native texture caches (CVMetalTextureCacheRef, VkDeviceMemory bindings) to prevent memory leaks and Vulkan descriptor exhaustion.

Services MUST manage lifetime and recycling of native texture caches (CVMetalTextureCacheRef, VkDeviceMemory bindings) to prevent memory leaks and Vulkan descriptor exhaustion.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER reallocate backing textures per-frame

Components NEVER reallocate backing textures per-frame; textures MUST be acquired from a double/triple-buffered hardware swapchain or ring buffer pool.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Zero-Copy Native Texture Ingestion for KMP Compose Multiplatform critical for AI coding assistants?

Enforces hardware-backed zero-copy texture sharing between platform-native rendering pipelines (AVFoundation/Metal on Apple, AHardwareBuffer/Vulkan on Android) and Compose Multiplatform's Skiko rasterization layer. Prohibits CPU heap allocation, readbacks, and synchronous GPU fence stalls to guarantee sustained 60/120 FPS performance. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Intermediate CPU Heap Readback and Synchronous GPU Stalls in Texture Render Loops) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

Related MOBILE Rules

View all rules →
1 Rule in Stack

Zero-Copy