Skip to content

Commit

Permalink
ndk/native_window: Use release/acquire fns for Drop and Clone
Browse files Browse the repository at this point in the history
Much like ALooper and AHardwareBuffer the reference counter of
ANativeWindow must be properly incremented on `Clone`, in turn allowing
us to also `_release` the window as soon as it is dropped.
  • Loading branch information
MarijnS95 committed Dec 6, 2021
1 parent 240389f commit 2d3df9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- ndk/native_window: Use `release`/`acquire` for `Drop` and `Clone` respectively

# 0.5.0 (2021-11-22)

- **Breaking:** Replace `add_fd_with_callback` `ident` with constant value `ALOOPER_POLL_CALLBACK`,
Expand Down
21 changes: 18 additions & 3 deletions ndk/src/native_window.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
//! Bindings for `ANativeWindow`
//! Bindings for [`ffi::ANativeWindow`]
use std::ptr::NonNull;

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NativeWindow {
ptr: NonNull<ffi::ANativeWindow>,
}

unsafe impl Send for NativeWindow {}
unsafe impl Sync for NativeWindow {}

impl Drop for NativeWindow {
fn drop(&mut self) {
unsafe { ffi::ANativeWindow_release(self.ptr.as_ptr()) }
}
}

impl Clone for NativeWindow {
fn clone(&self) -> Self {
unsafe {
ffi::ANativeWindow_acquire(self.ptr.as_ptr());
Self { ptr: self.ptr }
}
}
}

impl NativeWindow {
/// # Safety
/// `ptr` must be a valid pointer to an Android `ANativeWindow`.
/// `ptr` must be a valid pointer to an Android [`ffi::ANativeWindow`].
pub unsafe fn from_ptr(ptr: NonNull<ffi::ANativeWindow>) -> Self {
Self { ptr }
}
Expand Down

0 comments on commit 2d3df9f

Please sign in to comment.