Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android crash on resume with Glow backend #3867

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,14 @@ impl WinitApp for GlowWinitApp {

let running = if let Some(running) = &mut self.running {
// Not the first resume event. Create all outstanding windows.
running
.glutin
.borrow_mut()
.initialize_all_windows(event_loop);
{
let glutin = &mut running.glutin.borrow_mut();
#[cfg(target_os = "android")]
glutin.on_resume(self.native_options.viewport.clone(), event_loop)?;
#[cfg(not(target_os = "android"))]
glutin.initialize_all_windows(event_loop);
}

running
} else {
// First resume event. Created our root window etc.
Expand Down Expand Up @@ -507,7 +511,9 @@ impl GlowWinitRunning {

{
let glutin = self.glutin.borrow();
let viewport = &glutin.viewports[&viewport_id];
let Some(viewport) = glutin.viewports.get(&viewport_id) else {
return EventResult::Wait;
};
let is_immediate = viewport.viewport_ui_cb.is_none();
if is_immediate && viewport_id != ViewportId::ROOT {
// This will only happen if this is an immediate viewport.
Expand All @@ -524,7 +530,9 @@ impl GlowWinitRunning {
let (raw_input, viewport_ui_cb) = {
let mut glutin = self.glutin.borrow_mut();
let egui_ctx = glutin.egui_ctx.clone();
let viewport = glutin.viewports.get_mut(&viewport_id).unwrap();
let Some(viewport) = glutin.viewports.get_mut(&viewport_id) else {
return EventResult::Wait;
};
let window = viewport.window.as_ref().unwrap();
egui_winit::update_viewport_info(&mut viewport.info, &egui_ctx, window);

Expand Down Expand Up @@ -1112,6 +1120,9 @@ impl GlutinWindowContext {
viewport.gl_surface = None;
viewport.window = None;
}
#[cfg(target_os = "android")]
self.viewports.clear();

if let Some(current) = self.current_gl_context.take() {
log::debug!("context is current, so making it non-current");
self.not_current_gl_context = Some(current.make_not_current()?);
Expand All @@ -1121,6 +1132,46 @@ impl GlutinWindowContext {
Ok(())
}

#[cfg(target_os = "android")]
/// only applies for android. but we basically recreate root viewport
fn on_resume(
&mut self,
builder: ViewportBuilder,
event_loop: &EventLoopWindowTarget<UserEvent>,
) -> Result<()> {
log::debug!("received resume event. initializing new viewport");
let GlutinWindowContext {
egui_ctx,
viewports,
..
} = self;

initialize_or_update_viewport(
egui_ctx,
viewports,
ViewportIdPair::ROOT,
ViewportClass::Root,
builder,
None,
None,
);

self.initialize_window(ViewportId::ROOT, event_loop)?;

if let Some(not_current) = self.not_current_gl_context.take() {
log::debug!("context is non-current, so making it current");
let surface = self.viewports[&ViewportId::ROOT]
.gl_surface
.as_ref()
.unwrap();
self.current_gl_context = Some(not_current.make_current(surface)?);
} else {
log::debug!("context is already current??? could be duplicate resume event");
}

Ok(())
}

fn viewport(&self, viewport_id: ViewportId) -> &Viewport {
self.viewports
.get(&viewport_id)
Expand Down
Loading