blob: f19b908074dcedf44ac411ce786de5ae987064f7 [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 Walbrana0b3a9d2024-01-12 16:43:12 +0000206 /// Creates a new Rust HardwareBuffer to wrap the given AHardwareBuffer without taking ownership
207 /// of it.
208 ///
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
Jim Shargob69c6ef2023-10-05 22:54:51 +0000223 /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can
224 /// be used to provide a pointer to the AHB for a C/C++ API over the FFI.
225 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
226 let buffer = ManuallyDrop::new(self);
227 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000228 }
229
230 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
231 /// and undocumented circumstances.
232 ///
233 /// Available since API level 31.
234 pub fn id(&self) -> u64 {
235 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100236 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
237 // because it must have been allocated by `AHardwareBuffer_allocate`,
238 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
239 // released it. The id pointer must be valid because it comes from a reference.
240 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000241 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
242
243 out_id
244 }
245
Andrew Walbranabc932e2024-08-30 14:10:29 +0100246 /// Returns the description of this buffer.
247 pub fn description(&self) -> HardwareBufferDescription {
Jim Shargo7df9f752023-07-18 20:33:45 +0000248 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
249 width: 0,
250 height: 0,
251 layers: 0,
252 format: 0,
253 usage: 0,
254 stride: 0,
255 rfu0: 0,
256 rfu1: 0,
257 };
258 // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000259 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100260 HardwareBufferDescription(buffer_desc)
Jim Shargo7df9f752023-07-18 20:33:45 +0000261 }
262}
263
Jim Shargob69c6ef2023-10-05 22:54:51 +0000264impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000265 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100266 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
267 // because it must have been allocated by `AHardwareBuffer_allocate`,
268 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
269 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000270 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000271 }
272}
273
Jim Shargob69c6ef2023-10-05 22:54:51 +0000274impl Debug for HardwareBuffer {
Andrew Walbran8ee0ef12024-01-12 15:56:14 +0000275 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000276 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
277 }
278}
279
280impl Clone for HardwareBuffer {
281 fn clone(&self) -> Self {
282 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
283 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
284 Self(self.0)
285 }
286}
287
Andrew Walbrane9573af2024-01-11 16:34:16 +0000288impl UnstructuredParcelable for HardwareBuffer {
289 fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
290 let status =
291 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
292 // because it must have been allocated by `AHardwareBuffer_allocate`,
293 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
294 // released it.
295 unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
296 status_result(status)
297 }
298
299 fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
300 let mut buffer = null_mut();
301
302 let status =
303 // SAFETY: Both pointers must be valid because they are obtained from references.
304 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
305 // with them. If it returns success then it will have allocated a new
306 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
307 // release it.
308 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
309
310 status_result(status)?;
311
312 Ok(Self(
313 NonNull::new(buffer).expect(
314 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
315 ),
316 ))
Andrew Walbran43bddb62023-09-01 16:43:09 +0100317 }
318}
319
Andrew Walbrane9573af2024-01-11 16:34:16 +0000320impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
321impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100322
Jim Shargob69c6ef2023-10-05 22:54:51 +0000323// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
324unsafe impl Send for HardwareBuffer {}
325
326// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
327//
328// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
329// where they are not immutable are:
330//
331// - reallocation (which is never actually done across the codebase and requires special
332// privileges/platform code access to do)
333// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
334// according to the docs on the underlying gralloc calls)
335unsafe impl Sync for HardwareBuffer {}
336
Jim Shargo7df9f752023-07-18 20:33:45 +0000337#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000338mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000339 use super::*;
340
341 #[test]
342 fn create_valid_buffer_returns_ok() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100343 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000344 512,
345 512,
346 1,
347 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
348 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100349 0,
350 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000351 assert!(buffer.is_some());
352 }
353
354 #[test]
355 fn create_invalid_buffer_returns_err() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100356 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
357 512,
358 512,
359 1,
360 0,
361 AHardwareBuffer_UsageFlags(0),
362 0,
363 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000364 assert!(buffer.is_none());
365 }
366
367 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000368 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000369 let buffer_desc = ffi::AHardwareBuffer_Desc {
370 width: 1024,
371 height: 512,
372 layers: 1,
373 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
374 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
375 stride: 0,
376 rfu0: 0,
377 rfu1: 0,
378 };
379 let mut raw_buffer_ptr = ptr::null_mut();
380
Andrew Walbran03350bc2023-08-03 16:02:51 +0000381 // SAFETY: The pointers are valid because they come from references, and
382 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000383 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
384 assert_eq!(status, 0);
385
Andrew Walbran03350bc2023-08-03 16:02:51 +0000386 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
387 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000388 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100389 assert_eq!(buffer.description().width(), 1024);
Jim Shargo7df9f752023-07-18 20:33:45 +0000390 }
391
392 #[test]
393 fn basic_getters() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100394 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000395 1024,
396 512,
397 1,
398 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
399 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100400 0,
401 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000402 .expect("Buffer with some basic parameters was not created successfully");
403
Andrew Walbranabc932e2024-08-30 14:10:29 +0100404 let description = buffer.description();
405 assert_eq!(description.width(), 1024);
406 assert_eq!(description.height(), 512);
407 assert_eq!(description.layers(), 1);
Jim Shargo7df9f752023-07-18 20:33:45 +0000408 assert_eq!(
Andrew Walbranabc932e2024-08-30 14:10:29 +0100409 description.format(),
410 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
411 );
412 assert_eq!(
413 description.usage(),
Jim Shargo7df9f752023-07-18 20:33:45 +0000414 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
415 );
416 }
417
418 #[test]
419 fn id_getter() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100420 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000421 1024,
422 512,
423 1,
424 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
425 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100426 0,
427 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000428 .expect("Buffer with some basic parameters was not created successfully");
429
430 assert_ne!(0, buffer.id());
431 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000432
433 #[test]
434 fn clone() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100435 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000436 1024,
437 512,
438 1,
439 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
440 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100441 0,
442 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000443 .expect("Buffer with some basic parameters was not created successfully");
444 let buffer2 = buffer.clone();
445
446 assert_eq!(buffer, buffer2);
447 }
448
449 #[test]
450 fn into_raw() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100451 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000452 1024,
453 512,
454 1,
455 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
456 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100457 0,
458 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000459 .expect("Buffer with some basic parameters was not created successfully");
460 let buffer2 = buffer.clone();
461
462 let raw_buffer = buffer.into_raw();
463 // SAFETY: This is the same pointer we had before.
464 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
465
466 assert_eq!(remade_buffer, buffer2);
467 }
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100468
469 #[test]
470 fn native_handle_and_back() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100471 let buffer_description = HardwareBufferDescription::new(
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100472 1024,
473 512,
474 1,
475 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
476 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100477 1024,
478 );
479 let buffer = HardwareBuffer::new(&buffer_description)
480 .expect("Buffer with some basic parameters was not created successfully");
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100481
482 let native_handle =
483 buffer.cloned_native_handle().expect("Failed to get native handle for buffer");
Andrew Walbranabc932e2024-08-30 14:10:29 +0100484 let buffer2 = HardwareBuffer::create_from_handle(&native_handle, &buffer_description)
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100485 .expect("Failed to create buffer from native handle");
486
Andrew Walbranabc932e2024-08-30 14:10:29 +0100487 assert_eq!(buffer.description(), buffer_description);
488 assert_eq!(buffer2.description(), buffer_description);
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100489 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000490}