Skip to content

Commit 66eb8ca

Browse files
authored
ndk-glue: Document lock behaviour in Event and ensure pointer equality (#174)
This completes #134 by applying the same pointer-equality check in `on_input_queue_destroyed` to `on_window_destroyed`, and documents when the user should hold on to or release any of the `RWLock`s to ensure proper lifetimes for dependencies like surfaces in graphics APIs.
1 parent b0f2148 commit 66eb8ca

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ndk-glue/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Document when to lock and unlock the window/input queue when certain events are received.
4+
35
# 0.4.0 (2021-08-02)
46

57
- Looper is now created before returning from `ANativeActivity_onCreate`, solving

ndk-glue/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,16 @@ pub enum Event {
122122
WindowCreated,
123123
WindowResized,
124124
WindowRedrawNeeded,
125+
/// If the window is in use by ie. a graphics API, make sure the lock from
126+
/// [`native_window()`] is held on to until after freeing those resources.
127+
///
128+
/// After receiving this [`Event`] `ndk_glue` will block until that read-lock
129+
/// is released before returning to Android and allowing it to free up the window.
125130
WindowDestroyed,
126131
InputQueueCreated,
132+
/// After receiving this [`Event`] `ndk_glue` will block until the read-lock from
133+
/// [`input_queue()`] is released before returning to Android and allowing it to
134+
/// free up the input queue.
127135
InputQueueDestroyed,
128136
ContentRectChanged,
129137
}
@@ -284,10 +292,12 @@ unsafe extern "C" fn on_window_redraw_needed(
284292

285293
unsafe extern "C" fn on_window_destroyed(
286294
activity: *mut ANativeActivity,
287-
_window: *mut ANativeWindow,
295+
window: *mut ANativeWindow,
288296
) {
289297
wake(activity, Event::WindowDestroyed);
290-
*NATIVE_WINDOW.write().unwrap() = None;
298+
let mut native_window_guard = NATIVE_WINDOW.write().unwrap();
299+
assert_eq!(native_window_guard.as_ref().unwrap().ptr().as_ptr(), window);
300+
*native_window_guard = None;
291301
}
292302

293303
unsafe extern "C" fn on_input_queue_created(

0 commit comments

Comments
 (0)