diff --git a/ndk/src/hardware_buffer.rs b/ndk/src/hardware_buffer.rs index 25a503c5..63849f38 100644 --- a/ndk/src/hardware_buffer.rs +++ b/ndk/src/hardware_buffer.rs @@ -210,7 +210,9 @@ impl HardwareBuffer { width: desc.width, height: desc.height, layers: desc.layers, - format: desc.format.into(), + format: i32::try_from(desc.format) + .expect("i32->u32 overflow in HardwareBuffer::describe()") + .into(), usage: HardwareBufferUsage(ffi::AHardwareBuffer_UsageFlags(desc.usage)), stride: desc.stride, } @@ -496,7 +498,9 @@ impl HardwareBufferDesc { width: self.width, height: self.height, layers: self.layers, - format: self.format.into(), + format: i32::from(self.format) + .try_into() + .expect("i32->u32 overflow in HardwareBufferDesc::into_native()"), usage: self.usage.0 .0, stride: self.stride, rfu0: 0, diff --git a/ndk/src/hardware_buffer_format.rs b/ndk/src/hardware_buffer_format.rs index 4db5d276..555669f5 100644 --- a/ndk/src/hardware_buffer_format.rs +++ b/ndk/src/hardware_buffer_format.rs @@ -5,62 +5,66 @@ use num_enum::{FromPrimitive, IntoPrimitive}; /// Buffer pixel formats. -#[repr(u32)] +#[repr(i32)] #[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] #[allow(non_camel_case_types)] #[non_exhaustive] pub enum HardwareBufferFormat { /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBA_8888`].0. #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM")] - R8G8B8A8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM.0, + R8G8B8A8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM.0 as i32, /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBX_8888`].0. #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM")] - R8G8B8X8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM.0, + R8G8B8X8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM")] - R8G8B8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM.0, + R8G8B8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM.0 as i32, /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGB_565`].0. #[doc(alias = "AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM")] - R5G6B5_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM.0, + R5G6B5_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT")] - R16G16B16A16_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT.0, + R16G16B16A16_FLOAT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM")] - R10G10B10A2_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM.0, + R10G10B10A2_UNORM = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_BLOB")] - BLOB = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_BLOB.0, + BLOB = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_BLOB.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_D16_UNORM")] - D16_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D16_UNORM.0, + D16_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D16_UNORM.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM")] - D24_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM.0, + D24_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT")] - D24_UNORM_S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT.0, + D24_UNORM_S8_UINT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT")] - D32_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT.0, + D32_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT")] - D32_FLOAT_S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT.0, + D32_FLOAT_S8_UINT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_S8_UINT")] - S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT.0, + S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420")] - Y8Cb8Cr8_420 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420.0, + Y8Cb8Cr8_420 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420.0 as i32, #[cfg(feature = "api-level-26")] #[doc(alias = "AHARDWAREBUFFER_FORMAT_YCbCr_P010")] - YCbCr_P010 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_YCbCr_P010.0, + YCbCr_P010 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_YCbCr_P010.0 as i32, #[cfg(feature = "api-level-26")] - R8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8_UNORM.0, + R8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8_UNORM.0 as i32, #[doc(hidden)] #[num_enum(catch_all)] - __Unknown(u32), + __Unknown(i32), } impl HardwareBufferFormat { diff --git a/ndk/src/native_activity.rs b/ndk/src/native_activity.rs index cb889638..b30c49a9 100644 --- a/ndk/src/native_activity.rs +++ b/ndk/src/native_activity.rs @@ -216,14 +216,7 @@ impl NativeActivity { /// /// [`getWindow().setFormat()`]: https://developer.android.com/reference/android/view/Window#setFormat(int) pub fn set_window_format(&self, format: HardwareBufferFormat) { - unsafe { - ffi::ANativeActivity_setWindowFormat( - self.ptr.as_ptr(), - u32::from(format) - .try_into() - .expect("i32 overflow in set_window_format()"), - ) - } + unsafe { ffi::ANativeActivity_setWindowFormat(self.ptr.as_ptr(), format.into()) } } /// Change the window flags of the given activity. diff --git a/ndk/src/native_window.rs b/ndk/src/native_window.rs index 2054263d..4a8657f6 100644 --- a/ndk/src/native_window.rs +++ b/ndk/src/native_window.rs @@ -104,7 +104,6 @@ impl NativeWindow { /// Return the current pixel format ([`HardwareBufferFormat`]) of the window surface. pub fn format(&self) -> HardwareBufferFormat { let value = unsafe { ffi::ANativeWindow_getFormat(self.ptr.as_ptr()) }; - let value = u32::try_from(value).unwrap(); value.into() } @@ -123,11 +122,7 @@ impl NativeWindow { height: i32, format: Option, ) -> io::Result<()> { - let format = format.map_or(0, |f| { - u32::from(f) - .try_into() - .expect("i32 overflow in set_buffers_geometry") - }); + let format = format.map_or(0i32, |f| f.into()); let status = unsafe { ffi::ANativeWindow_setBuffersGeometry(self.ptr.as_ptr(), width, height, format) }; @@ -324,8 +319,7 @@ impl<'a> NativeWindowBufferLockGuard<'a> { /// The format of the buffer. One of [`HardwareBufferFormat`]. pub fn format(&self) -> HardwareBufferFormat { - let format = u32::try_from(self.buffer.format).unwrap(); - format.into() + self.buffer.format.into() } /// The actual bits.