-
Notifications
You must be signed in to change notification settings - Fork 109
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-glue: Wrap NativeWindow
/InputQueue
locks in a newtype
#288
Conversation
CC @rib. |
Although I'm not a fan of the current design that requires explicit locking, this patch in itself looks good to me for helping avoid leaking implementation details. |
Something else to consider here is that Something I was just experimenting with, that's somewhat related to this, was a reference counting wrapper for NativeWindow like: // XXX: NativeWindow is a ref-counted object but the NativeWindow rust API
// doesn't currently implement Clone() in terms of acquiring a reference
// and Drop() in terms of releasing a reference.
/// A reference to a `NativeWindow`, used for rendering
pub struct NativeWindowRef {
inner: NativeWindow
}
impl NativeWindowRef {
pub fn new(native_window: &NativeWindow) -> Self {
unsafe { ndk_sys::ANativeWindow_acquire(native_window.ptr().as_ptr()); }
Self { inner: native_window.clone() }
}
}
impl Drop for NativeWindowRef {
fn drop(&mut self) {
unsafe { ndk_sys::ANativeWindow_release(self.inner.ptr().as_ptr()) }
}
}
impl Deref for NativeWindowRef {
type Target = NativeWindow;
fn deref(&self) -> &Self::Target {
&self.inner
}
} And an API for querying the NativeWindow from a separate /// Queries the current [`NativeWindow`] for the application.
///
/// This will only return `Some(window)` between
/// [`AndroidAppMainEvent::InitWindow`] and [`AndroidAppMainEvent::TerminateWindow`]
/// events.
pub fn native_window<'a>(&self) -> Option<NativeWindowRef> {
let guard = self.native_window.read().unwrap();
if let Some(ref window) = *guard {
Some(NativeWindowRef::new(window))
} else {
None
}
} I was looking at remove the need to for any global/static API for accessing the In my case I'm also not concerned about locking the window a means to block Java from notifying the app about SurfaceView callbacks, so the reference it just about ensuring the pointer would remain valid. I understand that's not a concern here (since the lock is intended to block the code that might drop the reference and invalidate the pointer) but I figured it was worth mentioning here for future consideration. |
We discussed many times that this is something to be improved in the future, when implementing a unified API over
And the rest of your takes-5-minutes-to-read-wall-of-text becomes moot 😩 😫
#309...
We discussed this at length as well? |
As recently recommended/requested, this prevents `ndk_glue` from leaking its underlying lock implementation, and relieves downstream crates from having to import and specify the appropriate lock type. Especially in light of a scheduled migration to `parking_lot`. Note that this won't prevent against API breakage introduced by the intended change of transposing the `Option` inside this `LockReadGuard` as that can't be done prior to switching to `parking_lot`, and it'll be its own free-standing API ergonomics improvement regardless (`Option` now only needs to be unwrapped/checked once). Finally add some more doc-comments to link to the new lock and explain how the locks should be used on the `*Created` events and getter functions too, instead of describing it only on the `*Destroyed` events.
3483568
to
2ae10c3
Compare
As recently recommended/requested, this prevents
ndk_glue
from leaking its underlying lock implementation, and relieves downstream crates from having to import and specify the appropriate lock type. Especially in light of a scheduled migration toparking_lot
.Note that this won't prevent against API breakage introduced by the intended change of transposing the
Option
inside thisLockReadGuard
as that can't be done prior to switching toparking_lot
, and it'll be its own free-standing API ergonomics improvement regardless (Option
now only needs to be unwrapped/checked once).Finally add some more doc-comments to link to the new lock and explain how the locks should be used on the
*Created
events and getter functions too, instead of describing it only on the*Destroyed
events.