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

Glutin0.23 android #1274

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
44 changes: 26 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,32 @@ jobs:
matrix:
rust_version: [stable, nightly]
platform:
- { target: x86_64-pc-windows-msvc, os: windows-latest, }
- { target: i686-pc-windows-msvc, os: windows-latest, }
- { target: x86_64-pc-windows-gnu, os: windows-latest, host: -x86_64-pc-windows-gnu }
- { target: i686-pc-windows-gnu, os: windows-latest, host: -i686-pc-windows-gnu }
# - { target: x86_64-pc-windows-msvc, os: windows-latest, }
# - { target: i686-pc-windows-msvc, os: windows-latest, }
# - { target: x86_64-pc-windows-gnu, os: windows-latest, host: -x86_64-pc-windows-gnu }
# - { target: i686-pc-windows-gnu, os: windows-latest, host: -i686-pc-windows-gnu }
- { target: i686-unknown-linux-gnu, os: ubuntu-latest, }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-latest, }
- { target: x86_64-apple-darwin, os: macos-latest, }
- { target: x86_64-apple-ios, os: macos-latest, }
- { target: armv7-apple-ios, os: macos-latest, }
- { target: aarch64-apple-ios, os: macos-latest, }
- { target: aarch64-linux-android, os: ubuntu-latest, cmd: 'apk --' }
# - { target: x86_64-apple-darwin, os: macos-latest, }
# - { target: x86_64-apple-ios, os: macos-latest, }
# - { target: armv7-apple-ios, os: macos-latest, }
# - { target: aarch64-apple-ios, os: macos-latest, }
# We're using Windows rather than Ubuntu to run the wasm tests because caching cargo-web
# doesn't currently work on Linux.
#- { target: wasm32-unknown-unknown, os: windows-latest, features: stdweb, web: web }
#- { target: wasm32-unknown-unknown, os: windows-latest, features: web-sys, web: web }
#- { target: wasm32-unknown-unknown, os: windows-latest, features: stdweb, cmd: web }
#- { target: wasm32-unknown-unknown, os: windows-latest, features: web-sys, cmd: web }

env:
RUST_BACKTRACE: 1
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-C debuginfo=0"
FEATURES: ${{ format(',{0}', matrix.platform.features ) }}
WEB: ${{ matrix.platform.web }}
CMD: ${{ matrix.platform.cmd }}

runs-on: ${{ matrix.platform.os }}
steps:

# FIXME
- uses: actions/checkout@v1
with:
Expand Down Expand Up @@ -142,6 +143,10 @@ jobs:
if: matrix.platform.os == 'ubuntu-latest'
run: sudo apt-get update && sudo apt-get install libgbm-dev libdrm-dev

- name: Install cargo-apk
if: contains(matrix.platform.target, 'android')
run: cargo install cargo-apk

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE to self

- name: Install cargo-web
continue-on-error: true
if: contains(matrix.platform.target, 'wasm32')
Expand All @@ -150,19 +155,22 @@ jobs:
- name: Check documentation
shell: bash
if: matrix.platform.target != 'wasm32-unknown-unknown'
run: cargo doc --no-deps --target ${{ matrix.platform.target }} --features $FEATURES
run: cargo $CMD doc --no-deps --target ${{ matrix.platform.target }} --features $FEATURES

- name: Build
- name: Build
shell: bash
run: cargo $WEB build --verbose --target ${{ matrix.platform.target }} --features $FEATURES
run: cargo $CMD build --verbose --target ${{ matrix.platform.target }} --features $FEATURES

- name: Build tests
shell: bash
run: cargo $WEB test --no-run --verbose --target ${{ matrix.platform.target }} --features $FEATURES
run: cargo $CMD test --no-run --verbose --target ${{ matrix.platform.target }} --features $FEATURES
- name: Run tests
shell: bash
if: (!contains(matrix.platform.target, 'ios') && !contains(matrix.platform.target, 'wasm32'))
run: cargo $WEB test --verbose --target ${{ matrix.platform.target }} --features $FEATURES
if: (
!contains(matrix.platform.target, 'android') &&
!contains(matrix.platform.target, 'ios') &&
!contains(matrix.platform.target, 'wasm32'))
run: cargo $CMD test --verbose --target ${{ matrix.platform.target }} --features $FEATURES

- name: iOS example
shell: bash
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ gbm = { path = "../gbm.rs" }
drm = "0.3.4"

[target.'cfg(target_os = "android")'.dependencies]
android_glue = "0.2.3"
glutin_egl_sys = { version = "0.1.4", path = "../glutin_sys/glutin_egl_sys" }
parking_lot = "0.10.0"

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ Glutin is only officially supported on the latest stable version of the Rust com

### Android

To compile the examples for android, you have to use the `cargo apk` utility.
To compile the examples for android, you have to use the `cargo apk` utility. You can run an example
with `cargo apk run --example $EXAMPLE --manifest-path android/Cargo.toml`.

See [the `android-rs-glue` repository](https://github.com/rust-windowing/android-rs-glue) for instructions.
See [the `android-ndk-rs` repository](https://github.com/rust-windowing/android-ndk-rs) for more information.

### Emscripten with asmjs

Expand Down
26 changes: 26 additions & 0 deletions android/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "android-examples"
version = "0.1.0"
authors = ["David Craven <david@craven.ch>"]
edition = "2018"
publish = false
build = "../build.rs"

[dependencies]
glutin = { path = ".." }

[build-dependencies]
gl_generator = "0.14.0"

[dev-dependencies]
image = "0.22.4"
ndk-glue = { path = "../../android-ndk-rs/ndk-glue" }
simple_logger = "1.4.0"
takeable-option = "0.5.0"
winit = { path = "../../winit" }
winit_types = { path = "../../winit_types" }

[[example]]
name = "window"
path = "../examples/window.rs"
crate-type = ["cdylib"]
Empty file added android/src/lib.rs
Empty file.
57 changes: 41 additions & 16 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,70 @@ use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

#[cfg(target_os = "android")]
ndk_glue::ndk_glue!(main);

fn main() {
simple_logger::init().unwrap();
let el = EventLoop::new();
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these returns necessary?

}
_ => 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
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Default for ConfigsFinder {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
version: (Api::OpenGl, Version(3, 3)),
#[cfg(any(target_os = "android", target_os = "ios"))]
version: (Api::OpenGlEs, Version(2, 0)),
version: (Api::OpenGlEs, Version(3, 1)),
plat_attr: Default::default(),
}
}
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