blob: aab7df0c2ce6d205790aaa907b578be58c3cf22a [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 Walbran43bddb62023-09-01 16:43:09 +010019pub use ffi::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
Jim Shargo7df9f752023-07-18 20:33:45 +000020
Andrew Walbran43bddb62023-09-01 16:43:09 +010021use binder::{
Andrew Walbrane9573af2024-01-11 16:34:16 +000022 binder_impl::{BorrowedParcel, UnstructuredParcelable},
23 impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
Andrew Walbran43bddb62023-09-01 16:43:09 +010024 unstable_api::{status_result, AsNative},
25 StatusCode,
26};
27use ffi::{AHardwareBuffer, AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel};
Jim Shargob69c6ef2023-10-05 22:54:51 +000028use std::fmt::{self, Debug, Formatter};
29use std::mem::ManuallyDrop;
Andrew Walbran43bddb62023-09-01 16:43:09 +010030use std::ptr::{self, null_mut, NonNull};
Jim Shargo7df9f752023-07-18 20:33:45 +000031
Andrew Walbran43bddb62023-09-01 16:43:09 +010032/// Wrapper around an opaque C `AHardwareBuffer`.
Jim Shargob69c6ef2023-10-05 22:54:51 +000033#[derive(PartialEq, Eq)]
34pub struct HardwareBuffer(NonNull<AHardwareBuffer>);
Jim Shargo7df9f752023-07-18 20:33:45 +000035
Jim Shargob69c6ef2023-10-05 22:54:51 +000036impl HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +000037 /// Test whether the given format and usage flag combination is allocatable. If this function
38 /// returns true, it means that a buffer with the given description can be allocated on this
39 /// implementation, unless resource exhaustion occurs. If this function returns false, it means
40 /// that the allocation of the given description will never succeed.
41 ///
42 /// Available since API 29
43 pub fn is_supported(
44 width: u32,
45 height: u32,
46 layers: u32,
47 format: AHardwareBuffer_Format::Type,
48 usage: AHardwareBuffer_UsageFlags,
49 stride: u32,
50 ) -> bool {
51 let buffer_desc = ffi::AHardwareBuffer_Desc {
52 width,
53 height,
54 layers,
55 format,
56 usage: usage.0,
57 stride,
58 rfu0: 0,
59 rfu1: 0,
60 };
61 // SAFETY: *buffer_desc will never be null.
62 let status = unsafe { ffi::AHardwareBuffer_isSupported(&buffer_desc) };
63
64 status == 1
65 }
66
67 /// Allocates a buffer that matches the passed AHardwareBuffer_Desc. If allocation succeeds, the
68 /// buffer can be used according to the usage flags specified in its description. If a buffer is
69 /// used in ways not compatible with its usage flags, the results are undefined and may include
70 /// program termination.
71 ///
72 /// Available since API level 26.
Jim Shargoe4680d72023-08-07 16:46:45 +000073 #[inline]
Jim Shargo7df9f752023-07-18 20:33:45 +000074 pub fn new(
75 width: u32,
76 height: u32,
77 layers: u32,
78 format: AHardwareBuffer_Format::Type,
79 usage: AHardwareBuffer_UsageFlags,
80 ) -> Option<Self> {
81 let buffer_desc = ffi::AHardwareBuffer_Desc {
82 width,
83 height,
84 layers,
85 format,
86 usage: usage.0,
87 stride: 0,
88 rfu0: 0,
89 rfu1: 0,
90 };
Jim Shargob69c6ef2023-10-05 22:54:51 +000091 let mut ptr = ptr::null_mut();
Jim Shargo7df9f752023-07-18 20:33:45 +000092 // SAFETY: The returned pointer is valid until we drop/deallocate it. The function may fail
93 // and return a status, but we check it later.
Jim Shargob69c6ef2023-10-05 22:54:51 +000094 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut ptr) };
Jim Shargo7df9f752023-07-18 20:33:45 +000095
96 if status == 0 {
Jim Shargob69c6ef2023-10-05 22:54:51 +000097 Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null")))
Jim Shargo7df9f752023-07-18 20:33:45 +000098 } else {
99 None
100 }
101 }
102
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000103 /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
Jim Shargo7df9f752023-07-18 20:33:45 +0000104 ///
105 /// # Safety
106 ///
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000107 /// This function takes ownership of the pointer and does NOT increment the refcount on the
108 /// buffer. If the caller uses the pointer after the created object is dropped it will cause
109 /// undefined behaviour. If the caller wants to continue using the pointer after calling this
110 /// then use [`clone_from_raw`](Self::clone_from_raw) instead.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000111 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
112 Self(buffer_ptr)
113 }
114
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000115 /// Creates a new Rust HardwareBuffer to wrap the given AHardwareBuffer without taking ownership
116 /// of it.
117 ///
118 /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer.
119 /// This means that the caller can continue to use the raw buffer it passed in, and must call
120 /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to
121 /// avoid a memory leak.
122 ///
123 /// # Safety
124 ///
125 /// The buffer pointer must point to a valid `AHardwareBuffer`.
126 pub unsafe fn clone_from_raw(buffer: NonNull<AHardwareBuffer>) -> Self {
127 // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid.
128 unsafe { ffi::AHardwareBuffer_acquire(buffer.as_ptr()) };
129 Self(buffer)
130 }
131
Jim Shargob69c6ef2023-10-05 22:54:51 +0000132 /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can
133 /// be used to provide a pointer to the AHB for a C/C++ API over the FFI.
134 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
135 let buffer = ManuallyDrop::new(self);
136 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000137 }
138
139 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
140 /// and undocumented circumstances.
141 ///
142 /// Available since API level 31.
143 pub fn id(&self) -> u64 {
144 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100145 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
146 // because it must have been allocated by `AHardwareBuffer_allocate`,
147 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
148 // released it. The id pointer must be valid because it comes from a reference.
149 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000150 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
151
152 out_id
153 }
154
155 /// Get the width of this buffer
156 pub fn width(&self) -> u32 {
157 self.description().width
158 }
159
160 /// Get the height of this buffer
161 pub fn height(&self) -> u32 {
162 self.description().height
163 }
164
165 /// Get the number of layers of this buffer
166 pub fn layers(&self) -> u32 {
167 self.description().layers
168 }
169
170 /// Get the format of this buffer
171 pub fn format(&self) -> AHardwareBuffer_Format::Type {
172 self.description().format
173 }
174
175 /// Get the usage bitvector of this buffer
176 pub fn usage(&self) -> AHardwareBuffer_UsageFlags {
177 AHardwareBuffer_UsageFlags(self.description().usage)
178 }
179
180 /// Get the stride of this buffer
181 pub fn stride(&self) -> u32 {
182 self.description().stride
183 }
184
185 fn description(&self) -> ffi::AHardwareBuffer_Desc {
186 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
187 width: 0,
188 height: 0,
189 layers: 0,
190 format: 0,
191 usage: 0,
192 stride: 0,
193 rfu0: 0,
194 rfu1: 0,
195 };
196 // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000197 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000198 buffer_desc
199 }
200}
201
Jim Shargob69c6ef2023-10-05 22:54:51 +0000202impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000203 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100204 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
205 // because it must have been allocated by `AHardwareBuffer_allocate`,
206 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
207 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000208 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000209 }
210}
211
Jim Shargob69c6ef2023-10-05 22:54:51 +0000212impl Debug for HardwareBuffer {
213 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
214 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
215 }
216}
217
218impl Clone for HardwareBuffer {
219 fn clone(&self) -> Self {
220 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
221 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
222 Self(self.0)
223 }
224}
225
Andrew Walbrane9573af2024-01-11 16:34:16 +0000226impl UnstructuredParcelable for HardwareBuffer {
227 fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
228 let status =
229 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
230 // because it must have been allocated by `AHardwareBuffer_allocate`,
231 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
232 // released it.
233 unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
234 status_result(status)
235 }
236
237 fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
238 let mut buffer = null_mut();
239
240 let status =
241 // SAFETY: Both pointers must be valid because they are obtained from references.
242 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
243 // with them. If it returns success then it will have allocated a new
244 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
245 // release it.
246 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
247
248 status_result(status)?;
249
250 Ok(Self(
251 NonNull::new(buffer).expect(
252 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
253 ),
254 ))
Andrew Walbran43bddb62023-09-01 16:43:09 +0100255 }
256}
257
Andrew Walbrane9573af2024-01-11 16:34:16 +0000258impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
259impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100260
Jim Shargob69c6ef2023-10-05 22:54:51 +0000261// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
262unsafe impl Send for HardwareBuffer {}
263
264// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
265//
266// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
267// where they are not immutable are:
268//
269// - reallocation (which is never actually done across the codebase and requires special
270// privileges/platform code access to do)
271// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
272// according to the docs on the underlying gralloc calls)
273unsafe impl Sync for HardwareBuffer {}
274
Jim Shargo7df9f752023-07-18 20:33:45 +0000275#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000276mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000277 use super::*;
278
279 #[test]
280 fn create_valid_buffer_returns_ok() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000281 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000282 512,
283 512,
284 1,
285 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
286 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
287 );
288 assert!(buffer.is_some());
289 }
290
291 #[test]
292 fn create_invalid_buffer_returns_err() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000293 let buffer = HardwareBuffer::new(512, 512, 1, 0, AHardwareBuffer_UsageFlags(0));
Jim Shargo7df9f752023-07-18 20:33:45 +0000294 assert!(buffer.is_none());
295 }
296
297 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000298 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000299 let buffer_desc = ffi::AHardwareBuffer_Desc {
300 width: 1024,
301 height: 512,
302 layers: 1,
303 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
304 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
305 stride: 0,
306 rfu0: 0,
307 rfu1: 0,
308 };
309 let mut raw_buffer_ptr = ptr::null_mut();
310
Andrew Walbran03350bc2023-08-03 16:02:51 +0000311 // SAFETY: The pointers are valid because they come from references, and
312 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000313 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
314 assert_eq!(status, 0);
315
Andrew Walbran03350bc2023-08-03 16:02:51 +0000316 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
317 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000318 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000319 assert_eq!(buffer.width(), 1024);
320 }
321
322 #[test]
323 fn basic_getters() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000324 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000325 1024,
326 512,
327 1,
328 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
329 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
330 )
331 .expect("Buffer with some basic parameters was not created successfully");
332
333 assert_eq!(buffer.width(), 1024);
334 assert_eq!(buffer.height(), 512);
335 assert_eq!(buffer.layers(), 1);
336 assert_eq!(buffer.format(), AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);
337 assert_eq!(
338 buffer.usage(),
339 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
340 );
341 }
342
343 #[test]
344 fn id_getter() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000345 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000346 1024,
347 512,
348 1,
349 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
350 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
351 )
352 .expect("Buffer with some basic parameters was not created successfully");
353
354 assert_ne!(0, buffer.id());
355 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000356
357 #[test]
358 fn clone() {
359 let buffer = HardwareBuffer::new(
360 1024,
361 512,
362 1,
363 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
364 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
365 )
366 .expect("Buffer with some basic parameters was not created successfully");
367 let buffer2 = buffer.clone();
368
369 assert_eq!(buffer, buffer2);
370 }
371
372 #[test]
373 fn into_raw() {
374 let buffer = HardwareBuffer::new(
375 1024,
376 512,
377 1,
378 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
379 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
380 )
381 .expect("Buffer with some basic parameters was not created successfully");
382 let buffer2 = buffer.clone();
383
384 let raw_buffer = buffer.into_raw();
385 // SAFETY: This is the same pointer we had before.
386 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
387
388 assert_eq!(remade_buffer, buffer2);
389 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000390}