blob: d76028591863b4fddf39983ba766d8d15f6f54f8 [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::{
Andrew Walbran9e7e9092024-11-15 15:57:09 +000033 AHardwareBuffer, AHardwareBuffer_Desc, AHardwareBuffer_Plane, AHardwareBuffer_Planes,
34 AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel, ARect,
Andrew Walbranabc932e2024-08-30 14:10:29 +010035};
Andrew Walbrane0361622024-10-23 18:49:27 +010036use std::ffi::c_void;
Jim Shargob69c6ef2023-10-05 22:54:51 +000037use std::fmt::{self, Debug, Formatter};
Andrew Walbrane0361622024-10-23 18:49:27 +010038use std::mem::{forget, ManuallyDrop};
39use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, OwnedFd};
40use std::ptr::{self, null, null_mut, NonNull};
Jim Shargo7df9f752023-07-18 20:33:45 +000041
Andrew Walbranabc932e2024-08-30 14:10:29 +010042/// Wrapper around a C `AHardwareBuffer_Desc`.
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub struct HardwareBufferDescription(AHardwareBuffer_Desc);
45
46impl HardwareBufferDescription {
47 /// Creates a new `HardwareBufferDescription` with the given parameters.
48 pub fn new(
49 width: u32,
50 height: u32,
51 layers: u32,
52 format: AHardwareBuffer_Format::Type,
53 usage: AHardwareBuffer_UsageFlags,
54 stride: u32,
55 ) -> Self {
56 Self(AHardwareBuffer_Desc {
57 width,
58 height,
59 layers,
60 format,
61 usage: usage.0,
62 stride,
63 rfu0: 0,
64 rfu1: 0,
65 })
66 }
67
68 /// Returns the width from the buffer description.
69 pub fn width(&self) -> u32 {
70 self.0.width
71 }
72
73 /// Returns the height from the buffer description.
74 pub fn height(&self) -> u32 {
75 self.0.height
76 }
77
78 /// Returns the number from layers from the buffer description.
79 pub fn layers(&self) -> u32 {
80 self.0.layers
81 }
82
83 /// Returns the format from the buffer description.
84 pub fn format(&self) -> AHardwareBuffer_Format::Type {
85 self.0.format
86 }
87
88 /// Returns the usage bitvector from the buffer description.
89 pub fn usage(&self) -> AHardwareBuffer_UsageFlags {
90 AHardwareBuffer_UsageFlags(self.0.usage)
91 }
92
93 /// Returns the stride from the buffer description.
94 pub fn stride(&self) -> u32 {
95 self.0.stride
96 }
97}
98
99impl Default for HardwareBufferDescription {
100 fn default() -> Self {
101 Self(AHardwareBuffer_Desc {
102 width: 0,
103 height: 0,
104 layers: 0,
105 format: 0,
106 usage: 0,
107 stride: 0,
108 rfu0: 0,
109 rfu1: 0,
110 })
111 }
112}
113
Andrew Walbran43bddb62023-09-01 16:43:09 +0100114/// Wrapper around an opaque C `AHardwareBuffer`.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000115#[derive(PartialEq, Eq)]
116pub struct HardwareBuffer(NonNull<AHardwareBuffer>);
Jim Shargo7df9f752023-07-18 20:33:45 +0000117
Jim Shargob69c6ef2023-10-05 22:54:51 +0000118impl HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000119 /// Test whether the given format and usage flag combination is allocatable. If this function
120 /// returns true, it means that a buffer with the given description can be allocated on this
121 /// implementation, unless resource exhaustion occurs. If this function returns false, it means
122 /// that the allocation of the given description will never succeed.
123 ///
124 /// Available since API 29
Andrew Walbranabc932e2024-08-30 14:10:29 +0100125 pub fn is_supported(buffer_description: &HardwareBufferDescription) -> bool {
126 // SAFETY: The pointer comes from a reference so must be valid.
127 let status = unsafe { ffi::AHardwareBuffer_isSupported(&buffer_description.0) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000128
129 status == 1
130 }
131
132 /// Allocates a buffer that matches the passed AHardwareBuffer_Desc. If allocation succeeds, the
133 /// buffer can be used according to the usage flags specified in its description. If a buffer is
134 /// used in ways not compatible with its usage flags, the results are undefined and may include
135 /// program termination.
136 ///
137 /// Available since API level 26.
Jim Shargoe4680d72023-08-07 16:46:45 +0000138 #[inline]
Andrew Walbranabc932e2024-08-30 14:10:29 +0100139 pub fn new(buffer_description: &HardwareBufferDescription) -> Option<Self> {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000140 let mut ptr = ptr::null_mut();
Jim Shargo7df9f752023-07-18 20:33:45 +0000141 // SAFETY: The returned pointer is valid until we drop/deallocate it. The function may fail
142 // and return a status, but we check it later.
Andrew Walbranabc932e2024-08-30 14:10:29 +0100143 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_description.0, &mut ptr) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000144
145 if status == 0 {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000146 Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null")))
Jim Shargo7df9f752023-07-18 20:33:45 +0000147 } else {
148 None
149 }
150 }
151
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100152 /// Creates a `HardwareBuffer` from a native handle.
153 ///
154 /// The native handle is cloned, so this doesn't take ownership of the original handle passed
155 /// in.
156 pub fn create_from_handle(
157 handle: &NativeHandle,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100158 buffer_description: &HardwareBufferDescription,
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100159 ) -> Result<Self, StatusCode> {
160 let mut buffer = ptr::null_mut();
161 // SAFETY: The caller guarantees that `handle` is valid, and the buffer pointer is valid
162 // because it comes from a reference. The method we pass means that
163 // `AHardwareBuffer_createFromHandle` will clone the handle rather than taking ownership of
164 // it.
165 let status = unsafe {
166 ffi::AHardwareBuffer_createFromHandle(
Andrew Walbranabc932e2024-08-30 14:10:29 +0100167 &buffer_description.0,
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100168 handle.as_raw().as_ptr(),
169 ffi::CreateFromHandleMethod_AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE
170 .try_into()
171 .unwrap(),
172 &mut buffer,
173 )
174 };
175 status_result(status)?;
176 Ok(Self(NonNull::new(buffer).expect("Allocated AHardwareBuffer was null")))
177 }
178
179 /// Returns a clone of the native handle of the buffer.
180 ///
181 /// Returns `None` if the operation fails for any reason.
182 pub fn cloned_native_handle(&self) -> Option<NativeHandle> {
183 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
184 // because it must have been allocated by `AHardwareBuffer_allocate`,
185 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
186 // released it.
187 let native_handle = unsafe { ffi::AHardwareBuffer_getNativeHandle(self.0.as_ptr()) };
188 NonNull::new(native_handle.cast_mut()).and_then(|native_handle| {
189 // SAFETY: `AHardwareBuffer_getNativeHandle` should have returned a valid pointer which
190 // is valid at least as long as the buffer is, and `clone_from_raw` clones it rather
191 // than taking ownership of it so the original `native_handle` isn't stored.
192 unsafe { NativeHandle::clone_from_raw(native_handle) }
193 })
194 }
195
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000196 /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
Jim Shargo7df9f752023-07-18 20:33:45 +0000197 ///
198 /// # Safety
199 ///
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000200 /// This function takes ownership of the pointer and does NOT increment the refcount on the
201 /// buffer. If the caller uses the pointer after the created object is dropped it will cause
202 /// undefined behaviour. If the caller wants to continue using the pointer after calling this
203 /// then use [`clone_from_raw`](Self::clone_from_raw) instead.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000204 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
205 Self(buffer_ptr)
206 }
207
Andrew Walbran7e947d32024-10-23 17:03:59 +0100208 /// Creates a new Rust HardwareBuffer to wrap the given `AHardwareBuffer` without taking
209 /// ownership of it.
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000210 ///
211 /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer.
212 /// This means that the caller can continue to use the raw buffer it passed in, and must call
213 /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to
214 /// avoid a memory leak.
215 ///
216 /// # Safety
217 ///
218 /// The buffer pointer must point to a valid `AHardwareBuffer`.
219 pub unsafe fn clone_from_raw(buffer: NonNull<AHardwareBuffer>) -> Self {
220 // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid.
221 unsafe { ffi::AHardwareBuffer_acquire(buffer.as_ptr()) };
222 Self(buffer)
223 }
224
Andrew Walbran7e947d32024-10-23 17:03:59 +0100225 /// Returns the internal `AHardwareBuffer` pointer.
226 ///
227 /// This is only valid as long as this `HardwareBuffer` exists, so shouldn't be stored. It can
228 /// be used to provide a pointer for a C/C++ API over FFI.
Ren-Pei Zeng8237ba62024-10-22 15:20:18 +0000229 pub fn as_raw(&self) -> NonNull<AHardwareBuffer> {
230 self.0
231 }
232
Andrew Walbran7e947d32024-10-23 17:03:59 +0100233 /// Gets the internal `AHardwareBuffer` pointer without decrementing the refcount. This can
234 /// be used for a C/C++ API which takes ownership of the pointer.
235 ///
236 /// The caller is responsible for releasing the `AHardwareBuffer` pointer by calling
237 /// `AHardwareBuffer_release` when it is finished with it, or may convert it back to a Rust
238 /// `HardwareBuffer` by calling [`HardwareBuffer::from_raw`].
Jim Shargob69c6ef2023-10-05 22:54:51 +0000239 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
240 let buffer = ManuallyDrop::new(self);
241 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000242 }
243
244 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
245 /// and undocumented circumstances.
246 ///
247 /// Available since API level 31.
248 pub fn id(&self) -> u64 {
249 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100250 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
251 // because it must have been allocated by `AHardwareBuffer_allocate`,
252 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
253 // released it. The id pointer must be valid because it comes from a reference.
254 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000255 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
256
257 out_id
258 }
259
Andrew Walbranabc932e2024-08-30 14:10:29 +0100260 /// Returns the description of this buffer.
261 pub fn description(&self) -> HardwareBufferDescription {
Jim Shargo7df9f752023-07-18 20:33:45 +0000262 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
263 width: 0,
264 height: 0,
265 layers: 0,
266 format: 0,
267 usage: 0,
268 stride: 0,
269 rfu0: 0,
270 rfu1: 0,
271 };
Andrew Walbrane0361622024-10-23 18:49:27 +0100272 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid, and the
273 // AHardwareBuffer_Desc pointer is valid because it comes from a reference.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000274 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100275 HardwareBufferDescription(buffer_desc)
Jim Shargo7df9f752023-07-18 20:33:45 +0000276 }
Andrew Walbrane0361622024-10-23 18:49:27 +0100277
278 /// Locks the hardware buffer for direct CPU access.
279 ///
280 /// # Safety
281 ///
282 /// - If `fence` is `None`, the caller must ensure that all writes to the buffer have completed
283 /// before calling this function.
284 /// - If the buffer has `AHARDWAREBUFFER_FORMAT_BLOB`, multiple threads or process may lock the
285 /// buffer simultaneously, but the caller must ensure that they don't access it simultaneously
286 /// and break Rust's aliasing rules, like any other shared memory.
287 /// - Otherwise if `usage` includes `AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY` or
288 /// `AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN`, the caller must ensure that no other threads or
289 /// processes lock the buffer simultaneously for any usage.
290 /// - Otherwise, the caller must ensure that no other threads lock the buffer for writing
291 /// simultaneously.
292 /// - If `rect` is not `None`, the caller must not modify the buffer outside of that rectangle.
293 pub unsafe fn lock<'a>(
294 &'a self,
295 usage: AHardwareBuffer_UsageFlags,
296 fence: Option<BorrowedFd>,
297 rect: Option<&ARect>,
298 ) -> Result<HardwareBufferGuard<'a>, StatusCode> {
299 let fence = if let Some(fence) = fence { fence.as_raw_fd() } else { -1 };
300 let rect = rect.map(ptr::from_ref).unwrap_or(null());
301 let mut address = null_mut();
302 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid, and the buffer address out
303 // pointer is valid because it comes from a reference. Our caller promises that writes have
304 // completed and there will be no simultaneous read/write locks.
305 let status = unsafe {
306 ffi::AHardwareBuffer_lock(self.0.as_ptr(), usage.0, fence, rect, &mut address)
307 };
308 status_result(status)?;
309 Ok(HardwareBufferGuard {
310 buffer: self,
311 address: NonNull::new(address)
312 .expect("AHardwareBuffer_lock set a null outVirtualAddress"),
313 })
314 }
315
Andrew Walbran9e7e9092024-11-15 15:57:09 +0000316 /// Lock a potentially multi-planar hardware buffer for direct CPU access.
317 ///
318 /// # Safety
319 ///
320 /// - If `fence` is `None`, the caller must ensure that all writes to the buffer have completed
321 /// before calling this function.
322 /// - If the buffer has `AHARDWAREBUFFER_FORMAT_BLOB`, multiple threads or process may lock the
323 /// buffer simultaneously, but the caller must ensure that they don't access it simultaneously
324 /// and break Rust's aliasing rules, like any other shared memory.
325 /// - Otherwise if `usage` includes `AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY` or
326 /// `AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN`, the caller must ensure that no other threads or
327 /// processes lock the buffer simultaneously for any usage.
328 /// - Otherwise, the caller must ensure that no other threads lock the buffer for writing
329 /// simultaneously.
330 /// - If `rect` is not `None`, the caller must not modify the buffer outside of that rectangle.
331 pub unsafe fn lock_planes<'a>(
332 &'a self,
333 usage: AHardwareBuffer_UsageFlags,
334 fence: Option<BorrowedFd>,
335 rect: Option<&ARect>,
336 ) -> Result<Vec<PlaneGuard<'a>>, StatusCode> {
337 let fence = if let Some(fence) = fence { fence.as_raw_fd() } else { -1 };
338 let rect = rect.map(ptr::from_ref).unwrap_or(null());
339 let mut planes = AHardwareBuffer_Planes {
340 planeCount: 0,
341 planes: [const { AHardwareBuffer_Plane { data: null_mut(), pixelStride: 0, rowStride: 0 } };
342 4],
343 };
344
345 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid, and the various out
346 // pointers are valid because they come from references. Our caller promises that writes have
347 // completed and there will be no simultaneous read/write locks.
348 let status = unsafe {
349 ffi::AHardwareBuffer_lockPlanes(self.0.as_ptr(), usage.0, fence, rect, &mut planes)
350 };
351 status_result(status)?;
352 let plane_count = planes.planeCount.try_into().unwrap();
353 Ok(planes.planes[..plane_count]
354 .iter()
355 .map(|plane| PlaneGuard {
356 guard: HardwareBufferGuard {
357 buffer: self,
358 address: NonNull::new(plane.data)
359 .expect("AHardwareBuffer_lockAndGetInfo set a null outVirtualAddress"),
360 },
361 pixel_stride: plane.pixelStride,
362 row_stride: plane.rowStride,
363 })
364 .collect())
365 }
366
Andrew Walbrane0361622024-10-23 18:49:27 +0100367 /// Locks the hardware buffer for direct CPU access, returning information about the bytes per
368 /// pixel and stride as well.
369 ///
370 /// # Safety
371 ///
372 /// - If `fence` is `None`, the caller must ensure that all writes to the buffer have completed
373 /// before calling this function.
374 /// - If the buffer has `AHARDWAREBUFFER_FORMAT_BLOB`, multiple threads or process may lock the
375 /// buffer simultaneously, but the caller must ensure that they don't access it simultaneously
376 /// and break Rust's aliasing rules, like any other shared memory.
377 /// - Otherwise if `usage` includes `AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY` or
378 /// `AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN`, the caller must ensure that no other threads or
379 /// processes lock the buffer simultaneously for any usage.
380 /// - Otherwise, the caller must ensure that no other threads lock the buffer for writing
381 /// simultaneously.
382 pub unsafe fn lock_and_get_info<'a>(
383 &'a self,
384 usage: AHardwareBuffer_UsageFlags,
385 fence: Option<BorrowedFd>,
386 rect: Option<&ARect>,
387 ) -> Result<LockedBufferInfo<'a>, StatusCode> {
388 let fence = if let Some(fence) = fence { fence.as_raw_fd() } else { -1 };
389 let rect = rect.map(ptr::from_ref).unwrap_or(null());
390 let mut address = null_mut();
391 let mut bytes_per_pixel = 0;
392 let mut stride = 0;
393 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid, and the various out
394 // pointers are valid because they come from references. Our caller promises that writes have
395 // completed and there will be no simultaneous read/write locks.
396 let status = unsafe {
397 ffi::AHardwareBuffer_lockAndGetInfo(
398 self.0.as_ptr(),
399 usage.0,
400 fence,
401 rect,
402 &mut address,
403 &mut bytes_per_pixel,
404 &mut stride,
405 )
406 };
407 status_result(status)?;
408 Ok(LockedBufferInfo {
409 guard: HardwareBufferGuard {
410 buffer: self,
411 address: NonNull::new(address)
412 .expect("AHardwareBuffer_lockAndGetInfo set a null outVirtualAddress"),
413 },
414 bytes_per_pixel: bytes_per_pixel as u32,
415 stride: stride as u32,
416 })
417 }
418
419 /// Unlocks the hardware buffer from direct CPU access.
420 ///
421 /// Must be called after all changes to the buffer are completed by the caller. This will block
422 /// until the unlocking is complete and the buffer contents are updated.
423 fn unlock(&self) -> Result<(), StatusCode> {
424 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid.
425 let status = unsafe { ffi::AHardwareBuffer_unlock(self.0.as_ptr(), null_mut()) };
426 status_result(status)?;
427 Ok(())
428 }
429
430 /// Unlocks the hardware buffer from direct CPU access.
431 ///
432 /// Must be called after all changes to the buffer are completed by the caller.
433 ///
434 /// This may not block until all work is completed, but rather will return a file descriptor
435 /// which will be signalled once the unlocking is complete and the buffer contents is updated.
436 /// If `Ok(None)` is returned then unlocking has already completed and no further waiting is
437 /// necessary. The file descriptor may be passed to a subsequent call to [`Self::lock`].
438 pub fn unlock_with_fence(
439 &self,
440 guard: HardwareBufferGuard,
441 ) -> Result<Option<OwnedFd>, StatusCode> {
442 // Forget the guard so that its `Drop` implementation doesn't try to unlock the
443 // HardwareBuffer again.
444 forget(guard);
445
446 let mut fence = -2;
447 // SAFETY: The `AHardwareBuffer` pointer we wrap is always valid.
448 let status = unsafe { ffi::AHardwareBuffer_unlock(self.0.as_ptr(), &mut fence) };
449 let fence = if fence < 0 {
450 None
451 } else {
452 // SAFETY: `AHardwareBuffer_unlock` gives us ownership of the fence file descriptor.
453 Some(unsafe { OwnedFd::from_raw_fd(fence) })
454 };
455 status_result(status)?;
456 Ok(fence)
457 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000458}
459
Jim Shargob69c6ef2023-10-05 22:54:51 +0000460impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000461 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100462 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
463 // because it must have been allocated by `AHardwareBuffer_allocate`,
464 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
465 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000466 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000467 }
468}
469
Jim Shargob69c6ef2023-10-05 22:54:51 +0000470impl Debug for HardwareBuffer {
Andrew Walbran8ee0ef12024-01-12 15:56:14 +0000471 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000472 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
473 }
474}
475
476impl Clone for HardwareBuffer {
477 fn clone(&self) -> Self {
478 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
479 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
480 Self(self.0)
481 }
482}
483
Andrew Walbrane9573af2024-01-11 16:34:16 +0000484impl UnstructuredParcelable for HardwareBuffer {
485 fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
486 let status =
487 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
488 // because it must have been allocated by `AHardwareBuffer_allocate`,
489 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
490 // released it.
491 unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
492 status_result(status)
493 }
494
495 fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
496 let mut buffer = null_mut();
497
498 let status =
499 // SAFETY: Both pointers must be valid because they are obtained from references.
500 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
501 // with them. If it returns success then it will have allocated a new
502 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
503 // release it.
504 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
505
506 status_result(status)?;
507
508 Ok(Self(
509 NonNull::new(buffer).expect(
510 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
511 ),
512 ))
Andrew Walbran43bddb62023-09-01 16:43:09 +0100513 }
514}
515
Andrew Walbrane9573af2024-01-11 16:34:16 +0000516impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
517impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100518
Jim Shargob69c6ef2023-10-05 22:54:51 +0000519// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
520unsafe impl Send for HardwareBuffer {}
521
522// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
523//
524// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
525// where they are not immutable are:
526//
527// - reallocation (which is never actually done across the codebase and requires special
528// privileges/platform code access to do)
529// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
530// according to the docs on the underlying gralloc calls)
531unsafe impl Sync for HardwareBuffer {}
532
Andrew Walbrane0361622024-10-23 18:49:27 +0100533/// A guard for when a `HardwareBuffer` is locked.
534///
535/// The `HardwareBuffer` will be unlocked when this is dropped, or may be unlocked via
536/// [`HardwareBuffer::unlock_with_fence`].
537#[derive(Debug)]
538pub struct HardwareBufferGuard<'a> {
539 buffer: &'a HardwareBuffer,
540 /// The address of the buffer in memory.
541 pub address: NonNull<c_void>,
542}
543
544impl<'a> Drop for HardwareBufferGuard<'a> {
545 fn drop(&mut self) {
546 self.buffer
547 .unlock()
548 .expect("Failed to unlock HardwareBuffer when dropping HardwareBufferGuard");
549 }
550}
551
552/// A guard for when a `HardwareBuffer` is locked, with additional information about the number of
553/// bytes per pixel and stride.
554#[derive(Debug)]
555pub struct LockedBufferInfo<'a> {
556 /// The locked buffer guard.
557 pub guard: HardwareBufferGuard<'a>,
558 /// The number of bytes used for each pixel in the buffer.
559 pub bytes_per_pixel: u32,
560 /// The stride in bytes between rows in the buffer.
561 pub stride: u32,
562}
563
Andrew Walbran9e7e9092024-11-15 15:57:09 +0000564/// A guard for a single plane of a locked `HardwareBuffer`, with additional information about the
565/// stride.
566#[derive(Debug)]
567pub struct PlaneGuard<'a> {
568 /// The locked buffer guard.
569 pub guard: HardwareBufferGuard<'a>,
570 /// The stride in bytes between the color channel for one pixel to the next pixel.
571 pub pixel_stride: u32,
572 /// The stride in bytes between rows in the buffer.
573 pub row_stride: u32,
574}
575
Jim Shargo7df9f752023-07-18 20:33:45 +0000576#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000577mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000578 use super::*;
579
580 #[test]
581 fn create_valid_buffer_returns_ok() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100582 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000583 512,
584 512,
585 1,
586 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
587 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100588 0,
589 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000590 assert!(buffer.is_some());
591 }
592
593 #[test]
594 fn create_invalid_buffer_returns_err() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100595 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
596 512,
597 512,
598 1,
599 0,
600 AHardwareBuffer_UsageFlags(0),
601 0,
602 ));
Jim Shargo7df9f752023-07-18 20:33:45 +0000603 assert!(buffer.is_none());
604 }
605
606 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000607 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000608 let buffer_desc = ffi::AHardwareBuffer_Desc {
609 width: 1024,
610 height: 512,
611 layers: 1,
612 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
613 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
614 stride: 0,
615 rfu0: 0,
616 rfu1: 0,
617 };
618 let mut raw_buffer_ptr = ptr::null_mut();
619
Andrew Walbran03350bc2023-08-03 16:02:51 +0000620 // SAFETY: The pointers are valid because they come from references, and
621 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000622 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
623 assert_eq!(status, 0);
624
Andrew Walbran03350bc2023-08-03 16:02:51 +0000625 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
626 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000627 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Andrew Walbranabc932e2024-08-30 14:10:29 +0100628 assert_eq!(buffer.description().width(), 1024);
Jim Shargo7df9f752023-07-18 20:33:45 +0000629 }
630
631 #[test]
632 fn basic_getters() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100633 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000634 1024,
635 512,
636 1,
637 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
638 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100639 0,
640 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000641 .expect("Buffer with some basic parameters was not created successfully");
642
Andrew Walbranabc932e2024-08-30 14:10:29 +0100643 let description = buffer.description();
644 assert_eq!(description.width(), 1024);
645 assert_eq!(description.height(), 512);
646 assert_eq!(description.layers(), 1);
Jim Shargo7df9f752023-07-18 20:33:45 +0000647 assert_eq!(
Andrew Walbranabc932e2024-08-30 14:10:29 +0100648 description.format(),
649 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
650 );
651 assert_eq!(
652 description.usage(),
Jim Shargo7df9f752023-07-18 20:33:45 +0000653 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
654 );
655 }
656
657 #[test]
658 fn id_getter() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100659 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000660 1024,
661 512,
662 1,
663 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
664 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100665 0,
666 ))
Jim Shargo7df9f752023-07-18 20:33:45 +0000667 .expect("Buffer with some basic parameters was not created successfully");
668
669 assert_ne!(0, buffer.id());
670 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000671
672 #[test]
673 fn clone() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100674 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000675 1024,
676 512,
677 1,
678 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
679 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100680 0,
681 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000682 .expect("Buffer with some basic parameters was not created successfully");
683 let buffer2 = buffer.clone();
684
685 assert_eq!(buffer, buffer2);
686 }
687
688 #[test]
689 fn into_raw() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100690 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
Jim Shargob69c6ef2023-10-05 22:54:51 +0000691 1024,
692 512,
693 1,
694 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
695 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100696 0,
697 ))
Jim Shargob69c6ef2023-10-05 22:54:51 +0000698 .expect("Buffer with some basic parameters was not created successfully");
699 let buffer2 = buffer.clone();
700
701 let raw_buffer = buffer.into_raw();
702 // SAFETY: This is the same pointer we had before.
703 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
704
705 assert_eq!(remade_buffer, buffer2);
706 }
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100707
708 #[test]
709 fn native_handle_and_back() {
Andrew Walbranabc932e2024-08-30 14:10:29 +0100710 let buffer_description = HardwareBufferDescription::new(
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100711 1024,
712 512,
713 1,
714 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
715 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
Andrew Walbranabc932e2024-08-30 14:10:29 +0100716 1024,
717 );
718 let buffer = HardwareBuffer::new(&buffer_description)
719 .expect("Buffer with some basic parameters was not created successfully");
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100720
721 let native_handle =
722 buffer.cloned_native_handle().expect("Failed to get native handle for buffer");
Andrew Walbranabc932e2024-08-30 14:10:29 +0100723 let buffer2 = HardwareBuffer::create_from_handle(&native_handle, &buffer_description)
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100724 .expect("Failed to create buffer from native handle");
725
Andrew Walbranabc932e2024-08-30 14:10:29 +0100726 assert_eq!(buffer.description(), buffer_description);
727 assert_eq!(buffer2.description(), buffer_description);
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100728 }
Andrew Walbrane0361622024-10-23 18:49:27 +0100729
730 #[test]
731 fn lock() {
732 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
733 1024,
734 512,
735 1,
736 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
737 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
738 0,
739 ))
740 .expect("Failed to create buffer");
741
742 // SAFETY: No other threads or processes have access to the buffer.
743 let guard = unsafe {
744 buffer.lock(
745 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
746 None,
747 None,
748 )
749 }
750 .unwrap();
751
752 drop(guard);
753 }
754
755 #[test]
756 fn lock_with_rect() {
757 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
758 1024,
759 512,
760 1,
761 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
762 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
763 0,
764 ))
765 .expect("Failed to create buffer");
766 let rect = ARect { left: 10, right: 20, top: 35, bottom: 45 };
767
768 // SAFETY: No other threads or processes have access to the buffer.
769 let guard = unsafe {
770 buffer.lock(
771 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
772 None,
773 Some(&rect),
774 )
775 }
776 .unwrap();
777
778 drop(guard);
779 }
780
781 #[test]
782 fn unlock_with_fence() {
783 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
784 1024,
785 512,
786 1,
787 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
788 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
789 0,
790 ))
791 .expect("Failed to create buffer");
792
793 // SAFETY: No other threads or processes have access to the buffer.
794 let guard = unsafe {
795 buffer.lock(
796 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
797 None,
798 None,
799 )
800 }
801 .unwrap();
802
803 buffer.unlock_with_fence(guard).unwrap();
804 }
805
806 #[test]
807 fn lock_with_info() {
808 const WIDTH: u32 = 1024;
809 let buffer = HardwareBuffer::new(&HardwareBufferDescription::new(
810 WIDTH,
811 512,
812 1,
813 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
814 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
815 0,
816 ))
817 .expect("Failed to create buffer");
818
819 // SAFETY: No other threads or processes have access to the buffer.
820 let info = unsafe {
821 buffer.lock_and_get_info(
822 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
823 None,
824 None,
825 )
826 }
827 .unwrap();
828
829 assert_eq!(info.bytes_per_pixel, 4);
830 assert_eq!(info.stride, WIDTH * 4);
831 drop(info);
832 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000833}