Skip to content

Commit b2daffa

Browse files
authored
bitmap: Strip off repeated Android prefix from structs and enums (#430)
This convention is already applied to `(Android)BitmapError` and `(Android)BitmapFormat`, and to many other `A`-prefixed structs elsewhere in the `ndk` crate: there is no need to repeat the Android prefix or letter when describing strucures and enumerations. Also strip off the `Bitmap` prefix in a `Result` alias, which isn't common either when only filling in a default for the generic `Error` argument.
1 parent ffdea02 commit b2daffa

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

ndk/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
- native_window: Add `lock()` to blit raw pixel data. (#404)
1919
- hardware_buffer_format: Add `YCbCr_P010` and `R8_UNORM` variants. (#405)
2020
- **Breaking:** hardware_buffer_format: Add catch-all variant. (#407)
21-
- ndk/asset: Add missing `is_allocated()` and `open_file_descriptor()` methods. (#409)
21+
- asset: Add missing `is_allocated()` and `open_file_descriptor()` methods. (#409)
2222
- **Breaking:** media_codec: Add support for asynchronous notification callbacks. (#410)
2323
- Add panic guards to callbacks. (#412)
2424
- looper: Add `remove_fd()` to unregister events/callbacks for a file descriptor. (#416)
2525
- **Breaking:** Use `BorrowedFd` and `OwnedFd` to clarify possible ownership transitions. (#417)
2626
- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-beta0-2023-08-15). (#420)
2727
- **Breaking:** bitmap: Provide detailed implementation for `AndroidBitmapInfoFlags`. (#424)
28-
- ndk/native_window: Add `set_buffers_transform()`, `try_allocate_buffers()` and `set_frame_rate*()`. (#425)
28+
- native_window: Add `set_buffers_transform()`, `try_allocate_buffers()` and `set_frame_rate*()`. (#425)
2929
- hardware_buffer: Add `id()` to retrieve a system-wide unique identifier for a `HardwareBuffer`. (#428)
30+
- **Breaking:** bitmap: Strip `Android` prefix from structs and enums, and `Bitmap` from `Result`. (#430)
3031

3132
# 0.7.0 (2022-07-24)
3233

ndk/src/bitmap.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ pub enum BitmapError {
2525
JniException = ffi::ANDROID_BITMAP_RESULT_JNI_EXCEPTION,
2626
}
2727

28-
pub type BitmapResult<T, E = BitmapError> = Result<T, E>;
28+
pub type Result<T, E = BitmapError> = std::result::Result<T, E>;
2929

3030
impl BitmapError {
31-
pub(crate) fn from_status(status: i32) -> BitmapResult<()> {
31+
pub(crate) fn from_status(status: i32) -> Result<()> {
3232
Err(match status {
3333
ffi::ANDROID_BITMAP_RESULT_SUCCESS => return Ok(()),
3434
ffi::ANDROID_BITMAP_RESULT_ALLOCATION_FAILED => BitmapError::AllocationFailed,
@@ -39,7 +39,7 @@ impl BitmapError {
3939
}
4040
}
4141

42-
fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> BitmapResult<T> {
42+
fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> Result<T> {
4343
let mut result = MaybeUninit::uninit();
4444
let status = with_ptr(result.as_mut_ptr());
4545
BitmapError::from_status(status).map(|()| unsafe { result.assume_init() })
@@ -48,6 +48,7 @@ fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> BitmapResult<T> {
4848
#[repr(u32)]
4949
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
5050
#[allow(non_camel_case_types)]
51+
#[doc(alias = "AndroidBitmapFormat")]
5152
pub enum BitmapFormat {
5253
#[doc(alias = "ANDROID_BITMAP_FORMAT_NONE")]
5354
NONE = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_NONE.0,
@@ -70,13 +71,13 @@ pub enum BitmapFormat {
7071
///
7172
/// [`android.graphics.Bitmap`]: https://developer.android.com/reference/android/graphics/Bitmap
7273
#[derive(Debug)]
73-
pub struct AndroidBitmap {
74+
pub struct Bitmap {
7475
env: *mut JNIEnv,
7576
inner: jobject,
7677
}
7778

78-
impl AndroidBitmap {
79-
/// Create an [`AndroidBitmap`] wrapper from JNI pointers
79+
impl Bitmap {
80+
/// Create a [`Bitmap`] wrapper from JNI pointers
8081
///
8182
/// # Safety
8283
/// This function should be called with a healthy JVM pointer and with a non-null
@@ -87,43 +88,43 @@ impl AndroidBitmap {
8788
Self { env, inner: bitmap }
8889
}
8990

90-
/// Fills out and returns the [`AndroidBitmapInfo`] struct for the given Java bitmap object.
91+
/// Fills out and returns the [`BitmapInfo`] struct for the given Java bitmap object.
9192
#[doc(alias = "AndroidBitmap_getInfo")]
92-
pub fn get_info(&self) -> BitmapResult<AndroidBitmapInfo> {
93+
pub fn get_info(&self) -> Result<BitmapInfo> {
9394
let inner =
9495
construct(|res| unsafe { ffi::AndroidBitmap_getInfo(self.env, self.inner, res) })?;
9596

96-
Ok(AndroidBitmapInfo { inner })
97+
Ok(BitmapInfo { inner })
9798
}
9899

99100
/// Attempt to lock the pixel address.
100101
///
101102
/// Locking will ensure that the memory for the pixels will not move until the
102-
/// [`AndroidBitmap::unlock_pixels()`] call, and ensure that, if the pixels had been previously
103-
/// purged, they will have been restored.
103+
/// [`Bitmap::unlock_pixels()`] call, and ensure that, if the pixels had been previously purged,
104+
/// they will have been restored.
104105
///
105-
/// If this call succeeds, it must be balanced by a call to [`AndroidBitmap::unlock_pixels()`],
106-
/// after which time the address of the pixels should no longer be used.
106+
/// If this call succeeds, it must be balanced by a call to [`Bitmap::unlock_pixels()`], after
107+
/// which time the address of the pixels should no longer be used.
107108
#[doc(alias = "AndroidBitmap_lockPixels")]
108-
pub fn lock_pixels(&self) -> BitmapResult<*mut std::os::raw::c_void> {
109+
pub fn lock_pixels(&self) -> Result<*mut std::os::raw::c_void> {
109110
construct(|res| unsafe { ffi::AndroidBitmap_lockPixels(self.env, self.inner, res) })
110111
}
111112

112-
/// Call this to balance a successful call to [`AndroidBitmap::lock_pixels()`].
113+
/// Call this to balance a successful call to [`Bitmap::lock_pixels()`].
113114
#[doc(alias = "AndroidBitmap_unlockPixels")]
114-
pub fn unlock_pixels(&self) -> BitmapResult<()> {
115+
pub fn unlock_pixels(&self) -> Result<()> {
115116
let status = unsafe { ffi::AndroidBitmap_unlockPixels(self.env, self.inner) };
116117
BitmapError::from_status(status)
117118
}
118119

119120
/// Retrieve the native object associated with an [`ffi::ANDROID_BITMAP_FLAGS_IS_HARDWARE`]
120-
/// [`AndroidBitmap`] (requires [`AndroidBitmapInfoFlags::is_hardware()`] on
121-
/// [`AndroidBitmapInfo::flags()`] to return [`true`]).
121+
/// [`Bitmap`] (requires [`BitmapInfoFlags::is_hardware()`] on [`BitmapInfo::flags()`] to return
122+
/// [`true`]).
122123
///
123-
/// Client must not modify it while an [`AndroidBitmap`] is wrapping it.
124+
/// Client must not modify it while a [`Bitmap`] is wrapping it.
124125
#[cfg(feature = "api-level-30")]
125126
#[doc(alias = "AndroidBitmap_getHardwareBuffer")]
126-
pub fn get_hardware_buffer(&self) -> BitmapResult<HardwareBufferRef> {
127+
pub fn get_hardware_buffer(&self) -> Result<HardwareBufferRef> {
127128
unsafe {
128129
let result =
129130
construct(|res| ffi::AndroidBitmap_getHardwareBuffer(self.env, self.inner, res))?;
@@ -137,10 +138,11 @@ impl AndroidBitmap {
137138
}
138139
}
139140

140-
/// Possible values for [`ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK`] within [`AndroidBitmapInfoFlags`]
141+
/// Possible values for [`ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK`] within [`BitmapInfoFlags`]
141142
#[cfg(feature = "api-level-30")]
142143
#[derive(Clone, Copy, Debug)]
143-
pub enum AndroidBitmapInfoFlagsAlpha {
144+
#[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_MASK")]
145+
pub enum BitmapInfoFlagsAlpha {
144146
/// Pixel components are premultiplied by alpha.
145147
#[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_PREMUL")]
146148
Premultiplied,
@@ -154,16 +156,16 @@ pub enum AndroidBitmapInfoFlagsAlpha {
154156

155157
/// Bitfield containing information about the bitmap.
156158
#[cfg(feature = "api-level-30")]
157-
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
158159
#[repr(transparent)]
159-
pub struct AndroidBitmapInfoFlags(u32);
160+
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
161+
pub struct BitmapInfoFlags(u32);
160162

161163
#[cfg(feature = "api-level-30")]
162-
impl std::fmt::Debug for AndroidBitmapInfoFlags {
164+
impl std::fmt::Debug for BitmapInfoFlags {
163165
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164166
write!(
165167
f,
166-
"AndroidBitmapInfoFlags({:#x}, alpha: {:?}, is_hardware: {})",
168+
"BitmapInfoFlags({:#x}, alpha: {:?}, is_hardware: {})",
167169
self.0,
168170
self.alpha(),
169171
self.is_hardware()
@@ -172,25 +174,23 @@ impl std::fmt::Debug for AndroidBitmapInfoFlags {
172174
}
173175

174176
#[cfg(feature = "api-level-30")]
175-
impl AndroidBitmapInfoFlags {
177+
impl BitmapInfoFlags {
176178
/// Returns the alpha value contained in the [`ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK`] bit range
177179
#[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_MASK")]
178-
pub fn alpha(self) -> AndroidBitmapInfoFlagsAlpha {
180+
pub fn alpha(self) -> BitmapInfoFlagsAlpha {
179181
// Note that ffi::ANDROID_BITMAP_FLAGS_ALPHA_SHIFT is 0 and hence irrelevant.
180182
match self.0 & ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK {
181-
ffi::ANDROID_BITMAP_FLAGS_ALPHA_PREMUL => AndroidBitmapInfoFlagsAlpha::Premultiplied,
182-
ffi::ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE => AndroidBitmapInfoFlagsAlpha::Opaque,
183-
ffi::ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL => {
184-
AndroidBitmapInfoFlagsAlpha::Unpremultiplied
185-
}
183+
ffi::ANDROID_BITMAP_FLAGS_ALPHA_PREMUL => BitmapInfoFlagsAlpha::Premultiplied,
184+
ffi::ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE => BitmapInfoFlagsAlpha::Opaque,
185+
ffi::ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL => BitmapInfoFlagsAlpha::Unpremultiplied,
186186
3 => todo!("ALPHA_MASK value 3"),
187187
_ => unreachable!(),
188188
}
189189
}
190190

191191
/// Returns [`true`] when [`ffi::ANDROID_BITMAP_FLAGS_IS_HARDWARE`] is set, meaning this
192-
/// [`AndroidBitmap`] uses "HARDWARE Config" and its [`HardwareBufferRef`] can be retrieved via
193-
/// [`AndroidBitmap::get_hardware_buffer()`].
192+
/// [`Bitmap`] uses "HARDWARE Config" and its [`HardwareBufferRef`] can be retrieved via
193+
/// [`Bitmap::get_hardware_buffer()`].
194194
#[doc(alias = "ANDROID_BITMAP_FLAGS_IS_HARDWARE")]
195195
pub fn is_hardware(self) -> bool {
196196
// This constant is defined in a separate anonymous enum which bindgen treats as i32.
@@ -202,13 +202,14 @@ impl AndroidBitmapInfoFlags {
202202
///
203203
/// [`AndroidBitmapInfo`]: https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info
204204
#[derive(Clone, Copy)]
205-
pub struct AndroidBitmapInfo {
205+
#[doc(alias = "AndroidBitmapInfo")]
206+
pub struct BitmapInfo {
206207
inner: ffi::AndroidBitmapInfo,
207208
}
208209

209-
impl std::fmt::Debug for AndroidBitmapInfo {
210+
impl std::fmt::Debug for BitmapInfo {
210211
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
211-
let mut f = f.debug_struct("AndroidBitmapInfo");
212+
let mut f = f.debug_struct("BitmapInfo");
212213
f.field("width", &self.width())
213214
.field("height", &self.height())
214215
.field("stride", &self.stride())
@@ -221,7 +222,7 @@ impl std::fmt::Debug for AndroidBitmapInfo {
221222
}
222223
}
223224

224-
impl AndroidBitmapInfo {
225+
impl BitmapInfo {
225226
/// The bitmap width in pixels.
226227
pub fn width(&self) -> u32 {
227228
self.inner.width
@@ -242,8 +243,8 @@ impl AndroidBitmapInfo {
242243
/// # Panics
243244
///
244245
/// This function panics if the underlying value does not have a corresponding variant in
245-
/// [`BitmapFormat`]. Use [`try_format()`][AndroidBitmapInfo::try_format()] for an infallible
246-
/// version of this function.
246+
/// [`BitmapFormat`]. Use [`try_format()`][BitmapInfo::try_format()] for an infallible version
247+
/// of this function.
247248
pub fn format(&self) -> BitmapFormat {
248249
self.try_format().unwrap()
249250
}
@@ -258,7 +259,7 @@ impl AndroidBitmapInfo {
258259

259260
/// Bitfield containing information about the bitmap.
260261
#[cfg(feature = "api-level-30")]
261-
pub fn flags(&self) -> AndroidBitmapInfoFlags {
262-
AndroidBitmapInfoFlags(self.inner.flags)
262+
pub fn flags(&self) -> BitmapInfoFlags {
263+
BitmapInfoFlags(self.inner.flags)
263264
}
264265
}

0 commit comments

Comments
 (0)