drivers/graphics: Replace KmsObject trait with an enum
The set of possible KMS object types is fixed.
This commit is contained in:
@@ -2,13 +2,10 @@ use std::ffi::c_char;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use drm_sys::{
|
||||
drm_mode_modeinfo, DRM_MODE_CONNECTOR_Unknown, DRM_MODE_OBJECT_CONNECTOR,
|
||||
DRM_MODE_OBJECT_ENCODER, DRM_MODE_TYPE_PREFERRED,
|
||||
};
|
||||
use drm_sys::{drm_mode_modeinfo, DRM_MODE_CONNECTOR_Unknown, DRM_MODE_TYPE_PREFERRED};
|
||||
use syscall::Result;
|
||||
|
||||
use crate::kms::objects::{KmsObject, KmsObjectId, KmsObjects};
|
||||
use crate::kms::objects::{KmsObjectId, KmsObjects};
|
||||
use crate::GraphicsAdapter;
|
||||
|
||||
impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
@@ -62,7 +59,7 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct KmsConnector<T: Debug + 'static> {
|
||||
pub struct KmsConnector<T: Debug> {
|
||||
pub encoder_id: KmsObjectId,
|
||||
pub modes: Vec<drm_mode_modeinfo>,
|
||||
pub connector_type: u32,
|
||||
@@ -74,7 +71,7 @@ pub struct KmsConnector<T: Debug + 'static> {
|
||||
pub driver_data: T,
|
||||
}
|
||||
|
||||
impl<T: Debug + 'static> KmsConnector<T> {
|
||||
impl<T: Debug> KmsConnector<T> {
|
||||
pub fn update_from_size(&mut self, width: u32, height: u32) {
|
||||
self.modes = vec![Self::modeinfo_for_size(width, height)];
|
||||
}
|
||||
@@ -175,12 +172,6 @@ pub enum DrmSubpixelOrder {
|
||||
None,
|
||||
}
|
||||
|
||||
impl<T: Debug + 'static> KmsObject for Mutex<KmsConnector<T>> {
|
||||
fn object_type(&self) -> u32 {
|
||||
DRM_MODE_OBJECT_CONNECTOR
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME can we represent connector and encoder using a single struct?
|
||||
#[derive(Debug)]
|
||||
pub struct KmsEncoder {
|
||||
@@ -188,9 +179,3 @@ pub struct KmsEncoder {
|
||||
pub possible_crtcs: u32,
|
||||
pub possible_clones: u32,
|
||||
}
|
||||
|
||||
impl KmsObject for KmsEncoder {
|
||||
fn object_type(&self) -> u32 {
|
||||
DRM_MODE_OBJECT_ENCODER
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use drm_sys::{
|
||||
DRM_MODE_OBJECT_BLOB, DRM_MODE_OBJECT_CONNECTOR, DRM_MODE_OBJECT_ENCODER,
|
||||
DRM_MODE_OBJECT_PROPERTY,
|
||||
};
|
||||
use syscall::{Error, Result, EINVAL};
|
||||
|
||||
use crate::kms::connector::{KmsConnector, KmsEncoder};
|
||||
use crate::kms::properties::{KmsBlob, KmsProperty};
|
||||
use crate::GraphicsAdapter;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -13,7 +18,7 @@ pub struct KmsObjects<T: GraphicsAdapter> {
|
||||
next_id: KmsObjectId,
|
||||
pub(crate) connectors: Vec<KmsObjectId>,
|
||||
pub(crate) encoders: Vec<KmsObjectId>,
|
||||
pub(crate) objects: HashMap<KmsObjectId, Arc<KmsObjectData>>,
|
||||
pub(crate) objects: HashMap<KmsObjectId, Arc<KmsObjectData<T>>>,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
@@ -28,12 +33,12 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add<U: KmsObject>(&mut self, data: U) -> KmsObjectId {
|
||||
pub(crate) fn add<U: Into<KmsObjectKind<T>>>(&mut self, data: U) -> KmsObjectId {
|
||||
let id = self.next_id;
|
||||
self.objects.insert(
|
||||
id,
|
||||
Arc::new(KmsObjectData {
|
||||
kind: Box::new(data),
|
||||
kind: Box::new(data.into()),
|
||||
properties: Mutex::new(vec![]),
|
||||
}),
|
||||
);
|
||||
@@ -42,9 +47,12 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
id
|
||||
}
|
||||
|
||||
pub(crate) fn get<U: KmsObject>(&self, id: KmsObjectId) -> Result<&U> {
|
||||
pub(crate) fn get<'a, U: 'a>(&'a self, id: KmsObjectId) -> Result<&'a U>
|
||||
where
|
||||
&'a U: TryFrom<&'a KmsObjectKind<T>>,
|
||||
{
|
||||
let object = self.objects.get(&id).ok_or(Error::new(EINVAL))?;
|
||||
if let Some(object) = (&*object.kind as &dyn Any).downcast_ref::<U>() {
|
||||
if let Ok(object) = (&*object.kind).try_into() {
|
||||
Ok(object)
|
||||
} else {
|
||||
Err(Error::new(EINVAL))
|
||||
@@ -71,11 +79,53 @@ impl From<KmsObjectId> for u64 {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct KmsObjectData {
|
||||
kind: Box<dyn KmsObject + 'static>,
|
||||
pub(crate) struct KmsObjectData<T: GraphicsAdapter> {
|
||||
kind: Box<KmsObjectKind<T>>,
|
||||
pub(crate) properties: Mutex<Vec<(KmsObjectId, u64)>>,
|
||||
}
|
||||
|
||||
pub trait KmsObject: Any + Debug {
|
||||
fn object_type(&self) -> u32;
|
||||
macro_rules! define_object_kinds {
|
||||
(<$T:ident> $(
|
||||
$variant:ident($data:ty) = $type:ident,
|
||||
)*) => {
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum KmsObjectKind<$T: GraphicsAdapter> {
|
||||
$($variant($data),)*
|
||||
}
|
||||
|
||||
impl<$T: GraphicsAdapter> KmsObjectKind<$T> {
|
||||
fn object_type(&self) -> u32 {
|
||||
match self {
|
||||
$(Self::$variant(_) => $type,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$(
|
||||
impl<$T: GraphicsAdapter> From<$data> for KmsObjectKind<$T> {
|
||||
fn from(value: $data) -> Self {
|
||||
Self::$variant(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $T: GraphicsAdapter> TryFrom<&'a KmsObjectKind<$T>> for &'a $data {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &'a KmsObjectKind<T>) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
KmsObjectKind::$variant(data) => Ok(data),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
define_object_kinds! { <T>
|
||||
Connector(Mutex<KmsConnector<T::Connector>>) = DRM_MODE_OBJECT_CONNECTOR,
|
||||
Encoder(KmsEncoder) = DRM_MODE_OBJECT_ENCODER,
|
||||
Property(KmsProperty) = DRM_MODE_OBJECT_PROPERTY,
|
||||
Blob(KmsBlob) = DRM_MODE_OBJECT_BLOB,
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ use std::ffi::c_char;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use drm_sys::{DRM_MODE_OBJECT_BLOB, DRM_MODE_OBJECT_PROPERTY, DRM_PROP_NAME_LEN};
|
||||
use drm_sys::DRM_PROP_NAME_LEN;
|
||||
use syscall::{Error, Result, EINVAL};
|
||||
|
||||
use crate::kms::objects::{KmsObject, KmsObjectId, KmsObjects};
|
||||
use crate::kms::objects::{KmsObjectId, KmsObjects};
|
||||
use crate::GraphicsAdapter;
|
||||
|
||||
impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
@@ -111,19 +111,7 @@ pub enum KmsPropertyKind {
|
||||
SignedRange(i64, i64),
|
||||
}
|
||||
|
||||
impl KmsObject for KmsProperty {
|
||||
fn object_type(&self) -> u32 {
|
||||
DRM_MODE_OBJECT_PROPERTY
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct KmsBlob {
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl KmsObject for KmsBlob {
|
||||
fn object_type(&self) -> u32 {
|
||||
DRM_MODE_OBJECT_BLOB
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub struct StandardProperties {
|
||||
}
|
||||
|
||||
pub trait GraphicsAdapter: Sized + Debug {
|
||||
type Connector: Debug + 'static;
|
||||
type Connector: Debug;
|
||||
|
||||
type Buffer: Buffer;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user