Occlude and reclaim terminal renderers when the hosting window is hidden

Merged
LA
3mo ago
+338-11

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:

  • GhosttyNSView observes NSWindow.didChangeOcclusionStateNotification for its current window (rebinding in viewDidMoveToWindow; 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).
  • TerminalSurface gains rendererWindowVisible; occlusion, presentation, and releaseRenderer() 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.
  • RendererRealizationController feeds 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, TerminalSurfaceTeardownCallbackLifetimeTests pass (27 tests).
  • Note: TerminalSurfaceRuntimeTeardownFenceTests.swift fails to compile under swift test with Swift 6.2.4/6.3.3 at current main (pre-existing sending diagnostic, 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

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.

statemainthis PRdelta
windows covered (steady)547 MB303 MB-244 MB (-45%)
app hidden (cmd+H, +15s)546 MB320 MB-226 MB
windows restored on screen654 MB615 MBparity

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.