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

ndk/native_window: Add bindings to convert to and from Java Surface representation #272

Merged
merged 1 commit into from
May 14, 2022

Conversation

MarijnS95
Copy link
Member

ANativeWindow is the C counterpart to the android.view.Surface object in Java, and has these functions available to convert between the C and Java (JNI representation) variant of it.

… representation

`ANativeWindow` is the C counterpart to the `android.view.Surface`
object in Java, and has these functions available to convert between the
C and Java (JNI representation) variant of it.
@maxammann
Copy link

Looks good, haven't tested this though

@maxammann
Copy link

maxammann commented May 13, 2022

I'll quickly try my luck to get this working :) Hold my coffee! :D

Copy link

@maxammann maxammann left a comment

Choose a reason for hiding this comment

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

Works :)

image

/// `surface` is a valid pointer to an [`android.view.Surface`].
///
/// [`android.view.Surface`]: https://developer.android.com/reference/android/view/Surface
pub unsafe fn from_surface(env: *mut JNIEnv, surface: jobject) -> Option<Self> {

Choose a reason for hiding this comment

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

Do we somehow need to release this surface? (ANativeWindow_release)

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope! You'll get a NativeWindow here which already implements acquire/release semantics in clone() and drop(), respectively.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if you need to retain the life of the surface JNI object (in Java land), though - https://developer.android.com/ndk/reference/group/native-activity#anativewindow_fromsurface doesn't specify 😞

All I know is that to_surface gives you a Java Surface which internally keeps the NativeWindow alive, so you don't need to do that at least.

@MarijnS95 MarijnS95 merged commit d18e809 into master May 14, 2022
@MarijnS95 MarijnS95 deleted the native-window-jni-surface branch May 14, 2022 12:01
MarijnS95 added a commit to rust-windowing/glutin that referenced this pull request Jun 6, 2022
`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
MarijnS95 added a commit to rust-windowing/glutin that referenced this pull request Jun 7, 2022
`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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants