Skip to content

iOS screen stream at --fps 60 delivers 55-57 distinct app frames/s while the simulator composites at 115+/s #1551

Closed

Description

@0xdaniell

The problem

At --fps 60 the iOS screen stream doesn't deliver a steady 60 frames a second, even when the simulator composites much faster. The app renders at 60, and the simulator composites at ~115-125/s with Liquid Glass. At the viewer we count 55-57 distinct app frames/s while scrolling and 56-57 on a small animation. The p95 interval is 25-27 ms instead of 16.7.

We traced the losses with a per-frame log (frame callback, copy, grid drop, encode, write). There are four causes:

  1. The frame callback is sometimes late. On a small animation the stream lost ~3 app frames a second while the capture was idle 80% of the time. A 1 ms poll of the framebuffer IOSurface's seed saw all 60.0 composites a second, each on the surface for ~17 ms (p95 17.2). SimulatorKit's frame callback (registerScreenCallbacks) sometimes runs only after the next composite has replaced the surface, and a second callback follows ~1 ms later. The frame in between is never copied.
  2. The throttle drops early frames. The encode loop drops a frame when frame.atNanos - lastSent < intervalNanos. With a source faster than --fps but not a multiple of it (87-118 fps here), every frame that arrives a little early is dropped, and the next one is often more than 1.5 intervals later. Result: 50-52 fps sent at --fps 60.
  3. The capture copies frames the throttle then drops. The callback copies (vImage) every new surface before the throttle runs. At ~94 fps in and ~60 out, about a third of the copies (~10-20 ms each here) are thrown away. That saving is estimated, not measured.
  4. Liquid Glass makes every composite unique. The glass in the lower third changes on every composite, so whole-frame dedupe never fires. A 60 fps grid over that irregular ~100/s source samples the app's frames unevenly, with repeats and skips.

Who runs into this

Coding agents and their operator. The operator watches and drives leased iPhone simulators in an IDE's device panel, through the gateway's H.264 stream, while agents build and test apps on them. Our setup is one x86 macOS 26.7 VM (paravirtual GPU, VideoToolbox hardware encoder) that serves several agents, with up to 3 iPhone 17 Pro simulators at 1206x2622.

What you would like to happen

When the app renders at 60 or more, a --fps 60 stream should deliver ~60 distinct app frames a second at even intervals (p95 near 16.7 ms), without the capture costing more CPU than it does today.

We tried the following ideas, with measured results. All rounds used one viewer and were interleaved, and we counted distinct values of a 16-bit frame counter that a test app draws on each display-link tick:

  • A grid instead of the gap since the last frame:
    • The change: a deadline that advances one interval per frame sent, accepts a frame up to a quarter interval early, and trails the clock by at most half an interval, so there's no burst after a pause.
    • Result: 57-59 fps sent from the same source, against 50-52. Sources at or under --fps pass unchanged.
    • Size: ~10 lines. We can open a PR with a unit test.
  • Check that gate before the copy: the selection stays the same, and the wasted copies from cause 3 go away.
  • Copy only composites that carry a new app frame:
    • The change: hash the framebuffer in 16x32 tiles while it's locked (every 4th row). Copy a composite only when a tile changed that didn't change on the composite before, or when at least half of the intermittently changing tiles changed. Tiles that change on every composite (the glass) are ignored.
    • Safeguards: a composite can be skipped only if it comes sooner than 3/4 of an interval after the one before. Anything 1.5 intervals after the last sent frame is copied anyway. Without the 3/4 gate, a small animation fell to 32 fps.
    • Scrolling (3 rounds): 55.2-57.0 distinct/s against 51.8-54.6 with the grid alone. p95 24.7-26.5 ms against 27.5-28.6. Repeated frames down 8x, and ~0.4 core less CPU. Small animation: 56.4-56.9 against 57.3-57.5, about the same.
    • Size: ~150 lines (Photocopier + FrameCapture). It's off by default behind an env var in our build. Would you take it behind an option?
  • Copy on the polled seed instead of waiting for the callback (cause 1):
    • Size: ~70 lines, a DispatchSourceTimer reading IOSurfaceGetSeed that then calls the existing captureFrame.
    • Small animation: 58.0-58.8 distinct/s against 56.4-57.3.
    • Scrolling: p95 21.6-23.3 ms against 25.1-26.6. Distinct frames were about the same, because 0.3-1.2 s stalls on our VM hit both arms.
    • The capture's CPU halved: 95-98% to 43-50% of a core while scrolling, and 62% to 31-33% on the small animation.
    • The trade-off: copying at the seed change waits up to ~6 ms for the GPU to finish the composite, and that wait varies. On the small animation, a later round gave more frames (59.1 against 57.7 distinct/s) but a worse p95 (25.0 against 21.8 ms), so we didn't deploy it.
    • A callback delivered on time would avoid this trade-off. Is that something SimulatorKit can be asked for, or would a seed poll be welcome?
  • Smaller items:
    • NV12 from one vImage pass instead of BGRA, so VideoToolbox doesn't convert again and the encoder gets half the bytes.
    • On a still screen, send the alive tag instead of re-encoding the same frame at 5 fps for H.264 viewers.
  • The per-frame trace itself: an env var that logs each callback (seed, copy and lock time), grid drop, encode and write, plus a small reader that splits the losses by stage. We can contribute it if it's useful.

