blob: 014c912a6787b8b5ff5ed5bde9c69d55e95b983f [file] [log] [blame]
Jim Shargo7df9f752023-07-18 20:33:45 +00001// 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
17extern crate nativewindow_bindgen as ffi;
18
Andrew Walbran9487efd2024-08-20 18:37:59 +010019mod handle;
Jiyong Park8032bff2024-04-01 16:17:15 +090020mod surface;
Andrew Walbran9487efd2024-08-20 18:37:59 +010021
Andrew Walbran43bddb62023-09-01 16:43:09 +010022pub use ffi::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
Andrew Walbran30262982024-09-27 16:38:42 +010023pub use handle::NativeHandle;
24pub use surface::{buffer::Buffer, Surface};
Jim Shargo7df9f752023-07-18 20:33:45 +000025
Andrew Walbran43bddb62023-09-01 16:43:09 +010026use binder::{
Andrew Walbrane9573af2024-01-11 16:34:16 +000027 binder_impl::{BorrowedParcel, UnstructuredParcelable},
28 impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
Andrew Walbran43bddb62023-09-01 16:43:09 +010029 unstable_api::{status_result, AsNative},
30 StatusCode,
31};
Andrew Walbranabc932e2024-08-30 14:10:29 +010032use ffi::{
33 AHardwareBuffer, AHardwareBuffer_Desc, AHardwareBuffer_readFromParcel,
34 AHardwareBuffer_writeToParcel,
35};
Jim Shargob69c6ef2023-10-05 22:54:51 +000036use std::fmt::{self, Debug, Formatter};
37use std::mem::ManuallyDrop;
Andrew Walbran43bddb62023-09-01 16:43:09 +010038use std::ptr::{self, null_mut, NonNull};
Jim Shargo7df9f752023-07-18 20:33:45 +000039
Andrew Walbranabc932e2024-08-30 14:10:29 +010040/// Wrapper around a C `AHardwareBuffer_Desc`.
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct HardwareBufferDescription(AHardwareBuffer_Desc);
43
44impl HardwareBufferDescription {
45 /// Creates a new `HardwareBufferDescription` with the given parameters.
46 pub fn new(
47 width: u32,
48 height: u32,
49 layers: u32,
50 format: AHardwareBuffer_Format::Type,
51 usage: AHardwareBuffer_UsageFlags,
52 stride: u32,
53 ) -> Self {
54 Self(AHardwareBuffer_Desc {
55 width,
56 height,
57 layers,
58 format,
59 usage: usage.0,
60 stride,
61 rfu0: 0,
62 rfu1: 0,
63 })
64 }
65
66 /// Returns the width from the buffer description.
67 pub fn width(&self) -> u32 {
68 self.0.width
69 }
70
71 /// Returns the height from the buffer description.
72 pub fn height(&self) -> u32 {
73 self.0.height
74 }
75
76 /// Returns the number from layers from the buffer description.
77 pub fn layers(&self) -> u32 {
78 self.0.layers
79 }
80
81 /// Returns the format from the buffer description.
82 pub fn format(&self) -> AHardwareBuffer_Format::Type {
83 self.0.format
84 }
85
86 /// Returns the usage bitvector from the buffer description.
87 pub fn usage(&self) -> AHardwareBuffer_UsageFlags {
88 AHardwareBuffer_UsageFlags(self.0.usage)
89 }
90
91 /// Returns the stride from the buffer description.
92 pub fn stride(&self) -> u32 {
93 self.0.stride
94 }
95}
96
97impl Default for HardwareBufferDescription {
98 fn default() -> Self {
99 Self(AHardwareBuffer_Desc {
100 width: 0,
101 height: 0,
102 layers: 0,
103 format: 0,
104 usage: 0,
105 stride: 0,
106 rfu0: 0,
107 rfu1: 0,
108 })
109 }
110}
111
Andrew Walbran43bddb62023-09-01 16:43:09 +0100112/// Wrapper around an opaque C `AHardwareBuffer`.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000113#[derive(PartialEq, Eq)]
114pub struct HardwareBuffer(NonNull<AHardwareBuffer>);
Jim Shargo7df9f752023-07-18 20:33:45 +0000115
Jim Shargob69c6ef2023-10-05 22:54:51 +0000116impl HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000117 /// Test whether the given format and usage flag combination is allocatable. If this function
118 /// returns true, it means that a buffer with the given description can be allocated on this
119 /// implementation, unless resource exhaustion occurs. If this function returns false, it means
120 /// that the allocation of the given description will never succeed.
121 ///
122 /// Available since API 29
Andrew Walbranabc932e2024-08-30 14:10:29 +0100123 pub fn is_supported(buffer_description: &HardwareBufferDescription) -> bool {
124 // SAFETY: The pointer comes from a reference so must be valid.
125 let status = unsafe { ffi::AHardwareBuffer_isSupported(&buffer_description.0) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000126
127 status == 1
128 }
129
130 /// Allocates a buffer that matches the passed AHardwareBuffer_Desc. If allocation succeeds, the
131 /// buffer can be used according to the usage flags specified in its description. If a buffer is
132 /// used in ways not compatible with its usage flags, the results are undefined and may include
133 /// program termination.
134 ///
135 /// Available since API level 26.
Jim Shargoe4680d72023-08-07 16:46:45 +0000136 #[inline]
Andrew Walbranabc932e2024-08-30 14:10:29 +0100137 pub fn new(buffer_description: &HardwareBufferDescription) -> Option<Self> {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000138 let mut ptr = ptr::null_mut();
Jim Shargo7df9f752023-07-18 20:33:45 +0000139 // SAFETY: The returned pointer is valid until we drop/deallocate it. The function may fail
140 // and return a status, but we check it later.
Andrew Walbranabc932e2024-08-30 14:10:29 +0100141 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_description.0, &mut ptr) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000142
143 if status == 0 {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000144 Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null")))
Jim Shargo7df9f752023-07-18 20:33:45 +0000145 } else {
146 None
147 }
148 }
149
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100150 /// Creates a `HardwareBuffer` from a native handle.
151 ///
152 /// The native handle is cloned, so this doesn't take ownership of the original handle passed
153 /// in.
154 pub fn create_from_handle(
155 handle: &NativeHandle,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100156 buffer_description: &HardwareBufferDescription,
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100157 ) -> Result<Self, StatusCode> {
158 let mut buffer = ptr::null_mut();
159 // SAFETY: The caller guarantees that `handle` is valid, and the buffer pointer is valid
160 // because it comes from a reference. The method we pass means that
161 // `AHardwareBuffer_createFromHandle` will clone the handle rather than taking ownership of
162 // it.
163 let status = unsafe {
164 ffi::AHardwareBuffer_createFromHandle(
Andrew Walbranabc932e2024-08-30 14:10:29 +0100165 &buffer_description.0,
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100166 handle.as_raw().as_ptr(),
167 ffi::CreateFromHandleMethod_AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE
168 .try_into()
169 .unwrap(),
170 &mut buffer,
171 )
172 };
173 status_result(status)?;
174 Ok(Self(NonNull::new(buffer).expect("Allocated AHardwareBuffer was null")))
175 }
176
177 /// Returns a clone of the native handle of the buffer.
178 ///
179 /// Returns `None` if the operation fails for any reason.
180 pub fn cloned_native_handle(&self) -> Option<NativeHandle> {
181 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
182 // because it must have been allocated by `AHardwareBuffer_allocate`,
183 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
184 // released it.
185 let native_handle = unsafe { ffi::AHardwareBuffer_getNativeHandle(self.0.as_ptr()) };
186 NonNull::new(native_handle.cast_mut()).and_then(|native_handle| {
187 // SAFETY: `AHardwareBuffer_getNativeHandle` should have returned a valid pointer which
188 // is valid at least as long as the buffer is, and `clone_from_raw` clones it rather
189 // than taking ownership of it so the original `native_handle` isn't stored.
190 unsafe { NativeHandle::clone_from_raw(native_handle) }
191 })
192 }
193
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000194 /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
Jim Shargo7df9f752023-07-18 20:33:45 +0000195 ///
196 /// # Safety
197 ///
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000198 /// This function takes ownership of the pointer and does NOT increment the refcount on the
199 /// buffer. If the caller uses the pointer after the created object is dropped it will cause
200 /// undefined behaviour. If the caller wants to continue using the pointer after calling this
201 /// then use [`clone_from_raw`](Self::clone_from_raw) instead.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000202 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
203 Self(buffer_ptr)
204 }
205
Andrew Walbran7e947d32024-10-23 17:03:59 +0100206 /// Creates a new Rust HardwareBuffer to wrap the given `AHardwareBuffer` without taking
207 /// ownership of it.
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000208 ///
209 /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer.
210 /// This means that the caller can continue to use the raw buffer it passed in, and must call
211 /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to
212 /// avoid a memory leak.
213 ///
214 /// # Safety
215 ///
216 /// The buffer pointer must point to a valid `AHardwareBuffer`.
217 pub unsafe fn clone_from_raw(buffer: NonNull<AHardwareBuffer>) -> Self {
218 // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid.
219 unsafe { ffi::AHardwareBuffer_acquire(buffer.as_ptr()) };
220 Self(buffer)
221 }
222
Andrew Walbran7e947d32024-10-23 17:03:59 +0100223 /// Returns the internal `AHardwareBuffer` pointer.
224 ///
225 /// This is only valid as long as this `HardwareBuffer` exists, so shouldn't be stored. It can
226 /// be used to provide a pointer for a C/C++ API over FFI.
Ren-Pei Zeng8237ba62024-10-22 15:20:18 +0000227 pub fn as_raw(&self) -> NonNull<AHardwareBuffer> {
228 self.0
229 }
230
Andrew Walbran7e947d32024-10-23 17:03:59 +0100231 /// Gets the internal `AHardwareBuffer` pointer without decrementing the refcount. This can
232 /// be used for a C/C++ API which takes ownership of the pointer.
233 ///
234 /// The caller is responsible for releasing the `AHardwareBuffer` pointer by calling
235 /// `AHardwareBuffer_release` when it is finished with it, or may convert it back to a Rust
236 /// `HardwareBuffer` by calling [`HardwareBuffer::from_raw`].
Jim Shargob69c6ef2023-10-05 22:54:51 +0000237 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
238 let buffer = ManuallyDrop::new(self);
239 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000240 }
241
242 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
243 /// and undocumented circumstances.
244 ///
245 /// Available since API level 31.
246 pub fn id(&self) -> u64 {
247 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100248 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
249 // because it must have been allocated by `AHardwareBuffer_allocate`,
250 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
251 // released it. The id pointer must be valid because it comes from a reference.
252 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000253 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
254
255 out_id
256 }
257
Andrew Walbranabc932e2024-08-30 14:10:29 +0100258 /// Returns the description of this buffer.
259 pub fn description(&self) -> HardwareBufferDescription {
Jim Shargo7df9f752023-07-18 20:33:45 +0000260 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
261 width: 0,
262 height: 0,
263 layers: 0,
264 format: 0,
265 usage: 0,
266 stride: 0,
267 rfu0: 0,
268 rfu1: 0,
269 };
270 // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000271 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100272 HardwareBufferDescription(buffer_desc)
Jim Shargo7df9f752023-07-18 20:33:45 +0000273 }
274}
275
Jim Shargob69c6ef2023-10-05 22:54:51 +0000276impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000277 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100278 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
279 // because it must have been allocated by `AHardwareBuffer_allocate`,
280 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
281 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000282 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000283 }
284}
285
Jim Shargob69c6ef2023-10-05 22:54:51 +0000286impl Debug for HardwareBuffer {
Andrew Walbran8ee0ef12024-01-12 15:56:14 +0000287 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000288 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
289 }
290}
291
292impl Clone for HardwareBuffer {
293 fn clone(&self) -> Self {
294 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
295 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
296 Self(self.0)
297 }
298}
299
Andrew Walbrane9573af2024-01-11 16:34:16 +0000300impl UnstructuredParcelable for HardwareBuffer {
301 fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
302 let status =
303 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
304 // because it must have been allocated by `AHardwareBuffer_allocate`,
305 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
306 // released it.
307 unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
308 status_result(status)
309 }
310
311 fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
312 let mut buffer = null_mut();
313
314 let status =
315 // SAFETY: Both pointers must be valid because they are obtained from references.
316 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
317 // with them. If it returns success then it will have allocated a new
318 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
319 // release it.
320 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
321
322 status_result(status)?;
323
324 Ok(Self(
325 NonNull::new(buffer).expect(
326 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
327 ),
328 ))
Andrew Walbran43bddb62023-09-01 16:43:09 +0100329 }
330}
331
Andrew Walbrane9573af2024-01-11 16:34:16 +0000332impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
333impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100334
Jim Shargob69c6ef2023-10-05 22:54:51 +0000335// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
336unsafe impl Send for HardwareBuffer {}
337
338// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
339//
340// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
341// where they are not immutable are:
342//
343// - reallocation (which is never actually done across the codebase and requires special
344// privileges/platform code access to do)
345// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
346// according to the docs on the underlying gralloc calls)
347unsafe impl Sync for HardwareBuffer {}
348
Jim Shargo7df9f752023-07-18 20:33:45 +0000349#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000350mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000351 use super::*;
352
353 #[test]
354 fn create_valid_buffer_returns_ok() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100355 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000356 512,
357 512,
358 1,
359 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
360 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100361 0,
362 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000363 assert!(buffer.is_some());
364 }
365
366 #[test]
367 fn create_invalid_buffer_returns_err() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100368 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
369 512,
370 512,
371 1,
372 0,
373 AHardwareBuffer_UsageFlags(0),
374 0,
375 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000376 assert!(buffer.is_none());
377 }
378
379 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000380 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000381 let buffer_desc = ffi::AHardwareBuffer_Desc {
382 width: 1024,
383 height: 512,
384 layers: 1,
385 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
386 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
387 stride: 0,
388 rfu0: 0,
389 rfu1: 0,
390 };
391 let mut raw_buffer_ptr = ptr::null_mut();
392
Andrew Walbran03350bc2023-08-03 16:02:51 +0000393 // SAFETY: The pointers are valid because they come from references, and
394 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000395 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
396 assert_eq!(status, 0);
397
Andrew Walbran03350bc2023-08-03 16:02:51 +0000398 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
399 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000400 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100401 assert_eq!(buffer.description().width(), 1024);
Jim Shargo7df9f752023-07-18 20:33:45 +0000402 }
403
404 #[test]
405 fn basic_getters() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100406 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000407 1024,
408 512,
409 1,
410 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
411 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100412 0,
413 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000414 .expect("Buffer with some basic parameters was not created successfully");
415
Andrew Walbranabc932e2024-08-30 14:10:29 +0100416 let description = buffer.description();
417 assert_eq!(description.width(), 1024);
418 assert_eq!(description.height(), 512);
419 assert_eq!(description.layers(), 1);
Jim Shargo7df9f752023-07-18 20:33:45 +0000420 assert_eq!(
Andrew Walbranabc932e2024-08-30 14:10:29 +0100421 description.format(),
422 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
423 );
424 assert_eq!(
425 description.usage(),
Jim Shargo7df9f752023-07-18 20:33:45 +0000426 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
427 );
428 }
429
430 #[test]
431 fn id_getter() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100432 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000433 1024,
434 512,
435 1,
436 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
437 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100438 0,
439 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000440 .expect("Buffer with some basic parameters was not created successfully");
441
442 assert_ne!(0, buffer.id());
443 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000444
445 #[test]
446 fn clone() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100447 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000448 1024,
449 512,
450 1,
451 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
452 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100453 0,
454 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000455 .expect("Buffer with some basic parameters was not created successfully");
456 let buffer2 = buffer.clone();
457
458 assert_eq!(buffer, buffer2);
459 }
460
461 #[test]
462 fn into_raw() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100463 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000464 1024,
465 512,
466 1,
467 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
468 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100469 0,
470 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000471 .expect("Buffer with some basic parameters was not created successfully");
472 let buffer2 = buffer.clone();
473
474 let raw_buffer = buffer.into_raw();
475 // SAFETY: This is the same pointer we had before.
476 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
477
478 assert_eq!(remade_buffer, buffer2);
479 }
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100480
481 #[test]
482 fn native_handle_and_back() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100483 let buffer_description = HardwareBufferDescription::new(
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100484 1024,
485 512,
486 1,
487 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
488 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100489 1024,
490 );
491 let buffer = HardwareBuffer::new(&buffer_description)
492 .expect("Buffer with some basic parameters was not created successfully");
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100493
494 let native_handle =
495 buffer.cloned_native_handle().expect("Failed to get native handle for buffer");
Andrew Walbranabc932e2024-08-30 14:10:29 +0100496 let buffer2 = HardwareBuffer::create_from_handle(&native_handle, &buffer_description)
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100497 .expect("Failed to create buffer from native handle");
498
Andrew Walbranabc932e2024-08-30 14:10:29 +0100499 assert_eq!(buffer.description(), buffer_description);
500 assert_eq!(buffer2.description(), buffer_description);
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100501 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000502}