Skip to content

Commit

Permalink
Implement android backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Feb 9, 2020
1 parent 1d052ed commit 176b218
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 42 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ build = "build.rs"
[package.metadata.docs.rs]
features = ["serde_feature"]

[package.metadata.android]
build_targets = ["aarch64-linux-android"]

[features]
serde_feature = ["winit_types/serde_feature"]

Expand All @@ -38,7 +41,8 @@ gbm = { path = "../gbm.rs" }
drm = "0.3.4"

[target.'cfg(target_os = "android")'.dependencies]
android_glue = "0.2.3"
android_glue = { git = "https://github.com/rust-windowing/android-rs-glue" }
android-ndk = { git = "https://github.com/rust-windowing/android-ndk-rs" }
glutin_egl_sys = { version = "0.1.4", path = "../glutin_sys/glutin_egl_sys" }
parking_lot = "0.10.0"

Expand Down
54 changes: 38 additions & 16 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,61 @@ fn main() {
let wb = WindowBuilder::new().with_title("A fantastic window!");

let confs = unsafe { ConfigsFinder::new().find(&*el).unwrap() };
let conf = &confs[0];
let conf = confs[0].clone();
println!("Configeration chosen: {:?}", conf);

let ctx = unsafe { ContextBuilder::new().build(conf).unwrap() };
let (win, surf) = unsafe { Surface::new_window(conf, &*el, wb).unwrap() };

unsafe { ctx.make_current(&surf).unwrap() }
let gl = support::Gl::load(|s| ctx.get_proc_address(s).unwrap());
let ctx = unsafe { ContextBuilder::new().build(&conf).unwrap() };
let win = unsafe { Surface::build_window(&conf, &*el, wb).unwrap() };

// On android the surface can only be created after the resume event
// was received.
let mut surf = None;
let mut gl = None;
el.run(move |event, _, control_flow| {
println!("{:?}", event);
*control_flow = ControlFlow::Wait;

match event {
Event::LoopDestroyed => return,
Event::MainEventsCleared => {
win.request_redraw();
Event::Resumed => {
let surface = unsafe { Surface::new_from_existing_window(&conf, &win).unwrap() };
unsafe {
ctx.make_current(&surface).unwrap();
}
if gl.is_none() {
gl = Some(support::Gl::load(|s| ctx.get_proc_address(s).unwrap()));
}
surf = Some(surface)
}
Event::Suspended => surf = None,
Event::LoopDestroyed => return,
Event::MainEventsCleared => win.request_redraw(),
Event::RedrawRequested(_) => {
gl.draw_frame([1.0, 0.5, 0.7, 1.0]);
surf.swap_buffers().unwrap();
if let (Some(gl), Some(surf)) = (&gl, &surf) {
gl.draw_frame([1.0, 0.5, 0.7, 1.0]);
surf.swap_buffers().unwrap();
}
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(size) => {
Event::WindowEvent { ref event, .. } => {
let size = match event {
WindowEvent::ScaleFactorChanged {
new_inner_size: size,
..
} => size,
WindowEvent::Resized(size) => size,
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
return;
}
_ => return,
};
if let (Some(gl), Some(surf)) = (&gl, &surf) {
ctx.update_after_resize();
surf.update_after_resize(*size);
unsafe {
gl.gl.Viewport(0, 0, size.width as _, size.height as _);
}
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
_ => (),
},
}
_ => (),
}
});
Expand Down
54 changes: 34 additions & 20 deletions src/platform/android.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
#![cfg(any(target_os = "android"))]
use crate::config::Config;
use crate::context::Context;
use crate::surface::{Surface, SurfaceTypeTrait};
use std::os::raw;

use crate::platform::ContextTraitExt;
use crate::{Context, ContextCurrentState};
use crate::{SupportsPBuffersTrait, SupportsSurfacelessTrait, SupportsWindowSurfacesTrait};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ConfigPlatformAttributes;

pub use glutin_egl_sys::EGLContext;
pub trait ConfigExt {
unsafe fn raw_config(&self) -> *const raw::c_void;
unsafe fn raw_display(&self) -> *mut raw::c_void;
}

use std::os::raw;
impl ConfigExt for Config {
unsafe fn raw_config(&self) -> *const raw::c_void {
self.config.raw_config()
}

unsafe fn raw_display(&self) -> *mut raw::c_void {
self.config.raw_display()
}
}

pub trait SurfaceExt {
unsafe fn raw_surface(&self) -> *const raw::c_void;
}

impl<
IC: ContextCurrentState,
PBT: SupportsPBuffersTrait,
WST: SupportsWindowSurfacesTrait,
ST: SupportsSurfacelessTrait,
> ContextTraitExt for Context<IC, PBT, WST, ST>
{
type Handle = EGLContext;

#[inline]
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
impl<T: SurfaceTypeTrait> SurfaceExt for Surface<T> {
unsafe fn raw_surface(&self) -> *const raw::c_void {
self.0.raw_surface()
}
}

pub trait ContextExt {
unsafe fn raw_context(&self) -> *mut raw::c_void;
}

#[inline]
unsafe fn get_egl_display(&self) -> Option<*const raw::c_void> {
Some(self.context.get_egl_display())
impl ContextExt for Context {
unsafe fn raw_context(&self) -> *mut raw::c_void {
self.0.raw_context()
}
}
6 changes: 3 additions & 3 deletions src/platform_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod platform_impl;
//#[cfg(target_os = "macos")]
//#[path = "platform_impl/macos/macos.rs"]
//mod platform_impl;
//#[cfg(target_os = "android")]
//#[path = "platform_impl/android/android.rs"]
//mod platform_impl;
#[cfg(target_os = "android")]
#[path = "platform_impl/android/android.rs"]
mod platform_impl;
//#[cfg(target_os = "ios")]
//#[path = "platform_impl/ios/ios.rs"]
//mod platform_impl;
Loading

0 comments on commit 176b218

Please sign in to comment.