Skip to content

Commit

Permalink
Merge pull request #1164 from OlegOAndreev/master
Browse files Browse the repository at this point in the history
Add Android raw window handle support
  • Loading branch information
Cobrand authored Nov 2, 2021
2 parents 3572a1b + 3400b5d commit 8bddddb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,15 @@ On macOS the `RawWindowHandle.ns_view` field is returned null. Libraries consumi
`wgpu`) should determine a sane default for `ns_view`. If they do not, please file an issue with the associated
project.

### raw-window-handle on Android

On some platforms, including Android, SDL2 tries to create the OpenGL context by itself even without creating
a renderer. This can manifest in errors like `VK_ERROR_NATIVE_WINDOW_IN_USE_KHR` when initializing Vulkan or GLES.
Add the following code before creating a window to fix the errors:
```rust
sdl2::hint::set("SDL_VIDEO_EXTERNAL_CONTEXT", "1")
```

# When things go wrong
Rust, and Rust-SDL2, are both still heavily in development, and you may run
into teething issues when using this. Before panicking, check that you're using
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
In this file will be listed the changes, especially the breaking ones that one should be careful of
when upgrading from a version of rust-sdl2 to another.

### Unreleased

[PR #1164](https://github.com/Rust-SDL2/rust-sdl2/pull/1164) Added raw-window-handle support for Android

### v0.35.0

* **BREAKING CHANGE** Update `sdl2-sys/sdl_bindings.rs` to use enums instead of consts. If you were using `sdl2-sys`'s
Expand Down
56 changes: 48 additions & 8 deletions src/sdl2/raw_window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ unsafe impl HasRawWindowHandle for Window {
..IOSHandle::empty()
})
}
#[cfg(any(target_os = "android"))]
SDL_SYSWM_ANDROID => {
let window_system = match wm_info.subsystem {
SDL_SYSWM_ANDROID => "Android",
_ => unreachable!(),
};
panic!(
"raw-window-handle support for {} not yet implemented",
window_system
);
use self::raw_window_handle::android::AndroidHandle;
RawWindowHandle::Android(AndroidHandle {
a_native_window: unsafe { wm_info.info.android }.window as *mut libc::c_void,
..AndroidHandle::empty()
})
}
x => {
let window_system = match x {
Expand Down Expand Up @@ -160,6 +158,9 @@ pub struct SDL_SysWMinfo {

#[cfg(target_os = "ios")]
pub info: ios::IOSSysWMinfo,

#[cfg(target_os = "android")]
pub info: android::AndroidSysWMinfo,
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -371,3 +372,42 @@ pub mod ios {
_unused: [u8; 0],
}
}

#[cfg(target_os = "android")]
pub mod android {
#[repr(C)]
#[derive(Copy, Clone)]
pub union AndroidSysWMinfo {
pub android: AndroidInfo,
pub dummy: [u8; 64usize],
_bindgen_union_align: [u64; 8usize],
}

impl Default for AndroidSysWMinfo {
fn default() -> Self {
AndroidSysWMinfo {
android: AndroidInfo::default(),
}
}
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AndroidInfo {
pub window: *mut ANativeWindow,
}

impl Default for AndroidInfo {
fn default() -> Self {
AndroidInfo {
window: 0 as *mut ANativeWindow,
}
}
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ANativeWindow {
_unused: [u8; 0],
}
}

0 comments on commit 8bddddb

Please sign in to comment.