Skip to content

iOS simulator stream delivers each app frame once, evenly, at --fps 60 #1751

Closed
3 of 3 issues completed
Closed

Description

@V3RON

Request: #1551

Problem

An operator watches a leased iPhone simulator through the screen stream at --fps 60. The app draws 60 frames a second, and the simulator composites 100 or more a second with Liquid Glass. Yet the stream shows only 55-57 distinct app frames a second, and the gap between them is often 25-27 ms instead of 16.7 ms (p95), so scrolling looks uneven (#1551, measured by the reporter on an x86 VM).

Three causes are left after #1718 fixed the throttle (cause 2):

  1. SimulatorKit's frame callback is sometimes late: it runs only after the next composite has replaced the picture, so the picture in between is never copied.
  2. Every new composite is copied before the stream decides whether to send it, and about four in ten copies are then thrown away.
  3. Liquid Glass changes part of the screen on every composite, so every composite looks new. Sampling 60 a second out of an irregular ~100 a second repeats some app frames and skips others.

Who it is for

Operators who watch and drive a leased iPhone simulator in a device panel (the console or an IDE) while coding agents build and test apps on it, at stream.fps 60.

Outcome

At --fps 60, with an app drawing 60 or more frames a second, the stream shows each app frame once, at even gaps, and the capture costs no more CPU than today. Streams at a lower --fps, still screens and physical iPhones behave as today or better.

How it works for the user

flowchart LR
  A[App draws a frame] --> B[Simulator composites it]
  B --> C{Capture notices a new seed: callback or 1 ms check}
  C --> D[Tiles hashed, history updated]
  D --> E{Due on the 60 fps grid?}
  E -- no --> X[not copied]
  E -- yes --> F{Carries a new app frame?}
  F -- "no, only glass changed" --> X
  F -- yes --> G[copied, encoded, sent]
  G --> H[Operator sees every app frame once]
Loading

Nothing the user types changes. Before and after, the console's Screen tab and GET /v1/leases/{id}/stream take the same settings. Only what arrives is different:

At --fps 60, app drawing at 60 Today (reporter's VM) After
Distinct app frames a second 55-57 58 or more
p95 gap between frames 25-27 ms 20 ms or less
Capture CPU as measured on main no higher

Examples

Which composites the capture copies, at --fps 60 (interval 16.7 ms):

Composite Today After
Seed unchanged skipped skipped
New seed, 5 ms after the last frame sent copied, then dropped by the grid not copied
New seed, due, a steady tile changed copied, sent copied, sent
New seed, due, only always-changing tiles changed, 10 ms after the composite before copied, sent (a repeat) not copied; the grid's slot stays open
Same, but 13 ms (3/4 interval or more) after the composite before copied, sent copied, sent
Same, but 26 ms (1.5 intervals or more) since the last frame sent copied, sent copied, sent
Two composites between two callbacks the first one lost noticed by the 1 ms check
Still screen, 200 ms since the last copy (idle floor) copied, sent copied, sent
Panel switch on a foldable, under one interval after the last frame sent copied, dropped by the grid copied, dropped by the grid
First composite of a stream, or a new picture size copied, sent copied, sent

What could go wrong

  • The stream could skip a real app frame when it changes only tiles that changed on all of the last 8 composites. It shows as a tiny stutter, at most 1.5 frame intervals long. Likely only for small animations under the glass.
  • The 1 ms seed check could cost CPU on a host with many simulators. The reporter measured the opposite (about half the CPU), on one VM only.
  • A host too slow to copy every frame in time still drops frames, as today: the copy then takes the newest composite, not each one. The tile history then has gaps, so a scrolling area can look always-changing and its frames come through the 1.5-interval rule instead: uneven, but no worse than today.

Words used

  • composite: one picture the simulator puts on its screen surface. With Liquid Glass there can be 100 or more a second while the app draws 60.
  • seed: a number on the screen surface that changes every time a composite replaces it (IOSurfaceGetSeed). Same seed = same picture.
  • seen time: the moment the capture noticed a composite's new seed, before any copy. Every decision about a frame uses it.
  • grid: the stream's pacing from fix(capture): a source of at least --fps is written at --fps #1718. A frame is due one interval (16.7 ms at 60 fps) after the last one sent, and may come a quarter interval early.
  • tile: a 16x32-pixel block of the screen, hashed on every 4th row to tell which parts changed between two composites.
  • tile history: whether each tile changed, on each of the last 8 composites the copy queue took.
  • always-changing tile: changed on all 8 composites of the history, like the glass.
  • sometimes-changing tile: changed on 1 to 7 of them, like a scrolling list at 60 among ~100 composites.
  • steady tile: changed on none of them.
  • idle floor: the capture re-sends the current picture every 200 ms on a still screen, so a viewer that joins late gets one.

Non-goals

Completion conditions

Open questions

Decisions

  • No ADR: every change stays inside the simulator capture program, and none of it constrains another module or is hard to undo.
  • Decided in the session (maintainer): the reporter's VM numbers are trusted; there are no runs before the design.
  • Decided in the session (maintainer): tile selection is on for everyone, with no option.
  • Decided in the session (maintainer): a 1 ms seed check runs beside the callback, both on.
  • Decided in the session (maintainer): the done numbers are measured once, by delivery, with a counter app and reader kept in vendor/screen-capture/scripts/, not as a slow-lane test.
  • Decided in the session: the grid runs before the copy (cause 3), because it keeps the same frames and only saves work.
  • Decided in the session: the grid is asked whether a frame is due without moving, and moves only for a frame that is copied, because a slot spent on a skipped composite would drop the app frame right after it.
  • Decided in the session: the capture notices composites on one queue and copies on another, because a 1 ms check cannot run while a 10-20 ms copy holds the queue.
  • Decided in the session: tile history is 8 composites long, because that covers more than one app frame at 100+ composites a second, and it tells glass (every composite) from scrolling (most).
  • Decided in the session: NV12, the alive tag and the trace are out of scope, because they do not change which frames are sent.
  • Decided in the session: the new logic lives in a file of ours (FrameSelection.swift, MIT), and FrameCapture.swift gets a patch that calls it, because a smaller patch keeps the diff against serve-sim readable.
  • Decided in the session: a frame is stamped with its seen time, not after its copy, because the copy takes 10-20 ms and varies, and the grid would otherwise drop frames that were due.
  • Decided in the session: the counter app's build output is never committed; the measurement builds it with xcodebuild into a temporary folder, because the repo refuses binaries under vendor/.

Behaviours

Id Behaviour (invoker, action, what it sees) Seam Cases Task
B1 The encode loop keeps frames on the grid through FrameGrid (I1), with #1718's rule; asking isDue does not move it. FrameGrid and encodeLoop (SimlockCaptureTests) the four #1718 cases in EncodeLoopTests.testAt60FpsASourceOfAtLeast60FramesASecondIsWrittenAt60FramesASecond still pass; isDue asked twice gives the same answer and moves nothing; commit then isDue 5 ms later: not due #1752
B2 The simulator capture decides before it copies (FrameSelection.decide, I7): a composite the grid would drop is not copied, and the grid moves only for a copied one. A forced composite (idle floor, panel switch) is always copied, and moves the grid only when it was due. FrameSelection (SimlockCaptureTests), no tiles 100/s for 2 s at fps 60: 120 copied; 30/s at fps 60: 60 copied; a forced composite 200 ms after the last: copied, grid moves; a forced composite 5 ms after the last: copied, grid does not move; a new picture size: copied #1752
B3 Every frame the capture copies is written by the encode loop, because both judge the same seen time. pending(_:seenNanos:) (I5) and FrameSelection output fed to encodeLoop pending called later than the seen time keeps the seen time; the B2 cases: frames written equal frames copied, except the forced one under one interval, which the encode loop drops #1752
B4 The capture notices each new seed once, whether the callback or the 1 ms check sees it first, and stamps it then. SeedWatch.tick and SeedGate (I3) with a fake seed reader and clock (SimlockCaptureTests) two seeds between two callbacks: both noticed, each with its own time; callback and tick see the same seed: one notice; seed unchanged: none; a forced call with an unchanged seed: one notice; after stop(): tick does nothing; the watched surface replaced (watch(surface)): the next tick reads the new one #1753
B5 A due composite is copied only when it carries a new app frame: a tile that did not change on the composite before (the last one the copy queue took) changed (always-changing tiles aside), or at least half of the sometimes-changing tiles changed. It may be skipped only when it came under 3/4 interval after that composite before; it is copied anyway at 1.5 intervals or more since the last frame sent. Every composite the copy queue takes updates the history, due or not (one replaced in the hand-off before it is taken is never hashed). TileSelector (I2) and FrameSelection with tiles (SimlockCaptureTests) only always-changing tiles changed, 10 ms after the one before: skipped, grid slot open, and the next composite with a steady tile change 4 ms later: copied; a steady tile changed: copied; half the sometimes-changing tiles changed, none new: copied; under half: skipped; only always-changing changed, 13 ms after the one before: copied; 26 ms since the last sent: copied; no tile changed at all, 5 ms after: skipped; first composite: copied; new picture size: copied and history cleared; forced: copied; a scroll pattern (a block of tiles changing on 3 of every 5 composites, glass tiles on every one): every composite with a block change that is due is copied #1754
B6 Delivery measures the stream with the counter app and the reader (I4): the capture built at fb4267632, then at #1754's head. the reader against a simulator on this Mac (hardware, run once) scroll and animation scenarios at fps 60: distinct app frames a second, p95 gap, CPU; ffmpeg missing: exits 1 with ffmpeg not found #1754

Stored state

none

Interfaces

Id What Shape and bounds Errors and limits Where Task Used by
I1 FrameGrid struct FrameGrid { init(fps: Int); func isDue(atNanos: UInt64) -> Bool; mutating func commit(atNanos: UInt64); mutating func admit(atNanos: UInt64) -> Bool }. fps 1-60 as Options.fps. #1718's rule: the first frame is due; then due when atNanos + interval/4 >= deadline; commit sets the deadline to atNanos + interval for the first frame, else max(deadline + interval, atNanos + interval/2). admit is isDue, then commit when due. none SimlockCapture/FrameGrid.swift (ours, public) #1752 #1752 (encode loop, I7), #1754
I2 TileSelector struct TileSelector { init(intervalNanos: UInt64); mutating func carriesNewFrame(tiles: [UInt64], seenNanos: UInt64, lastSentNanos: UInt64?) -> Bool; mutating func reset() }. It is called for every composite the copy queue takes (I8), keeps the tiles and seen time of the last composite it was called for, and the 8-composite history (Words used), and answers B5's rule; with no previous composite it answers true. A tile count that differs from the previous one resets it. none simlock-screen-capture/FrameSelection.swift (ours) #1754 #1754 (I7)
I3 SeedWatch and SeedGate SeedGate { mutating func notice(key: ObjectIdentifier, seed: UInt32, force: Bool) -> Bool }, which replaces lastSeeds (true for a new seed, or when forced); final class SeedWatch { init(queue: DispatchQueue, readSeed: @escaping (IOSurface) -> UInt32 = IOSurfaceGetSeed, now: @escaping () -> UInt64, onSeed: @escaping (IOSurface, UInt32, UInt64) -> Void); func watch(_ surface: IOSurface?); func start(); func stop(); func tick() }. start runs tick from a DispatchSourceTimer every 1 ms (leeway 0.5 ms) on queue; tick reads the watched surface's seed and calls onSeed when it differs from the last one read. The surface is the one captureFrame last picked, set through watch on every pick (callback, surfaces-changed callback, idle tick, panel switch, rewire). none simlock-screen-capture/FrameSelection.swift (ours), started in FrameCapture.start, stopped in stop() #1753 #1753
I4 Counter app and reader The app: vendor/screen-capture/scripts/fps-counter/ holds an Xcode project of a SwiftUI iPhone app that draws a 16-bit counter on every display-link tick as 16 black or white blocks in one row just below the top safe area, with the status bar hidden, and runs one scenario by itself, from its launch argument: scroll (a list that scrolls up and down without end) or animation (one small moving square). node vendor/screen-capture/scripts/fps-reader.mjs --udid <UDID> --scenario scroll|animation --bin <path to simlock-screen-capture> [--seconds 20] builds the app with xcodebuild -sdk iphonesimulator into a temporary folder, installs and launches it with xcrun simctl, runs <bin> <UDID> --fps 60 for the given seconds, reads its framed stdout as unframe.mjs does, decodes it with ffmpeg, counts distinct counter values, samples the program's CPU with ps once a second, and prints distinct/s=<n> p95=<ms> cpu=<percent>. A --bin for the before run is built from a worktree at fb4267632 with swift build -c release --package-path vendor/screen-capture. exits 1 with ffmpeg not found when ffmpeg is not on PATH vendor/screen-capture/scripts/ (ours); no built .app is committed #1754 #1754
I5 ScreenSource.start onFrame @Sendable (CVPixelBuffer, UInt64) -> Void: the picture and its seen time in DispatchTime.now().uptimeNanoseconds. runCapture's onFrame calls func pending(_ buffer: CVPixelBuffer, seenNanos: UInt64) -> Pending, which keeps that time, so a test reaches the line. The iPhone source passes the time at emit and at the 200 ms re-send. none SimlockCapture/Options.swift:79 #1752 #1752
I6 SimulatorSource(fps:) and FrameCapture.start(deviceUDID:fps:onFrame:) fps from Options.fps, passed by main.swift to SimulatorSource, then to FrameCapture.start, which builds the FrameSelection; onFrame becomes (CVPixelBuffer, UInt64) -> Void, the seen time replacing today's frame-count CMTime none simlock-screen-capture #1752 #1753, #1754
I7 FrameSelection struct Composite { seenNanos: UInt64; force: Bool; width: Int; height: Int; tiles: [UInt64]? }; struct FrameSelection { init(fps: Int); mutating func decide(_ c: Composite) -> Bool }. Order: a new size resets the tiles and counts as forced; with tiles, TileSelector.carriesNewFrame is asked first, so history sees every composite the copy queue takes; forced: true, and commit when isDue; not isDue: false; tiles say no: false; else commit, true. #1752 passes tiles: nil; #1754 passes TileHasher.hashes(of:). none simlock-screen-capture/FrameSelection.swift (ours) #1752 #1754
I8 Notice and copy queues in FrameCapture The callbacks and the idle timer (and SeedWatch from #1753) run on the existing frame-capture queue and only notice: pick the surface, the seed check (today's lastSeeds, SeedGate from #1753), stamp the seen time, and put (surface, seed, seen, force) in a one-slot hand-off (a newer one replaces one not yet taken). A serial frame-copy queue takes it; when the surface's seed is no longer that seed it drops it (the newer one has its own notice); else it hashes the tiles (#1754), asks FrameSelection.decide, copies with Photocopier and calls onFrame(copy, seen). stop() drops a hand-off not yet taken, and the copy queue does nothing after stop(). none simlock-screen-capture/FrameCapture.swift #1752 #1753, #1754
I9 TileHasher.hashes(of:) static func hashes(of surface: IOSurface) -> [UInt64]: locks the surface read-only, one FNV-1a hash per 16x32 tile over every 4th row, row-major, edge tiles partial none simlock-screen-capture/FrameSelection.swift (ours) #1754 #1754

Existing code

Commit fb42676.

Searches: photocopier.copy (1), atNanos in Sources/ (7), onFrame in Sources/ (14), IOSurfaceGetSeed (1), registerScreenCallbacks (3), lastSeeds (5), pickBestDescriptor (4), --fps in src/ and vendor/ (3), runCapture in Tests/ (0).

Id Code (path:line) Today With this feature Handled by
X1 vendor/screen-capture/Sources/SimlockCapture/Lifecycle.swift:226-235 the encode loop's grid, inline FrameGrid.admit (I1), same rule B1, #1752
X2 vendor/screen-capture/Sources/SimlockCapture/Lifecycle.swift:411 stamps Pending.atNanos when onFrame is called, after the copy calls pending(_:seenNanos:) (I5), which a test reaches although runCapture returns Never B3, #1752
X3 vendor/screen-capture/Sources/simlock-screen-capture/FrameCapture.swift:264-305 captureFrame on the callback queue: picks the surface (:265), seed skip (:276-280), then photocopier.copy (:303) for every new seed, onFrame with a frame-count CMTime (:302) split: notice on frame-capture, decide and copy on frame-copy (I8) B2, B4, B5, #1752, #1754
X4 vendor/screen-capture/Sources/simlock-screen-capture/FrameCapture.swift:227-228 the frame and surfaces-changed callbacks call captureFrame() unchanged; they and SeedWatch reach the notice step B4, #1753
X5 vendor/screen-capture/Sources/simlock-screen-capture/FrameCapture.swift:243-257, :312, :339-347, :112 idle tick (forced, and rewire every ~1 s while no frame), stop(), panel switch (forced), rewire resets lastSeeds SeedGate replaces lastSeeds, and rewire resets it; each pick calls SeedWatch.watch; stop() stops SeedWatch (dropping the hand-off is I8's, #1752) B4, #1753
X6 vendor/screen-capture/Sources/simlock-screen-capture/SimulatorSource.swift:30-40 ignores FrameCapture's CMTime, rotates a Duo panel's picture, calls onFrame(buffer) passes the seen time on, after the rotation (I5) B3, #1752
X7 vendor/screen-capture/Sources/simlock-screen-capture/main.swift:56 SimulatorSource() SimulatorSource(fps: options.fps) (I6) #1752
X8 vendor/screen-capture/Sources/simlock-device-capture/DeviceScreenSource.swift:97, :333, :579, :595, :652, :684 the iPhone source's onFrame type and its two calls: the 200 ms re-send (keepAlive) and emit the type gains the time; both calls pass DispatchTime.now().uptimeNanoseconds, the same stamp runCapture takes today, so the iPhone stream sends the same frames I5, #1752
X9 vendor/screen-capture/NOTICE.md:53 and docs/internal/VENDORED-CODE.md serve-sim row FrameCapture.swift is "theirs, one patch" "modified by us", with a MODIFIED FILE header naming the notice/copy split and SeedWatch; FrameGrid.swift and FrameSelection.swift listed as ours #1752, #1753
X10 vendor/screen-capture/Sources/SimlockCapture/PixelBufferUtils.swift:75-101 Photocopier.copy: row-by-row memcpy under lock unchanged, now on frame-copy I8, #1752
X11 src/source-hygiene.repo.test.ts:73-103 refuses binaries under vendor/ the counter app is committed as source only I4, #1754
X12 src/drivers/ios/screen-capture.ts:97-98 passes --fps from stream.fps (1-60, default 30, src/stream/definition.ts:29-33) unchanged —

Tasks

flowchart LR
  A["#1752 grid before the copy, seen time"] --> B["#1753 1 ms seed check"] --> C["#1754 tile selection + measurement"]
Loading

Completion conditions: the measurement line is #1754's hardware Done when line; the swift test line is in every task's Done when.

Open work in the same area: #1583 (ROCKit fault storm and multi-second stalls on the iOS simulator stream, bug:new) shares the capture program but no function this feature changes; no Depends on. #1551 stays open until this feature ships.

Written by an agent.

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    feature:spec
    on Nov 19, 2026
  2. V3RON commented on Nov 19, 2026

    @V3RON
    ContributorAuthor

    Design posted: business sections, Decisions, Behaviours B1-B6, Interfaces I1-I6, Existing code X1-X10.

    Written by an agent.

  3. V3RON commented on Nov 19, 2026

    @V3RON
    ContributorAuthor

    Spec updated: tasks #1752, #1753, #1754 written; task ids in the rows replaced by their numbers; Tasks section filled.

    Written by an agent.

  4. added
    feature:planned
    and removed
    feature:spec
    on Nov 19, 2026
  5. V3RON commented on Nov 19, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: none (the stack #1769 → #1771 → #1773 stays open; #1769 and #1771 passed review and are ready)
    Parked: #1754 — hardware loop gave up: on this Mac the after medians are level with before or slightly worse (scroll p95 23.0 vs 22.4 ms, cpu +0.3/+0.4 points), and the before build already reaches about 60 distinct/s
    Waiting: none
    Decided: #1754 — 1 ms seed check vs the CPU line → CPU line kept, SeedWatch removed (undoes #1753's seed check); #1754 — p95 where before is already over 20 ms → "no higher than before" on three-run medians

    Written by an agent.

  6. github-actions commented on Nov 19, 2026

    @github-actions
    Contributor

    Closing: all 3 sub-issues are closed. The PR that closed the last one walks the completion conditions (delivery rule 9); reopen this if one does not hold on main. Flip its ADRs to Accepted if delivery has not.

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

    feature:planned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions