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

Add ndk-context. #223

Merged
merged 17 commits into from
Feb 14, 2022
Merged

Add ndk-context. #223

merged 17 commits into from
Feb 14, 2022

Conversation

dvc94ch
Copy link
Contributor

@dvc94ch dvc94ch commented Feb 7, 2022

Closes #211

ndk-context/src/lib.rs Outdated Show resolved Hide resolved
@zmerp
Copy link
Contributor

zmerp commented Feb 7, 2022

As this PR stands, winit will not be able to replace ndk-glue with ndk-context, but maybe this is intended. Besides that this should be enough for many other creates, including openxrs.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 7, 2022

As this PR stands, winit will not be able to replace ndk-glue with ndk-context, but maybe this is intended.

This is indeed intended. winit (and presumably a game engine) is highly tied to the runtime and cargo-apk. I think this should be enough for most crates wanting to add some android support. If there are crates for which this isn't sufficient we can collect the exact reasons why not in an issue and then see if we have to take further steps.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 12, 2022

Ping @MarijnS95

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 13, 2022

@msiglreith looks like @MarijnS95 is on vaccation or something. what are your thoughts on this PR?

Copy link
Contributor

@msiglreith msiglreith left a comment

Choose a reason for hiding this comment

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

API-wise it looks good to me. The crate should be listed in the README and probably also include a section regarding ndk-context vs ndk-glue somewhere

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

So wondering if we shoud remove the fn native_activity() from ndk-glue. winit is using it to access the AssetManager. I think there is probably a way to get an AssetManager using only ndk-context. Maybe marking it as deprecated would get the message accross to people to stop using it.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

Yep, we should deprecate it [0] shows how to access it using the jni.

@MarijnS95
Copy link
Member

looks like @MarijnS95 is on vaccation or something

Not anymore 😉. Taking a (still rather short) break from open source was well needed.


I thought the idea was to remove all statics from ndk-glue and shove them into ndk-context (with the premise that we never do any breaking changes with breaking updates). It is yet unclear to me if it's right to assume that all points of contention over these duplicate static variables originate from purely needing the vm and/or activity? Looks like "deprecating" native_activity() is a bit too harsh?

ndk-glue/src/lib.rs Outdated Show resolved Hide resolved
ndk-context/src/lib.rs Outdated Show resolved Hide resolved
ndk-context/src/lib.rs Outdated Show resolved Hide resolved
@MarijnS95
Copy link
Member

MarijnS95 commented Feb 14, 2022

So wondering if we shoud remove the fn native_activity() from ndk-glue. winit is using it to access the AssetManager. I think there is probably a way to get an AssetManager using only ndk-context. Maybe marking it as deprecated would get the message accross to people to stop using it.

Regarding this, since there don't seem to be many users of it after switching everyone over to ndk-context, perhaps it's still worth pushing through my idea (#211 (comment)) of passing the ANativeActivity object into fn main() and removing static-global access to it entirely? That way you eradicate this problem entirely without working around a deprecation warning in certain cases. Where needed, a version mismatch results in a compile error (and we can recommend raw pointer casts if a version bump is tricky).

Maybe good idea to document for most of these popular crates - https://crates.io/crates/ndk-glue/reverse_dependencies - what exactly they need from ndk-glue

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

Yes, think removing the static NATIVE_ACTIVITY is the right thing to do (and complementary to ndk-context). Although not sure about removing NATIVE_WINDOW, INPUT_QUEUE and CONTENT_RECT.

So in general are you ok with the ndk-context approach to partly solving this problem?

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

So removed the NATIVE_ACTIVITY global as I think this is one we agree on and is worth doing.

@MarijnS95
Copy link
Member

https://github.com/MarijnS95/android-ndk-rs/compare/anativeactivity-instance @dvc94ch This is my draft idea for removing fallible static globals entirely, I can open a PR and we can iterate on it in parallel with your changes.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

@MarijnS95 that looks good, I think it's an improvement for some use cases. But in general I still think we should try to discourage the use of ndk-glue unless it's absolutely necessary. If you open a PR we can merge it and I'll change jni-audio back to using ndk-context.

@MarijnS95
Copy link
Member

But in general I still think we should try to discourage the use of ndk-glue unless it's absolutely necessary.

I think both PRs are a great step in that direction 😉. Mine still has some open questions though:

  • what if a client application already uses the public instance field for its own cases?
  • static is gone now, but what if two different ndk-glue versions with incompatible GlueInstance layout try to cast and use the same pointer?

Will need some more thoughts, I think I'll get this merged + released + backported ASAP, then PR the migration to most obvious crates and see where to move next.

ndk-context/src/lib.rs Outdated Show resolved Hide resolved
@MarijnS95
Copy link
Member

MarijnS95 commented Feb 14, 2022

Looks like GH comment notifications are all over the place. Are we removing native_activity(), deprecating it at least, or leaving it to a separate PR entirely?

(Sorry about closing this, ctrl+enter vs ctrl+shift+enter muscle memory isn't what it's supposed to be)

@MarijnS95 MarijnS95 closed this Feb 14, 2022
@MarijnS95 MarijnS95 reopened this Feb 14, 2022
Co-authored-by: Marijn Suijten <marijns95@gmail.com>
@MarijnS95
Copy link
Member

Do we need a changelog for the new crate, and the changes listed in the ndk-glue crate? Then I think we are ready for merging and releasing. I'm preparing the necessary changes to backport this to older ndk-glue crates which are currently used by at least winit.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 14, 2022

Looks like GH comment notifications are all over the place. Are we removing native_activity(), deprecating it at least, or leaving it to a separate PR entirely?

Think we can hold off for with releasing until your PR lands, I removed my changes. Got to go afk for a while, will add a CHANGELOG afterwards.

@MarijnS95
Copy link
Member

MarijnS95 commented Feb 14, 2022

We can do a non-breaking patch release (same for the backports) now with #[deprecated = "Don't use for `vm()` purposes"], preparing everyone for this future. Or without the deprecation, as my PR would be breaking anyway.

@dvc94ch dvc94ch merged commit 21e78af into master Feb 14, 2022
@dvc94ch dvc94ch deleted the ndk-context branch February 14, 2022 17:45
MarijnS95 added a commit that referenced this pull request Feb 15, 2022
In order to show that [#223] solves our problem ([#211]) with multiple
ndk-glue versions in tree, all having their own `static` globals,
backport the `ndk-context` initialization to `ndk-glue 0.5` so that
everyone on `winit 0.26` (pending a `winit 0.27` release) can be
compatible with other crates using the Java VM and/or Android Context.
These crates will need to migrate to `ndk-context` first though.

[#211]: #211
[#223]: #223
@zmerp
Copy link
Contributor

zmerp commented Feb 15, 2022

The crate oboe requires the VM and Context pointers, but also the sdk version. Is there a way of getting the sdk version from the context pointer?

MarijnS95 added a commit that referenced this pull request Feb 15, 2022
In order to show that [#223] solves our problem ([#211]) with multiple
ndk-glue versions in tree, all having their own `static` globals,
backport the `ndk-context` initialization to `ndk-glue 0.5` so that
everyone on `winit 0.26` (pending a `winit 0.27` release) can be
compatible with other crates using the Java VM and/or Android Context.
These crates will need to migrate to `ndk-context` first though.

[#211]: #211
[#223]: #223
@dvc94ch
Copy link
Contributor Author

dvc94ch commented Feb 15, 2022

yes, should be possible, although will require fiddling a bit with the jni.

https://developer.android.com/reference/android/os/Build

@zmerp
Copy link
Contributor

zmerp commented Feb 15, 2022

@dvc94ch Oh that's great, thanks!

MarijnS95 added a commit to MarijnS95/app_dirs2 that referenced this pull request Feb 15, 2022
`ndk-glue` suffers one fatal flaw: it's "only" supposed to be used by
the crate providing `fn main()` and only supposed to end up in the
dependency graph once as it has `static` globals which get duplicated
across versions.

In the current case with `winit 0.26` still on `ndk-glue 0.5` but
`app_dirs2` booted from `0.4` to `0.6` it'll always panic in `fn
native_activity()` as the `static` globals on these versions are not
initialized:
app-dirs-rs#18 (comment)

Introducing `ndk-context`: a crate that holds these `static`s, with the
intention/premise to not see a breaking release /ever/ and make this a
problem of the past.  The crate is currently initialized with the VM and
Android Context on `ndk-glue` 0.5.1 and 0.6.1 making it compatible with
whatever is current, and the possibility for backporting to older
`ndk-glue` versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223
MarijnS95 added a commit that referenced this pull request Feb 15, 2022
In order to show that [#223] solves our problem ([#211]) with multiple
ndk-glue versions in tree, all having their own `static` globals,
backport the `ndk-context` initialization to `ndk-glue 0.4` so that
everyone on this older crate can be compatible with other crates using
the Java VM and/or Android Context.  These crates will need to migrate
to `ndk-context` first though.

[#211]: #211
[#223]: #223
MarijnS95 added a commit to MarijnS95/cpal that referenced this pull request Feb 15, 2022
`ndk-glue` suffers one fatal flaw: it's "only" supposed to be used by
the crate providing `fn main()` and only supposed to end up in the
dependency graph once as it has `static` globals which get duplicated
across versions.

In the current case with `winit 0.26` still on `ndk-glue 0.5` but
`cpal` on `ndk-glue 0.6` it'll always panic in `fn native_activity()` as
the `static` globals on this version is not initialized.

Introducing `ndk-context`: a crate that holds these `static`s, with the
intention/premise to not see a breaking release /ever/ and make this a
problem of the past.  The crate is currently initialized with the VM and
Android Context on `ndk-glue` 0.5.1 and 0.6.1 (0.4.1 pending) making it
compatible with whatever is current, and the possibility for backporting
to older `ndk-glue` versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223
MarijnS95 added a commit to MarijnS95/cpal that referenced this pull request Feb 15, 2022
`ndk-glue` suffers one fatal flaw: it's "only" supposed to be used by
the crate providing `fn main()` and only supposed to end up in the
dependency graph once as it has `static` globals which get duplicated
across versions.

In the current case with `winit 0.26` still on `ndk-glue 0.5` but
`cpal` on `ndk-glue 0.6` it'll always panic in `fn native_activity()` as
the `static` globals on this version is not initialized.

Introducing `ndk-context`: a crate that holds these `static`s, with the
intention/premise to not see a breaking release /ever/ and make this a
problem of the past.  The crate is currently initialized with the VM and
Android Context on `ndk-glue` 0.5.1 and 0.6.1 (0.4.1 pending) making it
compatible with whatever is current, and the possibility for backporting
to older `ndk-glue` versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223
MarijnS95 added a commit to MarijnS95/cpal that referenced this pull request Feb 15, 2022
`ndk-glue` suffers one fatal flaw: it's "only" supposed to be used by
the crate providing `fn main()` and only supposed to end up in the
dependency graph once as it has `static` globals which get duplicated
across versions.

In the current case with `winit 0.26` still on `ndk-glue 0.5` but
`cpal` on `ndk-glue 0.6` it'll always panic in `fn native_activity()` as
the `static` globals on this version is not initialized.

Introducing `ndk-context`: a crate that holds these `static`s, with the
intention/premise to not see a breaking release /ever/ and make this a
problem of the past.  The crate is currently initialized with the VM and
Android Context on `ndk-glue` 0.5.1 and 0.6.1 (0.4.1 pending) making it
compatible with whatever is current, and the possibility for backporting
to older `ndk-glue` versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223
MarijnS95 added a commit that referenced this pull request Feb 15, 2022
In order to show that [#223] solves our problem ([#211]) with multiple
ndk-glue versions in tree, all having their own `static` globals,
backport the `ndk-context` initialization to `ndk-glue 0.4` so that
everyone on this older crate can be compatible with other crates using
the Java VM and/or Android Context.  These crates will need to migrate
to `ndk-context` first though.

[#211]: #211
[#223]: #223
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.

Runtime problems when including multiple versions of ndk-glue
5 participants