Occlude and reclaim terminal renderers when the hosting window is hidden
What
cmux drives Ghostty occlusion only from in-window portal visibility (setVisibleInUI). The visible tab of a miniaturized window, a window fully covered by other windows, or a window on an inactive Space therefore keeps a live draw cadence and a warm Metal swap chain (~40MB per surface), and RendererRealizationController never reclaims it because its portal stays "visible". Upstream Ghostty forwards NSWindow.didChangeOcclusionStateNotification to ghostty_surface_set_occlusion (BaseTerminalController.windowDidChangeOcclusionState), which core-side releases the surface's GPU resources, stops the CVDisplayLink, and drops renderer-thread QoS.
This PR ports that behavior into cmux's effective-visibility model:
GhosttyNSViewobservesNSWindow.didChangeOcclusionStateNotificationfor its current window (rebinding inviewDidMoveToWindow; a nil-window reparenting transition keeps the last state, so portal moves cannot flap occlusion, which is why the old view-level no-op existed).TerminalSurfacegainsrendererWindowVisible; occlusion, presentation, andreleaseRenderer()protection now key off portal AND window visibility (isRendererEffectivelyVisible). A window hide occludes the core surface and stamps the reclamation clock; a show lifts occlusion or replays the existing rebuild transaction if the renderer was reclaimed while hidden.RendererRealizationControllerfeeds effective visibility to the planner, so surfaces in hidden windows age out of the warm set and their swap chains are released after the idle threshold. Memory-pressure passes may now also reclaim them.- Visibility-derived occlusion requests (portal reveal, canvas viewport entry) go through
applyVisibilityOcclusion, which folds in window visibility, so an agent/socket-driven workspace switch inside a hidden window cannot un-occlude it.
Why
Ports the one memory-relevant upstream macos/ technique cmux lacked (audited against upstream main 88f57ee66e). Core-side savings per surface in a hidden window: draw loop paused, display link stopped, and with reclamation the full Metal swap chain/IOSurface freed. Multi-window and multi-Space setups benefit the most.
Verification
- New
TerminalSurfaceWindowOcclusionTests(6 tests) cover: hide occludes and unprotects the renderer, show replays presentation after reclaim, show without reclaim only lifts occlusion, hidden-at-creation defers first presentation, portal reveal inside a hidden window stays occluded, hidden portal ignores window transitions. - Existing
TerminalSurfaceRendererPresentationTests,TerminalSurfacePortalHostVacancyTests,TerminalSurfaceTeardownCallbackLifetimeTestspass (27 tests). - Note:
TerminalSurfaceRuntimeTeardownFenceTests.swiftfails to compile underswift testwith Swift 6.2.4/6.3.3 at current main (pre-existingsendingdiagnostic, unrelated to this PR); it was temporarily sidelined for the local package-test runs only. - Tagged cloud build
gwocl, dogfooded via debug socket: miniaturize window, confirm occlusion + reclamation in logs, unminiaturize, confirm content restores without flash.
Notes
- Adds
renderer_realized/renderer_presented/renderer_portal_visible/renderer_window_visibleto the debug terminals payload so the socket can verify the behavior. - Rebased onto current main; the interim compile-fix cherry-picks were dropped after https://github.com/manaflow-ai/cmux/pull/10820 and https://github.com/manaflow-ai/cmux/pull/10822 repaired main. The branch is now exactly main + 3 commits.
A/B benchmark (identical workload)
Same scripted workload on main (gwbase) and this branch (gwocl), both Debug, same GhosttyKit pin 5045df3f2: 3 windows, 3 named workspaces each, split grids, seq 1 20000 seeded in every surface, 12 surfaces total, measured with footprint after a 30s settle.
| state | main | this PR | delta |
|---|---|---|---|
| windows covered (steady) | 547 MB | 303 MB | -244 MB (-45%) |
| app hidden (cmd+H, +15s) | 546 MB | 320 MB | -226 MB |
| windows restored on screen | 654 MB | 615 MB | parity |
GPU-side rows (IOSurface + IOAccelerator + owned graphics): 291 MB on main vs 26 MB on this PR while covered. On restore the candidate re-realized exactly its 6 portal-visible surfaces (renderer_realized=6, renderer_presented=6) and returned to parity, so the reclaim is fully reversible.
Summary by CodeRabbit
-
Bug Fixes
- Improved terminal rendering when application windows are minimized, covered, hidden, or shown again.
- Deferred rendering while a window is hidden and restored presentation when it becomes visible.
- Improved renderer memory reclamation for surfaces that are not effectively visible.
- Reduced visibility flicker during terminal view reparenting.
-
Diagnostics
- Added renderer visibility and presentation details to terminal diagnostics.