Harden Metal presentation lifetimes

X9
3mo ago
+375-85

Fixes verified replay completion safety. Adds renderer lifetime invalidation, generation-based stale-frame rejection, and moves synchronous callback delivery outside the draw mutex while preserving current swap-chain drain and iOS queued behavior.\n\nVerified: zig build test -Drenderer=metal passes.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Hardens Metal presentation lifetimes to prevent stale or late GPU completions from mutating UI or renderer state after resize or teardown. Adds generation-based validation and moves sync presentation callback delivery outside the draw mutex while preserving swap-chain drain and iOS’s queued behavior.

  • Bug Fixes
    • Generation-tracked IOSurfaceLayer lifetime rejects stale frames and cancels after teardown.
    • Surface size is clamped and observed; presentation_generation is passed through frame/present paths.
    • setSurfaceSync validates generation and size; returns false instead of touching invalid state.
    • Sync presentation returns the callback to the caller; delivery occurs after draw_mutex unlocks (async and iOS unchanged).
    • Added tests for lifetime invalidation and stale-generation rejection.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Metal presentation reliability by preventing stale or invalid frame updates from reaching the display.
    • Prevented queued presentation callbacks from affecting detached or inactive surfaces.
    • Added safeguards to reject surfaces whose dimensions do not match the current drawable area.
    • Clamped runtime surface sizes to supported device texture limits.
    • Improved synchronization for frame completion and presentation updates to reduce visual glitches during resizing or rapid frame changes.