Skip to content

Commit

Permalink
TODO many API fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Dec 22, 2023
1 parent 901c350 commit 3aed1a6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
8 changes: 8 additions & 0 deletions ndk/src/hardware_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ impl HardwareBufferUsage {
Self(ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_19);
}

impl core::ops::BitOr for HardwareBufferUsage {
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
Self(ffi::AHardwareBuffer_UsageFlags(self.0 .0 | rhs.0 .0))
}
}

pub type Rect = ffi::ARect;

fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> Result<T> {
Expand Down
2 changes: 2 additions & 0 deletions ndk/src/media/image_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ impl ImageReader {

match res {
Ok(inner) => Ok(Some(Image { inner })),
// TODO: Special-cased here...
Err(MediaError::ImgreaderNoBufferAvailable) => Ok(None),
Err(e) => Err(e),
}
Expand All @@ -234,6 +235,7 @@ impl ImageReader {
let mut fence = MaybeUninit::uninit();
let inner = construct_never_null(|res| {
ffi::AImageReader_acquireNextImageAsync(self.as_ptr(), res, fence.as_mut_ptr())
// But why not here?
})?;

let image = Image { inner };
Expand Down
60 changes: 50 additions & 10 deletions ndk/src/surface_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{
ffi::CStr,
fmt,
os::fd::{FromRawFd, IntoRawFd, OwnedFd},
ptr::NonNull,
};
Expand Down Expand Up @@ -50,8 +51,7 @@ impl SurfaceControl {
/// its parent. `debug_name` is a debug name associated with this surface. It can be used to
/// identify this surface in SurfaceFlinger's layer tree.
#[doc(alias = "ASurfaceControl_createFromWindow")]
pub fn create_from_window(parent: NativeWindow, debug_name: &CStr) -> Option<Self> {
let parent = parent.clone();
pub fn create_from_window(parent: &NativeWindow, debug_name: &CStr) -> Option<Self> {
let ptr = unsafe {
ffi::ASurfaceControl_createFromWindow(dbg!(parent.ptr().as_ptr()), debug_name.as_ptr())
// https://cs.android.com/android/platform/superproject/+/master:frameworks/native/libs/gui/SurfaceComposerClient.cpp;l=2101?q=createWithSurfaceParent%20lang:cpp&start=61 always Invalid argument
Expand Down Expand Up @@ -725,12 +725,49 @@ pub type OnCommit = Box<dyn FnMut(&SurfaceTransactionStats) + Send + Sync>;
/// An opaque handle returned during a callback that can be used to query general stats and stats
/// for surfaces which were either removed or for which buffers were updated after this transaction
/// was applied.
#[derive(Debug)]
#[doc(alias = "ASurfaceTransactionStats")]
pub struct SurfaceTransactionStats {
ptr: NonNull<ffi::ASurfaceTransactionStats>,
}

impl fmt::Debug for SurfaceTransactionStats {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// let surface_controls = self.surface_controls().as_slice().map(|)
struct DebugSurfaceControl<'a>(&'a SurfaceTransactionStats, &'a SurfaceControl);
impl<'a> fmt::Debug for DebugSurfaceControl<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SurfaceControl Stats")
.field("surface_control", &self.1)
.field("acquire_time", &self.0.acquire_time(self.1))
.field(
"previous_release_fence_fd",
&self.0.previous_release_fence_fd(self.1),
)
.finish()
}
}
struct DebugSurfaceControls<'a>(&'a SurfaceTransactionStats);
impl<'a> fmt::Debug for DebugSurfaceControls<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(
self.0
.surface_controls()
.as_mut()
.into_iter()
.map(|sc| DebugSurfaceControl(self.0, sc)),
)
.finish()
}
}
f.debug_struct("SurfaceTransactionStats")
.field("latch_time", &self.latch_time())
.field("present_fence_fd", &self.present_fence_fd())
.field("surface_controls", &DebugSurfaceControls(self))
.finish()
}
}

impl SurfaceTransactionStats {
/**
* Returns the timestamp of when the frame was latched by the framework. Once a frame is
Expand Down Expand Up @@ -842,12 +879,18 @@ pub struct SurfaceControls {
data: &'static mut [*mut ffi::ASurfaceControl],
}

impl SurfaceControls {
pub fn as_slice(&mut self) -> &mut [&mut SurfaceControl] {
impl AsMut<[SurfaceControl]> for SurfaceControls {
fn as_mut(&mut self) -> &mut [SurfaceControl] {
unsafe { std::mem::transmute_copy(&self.data) }
}
}

// impl SurfaceControls {
// pub fn as_slice(&mut self) -> &mut [SurfaceControl] {
// unsafe { std::mem::transmute_copy(&self.data) }
// }
// }

impl Drop for SurfaceControls {
/// Releases the array of [`SurfaceControl`]s that were returned by
/// [`SurfaceTransactionStats::surface_controls()`].
Expand All @@ -856,13 +899,10 @@ impl Drop for SurfaceControls {
unsafe { ffi::ASurfaceTransactionStats_releaseASurfaceControls(self.data.as_mut_ptr()) }
}
}
// impl<'a> Iterator for SurfaceControls
// {
// impl<'a> Iterator for SurfaceControls {
// type Item = &'a SurfaceControl;

// fn next(&mut self) -> Option<Self::Item> {

// }
// fn next(&mut self) -> Option<Self::Item> {}
// }

/// Parameter for [`SurfaceTransaction::set_visibility()`]`.
Expand Down

0 comments on commit 3aed1a6

Please sign in to comment.