use drm::control::Device as _;
use drmkit_core::Device;
use drmkit_planes::{PlaneRegistry, PlaneType};
pub const DEFAULT_PREFERENCE: [u32; 6] = [
drmkit_fmt::fourcc::XRGB8888,
drmkit_fmt::fourcc::XBGR8888,
drmkit_fmt::fourcc::ARGB8888,
drmkit_fmt::fourcc::ABGR8888,
drmkit_fmt::fourcc::RGB565,
drmkit_fmt::fourcc::BGR565,
];
#[must_use]
pub fn negotiate_scanout_format(device: &Device, crtc_id: u32, preferred: &[u32]) -> Option<u32> {
let registry = PlaneRegistry::probe(device).ok()?;
let resources = device.resource_handles().ok()?;
let index = resources
.crtcs()
.iter()
.position(|handle| u32::from(*handle) == crtc_id)?;
let index = u32::try_from(index).ok()?;
let prefs = if preferred.is_empty() {
&DEFAULT_PREFERENCE[..]
} else {
preferred
};
let pick = |plane: &drmkit_planes::PlaneCapabilities| {
prefs
.iter()
.copied()
.find(|fourcc| plane.supports_format(*fourcc))
};
if let Some(fourcc) = registry
.for_crtc(index)
.find(|plane| plane.plane_type == PlaneType::Primary)
.and_then(pick)
{
return Some(fourcc);
}
registry.for_crtc(index).find_map(pick)
}