drivers/graphics: Remove KmsObject and rename KmsObjectKind to KmsObject

This commit is contained in:
bjorn3
2026-03-19 22:45:21 +01:00
parent 79e9e0987d
commit ee1b5377db
2 changed files with 29 additions and 48 deletions
@@ -39,14 +39,9 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
objects
}
pub(crate) fn add<U: Into<KmsObjectKind<T>>>(&mut self, data: U) -> KmsObjectId {
pub(crate) fn add<U: Into<KmsObject<T>>>(&mut self, data: U) -> KmsObjectId {
let id = self.next_id;
self.objects.insert(
id,
Arc::new(KmsObject {
kind: Box::new(data.into()),
}),
);
self.objects.insert(id, Arc::new(data.into()));
self.next_id.0 += 1;
id
@@ -54,10 +49,10 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
pub(crate) fn get<'a, U: 'a>(&'a self, id: KmsObjectId) -> Result<&'a U>
where
&'a U: TryFrom<&'a KmsObjectKind<T>>,
&'a U: TryFrom<&'a KmsObject<T>>,
{
let object = self.objects.get(&id).ok_or(Error::new(EINVAL))?;
if let Ok(object) = (&*object.kind).try_into() {
if let Ok(object) = (&**object).try_into() {
Ok(object)
} else {
Err(Error::new(EINVAL))
@@ -66,7 +61,7 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
pub fn object_type(&self, id: KmsObjectId) -> Result<u32> {
let object = self.objects.get(&id).ok_or(Error::new(EINVAL))?;
Ok(object.kind.object_type())
Ok(object.object_type())
}
pub fn add_crtc(&mut self, driver_data: T::Crtc) -> KmsObjectId {
@@ -109,11 +104,10 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
}
pub fn remove_framebuffer(&mut self, id: KmsObjectId) -> Result<()> {
let kind = match self.objects.get(&id).map(|object| &**object) {
Some(KmsObject { kind, .. }) => kind,
_ => return Err(Error::new(EINVAL)),
let Some(object) = self.objects.get(&id).map(|object| &**object) else {
return Err(Error::new(EINVAL));
};
let KmsObjectKind::Framebuffer(_) = **kind else {
let KmsObject::Framebuffer(_) = object else {
return Err(Error::new(EINVAL));
};
self.objects.remove(&id).unwrap();
@@ -146,27 +140,16 @@ impl From<KmsObjectId> for u64 {
}
}
#[derive(Debug)]
pub(crate) struct KmsObject<T: GraphicsAdapter> {
kind: Box<KmsObjectKind<T>>,
}
impl<T: GraphicsAdapter> KmsObject<T> {
pub(super) fn kind(&self) -> &KmsObjectKind<T> {
&self.kind
}
}
macro_rules! define_object_kinds {
(<$T:ident> $(
$variant:ident($data:ty) = $type:ident,
)*) => {
#[derive(Debug)]
pub(crate) enum KmsObjectKind<$T: GraphicsAdapter> {
pub(crate) enum KmsObject<$T: GraphicsAdapter> {
$($variant($data),)*
}
impl<$T: GraphicsAdapter> KmsObjectKind<$T> {
impl<$T: GraphicsAdapter> KmsObject<$T> {
fn object_type(&self) -> u32 {
match self {
$(Self::$variant(_) => $type,)*
@@ -175,18 +158,18 @@ macro_rules! define_object_kinds {
}
$(
impl<$T: GraphicsAdapter> From<$data> for KmsObjectKind<$T> {
impl<$T: GraphicsAdapter> From<$data> for KmsObject<$T> {
fn from(value: $data) -> Self {
Self::$variant(value)
}
}
impl<'a, $T: GraphicsAdapter> TryFrom<&'a KmsObjectKind<$T>> for &'a $data {
impl<'a, $T: GraphicsAdapter> TryFrom<&'a KmsObject<$T>> for &'a $data {
type Error = ();
fn try_from(value: &'a KmsObjectKind<T>) -> Result<Self, Self::Error> {
fn try_from(value: &'a KmsObject<T>) -> Result<Self, Self::Error> {
match value {
KmsObjectKind::$variant(data) => Ok(data),
KmsObject::$variant(data) => Ok(data),
_ => Err(()),
}
}
@@ -8,7 +8,7 @@ use drm_sys::{
};
use syscall::{Error, Result, EINVAL};
use crate::kms::objects::{KmsObjectId, KmsObjects};
use crate::kms::objects::{KmsObject, KmsObjectId, KmsObjects};
use crate::GraphicsAdapter;
impl<T: GraphicsAdapter> KmsObjects<T> {
@@ -52,39 +52,37 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
pub fn set_object_property(&mut self, object: KmsObjectId, property: KmsObjectId, value: u64) {
let object = self.objects.get_mut(&object).unwrap();
// FIXME validate property existence and value
match object.kind() {
super::objects::KmsObjectKind::Crtc(crtc) => {
match &**object {
KmsObject::Crtc(crtc) => {
for (prop, val) in crtc.lock().unwrap().properties.iter_mut() {
if *prop == property {
*val = value;
}
}
}
super::objects::KmsObjectKind::Connector(connector) => {
KmsObject::Connector(connector) => {
for (prop, val) in connector.lock().unwrap().properties.iter_mut() {
if *prop == property {
*val = value;
}
}
}
super::objects::KmsObjectKind::Encoder(_)
| super::objects::KmsObjectKind::Property(_)
| super::objects::KmsObjectKind::Framebuffer(_)
| super::objects::KmsObjectKind::Blob(_) => unreachable!(),
KmsObject::Encoder(_)
| KmsObject::Property(_)
| KmsObject::Framebuffer(_)
| KmsObject::Blob(_) => unreachable!(),
}
}
pub fn get_object_properties_data(&self, id: KmsObjectId) -> Result<(Vec<u32>, Vec<u64>)> {
let object = self.objects.get(&id).ok_or(Error::new(EINVAL))?;
let props = match object.kind() {
super::objects::KmsObjectKind::Crtc(crtc) => crtc.lock().unwrap().properties.clone(),
super::objects::KmsObjectKind::Connector(connector) => {
connector.lock().unwrap().properties.clone()
}
super::objects::KmsObjectKind::Encoder(_) => vec![],
super::objects::KmsObjectKind::Property(_) => vec![],
super::objects::KmsObjectKind::Framebuffer(_) => vec![],
super::objects::KmsObjectKind::Blob(_) => vec![],
let props = match &**object {
KmsObject::Crtc(crtc) => crtc.lock().unwrap().properties.clone(),
KmsObject::Connector(connector) => connector.lock().unwrap().properties.clone(),
KmsObject::Encoder(_) => vec![],
KmsObject::Property(_) => vec![],
KmsObject::Framebuffer(_) => vec![],
KmsObject::Blob(_) => vec![],
};
Ok((
props.iter().map(|&(id, _)| id.0).collect::<Vec<_>>(),