Skip to content

Commit

Permalink
ndk: Add ASurfaceControl bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Dec 22, 2023
1 parent 9d0edf3 commit 350cf92
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions ndk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod media_error;
pub mod native_activity;
pub mod native_window;
pub mod shared_memory;
pub mod surface_control;
pub mod surface_texture;
pub mod sync;
pub mod trace;
Expand Down
66 changes: 66 additions & 0 deletions ndk/src/surface_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Bindings for [`ASurfaceControl`], [`ASurfaceTransaction`]
use std::{ffi::CStr, ptr::NonNull};

use crate::native_window::NativeWindow;

#[doc(alias = "ASurfaceControl")]
pub struct SurfaceControl {
ptr: NonNull<ffi::ASurfaceControl>,
}

impl SurfaceControl {
/// Assumes ownership of `ptr`
///
/// # Safety
/// `ptr` must be a valid pointer to an Android [`ffi::ASurfaceControl`].
pub unsafe fn from_ptr(ptr: NonNull<ffi::ASurfaceControl>) -> Self {
Self { ptr }
}

/// Acquires ownership of `ptr`
///
/// # Safety
/// `ptr` must be a valid pointer to an Android [`ffi::ASurfaceControl`].
pub unsafe fn clone_from_ptr(ptr: NonNull<ffi::ASurfaceControl>) -> Self {
ffi::ASurfaceControl_acquire(ptr.as_ptr());
Self::from_ptr(ptr)
}

/// BORROW
#[doc(alias = "ASurfaceControl_createFromWindow")]
pub fn create_from_window(parent: NativeWindow, debug_name: &CStr) -> Option<Self> {
let ptr = unsafe {
ffi::ASurfaceControl_createFromWindow(parent.ptr().as_ptr(), debug_name.as_ptr())
};
let s = NonNull::new(ptr)?;
Some(unsafe { Self::from_ptr(s) })
}

#[doc(alias = "ASurfaceControl_create")]
pub fn create(parent: &Self, debug_name: &CStr) -> Option<Self> {
let ptr = unsafe { ffi::ASurfaceControl_create(parent.ptr.as_ptr(), debug_name.as_ptr()) };
// NonNull::new(ptr).map(|ptr| Self { ptr })
let s = NonNull::new(ptr)?;
Some(unsafe { Self::from_ptr(s) })
}

pub fn ptr(&self) -> NonNull<ffi::ASurfaceControl> {
self.ptr
}
}

impl Drop for SurfaceControl {
#[doc(alias = "ASurfaceControl_release")]
fn drop(&mut self) {
unsafe { ffi::ASurfaceControl_release(self.ptr.as_ptr()) }
}
}

impl Clone for SurfaceControl {
#[doc(alias = "ASurfaceControl_acquire")]
fn clone(&self) -> Self {
unsafe { ffi::ASurfaceControl_acquire(self.ptr.as_ptr()) }
Self { ptr: self.ptr }
}
}

0 comments on commit 350cf92

Please sign in to comment.