Skip to content

fix(macos): stop per-frame environment refreshes and camera probes - #2821

Merged
houko merged 1 commit into
mainfrom
fix/macos-env-refresh-2813
Dec 1, 2026
Merged

houko merged 1 commit into
mainfrom
fix/macos-env-refresh-2813

Conversation

@houko

@houko houko commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

On macOS, every environment refresh asked AVFoundation for the default camera twice, and a display brightness ramp produced a new environment on every frame because the display snapshot's HDR headroom steps with the brightness. This change:

  • Probes the camera once, on first use, and keeps it current from AVCaptureDeviceWasConnectedNotification / AVCaptureDeviceWasDisconnectedNotification; a change of availability requests a refresh of every surface (internal/platform/macos/camera.go).
  • Coalesces the refreshes that storm-prone notifications request into one per main-queue turn: viewDidChangeBackingProperties, NSWindowDidChangeScreenNotification, NSWindowDidChangeScreenProfileNotification and NSApplicationDidChangeScreenParametersNotification. A direct refresh() in between satisfies a waiting request. Other notifications (resize, move, full screen, key, activation) still refresh synchronously, as their handlers depend on the order.
  • Rate-limits environments that change only displays' HDRHeadroom to one per 250 ms. A held change schedules one trailing refresh at the end of the interval, so the value the display settles at is always published. Any other change, including entering or leaving HDR, goes through at once (internal/platform/macos/refresh.go).

Which notification drives the per-frame refresh during a ramp could not be confirmed without a display to ramp; all candidates named in the issue are now coalesced, and the headroom gate limits the updates whichever one fires.

Other backends: iOS already caches camera availability (cameraAvailable, a sync.OnceValue over UIImagePickerController) and refreshes only on discrete UIKit notifications, so it needs no change. Windows and Wayland Linux report a headroom derived from static display luminance values, not a brightness-tracking current value.

Fixes #2813

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

None

Verification

Tests were written first. Before the fix (probe seam only, old behavior):

  ✗ TestEnvironmentDoesNotProbeTheCameraPerCall (1.92s)
      camera_test.go:33: 10 environments probed the camera 20 times, want at most once
  0 passed, 1 failed, 0 skipped

The coalescer, headroom gate and camera watch tests did not build before (undefined: cameraWatch, coalescer, headroomOnly, headroomGate). After:

  ✓ TestEnvironmentDoesNotProbeTheCameraPerCall (280ms)
  ✓ TestCameraWatchProbesOnceAndFollowsDevices (0s)
  ✓ TestCoalescerRunsOncePerTurn (0s)
  ✓ TestHeadroomOnlyChange (0s)
  ✓ TestHeadroomGateLimitsARamp (0s)
  5 passed, 0 failed, 0 skipped

TestHeadroomGateLimitsARamp steps the headroom every frame at 120 Hz for one second and requires at most 5 published environments and the settled value published at the end.

The full internal/platform/macos run fails only TestInputMethodCommitAndSinkCandidates ("timed out waiting for test window active and key"), which needs a window session; it fails identically on main at fd02adf on the same machine (main: 155 passed, 1 failed; this branch: 160 passed, 1 failed).

  • head: e26a142217a8912e2dc0664ba9c43e226f77da22
  • base: fd02adf8ed86177303d3db4d5a3ee119690c8175
  • commands:
    • go tool nsgo check -target macos,ios → 0
    • go tool nsgo test ./internal/platform/apple/ → 0
    • go tool nsgo test ./internal/platform/macos/ → 1 (only TestInputMethodCommitAndSinkCandidates, which needs an active key window and fails the same way on the base commit)
    • go tool nsgo test ./internal/moduletest/ → 0
    • go run ./tools/changelog check → 0
  • result: fail
  • not covered: run over SSH on a Mac with no logged-in window session, so window-activation tests cannot pass there; no real brightness ramp or camera hot-plug was exercised; iOS was vetted, not run; other targets are left to CI

Checklist

  • The title is a Conventional Commit, such as fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.
  • Docs whose facts change are updated, and a capability users can see has a line in a changelog.d/ fragment, not in CHANGELOG.md.
  • New third-party code, assets and dependencies name their source and license.

Probe the camera once and follow AVCaptureDevice connect and disconnect notifications instead of running AVFoundation device discovery twice on every environment refresh. Coalesce the refreshes that display parameter, screen, screen profile and backing scale notifications request in one run loop turn, and rate-limit environments that change only displays' HDR headroom to four a second, with a trailing refresh that publishes the settled value.

Fixes #2813
@github-actions github-actions Bot added the local-ci-fail The head commit declares failing local checks, so a maintainer audits it label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

The Local CI block declares fail for this pull request's head commit, so a maintainer audits the pull request before merging it.

@github-actions github-actions Bot added documentation Improvements or additions to documentation macOS bug Something isn't working size/M labels Dec 1, 2026
@houko
houko merged commit adc5584 into main Dec 1, 2026
48 checks passed
@houko
houko deleted the fix/macos-env-refresh-2813 branch December 1, 2026 01:39
houko added a commit to nsgo-dev/orbit-academy-game that referenced this pull request Dec 1, 2026
…ayer main

Move to nsgo main (8e5c1cc), which opens the main window maximized from its first frame (nsgo-dev/nsgo#2822) and brings the environment refresh (nsgo-dev/nsgo#2821) and sprite-frame crop cache (nsgo-dev/nsgo#2817) fixes, and to the game layer main (be78552), which builds against it (nsgo-dev/game#2723). Replace maximizeOnce, which waited for the first window frame to toggle maximize, with nsgo.WindowMaximized in main.go, and migrate the one numeric FlexShrink to core.Shrink.
houko added a commit to nsgo-dev/orbit-academy-game that referenced this pull request Dec 1, 2026
…ayer main (#5)

Move to nsgo main (8e5c1cc), which opens the main window maximized from its first frame (nsgo-dev/nsgo#2822) and brings the environment refresh (nsgo-dev/nsgo#2821) and sprite-frame crop cache (nsgo-dev/nsgo#2817) fixes, and to the game layer main (be78552), which builds against it (nsgo-dev/game#2723). Replace maximizeOnce, which waited for the first window frame to toggle maximize, with nsgo.WindowMaximized in main.go, and migrate the one numeric FlexShrink to core.Shrink.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation local-ci-fail The head commit declares failing local checks, so a maintainer audits it macOS size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: environment refresh queries the camera on every call and runs once per frame during brightness ramps

1 participant