From 6ae2bcb091c11f75ecbbeabcc69ff26d9287689b Mon Sep 17 00:00:00 2001 From: oiupoyt Date: Wed, 30 Sep 2026 08:12:32 +0530 Subject: [PATCH] feat(graphics): implement ANativeWindow C-ABI types and pixel formats --- crates/molotov-core/src/graphics/mod.rs | 2 + crates/molotov-core/src/graphics/types.rs | 50 +++++++++++++++++++ crates/molotov-core/src/lib.rs | 1 + .../molotov-core/tests/graphics_types_test.rs | 10 ++++ 4 files changed, 63 insertions(+) create mode 100644 crates/molotov-core/src/graphics/mod.rs create mode 100644 crates/molotov-core/src/graphics/types.rs create mode 100644 crates/molotov-core/tests/graphics_types_test.rs diff --git a/crates/molotov-core/src/graphics/mod.rs b/crates/molotov-core/src/graphics/mod.rs new file mode 100644 index 0000000..b50bdf4 --- /dev/null +++ b/crates/molotov-core/src/graphics/mod.rs @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pub mod types; diff --git a/crates/molotov-core/src/graphics/types.rs b/crates/molotov-core/src/graphics/types.rs new file mode 100644 index 0000000..f3421ff --- /dev/null +++ b/crates/molotov-core/src/graphics/types.rs @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +use std::ffi::c_void; + +pub const WINDOW_FORMAT_RGBA_8888: i32 = 1; +pub const WINDOW_FORMAT_RGBX_8888: i32 = 2; +pub const WINDOW_FORMAT_RGB_565: i32 = 4; + +#[repr(i32)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WindowFormat { + Rgba8888 = 1, + Rgbx8888 = 2, + Rgb565 = 4, +} + +impl WindowFormat { + pub fn from_i32(val: i32) -> Option { + match val { + 1 => Some(WindowFormat::Rgba8888), + 2 => Some(WindowFormat::Rgbx8888), + 4 => Some(WindowFormat::Rgb565), + _ => None, + } + } + + pub fn bytes_per_pixel(&self) -> usize { + match self { + WindowFormat::Rgba8888 | WindowFormat::Rgbx8888 => 4, + WindowFormat::Rgb565 => 2, + } + } +} + +#[repr(C)] +pub struct ARect { + pub left: i32, + pub top: i32, + pub right: i32, + pub bottom: i32, +} + +#[repr(C)] +pub struct ANativeWindow_Buffer { + pub width: i32, + pub height: i32, + pub stride: i32, + pub format: i32, + pub bits: *mut c_void, + pub reserved: [u32; 6], +} diff --git a/crates/molotov-core/src/lib.rs b/crates/molotov-core/src/lib.rs index c4b8822..aa7bdf5 100644 --- a/crates/molotov-core/src/lib.rs +++ b/crates/molotov-core/src/lib.rs @@ -2,6 +2,7 @@ pub mod audio; pub mod dynamic; pub mod elf; +pub mod graphics; pub mod input; pub mod jni; pub mod linker; diff --git a/crates/molotov-core/tests/graphics_types_test.rs b/crates/molotov-core/tests/graphics_types_test.rs new file mode 100644 index 0000000..325fadd --- /dev/null +++ b/crates/molotov-core/tests/graphics_types_test.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +use molotov_core::graphics::types::{WindowFormat, WINDOW_FORMAT_RGBA_8888, WINDOW_FORMAT_RGBX_8888}; + +#[test] +fn test_window_format_constants() { + assert_eq!(WindowFormat::Rgba8888 as i32, WINDOW_FORMAT_RGBA_8888); + assert_eq!(WindowFormat::Rgbx8888 as i32, WINDOW_FORMAT_RGBX_8888); + assert_eq!(WindowFormat::from_i32(1), Some(WindowFormat::Rgba8888)); + assert_eq!(WindowFormat::from_i32(999), None); +}