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

Compose Multiplatform iOS CADisplayLink VSync & ProMotion Refresh Invariant

Enforces deterministic synchronization between the Skiko rendering pipeline and iOS native CADisplayLink. Prevents thermal throttling, visual tearing, and micro-stutter by binding draw passes to hardware VSync signals and dynamically configuring CAFrameRateRange for ProMotion displays.

Prohibited Anti-Pattern: Free-running coroutine render loop bypassing CADisplayLink and causing battery drainage and frame drops

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Compose Multiplatform iOS CADisplayLink VSync & ProMotion Refresh Invariant - Enforces deterministic synchronization between the Skiko rendering pipeline and iOS native CADisplayLink. Prevents thermal throttling, visual tearing, and micro-stutter by binding draw passes to hardware VSync signals and dynamically configuring CAFrameRateRange for ProMotion displays.
globs: **/iosMain/**/*.kt, **/iosApp/**/*.swift, **/skiko/**/*.kt, **/view/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/compose-multiplatform-ios-cadisplaylink-vsync-invariant"
---

# Compose Multiplatform iOS CADisplayLink VSync & ProMotion Refresh Invariant

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Free-running coroutine render loop bypassing CADisplayLink and causing battery drainage and frame drops

## System Constraints
You are a Principal Software Architect enforcing hardware-synchronized rendering constraints for Compose Multiplatform on iOS. You must mandate that all Skiko rendering passes are driven strictly through native CADisplayLink callbacks rather than decoupled timer loops or unconstrained coroutine dispatches. Ensure dynamic CAFrameRateRange configurations to honor ProMotion 120Hz refresh budgets while properly halting rendering when views are obscured or idle to avoid device overheating.

## Strict Guidelines
- Engineers MUST ALWAYS bind Skiko draw dispatches directly to CADisplayLink hardware ticks added to NSRunLoop.mainRunLoop in NSRunLoopCommonModes.
- Engineers MUST configure preferredFrameRateRange via CAFrameRateRange with minimum, preferred, and maximum limits (e.g., 60-120-120 Hz) on ProMotion-capable iOS targets.
- Code MUST set CADisplayLink.paused to true immediately when the Compose UI view is off-screen, minimized, or in an idle steady-state with no active animations.
- Services MUST NOT employ fixed-delay Kotlin coroutine loops (e.g., delay(16)) or NSTimer instances to trigger redraw requests or Compose canvas re-renders.
- Components NEVER trigger immediate synchronous Metal draw calls outside the CADisplayLink callback boundary.

## Bad vs Good Implementations
### Anti-Pattern: Decoupled Polling Loop Causing Overheating

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Driving frames with a raw delay() ignores display hardware clock phase, causing frame pacing mismatch, severe visual tearing, missed refresh deadlines, and excessive CPU/GPU thermal dissipation.
```kotlin
// Anti-Pattern: Unconstrained frame loop in Kotlin coroutine ignores VSync and ProMotion
import kotlinx.coroutines.*

class UnboundedRenderDriver(private val renderer: SkikoRenderer) {
    private val scope = CoroutineScope(Dispatchers.Main)
    private var running = false

    fun start() {
        running = true
        scope.launch {
            while (running) {
                // Blindly spins without hardware VSync phase awareness
                renderer.drawFrame()
                delay(8) // Attempts naive ~120fps emulation, causing thermal throttling
            }
        }
    }

    fun stop() {
        running = false
    }
}
```

#### Production Standard (Correct)
> **Standard**: Synchronizing with CADisplayLink locks draws directly to the hardware display controller, leverages CAFrameRateRange for ProMotion, avoids tearing, and eliminates redundant CPU spin-cycles by pausing when idle.
```kotlin
// Production Guardrail: Native CADisplayLink integration respecting VSync & ProMotion
import platform.QuartzCore.CADisplayLink
import platform.QuartzCore.CAFrameRateRangeMake
import platform.Foundation.NSRunLoop
import platform.Foundation.NSRunLoopCommonModes
import platform.darwin.NSObject
import kotlinx.cinterop.ObjCAction

class HardwareVsyncRenderDriver(
    private val renderer: SkikoRenderer
) : NSObject() {
    private var displayLink: CADisplayLink? = null

    fun start() {
        if (displayLink != null) return
        val link = CADisplayLink.displayLinkWithTarget(this, sel_registerName("onVsync:"))
        
        // Explicitly declare ProMotion frame budget for smooth 120Hz pacing
        link.preferredFrameRateRange = CAFrameRateRangeMake(minimum = 60.0f, preferred = 120.0f, maximum = 120.0f)
        link.addToRunLoop(NSRunLoop.mainRunLoop, NSRunLoopCommonModes)
        displayLink = link
    }

    @ObjCAction
    fun onVsync(link: CADisplayLink) {
        if (renderer.needsRedraw()) {
            renderer.drawFrame()
        } else {
            // Pause hardware link during idle states to conserve battery
            link.paused = true
        }
    }

    fun requestRedraw() {
        displayLink?.paused = false
    }

    fun dispose() {
        displayLink?.invalidate()
        displayLink = null
    }
}
```

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: Compose Multiplatform iOS CADisplayLink VSync & ProMotion Refresh Invariant

Compose Multiplatform iOS CADisplayLink VSync & ProMotion Refresh Invariant defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic synchronization between the Skiko rendering pipeline and iOS native CADisplayLink. Prevents thermal throttling, visual tearing, and micro-stutter by binding draw passes to hardware VSync signals and dynamically configuring CAFrameRateRange for ProMotion displays. 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 coroutine render loop bypassing CADisplayLink and causing battery drainage and frame drops 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 coroutine render loop bypassing CADisplayLink and causing battery drainage and frame dropsCritical
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 bind Skiko draw dispatches directly to CADisplayLink hardware ticks added to NSRunLoop.mainRunLoop in NSRunLoopCommonModes.

Engineers MUST ALWAYS bind Skiko draw dispatches directly to CADisplayLink hardware ticks added to NSRunLoop.mainRunLoop in NSRunLoopCommonModes.

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 configure preferredFrameRateRange via CAFrameRateRange with minimum, preferred, and maximum limits (e.g., 60-120-120 Hz) on ProMotion-capable iOS targets.

Engineers MUST configure preferredFrameRateRange via CAFrameRateRange with minimum, preferred, and maximum limits (e.g., 60-120-120 Hz) on ProMotion-capable iOS targets.

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 set CADisplayLink.paused to true immediately when the Compose UI view is off-screen, minimized, or in an idle steady-state with no active animations.

Code MUST set CADisplayLink.paused to true immediately when the Compose UI view is off-screen, minimized, or in an idle steady-state with no active animations.

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 NOT employ fixed-delay Kotlin coroutine loops (e.g., delay(16)) or NSTimer instances to trigger redraw requests or Compose canvas re-renders.

Services MUST NOT employ fixed-delay Kotlin coroutine loops (e.g., delay(16)) or NSTimer instances to trigger redraw requests or Compose canvas re-renders.

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 trigger immediate synchronous Metal draw calls outside the CADisplayLink callback boundary.

Components NEVER trigger immediate synchronous Metal draw calls outside the CADisplayLink callback boundary.

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 iOS CADisplayLink VSync & ProMotion Refresh Invariant critical for AI coding assistants?

Enforces deterministic synchronization between the Skiko rendering pipeline and iOS native CADisplayLink. Prevents thermal throttling, visual tearing, and micro-stutter by binding draw passes to hardware VSync signals and dynamically configuring CAFrameRateRange for ProMotion displays. 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 coroutine render loop bypassing CADisplayLink and causing battery drainage and frame drops) 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