blob: dc3f51f7fd93b3ab34e148da6f093503967dad2a [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
Jiyong Park8032bff2024-04-01 16:17:15 +090019mod surface;
20pub use surface::Surface;
Andrew Walbran8ee0ef12024-01-12 15:56:14 +000021
Andrew Walbran43bddb62023-09-01 16:43:09 +010022pub use ffi::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
Jim Shargo7df9f752023-07-18 20:33:45 +000023
Andrew Walbran43bddb62023-09-01 16:43:09 +010024use binder::{
Andrew Walbrane9573af2024-01-11 16:34:16 +000025 binder_impl::{BorrowedParcel, UnstructuredParcelable},
26 impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
Andrew Walbran43bddb62023-09-01 16:43:09 +010027 unstable_api::{status_result, AsNative},
28 StatusCode,
29};
30use ffi::{AHardwareBuffer, AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel};
Jim Shargob69c6ef2023-10-05 22:54:51 +000031use std::fmt::{self, Debug, Formatter};
32use std::mem::ManuallyDrop;
Andrew Walbran43bddb62023-09-01 16:43:09 +010033use std::ptr::{self, null_mut, NonNull};
Jim Shargo7df9f752023-07-18 20:33:45 +000034
Andrew Walbran43bddb62023-09-01 16:43:09 +010035/// Wrapper around an opaque C `AHardwareBuffer`.
Jim Shargob69c6ef2023-10-05 22:54:51 +000036#[derive(PartialEq, Eq)]
37pub struct HardwareBuffer(NonNull<AHardwareBuffer>);
Jim Shargo7df9f752023-07-18 20:33:45 +000038
Jim Shargob69c6ef2023-10-05 22:54:51 +000039impl HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +000040 /// Test whether the given format and usage flag combination is allocatable. If this function
41 /// returns true, it means that a buffer with the given description can be allocated on this
42 /// implementation, unless resource exhaustion occurs. If this function returns false, it means
43 /// that the allocation of the given description will never succeed.
44 ///
45 /// Available since API 29
46 pub fn is_supported(
47 width: u32,
48 height: u32,
49 layers: u32,
50 format: AHardwareBuffer_Format::Type,
51 usage: AHardwareBuffer_UsageFlags,
52 stride: u32,
53 ) -> bool {
54 let buffer_desc = ffi::AHardwareBuffer_Desc {
55 width,
56 height,
57 layers,
58 format,
59 usage: usage.0,
60 stride,
61 rfu0: 0,
62 rfu1: 0,
63 };
64 // SAFETY: *buffer_desc will never be null.
65 let status = unsafe { ffi::AHardwareBuffer_isSupported(&buffer_desc) };
66
67 status == 1
68 }
69
70 /// Allocates a buffer that matches the passed AHardwareBuffer_Desc. If allocation succeeds, the
71 /// buffer can be used according to the usage flags specified in its description. If a buffer is
72 /// used in ways not compatible with its usage flags, the results are undefined and may include
73 /// program termination.
74 ///
75 /// Available since API level 26.
Jim Shargoe4680d72023-08-07 16:46:45 +000076 #[inline]
Jim Shargo7df9f752023-07-18 20:33:45 +000077 pub fn new(
78 width: u32,
79 height: u32,
80 layers: u32,
81 format: AHardwareBuffer_Format::Type,
82 usage: AHardwareBuffer_UsageFlags,
83 ) -> Option<Self> {
84 let buffer_desc = ffi::AHardwareBuffer_Desc {
85 width,
86 height,
87 layers,
88 format,
89 usage: usage.0,
90 stride: 0,
91 rfu0: 0,
92 rfu1: 0,
93 };
Jim Shargob69c6ef2023-10-05 22:54:51 +000094 let mut ptr = ptr::null_mut();
Jim Shargo7df9f752023-07-18 20:33:45 +000095 // SAFETY: The returned pointer is valid until we drop/deallocate it. The function may fail
96 // and return a status, but we check it later.
Jim Shargob69c6ef2023-10-05 22:54:51 +000097 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut ptr) };
Jim Shargo7df9f752023-07-18 20:33:45 +000098
99 if status == 0 {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000100 Some(Self(NonNull::new(ptr).expect("Allocated AHardwareBuffer was null")))
Jim Shargo7df9f752023-07-18 20:33:45 +0000101 } else {
102 None
103 }
104 }
105
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000106 /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
Jim Shargo7df9f752023-07-18 20:33:45 +0000107 ///
108 /// # Safety
109 ///
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000110 /// This function takes ownership of the pointer and does NOT increment the refcount on the
111 /// buffer. If the caller uses the pointer after the created object is dropped it will cause
112 /// undefined behaviour. If the caller wants to continue using the pointer after calling this
113 /// then use [`clone_from_raw`](Self::clone_from_raw) instead.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000114 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
115 Self(buffer_ptr)
116 }
117
Andrew Walbrana0b3a9d2024-01-12 16:43:12 +0000118 /// Creates a new Rust HardwareBuffer to wrap the given AHardwareBuffer without taking ownership
119 /// of it.
120 ///
121 /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer.
122 /// This means that the caller can continue to use the raw buffer it passed in, and must call
123 /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to
124 /// avoid a memory leak.
125 ///
126 /// # Safety
127 ///
128 /// The buffer pointer must point to a valid `AHardwareBuffer`.
129 pub unsafe fn clone_from_raw(buffer: NonNull<AHardwareBuffer>) -> Self {
130 // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid.
131 unsafe { ffi::AHardwareBuffer_acquire(buffer.as_ptr()) };
132 Self(buffer)
133 }
134
Jim Shargob69c6ef2023-10-05 22:54:51 +0000135 /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can
136 /// be used to provide a pointer to the AHB for a C/C++ API over the FFI.
137 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
138 let buffer = ManuallyDrop::new(self);
139 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000140 }
141
142 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
143 /// and undocumented circumstances.
144 ///
145 /// Available since API level 31.
146 pub fn id(&self) -> u64 {
147 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100148 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
149 // because it must have been allocated by `AHardwareBuffer_allocate`,
150 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
151 // released it. The id pointer must be valid because it comes from a reference.
152 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000153 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
154
155 out_id
156 }
157
158 /// Get the width of this buffer
159 pub fn width(&self) -> u32 {
160 self.description().width
161 }
162
163 /// Get the height of this buffer
164 pub fn height(&self) -> u32 {
165 self.description().height
166 }
167
168 /// Get the number of layers of this buffer
169 pub fn layers(&self) -> u32 {
170 self.description().layers
171 }
172
173 /// Get the format of this buffer
174 pub fn format(&self) -> AHardwareBuffer_Format::Type {
175 self.description().format
176 }
177
178 /// Get the usage bitvector of this buffer
179 pub fn usage(&self) -> AHardwareBuffer_UsageFlags {
180 AHardwareBuffer_UsageFlags(self.description().usage)
181 }
182
183 /// Get the stride of this buffer
184 pub fn stride(&self) -> u32 {
185 self.description().stride
186 }
187
188 fn description(&self) -> ffi::AHardwareBuffer_Desc {
189 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
190 width: 0,
191 height: 0,
192 layers: 0,
193 format: 0,
194 usage: 0,
195 stride: 0,
196 rfu0: 0,
197 rfu1: 0,
198 };
199 // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000200 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000201 buffer_desc
202 }
203}
204
Jim Shargob69c6ef2023-10-05 22:54:51 +0000205impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000206 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100207 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
208 // because it must have been allocated by `AHardwareBuffer_allocate`,
209 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
210 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000211 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000212 }
213}
214
Jim Shargob69c6ef2023-10-05 22:54:51 +0000215impl Debug for HardwareBuffer {
Andrew Walbran8ee0ef12024-01-12 15:56:14 +0000216 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000217 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
218 }
219}
220
221impl Clone for HardwareBuffer {
222 fn clone(&self) -> Self {
223 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
224 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
225 Self(self.0)
226 }
227}
228
Andrew Walbrane9573af2024-01-11 16:34:16 +0000229impl UnstructuredParcelable for HardwareBuffer {
230 fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
231 let status =
232 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
233 // because it must have been allocated by `AHardwareBuffer_allocate`,
234 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
235 // released it.
236 unsafe { AHardwareBuffer_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
237 status_result(status)
238 }
239
240 fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
241 let mut buffer = null_mut();
242
243 let status =
244 // SAFETY: Both pointers must be valid because they are obtained from references.
245 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
246 // with them. If it returns success then it will have allocated a new
247 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
248 // release it.
249 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
250
251 status_result(status)?;
252
253 Ok(Self(
254 NonNull::new(buffer).expect(
255 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
256 ),
257 ))
Andrew Walbran43bddb62023-09-01 16:43:09 +0100258 }
259}
260
Andrew Walbrane9573af2024-01-11 16:34:16 +0000261impl_deserialize_for_unstructured_parcelable!(HardwareBuffer);
262impl_serialize_for_unstructured_parcelable!(HardwareBuffer);
Andrew Walbran43bddb62023-09-01 16:43:09 +0100263
Jim Shargob69c6ef2023-10-05 22:54:51 +0000264// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
265unsafe impl Send for HardwareBuffer {}
266
267// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
268//
269// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
270// where they are not immutable are:
271//
272// - reallocation (which is never actually done across the codebase and requires special
273// privileges/platform code access to do)
274// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
275// according to the docs on the underlying gralloc calls)
276unsafe impl Sync for HardwareBuffer {}
277
Jim Shargo7df9f752023-07-18 20:33:45 +0000278#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000279mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000280 use super::*;
281
282 #[test]
283 fn create_valid_buffer_returns_ok() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000284 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000285 512,
286 512,
287 1,
288 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
289 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
290 );
291 assert!(buffer.is_some());
292 }
293
294 #[test]
295 fn create_invalid_buffer_returns_err() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000296 let buffer = HardwareBuffer::new(512, 512, 1, 0, AHardwareBuffer_UsageFlags(0));
Jim Shargo7df9f752023-07-18 20:33:45 +0000297 assert!(buffer.is_none());
298 }
299
300 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000301 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000302 let buffer_desc = ffi::AHardwareBuffer_Desc {
303 width: 1024,
304 height: 512,
305 layers: 1,
306 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
307 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
308 stride: 0,
309 rfu0: 0,
310 rfu1: 0,
311 };
312 let mut raw_buffer_ptr = ptr::null_mut();
313
Andrew Walbran03350bc2023-08-03 16:02:51 +0000314 // SAFETY: The pointers are valid because they come from references, and
315 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000316 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
317 assert_eq!(status, 0);
318
Andrew Walbran03350bc2023-08-03 16:02:51 +0000319 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
320 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000321 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000322 assert_eq!(buffer.width(), 1024);
323 }
324
325 #[test]
326 fn basic_getters() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000327 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000328 1024,
329 512,
330 1,
331 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
332 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
333 )
334 .expect("Buffer with some basic parameters was not created successfully");
335
336 assert_eq!(buffer.width(), 1024);
337 assert_eq!(buffer.height(), 512);
338 assert_eq!(buffer.layers(), 1);
339 assert_eq!(buffer.format(), AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);
340 assert_eq!(
341 buffer.usage(),
342 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
343 );
344 }
345
346 #[test]
347 fn id_getter() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000348 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000349 1024,
350 512,
351 1,
352 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
353 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
354 )
355 .expect("Buffer with some basic parameters was not created successfully");
356
357 assert_ne!(0, buffer.id());
358 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000359
360 #[test]
361 fn clone() {
362 let buffer = HardwareBuffer::new(
363 1024,
364 512,
365 1,
366 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
367 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
368 )
369 .expect("Buffer with some basic parameters was not created successfully");
370 let buffer2 = buffer.clone();
371
372 assert_eq!(buffer, buffer2);
373 }
374
375 #[test]
376 fn into_raw() {
377 let buffer = HardwareBuffer::new(
378 1024,
379 512,
380 1,
381 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
382 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
383 )
384 .expect("Buffer with some basic parameters was not created successfully");
385 let buffer2 = buffer.clone();
386
387 let raw_buffer = buffer.into_raw();
388 // SAFETY: This is the same pointer we had before.
389 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
390
391 assert_eq!(remade_buffer, buffer2);
392 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000393}