Skip to content

Commit

Permalink
Added AndroidContextExt trait to handle surface created/destroyed
Browse files Browse the repository at this point in the history
This is temporary solution as starting point
  • Loading branch information
katyo committed Sep 21, 2020
1 parent d670a4c commit e12092f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
30 changes: 29 additions & 1 deletion glutin/src/api/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ impl Context {
) -> Result<(winit::window::Window, Self), CreationError> {
let win = wb.build(el)?;
let gl_attr = gl_attr.clone().map_sharing(|c| &c.0.egl_context);
let nwin = unsafe { ndk_glue::native_window() };
let nwin = ndk_glue::native_window();
// FIXME: The native window is null until winit's `Resumed` event, so context should be created after.
if nwin.is_none() {
return Err(OsError("Android's native window is null".to_string()));
}
Expand Down Expand Up @@ -109,6 +110,33 @@ impl Context {
Ok((win, context))
}

#[inline]
pub fn suspend(&self) {
let ctx = &self.0;
let mut stopped = ctx.stopped.as_ref().unwrap().lock();
*stopped = true;
// Android has stopped the activity or sent it to background.
// Release the EGL surface and stop the animation loop.
unsafe {
ctx.egl_context.on_surface_destroyed();
}
}

#[inline]
pub fn resume(&self) {
let ctx = &self.0;
let mut stopped = ctx.stopped.as_ref().unwrap().lock();
*stopped = false;
// Android has started the activity or sent it to foreground.
// Restore the EGL surface and animation loop.
unsafe {
let nwin = ndk_glue::native_window();
ctx.egl_context.on_surface_created(
nwin.as_ref().unwrap().ptr().as_ptr() as *const _,
);
}
}

#[inline]
pub fn new_headless<T>(
_el: &EventLoopWindowTarget<T>,
Expand Down
15 changes: 15 additions & 0 deletions glutin/src/platform/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ impl<T: ContextCurrentState> ContextTraitExt for Context<T> {
Some(self.context.get_egl_display())
}
}

pub trait AndroidContextExt {
fn suspend(&self);
fn resume(&self);
}

impl<T: ContextCurrentState> AndroidContextExt for Context<T> {
fn suspend(&self) {
self.context.suspend()
}

fn resume(&self) {
self.context.resume()
}
}

0 comments on commit e12092f

Please sign in to comment.