blob: 54dad040718f2574fdb71be305d673933cda1fc7 [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
22pub use handle::NativeHandle;
Jiyong Park8032bff2024-04-01 16:17:15 +090023pub use surface::Surface;
Andrew Walbran8ee0ef12024-01-12 15:56:14 +000024
Andrew Walbran43bddb62023-09-01 16:43:09 +010025pub use ffi::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
Jim Shargo7df9f752023-07-18 20:33:45 +000026
Andrew Walbran43bddb62023-09-01 16:43:09 +010027use binder::{
Andrew Walbrane9573af2024-01-11 16:34:16 +000028 binder_impl::{BorrowedParcel, UnstructuredParcelable},
29 impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
Andrew Walbran43bddb62023-09-01 16:43:09 +010030 unstable_api::{status_result, AsNative},
31 StatusCode,
32};
33use ffi::{AHardwareBuffer, AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel};
Jim Shargob69c6ef2023-10-05 22:54:51 +000034use std::fmt::{self, Debug, Formatter};
35use std::mem::ManuallyDrop;
Andrew Walbran43bddb62023-09-01 16:43:09 +010036use std::ptr::{self, null_mut, NonNull};
Jim Shargo7df9f752023-07-18 20:33:45 +000037
Andrew Walbran43bddb62023-09-01 16:43:09 +010038/// Wrapper around an opaque C `AHardwareBuffer`.
Jim Shargob69c6ef2023-10-05 22:54:51 +000039#[derive(PartialEq, Eq)]
40pub struct HardwareBuffer(NonNull<AHardwareBuffer>);
Jim Shargo7df9f752023-07-18 20:33:45 +000041
Jim Shargob69c6ef2023-10-05 22:54:51 +000042impl HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +000043 /// 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 Shargoe4680d72023-08-07 16:46:45 +000079 #[inline]
Jim Shargo7df9f752023-07-18 20:33:45 +000080 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 Shargob69c6ef2023-10-05 22:54:51 +000097 let mut ptr = ptr::null_mut();
Jim Shargo7df9f752023-07-18 20:33:45 +000098 // 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 Shargob69c6ef2023-10-05 22:54:51 +0000100 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut ptr) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000101
102 if status == 0 {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000103 Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null")))
Jim Shargo7df9f752023-07-18 20:33:45 +0000104 } else {
105 None
106 }
107 }
108
Andrew Walbrana73d62c2024-08-20 17:20:56 +0100109 /// 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 Walbrana0b3a9d2024-01-12 16:43:12 +0000153 /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
Jim Shargo7df9f752023-07-18 20:33:45 +0000154 ///
155 /// # Safety
156 ///
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000157 /// 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 Shargob69c6ef2023-10-05 22:54:51 +0000161 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
162 Self(buffer_ptr)
163 }
164
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000165 /// 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 Shargob69c6ef2023-10-05 22:54:51 +0000182 /// 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 Shargo7df9f752023-07-18 20:33:45 +0000187 }
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 Walbran43bddb62023-09-01 16:43:09 +0100195 // 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 Shargo7df9f752023-07-18 20:33:45 +0000200 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 Shargob69c6ef2023-10-05 22:54:51 +0000247 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000248 buffer_desc
249 }
250}
251
Jim Shargob69c6ef2023-10-05 22:54:51 +0000252impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000253 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100254 // 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 Shargob69c6ef2023-10-05 22:54:51 +0000258 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000259 }
260}
261
Jim Shargob69c6ef2023-10-05 22:54:51 +0000262impl Debug for HardwareBuffer {
Andrew Walbran8ee0ef12024-01-12 15:56:14 +0000263 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000264 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
265 }
266}
267
268impl 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 Walbrane9573af2024-01-11 16:34:16 +0000276impl 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 Walbran43bddb62023-09-01 16:43:09 +0100305 }
306}
307
Andrew Walbrane9573af2024-01-11 16:34:16 +0000308impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
309impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100310
Jim Shargob69c6ef2023-10-05 22:54:51 +0000311// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
312unsafe 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)
323unsafe impl Sync for HardwareBuffer {}
324
Jim Shargo7df9f752023-07-18 20:33:45 +0000325#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000326mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000327 use super::*;
328
329 #[test]
330 fn create_valid_buffer_returns_ok() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000331 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000332 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 Shargob69c6ef2023-10-05 22:54:51 +0000343 let buffer = HardwareBuffer::new(512, 512, 1, 0, AHardwareBuffer_UsageFlags(0));
Jim Shargo7df9f752023-07-18 20:33:45 +0000344 assert!(buffer.is_none());
345 }
346
347 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000348 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000349 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 Walbran03350bc2023-08-03 16:02:51 +0000361 // SAFETY: The pointers are valid because they come from references, and
362 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000363 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
364 assert_eq!(status, 0);
365
Andrew Walbran03350bc2023-08-03 16:02:51 +0000366 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
367 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000368 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000369 assert_eq!(buffer.width(), 1024);
370 }
371
372 #[test]
373 fn basic_getters() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000374 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000375 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 Shargob69c6ef2023-10-05 22:54:51 +0000395 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000396 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 Shargob69c6ef2023-10-05 22:54:51 +0000406
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 Walbrana73d62c2024-08-20 17:20:56 +0100440
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 Shargo7df9f752023-07-18 20:33:45 +0000470}