Skip to content

macos: add a headless mode, with no app or window - #714

Open
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:macos-headless
Open

benface wants to merge 1 commit into
not-fl3:masterfrom
benface:macos-headless

Conversation

@benface

@benface benface commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Why

On macOS, run always makes the process an app: it creates NSApplication, a window, and an MTKView whose layer AppKit backs. A program that only draws into render targets and reads them back, such as a Quick Look extension's renderer or a tool that saves frames, doesn't need any of that, and pays for it:

  • It checks in with LaunchServices as a running app. Each launch of an app bundle's executable updates the LaunchServices database, which sends com.apple.LaunchServices.database to every process watching it. The Quick Look thumbnail agent, for one, cancels the requests it has in flight when that notification arrives, so a thumbnail drawn this way never arrives.
  • It shows a window, even one ordered to the back, and a Dock icon unless the activation policy says otherwise.

A layer-backed NSView alone is enough to start NSApplication: -[MTKView initWithFrame:device:] creates its layer, and +[NSViewBackingLayer defaultValueForKey:] calls +[NSApplication sharedApplication].

Change

Platform::macos_headless, false by default. When it's set, run hands over to run_headless, which creates no NSApplication, no window and no NSView:

  • The view is a plain NSObject subclass that answers what the Metal backend asks of an MTKView (device, colorPixelFormat, depthStencilPixelFormat, sampleCount, bounds, drawableSize, setDrawableSize:, currentDrawable, currentRenderPassDescriptor and draw) from a CAMetalLayer nothing shows. Its render pass descriptor is built as MTKView builds its own, with a multisampled color texture resolving into the drawable and a depth/stencil texture, both kept until the drawable's size changes.
  • The loop processes requests and calls perform_redraw until the app quits, inside an autorelease pool each time round, as nothing drains one otherwise.

It only draws with Metal: run_headless asserts it. The display setup run and run_headless share moves into new_display. With the default, the code path is the same as before. Other platforms ignore the field.

Verification

On macOS 27, in a macroquad game (CHOMP) that draws its frames into render targets and reads them back:

  • The frames are drawn as with a window, at the same speed.
  • No NSApplication is created (a breakpoint on +[NSApplication sharedApplication] is never hit), no window exists for the process, and over repeated launches of the app bundle's executable, com.apple.LaunchServices.database is never posted, where each launch posted it before.
  • The game's Quick Look thumbnail extension, which runs it this way, now gets its thumbnails.

cargo build has no new warnings. rustfmt --check flags nothing in the added lines, only formatting that's already on master.

Co-authored-by: multica-agent <github@multica.ai>
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