Skip to content

Commit

Permalink
eframe: defer graphics state initialization until app Resumed
Browse files Browse the repository at this point in the history
Conceptually the Winit `Resumed` event signifies that the application is
ready to run and render and since
rust-windowing/winit#2331 all platforms now
consistently emit a Resumed event that can be used to initialize
graphics state for an application.

On Android in particular it's important to wait until the application
has Resumed before initializing graphics state since it won't have an
associated SurfaceView while paused.

Without this change then Android applications are likely to just show
a black screen.

This updates the Wgpu+Winit and Glow+Winit integration for eframe but
it's worth noting that the Glow integration is still not able to fully
support Android due to limitations in with Glutin's API that mean
we can't destroy and create a Window without also destroying the
GL context, and therefore the entire application.

There is a plan (and progress on) to improve the Glutin API here:
rust-windowing/glutin#1435 and with that change
it should be an incremental change to enable Android support with Glow
later.

In the mean time the Glow changes keep the implementation consistent
with the wgpu integration and it should now at least be possible to
start an Android application - even though it won't be able to suspend
correctly.
  • Loading branch information
rib committed Aug 21, 2022
1 parent eeeb4b7 commit 236910d
Show file tree
Hide file tree
Showing 3 changed files with 553 additions and 312 deletions.
18 changes: 17 additions & 1 deletion eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

#![warn(missing_docs)] // Let's keep `epi` well-documented.

pub use winit::event_loop::EventLoopBuilder;
pub use crate::native::run::RequestRepaintEvent;

/// Hook into the building of an event loop before it is run
///
/// You can configure any platform specific details required on top of the default configuration
/// done by EFrame.
pub type EventLoopBuilderHook = Box<dyn FnOnce(&mut EventLoopBuilder<RequestRepaintEvent>)>;

/// This is how your app is created.
///
/// You can use the [`CreationContext`] to setup egui, restore state, setup OpenGL things, etc.
Expand Down Expand Up @@ -173,7 +182,6 @@ pub enum HardwareAcceleration {
///
/// Only a single native window is currently supported.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone)]
pub struct NativeOptions {
/// Sets whether or not the window will always be on top of other windows.
pub always_on_top: bool,
Expand Down Expand Up @@ -288,6 +296,13 @@ pub struct NativeOptions {
/// When `true`, [`winit::platform::run_return::EventLoopExtRunReturn::run_return`] is used.
/// When `false`, [`winit::event_loop::EventLoop::run`] is used.
pub run_and_return: bool,

/// Hook into the building of an event loop before it is run.
///
/// Specify a callback here in case you need to make platform specific changes to the
/// event loop before it is run.
pub event_loop_builder: Option<EventLoopBuilderHook>,

}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -315,6 +330,7 @@ impl Default for NativeOptions {
follow_system_theme: cfg!(target_os = "macos") || cfg!(target_os = "windows"),
default_theme: Theme::Dark,
run_and_return: true,
event_loop_builder: None
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: Ap
#[cfg(feature = "glow")]
Renderer::Glow => {
tracing::debug!("Using the glow renderer");
native::run::run_glow(app_name, &native_options, app_creator);
native::run::run_glow(app_name, native_options, app_creator);
}

#[cfg(feature = "wgpu")]
Renderer::Wgpu => {
tracing::debug!("Using the wgpu renderer");
native::run::run_wgpu(app_name, &native_options, app_creator);
native::run::run_wgpu(app_name, native_options, app_creator);
}
}
}
Expand Down
Loading

0 comments on commit 236910d

Please sign in to comment.