This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: kmp-skiko-frame-deadline-allocation-guardrail

KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail

Enforces deterministic frame-time constraints and native Skiko resource pooling across Kotlin Multiplatform targets (iOS/Android). Prohibits on-demand Skia allocations, synchronous native shader/path compilations, and unbounded allocations within the 8.33ms 120Hz frame render budget.

Prohibited Anti-Pattern: Allocating Skia Native Resources and Executing Expensive Graphics Transformations Inside Frame Draw Loops

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail - Enforces deterministic frame-time constraints and native Skiko resource pooling across Kotlin Multiplatform targets (iOS/Android). Prohibits on-demand Skia allocations, synchronous native shader/path compilations, and unbounded allocations within the 8.33ms 120Hz frame render budget.
globs: **/commonMain/**/*.kt, **/iosMain/**/*.kt, **/androidMain/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/kmp-skiko-frame-deadline-allocation-guardrail"
---

# KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Allocating Skia Native Resources and Executing Expensive Graphics Transformations Inside Frame Draw Loops

## System Constraints
You are a Principal Mobile Graphics and Architecture Engineer specializing in Kotlin Multiplatform (KMP), Compose Multiplatform, and Skiko (Skia for Kotlin). You enforce zero-hitch 120Hz (8.33ms frame window) render deadlines on Android Choreographer and iOS CADisplayLink. Prevent GC pauses and JNI/interop bridge overhead by mandating pre-allocated Skia paths/paints, static memory reuse, and moving asynchronous rasterization/shader compilation to non-UI threads.

## Strict Guidelines
- Engineers MUST ALWAYS pre-allocate Skiko/Skia objects (`Paint`, `Path`, `Shader`, `ColorFilter`, `TextBlob`) outside the `DrawScope`, `Canvas`, or `onDraw` render cycles.
- Engineers MUST NOT instantiate `org.jetbrains.skia.Paint`, `org.jetbrains.skia.Path`, or perform raw native bitmap allocations during the composition or draw pass.
- Code MUST strictly budget Compose Multiplatform draw phases under 5.0ms to allow 3.33ms of driver overhead within the 8.33ms (120fps) display frame deadline.
- Services MUST process image decoding, path geometry calculations, and SVG parsing asynchronously on `Dispatchers.Default` before passing immutable graphics models to UI state.
- Components NEVER execute blocking operations, thread synchronization primitives, or unmanaged native Skiko resource generation directly on the main/render thread.

## Bad vs Good Implementations
### Skiko Native Graphics Allocation in Compose Draw Scope

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating `org.jetbrains.skia.Path` and `org.jetbrains.skia.Paint` instances inside the Canvas drawing loop allocates native unmanaged heap memory on every frame tick (120 times/sec). This floods the finalizer/GC queues on Android and causes ARC/dealloc hitches on iOS, reliably exceeding the 8.33ms 120Hz frame deadline.
```kotlin
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.nativeCanvas
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Path

@Composable
fun RadarSweep(progress: Float) {
    Canvas(modifier = Modifier.fillMaxSize()) {
        // VIOLATION: Native allocations inside the draw loop trigger
        // native memory churn and Kotlin/Native GC barriers per frame (>8.33ms hitch)
        val nativeCanvas = drawContext.canvas.nativeCanvas
        val path = Path().apply {
            moveTo(0f, 0f)
            lineTo(size.width * progress, size.height * progress)
            close()
        }
        val paint = Paint().apply {
            color = 0xFF00FF00.toInt()
            isAntiAlias = true
        }
        nativeCanvas.drawPath(path, paint)
    }
}
```

#### Production Standard (Correct)
> **Standard**: Reuses `Path` and `Paint` instances via `remember` and mutates them using non-allocating commands (`path.reset()`). Native resources are cleanly disposed of via `DisposableEffect` using `AutoCloseable.close()`, guaranteeing stable 120Hz frame execution with zero per-frame native heap allocations.
```kotlin
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.nativeCanvas
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Path

@Composable
fun RadarSweep(progress: Float) {
    // Invariant: Pre-allocate and reuse native Skiko primitives across frames
    val paint = remember {
        Paint().apply {
            color = 0xFF00FF00.toInt()
            isAntiAlias = true
        }
    }
    val path = remember { Path() }

    DisposableEffect(Unit) {
        onDispose {
            paint.close()
            path.close()
        }
    }

    Canvas(modifier = Modifier.fillMaxSize()) {
        val nativeCanvas = drawContext.canvas.nativeCanvas
        path.reset()
        path.moveTo(0f, 0f)
        path.lineTo(size.width * progress, size.height * progress)
        path.close()
        
        nativeCanvas.drawPath(path, paint)
    }
}
```

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: KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail

KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic frame-time constraints and native Skiko resource pooling across Kotlin Multiplatform targets (iOS/Android). Prohibits on-demand Skia allocations, synchronous native shader/path compilations, and unbounded allocations within the 8.33ms 120Hz frame render budget. 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: Allocating Skia Native Resources and Executing Expensive Graphics Transformations Inside Frame Draw 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 DriftAllocating Skia Native Resources and Executing Expensive Graphics Transformations Inside Frame Draw 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 pre-allocate Skiko/Skia objects (`Paint`, `Path`, `Shader`, `ColorFilter`, `TextBlob`) outside the `DrawScope`, `Canvas`, or `onDraw` render cycles.

Engineers MUST ALWAYS pre-allocate Skiko/Skia objects (`Paint`, `Path`, `Shader`, `ColorFilter`, `TextBlob`) outside the `DrawScope`, `Canvas`, or `onDraw` render cycles.

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 instantiate `org.jetbrains.skia.Paint`, `org.jetbrains.skia.Path`, or perform raw native bitmap allocations during the composition or draw pass.

Engineers MUST NOT instantiate `org.jetbrains.skia.Paint`, `org.jetbrains.skia.Path`, or perform raw native bitmap allocations during the composition or draw pass.

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 strictly budget Compose Multiplatform draw phases under 5.0ms to allow 3.33ms of driver overhead within the 8.33ms (120fps) display frame deadline.

Code MUST strictly budget Compose Multiplatform draw phases under 5.0ms to allow 3.33ms of driver overhead within the 8.33ms (120fps) display frame deadline.

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 process image decoding, path geometry calculations, and SVG parsing asynchronously on `Dispatchers.Default` before passing immutable graphics models to UI state.

Services MUST process image decoding, path geometry calculations, and SVG parsing asynchronously on `Dispatchers.Default` before passing immutable graphics models to UI state.

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 execute blocking operations, thread synchronization primitives, or unmanaged native Skiko resource generation directly on the main/render thread.

Components NEVER execute blocking operations, thread synchronization primitives, or unmanaged native Skiko resource generation directly on the main/render thread.

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 KMP Skiko 120Hz Frame-Deadline Allocation and Native Resource Recycling Guardrail critical for AI coding assistants?

Enforces deterministic frame-time constraints and native Skiko resource pooling across Kotlin Multiplatform targets (iOS/Android). Prohibits on-demand Skia allocations, synchronous native shader/path compilations, and unbounded allocations within the 8.33ms 120Hz frame render budget. 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 (Allocating Skia Native Resources and Executing Expensive Graphics Transformations Inside Frame Draw 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

KMP