blob: 6f86c4a48fffa67c6438292b7c6aeedd670d7229 [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::{
22 binder_impl::{
23 BorrowedParcel, Deserialize, DeserializeArray, DeserializeOption, Serialize,
24 SerializeArray, SerializeOption, NON_NULL_PARCELABLE_FLAG, NULL_PARCELABLE_FLAG,
25 },
26 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
105 /// Adopts the raw pointer and wraps it in a Rust AHardwareBuffer.
106 ///
107 /// # Errors
108 ///
109 /// Will panic if buffer_ptr is null.
110 ///
111 /// # Safety
112 ///
113 /// This function adopts the pointer but does NOT increment the refcount on the buffer. If the
114 /// caller uses the pointer after the created object is dropped it will cause a memory leak.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000115 pub unsafe fn from_raw(buffer_ptr: NonNull<AHardwareBuffer>) -> Self {
116 Self(buffer_ptr)
117 }
118
119 /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can
120 /// be used to provide a pointer to the AHB for a C/C++ API over the FFI.
121 pub fn into_raw(self) -> NonNull<AHardwareBuffer> {
122 let buffer = ManuallyDrop::new(self);
123 buffer.0
Jim Shargo7df9f752023-07-18 20:33:45 +0000124 }
125
126 /// Get the system wide unique id for an AHardwareBuffer. This function may panic in extreme
127 /// and undocumented circumstances.
128 ///
129 /// Available since API level 31.
130 pub fn id(&self) -> u64 {
131 let mut out_id = 0;
Andrew Walbran43bddb62023-09-01 16:43:09 +0100132 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
133 // because it must have been allocated by `AHardwareBuffer_allocate`,
134 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
135 // released it. The id pointer must be valid because it comes from a reference.
136 let status = unsafe { ffi::AHardwareBuffer_getId(self.0.as_ptr(), &mut out_id) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000137 assert_eq!(status, 0, "id() failed for AHardwareBuffer with error code: {status}");
138
139 out_id
140 }
141
142 /// Get the width of this buffer
143 pub fn width(&self) -> u32 {
144 self.description().width
145 }
146
147 /// Get the height of this buffer
148 pub fn height(&self) -> u32 {
149 self.description().height
150 }
151
152 /// Get the number of layers of this buffer
153 pub fn layers(&self) -> u32 {
154 self.description().layers
155 }
156
157 /// Get the format of this buffer
158 pub fn format(&self) -> AHardwareBuffer_Format::Type {
159 self.description().format
160 }
161
162 /// Get the usage bitvector of this buffer
163 pub fn usage(&self) -> AHardwareBuffer_UsageFlags {
164 AHardwareBuffer_UsageFlags(self.description().usage)
165 }
166
167 /// Get the stride of this buffer
168 pub fn stride(&self) -> u32 {
169 self.description().stride
170 }
171
172 fn description(&self) -> ffi::AHardwareBuffer_Desc {
173 let mut buffer_desc = ffi::AHardwareBuffer_Desc {
174 width: 0,
175 height: 0,
176 layers: 0,
177 format: 0,
178 usage: 0,
179 stride: 0,
180 rfu0: 0,
181 rfu1: 0,
182 };
183 // SAFETY: neither the buffer nor AHardwareBuffer_Desc pointers will be null.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000184 unsafe { ffi::AHardwareBuffer_describe(self.0.as_ref(), &mut buffer_desc) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000185 buffer_desc
186 }
187}
188
Jim Shargob69c6ef2023-10-05 22:54:51 +0000189impl Drop for HardwareBuffer {
Jim Shargo7df9f752023-07-18 20:33:45 +0000190 fn drop(&mut self) {
Andrew Walbran43bddb62023-09-01 16:43:09 +0100191 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
192 // because it must have been allocated by `AHardwareBuffer_allocate`,
193 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
194 // released it.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000195 unsafe { ffi::AHardwareBuffer_release(self.0.as_ptr()) }
Jim Shargo7df9f752023-07-18 20:33:45 +0000196 }
197}
198
Jim Shargob69c6ef2023-10-05 22:54:51 +0000199impl Debug for HardwareBuffer {
200 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
201 f.debug_struct("HardwareBuffer").field("id", &self.id()).finish()
202 }
203}
204
205impl Clone for HardwareBuffer {
206 fn clone(&self) -> Self {
207 // SAFETY: ptr is guaranteed to be non-null and the acquire can not fail.
208 unsafe { ffi::AHardwareBuffer_acquire(self.0.as_ptr()) };
209 Self(self.0)
210 }
211}
212
Andrew Walbran43bddb62023-09-01 16:43:09 +0100213impl Serialize for HardwareBuffer {
214 fn serialize(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
215 SerializeOption::serialize_option(Some(self), parcel)
216 }
217}
218
219impl SerializeOption for HardwareBuffer {
220 fn serialize_option(
221 this: Option<&Self>,
222 parcel: &mut BorrowedParcel,
223 ) -> Result<(), StatusCode> {
224 if let Some(this) = this {
225 parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
226
227 let status =
228 // SAFETY: The AHardwareBuffer pointer we pass is guaranteed to be non-null and valid
229 // because it must have been allocated by `AHardwareBuffer_allocate`,
230 // `AHardwareBuffer_readFromParcel` or the caller of `from_raw` and we have not yet
231 // released it.
232 unsafe { AHardwareBuffer_writeToParcel(this.0.as_ptr(), parcel.as_native_mut()) };
233 status_result(status)
234 } else {
235 parcel.write(&NULL_PARCELABLE_FLAG)
236 }
237 }
238}
239
240impl Deserialize for HardwareBuffer {
241 type UninitType = Option<Self>;
242
243 fn uninit() -> Option<Self> {
244 None
245 }
246
247 fn from_init(value: Self) -> Option<Self> {
248 Some(value)
249 }
250
251 fn deserialize(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
252 DeserializeOption::deserialize_option(parcel)
253 .transpose()
254 .unwrap_or(Err(StatusCode::UNEXPECTED_NULL))
255 }
256}
257
258impl DeserializeOption for HardwareBuffer {
259 fn deserialize_option(parcel: &BorrowedParcel) -> Result<Option<Self>, StatusCode> {
260 let present: i32 = parcel.read()?;
261 match present {
262 NULL_PARCELABLE_FLAG => Ok(None),
263 NON_NULL_PARCELABLE_FLAG => {
264 let mut buffer = null_mut();
265
266 let status =
267 // SAFETY: Both pointers must be valid because they are obtained from references.
268 // `AHardwareBuffer_readFromParcel` doesn't store them or do anything else special
269 // with them. If it returns success then it will have allocated a new
270 // `AHardwareBuffer` and incremented the reference count, so we can use it until we
271 // release it.
272 unsafe { AHardwareBuffer_readFromParcel(parcel.as_native(), &mut buffer) };
273
274 status_result(status)?;
275
276 Ok(Some(Self(NonNull::new(buffer).expect(
277 "AHardwareBuffer_readFromParcel returned success but didn't allocate buffer",
278 ))))
279 }
280 _ => Err(StatusCode::BAD_VALUE),
281 }
282 }
283}
284
285impl SerializeArray for HardwareBuffer {}
286
287impl DeserializeArray for HardwareBuffer {}
288
Jim Shargob69c6ef2023-10-05 22:54:51 +0000289// SAFETY: The underlying *AHardwareBuffers can be moved between threads.
290unsafe impl Send for HardwareBuffer {}
291
292// SAFETY: The underlying *AHardwareBuffers can be used from multiple threads.
293//
294// AHardwareBuffers are backed by C++ GraphicBuffers, which are mostly immutable. The only cases
295// where they are not immutable are:
296//
297// - reallocation (which is never actually done across the codebase and requires special
298// privileges/platform code access to do)
299// - "locking" for reading/writing (which is explicitly allowed to be done across multiple threads
300// according to the docs on the underlying gralloc calls)
301unsafe impl Sync for HardwareBuffer {}
302
Jim Shargo7df9f752023-07-18 20:33:45 +0000303#[cfg(test)]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000304mod test {
Jim Shargo7df9f752023-07-18 20:33:45 +0000305 use super::*;
306
307 #[test]
308 fn create_valid_buffer_returns_ok() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000309 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000310 512,
311 512,
312 1,
313 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
314 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
315 );
316 assert!(buffer.is_some());
317 }
318
319 #[test]
320 fn create_invalid_buffer_returns_err() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000321 let buffer = HardwareBuffer::new(512, 512, 1, 0, AHardwareBuffer_UsageFlags(0));
Jim Shargo7df9f752023-07-18 20:33:45 +0000322 assert!(buffer.is_none());
323 }
324
325 #[test]
Jim Shargob69c6ef2023-10-05 22:54:51 +0000326 fn from_raw_allows_getters() {
Jim Shargo7df9f752023-07-18 20:33:45 +0000327 let buffer_desc = ffi::AHardwareBuffer_Desc {
328 width: 1024,
329 height: 512,
330 layers: 1,
331 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
332 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0,
333 stride: 0,
334 rfu0: 0,
335 rfu1: 0,
336 };
337 let mut raw_buffer_ptr = ptr::null_mut();
338
Andrew Walbran03350bc2023-08-03 16:02:51 +0000339 // SAFETY: The pointers are valid because they come from references, and
340 // `AHardwareBuffer_allocate` doesn't retain them after it returns.
Jim Shargo7df9f752023-07-18 20:33:45 +0000341 let status = unsafe { ffi::AHardwareBuffer_allocate(&buffer_desc, &mut raw_buffer_ptr) };
342 assert_eq!(status, 0);
343
Andrew Walbran03350bc2023-08-03 16:02:51 +0000344 // SAFETY: The pointer must be valid because it was just allocated successfully, and we
345 // don't use it after calling this.
Jim Shargob69c6ef2023-10-05 22:54:51 +0000346 let buffer = unsafe { HardwareBuffer::from_raw(NonNull::new(raw_buffer_ptr).unwrap()) };
Jim Shargo7df9f752023-07-18 20:33:45 +0000347 assert_eq!(buffer.width(), 1024);
348 }
349
350 #[test]
351 fn basic_getters() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000352 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000353 1024,
354 512,
355 1,
356 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
357 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
358 )
359 .expect("Buffer with some basic parameters was not created successfully");
360
361 assert_eq!(buffer.width(), 1024);
362 assert_eq!(buffer.height(), 512);
363 assert_eq!(buffer.layers(), 1);
364 assert_eq!(buffer.format(), AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);
365 assert_eq!(
366 buffer.usage(),
367 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
368 );
369 }
370
371 #[test]
372 fn id_getter() {
Jim Shargob69c6ef2023-10-05 22:54:51 +0000373 let buffer = HardwareBuffer::new(
Jim Shargo7df9f752023-07-18 20:33:45 +0000374 1024,
375 512,
376 1,
377 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
378 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
379 )
380 .expect("Buffer with some basic parameters was not created successfully");
381
382 assert_ne!(0, buffer.id());
383 }
Jim Shargob69c6ef2023-10-05 22:54:51 +0000384
385 #[test]
386 fn clone() {
387 let buffer = HardwareBuffer::new(
388 1024,
389 512,
390 1,
391 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
392 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
393 )
394 .expect("Buffer with some basic parameters was not created successfully");
395 let buffer2 = buffer.clone();
396
397 assert_eq!(buffer, buffer2);
398 }
399
400 #[test]
401 fn into_raw() {
402 let buffer = HardwareBuffer::new(
403 1024,
404 512,
405 1,
406 AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
407 AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
408 )
409 .expect("Buffer with some basic parameters was not created successfully");
410 let buffer2 = buffer.clone();
411
412 let raw_buffer = buffer.into_raw();
413 // SAFETY: This is the same pointer we had before.
414 let remade_buffer = unsafe { HardwareBuffer::from_raw(raw_buffer) };
415
416 assert_eq!(remade_buffer, buffer2);
417 }
Jim Shargo7df9f752023-07-18 20:33:45 +0000418}