Skip to content

Commit

Permalink
Binder and parcel for HardwareBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Oct 29, 2024
1 parent 59a3ff6 commit 4705eb3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions ndk-sys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Generate against upstream NDK build `11769913`. (#471)
- Add `nativewindow` feature to link against `libnativewindow`. (#465)
- Add `AIBinder` and `Parcel`

# 0.5.0 (2023-10-15)

Expand Down
4 changes: 4 additions & 0 deletions ndk-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ categories = ["os", "os::android-apis", "external-ffi-bindings"]

[dependencies]
jni-sys = "0.3.0"
# Official crate from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/rust/sys/
android-binder-ndk-sys = { version = "0.1.0", optional = true }

[features]
test = []

audio = []
binder = ["android-binder-ndk-sys"]
bitmap = []
media = []
nativewindow = []
Expand Down
8 changes: 8 additions & 0 deletions ndk-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ extern "C" {}
#[link(name = "mediandk")]
extern "C" {}

// TODO: Delete. Taken care of in android-binder-ndk-sys
// #[cfg(all(feature = "binder", target_os = "android"))]
// #[link(name = "binder_ndk")]
// extern "C" {}

#[cfg(all(feature = "bitmap", target_os = "android"))]
#[link(name = "jnigraphics")]
extern "C" {}
Expand All @@ -56,3 +61,6 @@ extern "C" {}
#[cfg(all(feature = "sync", target_os = "android"))]
#[link(name = "sync")]
extern "C" {}

#[cfg(all(feature = "binder", target_os = "android"))]
pub use android_binder_ndk_sys::*;
2 changes: 2 additions & 0 deletions ndk-sys/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <android/api-level.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
// XXX: Binder APIs are included/reexported/forwarded to official crate:
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/rust/sys/
// #include <android/binder_auto_utils.h>
// #include <android/binder_enums.h>
// #include <android/binder_ibinder.h>
Expand Down
5 changes: 4 additions & 1 deletion ndk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ categories = ["os", "os::android-apis", "api-bindings"]

[features]
default = ["rwh_06"]
all = ["audio", "bitmap", "media", "nativewindow", "sync", "api-level-34", "rwh_04", "rwh_05", "rwh_06"]
all = ["audio", "binder", "bitmap", "media", "nativewindow", "sync", "api-level-34", "rwh_04", "rwh_05", "rwh_06"]

audio = ["ffi/audio", "api-level-26"]
binder = ["ffi/binder", "dep:android-binder"]
bitmap = ["ffi/bitmap"]
media = ["ffi/media"]
nativewindow = ["ffi/nativewindow"]
Expand Down Expand Up @@ -47,6 +48,8 @@ rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true }
rwh_05 = { package = "raw-window-handle", version = "0.5", optional = true }
rwh_06 = { package = "raw-window-handle", version = "0.6", optional = true }
thiserror = "1.0.23"
# Official crate from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/rust/
android-binder = { version = "0.1.0", optional = true }

[dependencies.jni]
version = "0.21"
Expand Down
30 changes: 30 additions & 0 deletions ndk/src/hardware_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,36 @@ impl HardwareBuffer {
HardwareBufferRef::from_ptr(self.inner)
}
}

#[cfg(all(feature = "binder", feature = "api-level-34"))]
pub fn read_from_parcel(
parcel: android_binder::binder_impl::BorrowedParcel<'_>,
) -> std::result::Result<HardwareBufferRef, android_binder::StatusCode> {
use android_binder::unstable_api::AsNative;

let mut out = MaybeUninit::uninit();
let status =
unsafe { ffi::AHardwareBuffer_readFromParcel(parcel.as_native(), out.as_mut_ptr()) };
android_binder::unstable_api::status_result(status)
.and_then(|()| {
NonNull::new(unsafe { out.assume_init() })
.ok_or(android_binder::StatusCode::UNEXPECTED_NULL)
})
.map(|p| unsafe { HardwareBufferRef::from_ptr(p) })
}

#[cfg(all(feature = "binder", feature = "api-level-34"))]
pub fn write_to_parcel(
&self,
// TODO: mut parcel borrows? This API seems to allow writing either way.
mut parcel: android_binder::binder_impl::BorrowedParcel<'_>,
) -> std::result::Result<(), android_binder::StatusCode> {
use android_binder::unstable_api::AsNative;

let status =
unsafe { ffi::AHardwareBuffer_writeToParcel(self.as_ptr(), parcel.as_native_mut()) };
android_binder::unstable_api::status_result(status)
}
}

/// A [`HardwareBuffer`] with an owned reference, that is released when dropped.
Expand Down

0 comments on commit 4705eb3

Please sign in to comment.