Anything else

  • Caveats:
    • Everything was measured on one x86 VM, where callback delivery may be slower than on Apple silicon. Please check on a real Mac before acting on it.
    • The numbers come from one setup and are indicative.
    • The 0.3-1.2 s stalls are on our host's side (memory reclaim and CPU contention), not Simlock's.
  • Today's workaround: we run the grid and tile selection as a local overlay on 0.7.0. We'd rather drop the overlay and follow your releases.
  • Related: One capture per lease, shared by every viewer of its screen #602 (one capture per lease, shared by every viewer). With two viewers of one animated iPhone, each capture process copied and encoded at full resolution, and each stream fell from ~49 to ~16 fps. With one viewer it was back at 48.6.
  • We're happy to split any of this into separate issues or PRs, whichever you prefer.

Activity

  1. added
    bug:new
    on Nov 18, 2026
  2. self-assigned this
    on Nov 18, 2026
  3. V3RON commented on Nov 18, 2026

    @V3RON
    Contributor

    Reproduction

    Cause 2 (the throttle) is confirmed and reproduced. Causes 1, 3 and 4 are not in this test: they need a real simulator, and the issue itself asks a question (an option for tile selection, a seed poll). They are a feature for a spec session.

    vendor/screen-capture/Tests/SimlockCaptureTests/EncodeLoopTests.swift, testAt60FpsASourceOfAtLeast60FramesASecondIsWrittenAt60FramesASecond, on bug/1551-repro. It is worse than reported: a 60 Hz source with 16.2/17.1 ms jitter is cut to 30 fps.

    swift test --package-path vendor/screen-capture --filter 'EncodeLoopTests/testAt60'
    ("100.0") is not equal to ("120.0") +/- ("2.0") - 100 frames a second for 2 s
    ("90.0") is not equal to ("120.0") +/- ("2.0") - 90 frames a second for 2 s
    ("60.0") is not equal to ("120.0") +/- ("2.0") - 60 frames a second with jitter for 2 s
    ("75.0") is not equal to ("90.0") +/- ("2.0") - 100 frames a second, a 1 s pause, then 100 a second for 1 s
    

    Root cause

    Defect. vendor/screen-capture/Sources/SimlockCapture/Lifecycle.swift:227 drops any frame that arrives less than one interval after the last frame sent. If a frame comes even 0.1 ms early, it is dropped, and the next one sent is up to two intervals later. Both capture programs share this loop, so physical iPhones are hit too.

    Simplest fix

    The reporter's grid: a deadline that moves one interval for each frame sent. A frame up to a quarter interval early is accepted, and the deadline never falls more than half an interval behind, so a pause does not end in a burst.

    var deadline: UInt64 = 0
    ...
    if started, frame.atNanos &+ intervalNanos / 4 < deadline { continue }
    deadline = max(deadline + intervalNanos, frame.atNanos + intervalNanos / 2)

    Tried: the reproduction and all 11 Swift tests pass (swift test --package-path vendor/screen-capture).

    Alternatives rejected

    • Lower the threshold to 0.75 of an interval: lets more than --fps through from a 100/s source.
    • Grid with no catch-up clamp: bursts after a pause (the pause case fails).

    Risk

    • Short-term rate can go above --fps by a quarter interval; the average stays at or under it.
    • Not checked on a real simulator; the reporter measured 57-59 fps sent with this change.

    Fast track

    • Defect: yes
    • Fix tried: yes
    • Nothing public added or broken: yes
    • No ADR or rule changed: yes
    • One fix: no — causes 1, 3, 4 need a feature; this closes only cause 2

    Written by an agent.

  4. removed their assignment
    on Nov 18, 2026
  5. V3RON commented on Nov 18, 2026

    @V3RON
    Contributor

    Cause 2 is split into #1717 (bug:ready), with the unit-test reproduction and a real-simulator measurement: on iPhone 17 Pro / iOS 26.5 at --fps 60, the fix raised the sent rate from 36.6–37.9 to 57.7–58.5 fps from a ~58/s source. The source never went above 60 a second there, so the Liquid Glass case is covered by the unit test and your own numbers only.

    Causes 1, 3 and 4 (callback timing, copying frames the throttle drops, tile selection) stay here for a spec session.

    Written by an agent.

  6. added
    bug:ready
    and removed
    bug:new
    on Nov 19, 2026
  7. V3RON commented on Nov 19, 2026

    @V3RON
    Contributor

    Causes 1, 3 and 4 are being specified in #1751. Cause 2 shipped in #1718. This issue stays open until that feature ships.

    Written by an agent.

  8. added
    bug:blocked
    and removed
    bug:ready
    on Nov 19, 2026
  9. github-actions commented on Nov 19, 2026

    @github-actions
    Contributor

    Shipped as #1751.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug:blocked

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions