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