Skip to content

Commit

Permalink
Switch from ndk-glue to raw-window-handle crate
Browse files Browse the repository at this point in the history
`winit` provides a `raw-window-handle` specifically for crates to
process window pointers/references in a crate-agnostic way.  `glutin`
can use it too, to get rid of its `ndk-glue` dependency which otherwise
has a hard versioning dependency  as it should be updated in sync with
`winit`.

Over time this should be worked into the other backends, in turn
removing `winit` from the `new_windowed()` function signature and
replacing it with `impl HasRawWindowHandle` or `dyn HasRawWindowHandle`.
This will also allow Android users to pass generic `Surface`s (first
converted to a so-called `NativeWindow` in the NDK through [#272], then
passed as `RawWindowHandle` thanks to [#274]) created in Java / Kotlin /
Flutter to render to individual UI elements instead of the entire screen
with a `NativeActivity` (`ndk-glue`).  It'll in turn make the code more
generic, too, as `raw-window-handle` has a variant for every platform /
windowing system currently supported by glutin.

[#272]: rust-mobile/ndk#272
[#274]: rust-mobile/ndk#274
  • Loading branch information
MarijnS95 committed Jun 6, 2022
1 parent 58bbee3 commit d166f27
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Return an `Err` instead of panicking when surfaceless GLX context creation fails on Linux.
- Fix compilation on Android:
- Switch from `StaticStructGenerator` to `StructGenerator` to dynamically load symbols.
- Replace `android_glue` dependency with `ndk-glue`, and remove broken lifecycle event handling.
- Replace `android_glue` dependency with `raw-window-handle`, and remove broken lifecycle event handling.
- Glutin can now be used on Android, however, the application must ensure it only creates the `Context` following a winit `Event::Resumed` event, and destroys the `Context` in response to a `Event::Suspended` event.

# Version 0.28.0 (2021-12-02)
Expand Down
2 changes: 1 addition & 1 deletion glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ winit = { version = "0.26", default-features = false }
[target.'cfg(target_os = "android")'.dependencies]
glutin_egl_sys = { version = "0.1.5", path = "../glutin_egl_sys" }
libloading = "0.7"
ndk-glue = "0.5" # Keep in sync with winit
parking_lot = "0.11"
raw-window-handle = "0.4"

[target.'cfg(target_os = "emscripten")'.dependencies]
glutin_emscripten_sys = { version = "0.1.1", path = "../glutin_emscripten_sys" }
Expand Down
12 changes: 9 additions & 3 deletions glutin/src/api/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{Api, ContextError, GlAttributes, PixelFormat, PixelFormatRequirement

use glutin_egl_sys as ffi;
use parking_lot::Mutex;
use raw_window_handle::{AndroidNdkHandle, HasRawWindowHandle, RawWindowHandle};
use winit;
use winit::dpi;
use winit::event_loop::EventLoopWindowTarget;
Expand All @@ -32,15 +33,20 @@ 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 = ndk_glue::native_window();
let nwin =
nwin.as_ref().ok_or_else(|| OsError("Android's native window is null".to_string()))?;
if let RawWindowHandle::AndroidNdk(AndroidNdkHandle { a_native_window, .. }) =
win.raw_window_handle()
{
a_native_window
} else {
return Err(OsError("raw_window_handle() is not for Android".to_string()));
};
let native_display = NativeDisplay::Android;
let egl_context =
EglContext::new(pf_reqs, &gl_attr, native_display, EglSurfaceType::Window, |c, _| {
Ok(c[0])
})
.and_then(|p| p.finish(nwin.ptr().as_ptr() as *const _))?;
.and_then(|p| p.finish(nwin))?;
let ctx = Arc::new(AndroidContext { egl_context, stopped: Some(Mutex::new(false)) });

let context = Context(ctx.clone());
Expand Down

0 comments on commit d166f27

Please sign in to comment.