Skip to content

Commit 78c6534

Browse files
committed
Wrap more methods to get and set properties of ANativeWindow.
Bug: 307535208 Test: atest libnativewindow_rs-internal_test Change-Id: I18e4158321f71bd95bb6d3f4868bae4d8a7d417e
1 parent 8dd8927 commit 78c6534

2 files changed

Lines changed: 114 additions & 3 deletions

File tree

libs/nativewindow/rust/Android.bp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rust_bindgen {
2626
source_stem: "bindings",
2727
bindgen_flags: [
2828
"--constified-enum-module=AHardwareBuffer_Format",
29+
"--bitfield-enum=ADataSpace",
2930
"--bitfield-enum=AHardwareBuffer_UsageFlags",
3031

3132
"--allowlist-file=.*/nativewindow/include/.*\\.h",
@@ -110,6 +111,7 @@ rust_defaults {
110111
srcs: ["src/lib.rs"],
111112
rustlibs: [
112113
"libbinder_rs",
114+
"libbitflags",
113115
"libnativewindow_bindgen",
114116
],
115117
}

libs/nativewindow/rust/src/surface.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ use binder::{
2020
unstable_api::{status_result, AsNative},
2121
StatusCode,
2222
};
23+
use bitflags::bitflags;
2324
use nativewindow_bindgen::{
24-
AHardwareBuffer_Format, ANativeWindow, ANativeWindow_acquire, ANativeWindow_getFormat,
25-
ANativeWindow_getHeight, ANativeWindow_getWidth, ANativeWindow_readFromParcel,
26-
ANativeWindow_release, ANativeWindow_writeToParcel,
25+
ADataSpace, AHardwareBuffer_Format, ANativeWindow, ANativeWindow_acquire,
26+
ANativeWindow_getBuffersDataSpace, ANativeWindow_getBuffersDefaultDataSpace,
27+
ANativeWindow_getFormat, ANativeWindow_getHeight, ANativeWindow_getWidth,
28+
ANativeWindow_readFromParcel, ANativeWindow_release, ANativeWindow_setBuffersDataSpace,
29+
ANativeWindow_setBuffersGeometry, ANativeWindow_setBuffersTransform,
30+
ANativeWindow_writeToParcel,
2731
};
2832
use std::error::Error;
2933
use std::fmt::{self, Debug, Display, Formatter};
@@ -60,6 +64,95 @@ impl Surface {
6064
let format = unsafe { ANativeWindow_getFormat(self.0.as_ptr()) };
6165
format.try_into().map_err(|_| ErrorCode(format))
6266
}
67+
68+
/// Changes the format and size of the window buffers.
69+
///
70+
/// The width and height control the number of pixels in the buffers, not the dimensions of the
71+
/// window on screen. If these are different than the window's physical size, then its buffer
72+
/// will be scaled to match that size when compositing it to the screen. The width and height
73+
/// must be either both zero or both non-zero. If both are 0 then the window's base value will
74+
/// come back in force.
75+
pub fn set_buffers_geometry(
76+
&mut self,
77+
width: i32,
78+
height: i32,
79+
format: AHardwareBuffer_Format::Type,
80+
) -> Result<(), ErrorCode> {
81+
// SAFETY: The ANativeWindow pointer we pass is guaranteed to be non-null and valid because
82+
// it must have been allocated by `ANativeWindow_allocate` or `ANativeWindow_readFromParcel`
83+
// and we have not yet released it.
84+
let status = unsafe {
85+
ANativeWindow_setBuffersGeometry(
86+
self.0.as_ptr(),
87+
width,
88+
height,
89+
format.try_into().expect("Invalid format"),
90+
)
91+
};
92+
93+
if status == 0 {
94+
Ok(())
95+
} else {
96+
Err(ErrorCode(status))
97+
}
98+
}
99+
100+
/// Sets a transfom that will be applied to future buffers posted to the window.
101+
pub fn set_buffers_transform(&mut self, transform: Transform) -> Result<(), ErrorCode> {
102+
// SAFETY: The ANativeWindow pointer we pass is guaranteed to be non-null and valid because
103+
// it must have been allocated by `ANativeWindow_allocate` or `ANativeWindow_readFromParcel`
104+
// and we have not yet released it.
105+
let status =
106+
unsafe { ANativeWindow_setBuffersTransform(self.0.as_ptr(), transform.bits() as i32) };
107+
108+
if status == 0 {
109+
Ok(())
110+
} else {
111+
Err(ErrorCode(status))
112+
}
113+
}
114+
115+
/// Sets the data space that will be applied to future buffers posted to the window.
116+
pub fn set_buffers_data_space(&mut self, data_space: ADataSpace) -> Result<(), ErrorCode> {
117+
// SAFETY: The ANativeWindow pointer we pass is guaranteed to be non-null and valid because
118+
// it must have been allocated by `ANativeWindow_allocate` or `ANativeWindow_readFromParcel`
119+
// and we have not yet released it.
120+
let status = unsafe { ANativeWindow_setBuffersDataSpace(self.0.as_ptr(), data_space.0) };
121+
122+
if status == 0 {
123+
Ok(())
124+
} else {
125+
Err(ErrorCode(status))
126+
}
127+
}
128+
129+
/// Gets the data space of the buffers in the window.
130+
pub fn get_buffers_data_space(&mut self) -> Result<ADataSpace, ErrorCode> {
131+
// SAFETY: The ANativeWindow pointer we pass is guaranteed to be non-null and valid because
132+
// it must have been allocated by `ANativeWindow_allocate` or `ANativeWindow_readFromParcel`
133+
// and we have not yet released it.
134+
let data_space = unsafe { ANativeWindow_getBuffersDataSpace(self.0.as_ptr()) };
135+
136+
if data_space < 0 {
137+
Err(ErrorCode(data_space))
138+
} else {
139+
Ok(ADataSpace(data_space))
140+
}
141+
}
142+
143+
/// Gets the default data space of the buffers in the window as set by the consumer.
144+
pub fn get_buffers_default_data_space(&mut self) -> Result<ADataSpace, ErrorCode> {
145+
// SAFETY: The ANativeWindow pointer we pass is guaranteed to be non-null and valid because
146+
// it must have been allocated by `ANativeWindow_allocate` or `ANativeWindow_readFromParcel`
147+
// and we have not yet released it.
148+
let data_space = unsafe { ANativeWindow_getBuffersDefaultDataSpace(self.0.as_ptr()) };
149+
150+
if data_space < 0 {
151+
Err(ErrorCode(data_space))
152+
} else {
153+
Ok(ADataSpace(data_space))
154+
}
155+
}
63156
}
64157

65158
impl Drop for Surface {
@@ -141,3 +234,19 @@ impl Display for ErrorCode {
141234
write!(f, "Error {}", self.0)
142235
}
143236
}
237+
238+
bitflags! {
239+
/// Transforms that can be applied to buffers as they are displayed to a window.
240+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
241+
pub struct Transform: u32 {
242+
const MIRROR_HORIZONTAL = 0x01;
243+
const MIRROR_VERTICAL = 0x02;
244+
const ROTATE_90 = 0x04;
245+
}
246+
}
247+
248+
impl Transform {
249+
pub const IDENTITY: Self = Self::empty();
250+
pub const ROTATE_180: Self = Self::MIRROR_HORIZONTAL.union(Self::MIRROR_VERTICAL);
251+
pub const ROTATE_270: Self = Self::ROTATE_180.union(Self::ROTATE_90);
252+
}

0 commit comments

Comments
 (0)