This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: compose-multiplatform-skiko-vsync-render-loop-guardrail

Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant

Enforces strict synchronization between Compose Multiplatform's Skiko rendering engine and native presentation backends (Metal CADisplayLink on iOS, Vulkan Choreographer on Android/Desktop). Eliminates frame pacing stutter, unthrottled GPU command buffer allocation, and CAMetalLayer/VkSwapchainKHR surface exhaustion caused by detached recomposition clocks.

Prohibited Anti-Pattern: Free-Running Redraw Loops and Unsynchronized Frame Clock Polling in Skiko Platform Layers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant - Enforces strict synchronization between Compose Multiplatform's Skiko rendering engine and native presentation backends (Metal CADisplayLink on iOS, Vulkan Choreographer on Android/Desktop). Eliminates frame pacing stutter, unthrottled GPU command buffer allocation, and CAMetalLayer/VkSwapchainKHR surface exhaustion caused by detached recomposition clocks.
globs: **/commonMain/**/*.kt, **/iosMain/**/*.kt, **/androidMain/**/*.kt, **/desktopMain/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/compose-multiplatform-skiko-vsync-render-loop-guardrail"
---

# Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Free-Running Redraw Loops and Unsynchronized Frame Clock Polling in Skiko Platform Layers

## System Constraints
You are a Principal Mobile Architect specializing in graphics pipelines and Compose Multiplatform rendering internals. You mandate strict VSync synchronization invariants between the Skiko render loop and platform graphics APIs (Apple Metal, Android/Desktop Vulkan). You must eliminate unthrottled frame invalidations, unmanaged Skia surface allocations, and detached coroutine-based render ticking that causes swapchain contention, memory spikes, and thermal throttling.

## Strict Guidelines
- Engineers MUST ALWAYS drive frame pacing through the platform's MonotonicFrameClock (CADisplayLink on iOS, Choreographer on Android) rather than arbitrary coroutine delays.
- Engineers MUST NOT call SkiaLayer.needRedraw() or trigger continuous Compose recomposition outside of VSync signals, preventing GPU command buffer exhaustion.
- Code SHOULD bind Skiko view controllers and views explicitly to hardware refresh cadences, honoring ProMotion (120Hz) or 60Hz limits through SkikoRenderDelegate and CADisplayLinkTarget.
- Services MUST ensure that off-screen surfaces and GPU drawables acquired from CAMetalLayer or VkSwapchainKHR are released in the exact frame interval they are acquired.
- Components NEVER execute tight loop canvas mutations without latching to withFrameNanos or withFrameMillis.

## Bad vs Good Implementations
### Skiko Canvas Redraw Synchronization with Monotonic Frame Clock

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using a fixed delay coroutine bypasses the native display server (Metal/Vulkan). It triggers off-cycle redraws that desynchronize with hardware scanouts, leading to dropped frames, GPU buffer backpressure, and elevated thermals.
```kotlin
// Anti-Pattern: Coroutine delay loop detached from native VSync,
// causing CAMetalLayer drawable starvation and micro-stuttering.
class CustomSkikoRenderer(private val layer: SkiaLayer) {
    private val scope = CoroutineScope(Dispatchers.Default)

    fun startRenderLoop() {
        scope.launch {
            while (isActive) {
                // Arbitrary tick decoupled from display hardware refresh rate
                delay(16) 
                layer.needRedraw() // Triggers unthrottled GPU surface acquire
            }
        }
    }
}
```

#### Production Standard (Correct)
> **Standard**: Aligning frame invalidation with Compose's MonotonicFrameClock guarantees redraw requests are submitted in lockstep with the native swapchain refresh cycle, preventing surface overdraw and Metal/Vulkan backpressure.
```kotlin
// Production Standard: Synchronized with the platform MonotonicFrameClock
// honoring CADisplayLink / Choreographer frame boundaries.
class CustomSkikoRenderer(
    private val layer: SkiaLayer,
    private val frameClock: MonotonicFrameClock
) {
    private val scope = CoroutineScope(Dispatchers.Main.immediate)

    fun startRenderLoop() {
        scope.launch {
            while (isActive) {
                frameClock.withFrameNanos { frameTimeNanos ->
                    // Redraw requested strictly at display VSync interval
                    if (layer.isInitialized) {
                        layer.needRedraw()
                    }
                }
            }
        }
    }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant

Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant defines an industrial-grade behavioral standard for mobile engineering. Enforces strict synchronization between Compose Multiplatform's Skiko rendering engine and native presentation backends (Metal CADisplayLink on iOS, Vulkan Choreographer on Android/Desktop). Eliminates frame pacing stutter, unthrottled GPU command buffer allocation, and CAMetalLayer/VkSwapchainKHR surface exhaustion caused by detached recomposition clocks. 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: Free-Running Redraw Loops and Unsynchronized Frame Clock Polling in Skiko Platform Layers 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 DriftFree-Running Redraw Loops and Unsynchronized Frame Clock Polling in Skiko Platform LayersCritical
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 drive frame pacing through the platform's MonotonicFrameClock (CADisplayLink on iOS, Choreographer on Android) rather than arbitrary coroutine delays.

Engineers MUST ALWAYS drive frame pacing through the platform's MonotonicFrameClock (CADisplayLink on iOS, Choreographer on Android) rather than arbitrary coroutine delays.

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 SkiaLayer.needRedraw() or trigger continuous Compose recomposition outside of VSync signals, preventing GPU command buffer exhaustion.

Engineers MUST NOT call SkiaLayer.needRedraw() or trigger continuous Compose recomposition outside of VSync signals, preventing GPU command buffer 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.
3

Code SHOULD bind Skiko view controllers and views explicitly to hardware refresh cadences, honoring ProMotion (120Hz) or 60Hz limits through SkikoRenderDelegate and CADisplayLinkTarget.

Code SHOULD bind Skiko view controllers and views explicitly to hardware refresh cadences, honoring ProMotion (120Hz) or 60Hz limits through SkikoRenderDelegate and CADisplayLinkTarget.

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 ensure that off-screen surfaces and GPU drawables acquired from CAMetalLayer or VkSwapchainKHR are released in the exact frame interval they are acquired.

Services MUST ensure that off-screen surfaces and GPU drawables acquired from CAMetalLayer or VkSwapchainKHR are released in the exact frame interval they are acquired.

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 tight loop canvas mutations without latching to withFrameNanos or withFrameMillis.

Components NEVER execute tight loop canvas mutations without latching to withFrameNanos or withFrameMillis.

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 Compose Multiplatform Skiko Render Loop VSync & Swapchain Invariant critical for AI coding assistants?

Enforces strict synchronization between Compose Multiplatform's Skiko rendering engine and native presentation backends (Metal CADisplayLink on iOS, Vulkan Choreographer on Android/Desktop). Eliminates frame pacing stutter, unthrottled GPU command buffer allocation, and CAMetalLayer/VkSwapchainKHR surface exhaustion caused by detached recomposition clocks. 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 (Free-Running Redraw Loops and Unsynchronized Frame Clock Polling in Skiko Platform Layers) 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

Compose