use crate::buffer::BufferDescriptor;
use crate::color::PixelFormat;
use crate::geometry::Size;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct DisplayInfo {
pub id: u32,
pub width: u32,
pub height: u32,
pub refresh_rate_mhz: u32,
pub format: PixelFormat,
pub stride: u32,
}
impl DisplayInfo {
#[inline]
pub const fn new(
id: u32,
width: u32,
height: u32,
refresh_rate_mhz: u32,
format: PixelFormat,
stride: u32,
) -> Self {
Self {
id,
width,
height,
refresh_rate_mhz,
format,
stride,
}
}
#[inline]
pub const fn size(&self) -> Size {
Size::new(self.width, self.height)
}
#[inline]
pub const fn as_buffer_descriptor(&self) -> BufferDescriptor {
BufferDescriptor {
width: self.width,
height: self.height,
stride: self.stride,
format: self.format,
}
}
#[inline]
pub const fn refresh_rate_hz(&self) -> u32 {
self.refresh_rate_mhz / 1000
}
#[inline]
pub fn refresh_rate_hz_f(&self) -> f32 {
self.refresh_rate_mhz as f32 / 1000.0
}
#[inline]
pub const fn area(&self) -> u64 {
self.width as u64 * self.height as u64
}
#[inline]
pub const fn framebuffer_size(&self) -> usize {
(self.stride as usize) * (self.height as usize)
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DisplayMode {
pub width: u32,
pub height: u32,
pub refresh_rate_mhz: u32,
pub flags: u32,
}
impl DisplayMode {
pub const FLAG_PREFERRED: u32 = 1 << 0;
pub const FLAG_CURRENT: u32 = 1 << 1;
pub const FLAG_INTERLACED: u32 = 1 << 2;
#[inline]
pub const fn new(width: u32, height: u32, refresh_rate_mhz: u32) -> Self {
Self {
width,
height,
refresh_rate_mhz,
flags: 0,
}
}
#[inline]
pub const fn size(&self) -> Size {
Size::new(self.width, self.height)
}
#[inline]
pub const fn is_preferred(&self) -> bool {
(self.flags & Self::FLAG_PREFERRED) != 0
}
#[inline]
pub const fn is_current(&self) -> bool {
(self.flags & Self::FLAG_CURRENT) != 0
}
#[inline]
pub const fn is_interlaced(&self) -> bool {
(self.flags & Self::FLAG_INTERLACED) != 0
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
pub enum VsyncMode {
Off = 0,
#[default]
On = 1,
Adaptive = 2,
Mailbox = 3,
}
impl VsyncMode {
#[inline]
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Off),
1 => Some(Self::On),
2 => Some(Self::Adaptive),
3 => Some(Self::Mailbox),
_ => None,
}
}
#[inline]
pub const fn name(&self) -> &'static str {
match self {
Self::Off => "Off",
Self::On => "On",
Self::Adaptive => "Adaptive",
Self::Mailbox => "Mailbox",
}
}
}