fix: removed WinitPlugin from headless_renderer example (#15818)

# Objective

The `headless_renderer` example is meant to showcase running bevy as a
headless renderer, but if run without a display server (for example,
over an SSH connection), a panic occurs in `bevy_winit` despite never
creating a window:

```rust
bevy_winit-0.14.1/src/lib.rs:132:14:
winit-0.30.5/src/platform_impl/linux/mod.rs:
neither WAYLAND_DISPLAY nor WAYLAND_SOCKET nor DISPLAY is set.
```

This example should run successfully in situations without an available
display server, as although the GPU is used for rendering, no window is
ever created.

## Solution

Disabling WinitPlugin, where the above panic occurs, allows the example
to run in a fully headless environment.

## Testing

- I tested this change in normal circumstances with a display server (on
macOS Sequoia and Asahi Linux) and behavior was normal.
- I tested with no display server by connecting via SSH, and running the
example (on Asahi Linux). Previously this panics, but with this change
it runs normally.

## Considerations

- One could argue that ultimately the user should not need to remove
`WinitPlugin`, and instead bevy should only throw the above panic when
the application first attempts to create a window.
This commit is contained in:
Cooper Jax Reiff 2024-10-28 15:08:20 -07:00 committed by GitHub
parent 069291d6d8
commit 67567702f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,6 +24,7 @@ use bevy::{
texture::{BevyDefault, TextureFormatPixelInfo}, texture::{BevyDefault, TextureFormatPixelInfo},
Extract, Render, RenderApp, RenderSet, Extract, Render, RenderApp, RenderSet,
}, },
winit::WinitPlugin,
}; };
use crossbeam_channel::{Receiver, Sender}; use crossbeam_channel::{Receiver, Sender};
use std::{ use std::{
@ -81,16 +82,20 @@ fn main() {
.add_plugins( .add_plugins(
DefaultPlugins DefaultPlugins
.set(ImagePlugin::default_nearest()) .set(ImagePlugin::default_nearest())
// Do not create a window on startup. // Not strictly necessary, as the inclusion of ScheduleRunnerPlugin below
// replaces the bevy_winit app runner and so a window is never created.
.set(WindowPlugin { .set(WindowPlugin {
primary_window: None, primary_window: None,
exit_condition: bevy::window::ExitCondition::DontExit, ..default()
close_when_requested: false, })
}), // WinitPlugin will panic in environments without a display server.
.disable::<WinitPlugin>(),
) )
.add_plugins(ImageCopyPlugin) .add_plugins(ImageCopyPlugin)
// headless frame capture // headless frame capture
.add_plugins(CaptureFramePlugin) .add_plugins(CaptureFramePlugin)
// ScheduleRunnerPlugin provides an alternative to the default bevy_winit app runner, which
// manages the loop without creating a window.
.add_plugins(ScheduleRunnerPlugin::run_loop( .add_plugins(ScheduleRunnerPlugin::run_loop(
// Run 60 times per second. // Run 60 times per second.
Duration::from_secs_f64(1.0 / 60.0), Duration::from_secs_f64(1.0 / 60.0),