Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 1 | // Copyright (C) 2023 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Pleasant Rust bindings for libnativewindow, including AHardwareBuffer |
| 16 | |
| 17 | extern crate nativewindow_bindgen as ffi; |
| 18 | |
Andrew Walbran | 9487efd | 2024-08-20 18:37:59 +0100 | [diff] [blame] | 19 | mod handle; |
Jiyong Park | 8032bff | 2024-04-01 16:17:15 +0900 | [diff] [blame] | 20 | mod surface; |
Andrew Walbran | 9487efd | 2024-08-20 18:37:59 +0100 | [diff] [blame] | 21 | |
| 22 | pub use handle::NativeHandle; |
Jiyong Park | 8032bff | 2024-04-01 16:17:15 +0900 | [diff] [blame] | 23 | pub use surface::Surface; |
Andrew Walbran | 8ee0ef1 | 2024-01-12 15:56:14 +0000 | [diff] [blame] | 24 | |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 25 | pub use ffi::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags}; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 26 | |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 27 | use binder::{ |
Andrew Walbran | e9573af | 2024-01-11 16:34:16 +0000 | [diff] [blame] | 28 | binder_impl::{BorrowedParcel, UnstructuredParcelable}, |
| 29 | impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable, |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 30 | unstable_api::{status_result, AsNative}, |
| 31 | StatusCode, |
| 32 | }; |
| 33 | use ffi::{AHardwareBuffer, AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel}; |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 34 | use std::fmt::{self, Debug, Formatter}; |
| 35 | use std::mem::ManuallyDrop; |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 36 | use std::ptr::{self, null_mut, NonNull}; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 37 | |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 38 | /// Wrapper around an opaque C `AHardwareBuffer`. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 39 | #[derive(PartialEq, Eq)] |
| 40 | pub struct HardwareBuffer(NonNull<AHardwareBuffer>); |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 41 | |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 42 | impl HardwareBuffer { |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 43 | /// Test whether the given format and usage flag combination is allocatable. If this function |
| 44 | /// returns true, it means that a buffer with the given description can be allocated on this |
| 45 | /// implementation, unless resource exhaustion occurs. If this function returns false, it means |
| 46 | /// that the allocation of the given description will never succeed. |
| 47 | /// |
| 48 | /// Available since API 29 |
| 49 | pub fn is_supported( |
| 50 | width: u32, |
| 51 | height: u32, |
| 52 | layers: u32, |
| 53 | format: AHardwareBuffer_Format::Type, |
| 54 | usage: AHardwareBuffer_UsageFlags, |
| 55 | stride: u32, |
| 56 | ) -> bool { |
| 57 | let buffer_desc = ffi::AHardwareBuffer_Desc { |
| 58 | width, |
| 59 | height, |
| 60 | layers, |
| 61 | format, |
| 62 | usage: usage.0, |
| 63 | stride, |
| 64 | rfu0: 0, |
| 65 | rfu1: 0, |
| 66 | }; |
| 67 | // SAFETY: *buffer_desc will never be null. |
| 68 | let status = unsafe { ffi::AHardwareBuffer_isSupported(&buffer_desc) }; |
| 69 | |
| 70 | status == 1 |
| 71 | } |
| 72 | |
| 73 | /// Allocates a buffer that matches the passed AHardwareBuffer_Desc. If allocation succeeds, the |
| 74 | /// buffer can be used according to the usage flags specified in its description. If a buffer is |
| 75 | /// used in ways not compatible with its usage flags, the results are undefined and may include |
| 76 | /// program termination. |
| 77 | /// |
| 78 | /// Available since API level 26. |
Jim Shargo | e4680d7 | 2023-08-07 16:46:45 +0000 | [diff] [blame] | 79 | #[inline] |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 80 | pub fn new( |
| 81 | width: u32, |
| 82 | height: u32, |
| 83 | layers: u32, |
| 84 | format: AHardwareBuffer_Format::Type, |
| 85 | usage: AHardwareBuffer_UsageFlags, |
| 86 | ) -> Option<Self> { |
| 87 | let buffer_desc = ffi::AHardwareBuffer_Desc { |
| 88 | width, |
| 89 | height, |
| 90 | layers, |
| 91 | format, |
| 92 | usage: usage.0, |
| 93 | stride: 0, |
| 94 | rfu0: 0, |
| 95 | rfu1: 0, |
| 96 | }; |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 97 | let mut ptr = ptr::null_mut(); |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 98 | // SAFETY: The returned pointer is valid until we drop/deallocate it. The function may fail |
| 99 | // and return a status, but we check it later. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 100 | let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut ptr) }; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 101 | |
| 102 | if status == 0 { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 103 | Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null"))) |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 104 | } else { |
| 105 | None |
| 106 | } |
| 107 | } |
| 108 | |
Andrew Walbran | a73d62c | 2024-08-20 17:20:56 +0100 | [diff] [blame^] | 109 | /// Creates a `HardwareBuffer` from a native handle. |
| 110 | /// |
| 111 | /// The native handle is cloned, so this doesn't take ownership of the original handle passed |
| 112 | /// in. |
| 113 | pub fn create_from_handle( |
| 114 | handle: &NativeHandle, |
| 115 | buffer_desc: &ffi::AHardwareBuffer_Desc, |
| 116 | ) -> Result<Self, StatusCode> { |
| 117 | let mut buffer = ptr::null_mut(); |
| 118 | // SAFETY: The caller guarantees that `handle` is valid, and the buffer pointer is valid |
| 119 | // because it comes from a reference. The method we pass means that |
| 120 | // `AHardwareBuffer_createFromHandle` will clone the handle rather than taking ownership of |
| 121 | // it. |
| 122 | let status = unsafe { |
| 123 | ffi::AHardwareBuffer_createFromHandle( |
| 124 | buffer_desc, |
| 125 | handle.as_raw().as_ptr(), |
| 126 | ffi::CreateFromHandleMethod_AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE |
| 127 | .try_into() |
| 128 | .unwrap(), |
| 129 | &mut buffer, |
| 130 | ) |
| 131 | }; |
| 132 | status_result(status)?; |
| 133 | Ok(Self(NonNull::new(buffer).expect("Allocated AHardwareBuffer was null"))) |
| 134 | } |
| 135 | |
| 136 | /// Returns a clone of the native handle of the buffer. |
| 137 | /// |
| 138 | /// Returns `None` if the operation fails for any reason. |
| 139 | pub fn cloned_native_handle(&self) -> Option<NativeHandle> { |
| 140 | // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid |
| 141 | // because it must have been allocated by `AHardwareBuffer_allocate`, |
| 142 | // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet |
| 143 | // released it. |
| 144 | let native_handle = unsafe { ffi::AHardwareBuffer_getNativeHandle(self.0.as_ptr()) }; |
| 145 | NonNull::new(native_handle.cast_mut()).and_then(|native_handle| { |
| 146 | // SAFETY: `AHardwareBuffer_getNativeHandle` should have returned a valid pointer which |
| 147 | // is valid at least as long as the buffer is, and `clone_from_raw` clones it rather |
| 148 | // than taking ownership of it so the original `native_handle` isn't stored. |
| 149 | unsafe { NativeHandle::clone_from_raw(native_handle) } |
| 150 | }) |
| 151 | } |
| 152 | |
Andrew Walbran | a0b3a9d | 2024-01-12 16:43:12 +0000 | [diff] [blame] | 153 | /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer. |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 154 | /// |
| 155 | /// # Safety |
| 156 | /// |
Andrew Walbran | a0b3a9d | 2024-01-12 16:43:12 +0000 | [diff] [blame] | 157 | /// This function takes ownership of the pointer and does NOT increment the refcount on the |
| 158 | /// buffer. If the caller uses the pointer after the created object is dropped it will cause |
| 159 | /// undefined behaviour. If the caller wants to continue using the pointer after calling this |
| 160 | /// then use [`clone_from_raw`](Self::clone_from_raw) instead. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 161 | pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self { |
| 162 | Self(buffer_ptr) |
| 163 | } |
| 164 | |
Andrew Walbran | a0b3a9d | 2024-01-12 16:43:12 +0000 | [diff] [blame] | 165 | /// Creates a new Rust HardwareBuffer to wrap the given AHardwareBuffer without taking ownership |
| 166 | /// of it. |
| 167 | /// |
| 168 | /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer. |
| 169 | /// This means that the caller can continue to use the raw buffer it passed in, and must call |
| 170 | /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to |
| 171 | /// avoid a memory leak. |
| 172 | /// |
| 173 | /// # Safety |
| 174 | /// |
| 175 | /// The buffer pointer must point to a valid `AHardwareBuffer`. |
| 176 | pub unsafe fn clone_from_raw(buffer: NonNull<AHardwareBuffer>) -> Self { |
| 177 | // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid. |
| 178 | unsafe { ffi::AHardwareBuffer_acquire(buffer.as_ptr()) }; |
| 179 | Self(buffer) |
| 180 | } |
| 181 | |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 182 | /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can |
| 183 | /// be used to provide a pointer to the AHB for a C/C++ API over the FFI. |
| 184 | pub fn into_raw(self) -> NonNull<AHardwareBuffer> { |
| 185 | let buffer = ManuallyDrop::new(self); |
| 186 | buffer.0 |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme |
| 190 | /// and undocumented circumstances. |
| 191 | /// |
| 192 | /// Available since API level 31. |
| 193 | pub fn id(&self) -> u64 { |
| 194 | let mut out_id = 0; |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 195 | // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid |
| 196 | // because it must have been allocated by `AHardwareBuffer_allocate`, |
| 197 | // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet |
| 198 | // released it. The id pointer must be valid because it comes from a reference. |
| 199 | let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) }; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 200 | assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}"); |
| 201 | |
| 202 | out_id |
| 203 | } |
| 204 | |
| 205 | /// Get the width of this buffer |
| 206 | pub fn width(&self) -> u32 { |
| 207 | self.description().width |
| 208 | } |
| 209 | |
| 210 | /// Get the height of this buffer |
| 211 | pub fn height(&self) -> u32 { |
| 212 | self.description().height |
| 213 | } |
| 214 | |
| 215 | /// Get the number of layers of this buffer |
| 216 | pub fn layers(&self) -> u32 { |
| 217 | self.description().layers |
| 218 | } |
| 219 | |
| 220 | /// Get the format of this buffer |
| 221 | pub fn format(&self) -> AHardwareBuffer_Format::Type { |
| 222 | self.description().format |
| 223 | } |
| 224 | |
| 225 | /// Get the usage bitvector of this buffer |
| 226 | pub fn usage(&self) -> AHardwareBuffer_UsageFlags { |
| 227 | AHardwareBuffer_UsageFlags(self.description().usage) |
| 228 | } |
| 229 | |
| 230 | /// Get the stride of this buffer |
| 231 | pub fn stride(&self) -> u32 { |
| 232 | self.description().stride |
| 233 | } |
| 234 | |
| 235 | fn description(&self) -> ffi::AHardwareBuffer_Desc { |
| 236 | let mut buffer_desc = ffi::AHardwareBuffer_Desc { |
| 237 | width: 0, |
| 238 | height: 0, |
| 239 | layers: 0, |
| 240 | format: 0, |
| 241 | usage: 0, |
| 242 | stride: 0, |
| 243 | rfu0: 0, |
| 244 | rfu1: 0, |
| 245 | }; |
| 246 | // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 247 | unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) }; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 248 | buffer_desc |
| 249 | } |
| 250 | } |
| 251 | |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 252 | impl Drop for HardwareBuffer { |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 253 | fn drop(&mut self) { |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 254 | // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid |
| 255 | // because it must have been allocated by `AHardwareBuffer_allocate`, |
| 256 | // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet |
| 257 | // released it. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 258 | unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) } |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 262 | impl Debug for HardwareBuffer { |
Andrew Walbran | 8ee0ef1 | 2024-01-12 15:56:14 +0000 | [diff] [blame] | 263 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 264 | f.debug_struct("HardwareBuffer").field("id", &self.id()).finish() |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | impl Clone for HardwareBuffer { |
| 269 | fn clone(&self) -> Self { |
| 270 | // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail. |
| 271 | unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) }; |
| 272 | Self(self.0) |
| 273 | } |
| 274 | } |
| 275 | |
Andrew Walbran | e9573af | 2024-01-11 16:34:16 +0000 | [diff] [blame] | 276 | impl UnstructuredParcelable for HardwareBuffer { |
| 277 | fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> { |
| 278 | let status = |
| 279 | // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid |
| 280 | // because it must have been allocated by `AHardwareBuffer_allocate`, |
| 281 | // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet |
| 282 | // released it. |
| 283 | unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) }; |
| 284 | status_result(status) |
| 285 | } |
| 286 | |
| 287 | fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> { |
| 288 | let mut buffer = null_mut(); |
| 289 | |
| 290 | let status = |
| 291 | // SAFETY: Both pointers must be valid because they are obtained from references. |
| 292 | // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special |
| 293 | // with them. If it returns success then it will have allocated a new |
| 294 | // `AHardwareBuffer` and incremented the reference count, so we can use it until we |
| 295 | // release it. |
| 296 | unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) }; |
| 297 | |
| 298 | status_result(status)?; |
| 299 | |
| 300 | Ok(Self( |
| 301 | NonNull::new(buffer).expect( |
| 302 | "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer", |
| 303 | ), |
| 304 | )) |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Andrew Walbran | e9573af | 2024-01-11 16:34:16 +0000 | [diff] [blame] | 308 | impl_deserialize_for_unstructured_parcelable!(HardwareBuffer); |
| 309 | impl_serialize_for_unstructured_parcelable!(HardwareBuffer); |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 310 | |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 311 | // SAFETY: The underlying *AHardwareBuffers can be moved between threads. |
| 312 | unsafe impl Send for HardwareBuffer {} |
| 313 | |
| 314 | // SAFETY: The underlying *AHardwareBuffers can be used from multiple threads. |
| 315 | // |
| 316 | // AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases |
| 317 | // where they are not immutable are: |
| 318 | // |
| 319 | // - reallocation (which is never actually done across the codebase and requires special |
| 320 | // privileges/platform code access to do) |
| 321 | // - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads |
| 322 | // according to the docs on the underlying gralloc calls) |
| 323 | unsafe impl Sync for HardwareBuffer {} |
| 324 | |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 325 | #[cfg(test)] |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 326 | mod test { |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 327 | use super::*; |
| 328 | |
| 329 | #[test] |
| 330 | fn create_valid_buffer_returns_ok() { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 331 | let buffer = HardwareBuffer::new( |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 332 | 512, |
| 333 | 512, |
| 334 | 1, |
| 335 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 336 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 337 | ); |
| 338 | assert!(buffer.is_some()); |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn create_invalid_buffer_returns_err() { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 343 | let buffer = HardwareBuffer::new(512, 512, 1, 0, AHardwareBuffer_UsageFlags(0)); |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 344 | assert!(buffer.is_none()); |
| 345 | } |
| 346 | |
| 347 | #[test] |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 348 | fn from_raw_allows_getters() { |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 349 | let buffer_desc = ffi::AHardwareBuffer_Desc { |
| 350 | width: 1024, |
| 351 | height: 512, |
| 352 | layers: 1, |
| 353 | format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 354 | usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0, |
| 355 | stride: 0, |
| 356 | rfu0: 0, |
| 357 | rfu1: 0, |
| 358 | }; |
| 359 | let mut raw_buffer_ptr = ptr::null_mut(); |
| 360 | |
Andrew Walbran | 03350bc | 2023-08-03 16:02:51 +0000 | [diff] [blame] | 361 | // SAFETY: The pointers are valid because they come from references, and |
| 362 | // `AHardwareBuffer_allocate` doesn't retain them after it returns. |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 363 | let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) }; |
| 364 | assert_eq!(status, 0); |
| 365 | |
Andrew Walbran | 03350bc | 2023-08-03 16:02:51 +0000 | [diff] [blame] | 366 | // SAFETY: The pointer must be valid because it was just allocated successfully, and we |
| 367 | // don't use it after calling this. |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 368 | let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) }; |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 369 | assert_eq!(buffer.width(), 1024); |
| 370 | } |
| 371 | |
| 372 | #[test] |
| 373 | fn basic_getters() { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 374 | let buffer = HardwareBuffer::new( |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 375 | 1024, |
| 376 | 512, |
| 377 | 1, |
| 378 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 379 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 380 | ) |
| 381 | .expect("Buffer with some basic parameters was not created successfully"); |
| 382 | |
| 383 | assert_eq!(buffer.width(), 1024); |
| 384 | assert_eq!(buffer.height(), 512); |
| 385 | assert_eq!(buffer.layers(), 1); |
| 386 | assert_eq!(buffer.format(), AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM); |
| 387 | assert_eq!( |
| 388 | buffer.usage(), |
| 389 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
| 390 | ); |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn id_getter() { |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 395 | let buffer = HardwareBuffer::new( |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 396 | 1024, |
| 397 | 512, |
| 398 | 1, |
| 399 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 400 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 401 | ) |
| 402 | .expect("Buffer with some basic parameters was not created successfully"); |
| 403 | |
| 404 | assert_ne!(0, buffer.id()); |
| 405 | } |
Jim Shargo | b69c6ef | 2023-10-05 22:54:51 +0000 | [diff] [blame] | 406 | |
| 407 | #[test] |
| 408 | fn clone() { |
| 409 | let buffer = HardwareBuffer::new( |
| 410 | 1024, |
| 411 | 512, |
| 412 | 1, |
| 413 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 414 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 415 | ) |
| 416 | .expect("Buffer with some basic parameters was not created successfully"); |
| 417 | let buffer2 = buffer.clone(); |
| 418 | |
| 419 | assert_eq!(buffer, buffer2); |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn into_raw() { |
| 424 | let buffer = HardwareBuffer::new( |
| 425 | 1024, |
| 426 | 512, |
| 427 | 1, |
| 428 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 429 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 430 | ) |
| 431 | .expect("Buffer with some basic parameters was not created successfully"); |
| 432 | let buffer2 = buffer.clone(); |
| 433 | |
| 434 | let raw_buffer = buffer.into_raw(); |
| 435 | // SAFETY: This is the same pointer we had before. |
| 436 | let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) }; |
| 437 | |
| 438 | assert_eq!(remade_buffer, buffer2); |
| 439 | } |
Andrew Walbran | a73d62c | 2024-08-20 17:20:56 +0100 | [diff] [blame^] | 440 | |
| 441 | #[test] |
| 442 | fn native_handle_and_back() { |
| 443 | let buffer = HardwareBuffer::new( |
| 444 | 1024, |
| 445 | 512, |
| 446 | 1, |
| 447 | AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 448 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 449 | ) |
| 450 | .expect("Buffer with some basic parameters was not created successfully"); |
| 451 | |
| 452 | let native_handle = |
| 453 | buffer.cloned_native_handle().expect("Failed to get native handle for buffer"); |
| 454 | let buffer_desc = ffi::AHardwareBuffer_Desc { |
| 455 | width: 1024, |
| 456 | height: 512, |
| 457 | layers: 1, |
| 458 | format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 459 | usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0, |
| 460 | stride: 1024, |
| 461 | rfu0: 0, |
| 462 | rfu1: 0, |
| 463 | }; |
| 464 | let buffer2 = HardwareBuffer::create_from_handle(&native_handle, &buffer_desc) |
| 465 | .expect("Failed to create buffer from native handle"); |
| 466 | |
| 467 | assert_eq!(buffer.description(), buffer_desc); |
| 468 | assert_eq!(buffer2.description(), buffer_desc); |
| 469 | } |
Jim Shargo | 7df9f75 | 2023-07-18 20:33:45 +0000 | [diff] [blame] | 470 | } |