Skip to content

fix(macos,render): shrink hidden Metal drawables to 1x1 and drop retained damage on unseen rest - #2687

Merged
Hona merged 1 commit into
devfrom
fix/161-hidden-window-drawables
Nov 28, 2026
Merged

Hona merged 1 commit into
devfrom
fix/161-hidden-window-drawables

Conversation

@Hona

@Hona Hona commented Nov 28, 2026

Copy link
Copy Markdown
Member

Addresses #161

Summary

Fresh macOS footprint -v measurements on dev (dacf5d89, #161 (comment)) showed that image textures and upload staging already free on hide, leaving two GPU-side retention sources on hidden windows and overlays:

  1. Idle full-size CAMetalLayer drawables (34 MB across hidden main window, region overlay, and HUD): Nothing changed CAMetalLayer.drawableSize on hide, so QuartzCore kept two (up to maximumDrawableCount) full-size IOSurface drawables per hidden window and overlay.
  2. Full-window retained partial-damage texture: Renderer::give_back(true, ...) cleared scratch layers, image textures, backdrop copies, and blur pyramids, but kept DamageState::retained (a full-window GPU texture, e.g. 12.8 MB at 2200×1520 on an opaque main window) indefinitely while hidden or covered.

Changes

  • Hidden CAMetalLayer drawable shrink/restore (macOS):
    • Added explicit HiddenDrawable state machine (Full / Shrunk) and window/overlay shrink policies in crates/rune/src/window/surface/hidden_drawable.rs (with unit tests in hidden_drawable_tests.rs).
    • Added platform::macos::set_drawable_size(surface, width, height) via gpu::with_layer so both Rune's native Metal backend and wgpu's Metal backend update CAMetalLayer.drawableSize.
    • Wired shrink-on-hide (excluding launchers, release_gpu_when_closed, fading overlays, and Release::Now) and restore-before-show / restore-before-acquire into Win (Host::hide_window, Win::show, WindowFrame::acquire, plus configure resets) and OverlayCore (carry_out, show_fading, OverlayFrame::acquire, rescale, reblend_surface).
    • Added a detached CAMetalLayer regression test in crates/rune/src/platform/macos/surface_tests.rs covering shrink to 1×1, restore to full size, and clean full-size redraw on both native Metal and wgpu Metal (preserving all existing HAL counter tests untouched).
  • Retained partial-damage texture release on unseen rest (all native platforms):
    • Renderer::give_back(true, ...) now drops DamageState::retained via Renderer::release_damage_retained() while leaving visible idle rest (give_back(false, ...)) unchanged; the next armed frame catches up whole (KeptDraw::Whole) and resumes partial redraws cleanly.
    • Added GPU counter regression test give_back_unseen_drops_retained_damage_texture_and_wakes_cleanly in crates/rune/src/render/damaged_tests.rs.
  • Deferred overlay upload trim:
    • Retire::poll_due now returns whether a pending retirement finished, and OverlayCore::wake_due calls renderer.trim_uploads() once in-flight work retires while rested.
  • Added changelog/unreleased/2026-11-28-hidden-window-drawable-and-damage-reclaim.md.

Verification

  • Verified locally (Windows, --features rune/test-gpu-counters,rune-gpu/test-gpu-counters):
    • cargo check -p rune --lib: passed with 0 errors, 0 warnings in touched files.
    • cargo test -p rune --features rune/test-gpu-counters,rune-gpu/test-gpu-counters --lib -- hidden_drawable give_back_unseen_drops_retained_damage_texture overlay::rest_tests retire_state_machine: 12 passed, 0 failed (verifies HiddenDrawable transitions/policies and proves give_back(true) drops the retained damage HAL texture back to baseline and wakes with pixel-exact catch-up).
    • cargo test -p rune --features rune/test-gpu-counters,rune-gpu/test-gpu-counters --lib -- window::tests::: 26 passed, 0 failed.
  • Not run locally on real hardware: macOS Metal CAMetalLayer setDrawableSize runtime test and live macOS footprint -v IOSurface / owned-unmapped measurements (needs macOS CI / hardware verification).

…ined damage on unseen rest

Addresses #161.

- Add an explicit HiddenDrawable state machine (window/surface/hidden_drawable.rs)
  and platform::macos::set_drawable_size seam via gpu::with_layer (works on both
  Rune's native Metal backend and wgpu's Metal backend).
- On macOS, shrink CAMetalLayer.drawableSize to 1x1 when a non-launcher window
  hides (Host::hide_window) or when a non-released overlay finishes hiding
  (OverlayCore::carry_out after exit fade), so Core Animation releases unused
  full-size drawables from its pool while leaving any displayed surface intact.
- Restore drawableSize to (config.width, config.height) before showing (Win::show,
  OverlayCore::show_fading) and as a guard before acquiring a drawable, and clear
  the shrunk state on surface reconfigures.
- Drop the renderer's full-window retained partial-damage texture in
  Renderer::give_back(true, ...) while keeping visible idle rest unchanged.
- Trim overlay staging uploads when OverlayCore::retire.poll_due finishes retiring
  in-flight work while rested.
@Hona
Hona merged commit 3705e45 into dev Nov 28, 2026
4 checks passed
@Hona
Hona deleted the fix/161-hidden-window-drawables branch November 28, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant