| 1 | |
| 2 | |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use cros_libva::{GenericValue, QuerySurfaceAttributesError, VAConfigAttrib, VASurfaceAttribType}; |
| 6 | |
| 7 | use crate::caps::{Entrypoint, Profile}; |
| 8 | use crate::display::Display; |
| 9 | use crate::error::{Error, Result, va_ok}; |
| 10 | |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | pub struct ConfigAttrib { |
| 14 | |
| 15 | pub type_: u32, |
| 16 | |
| 17 | pub value: u32, |
| 18 | } |
| 19 | |
| 20 | impl ConfigAttrib { |
| 21 | |
| 22 | pub fn rt_format(mask: u32) -> Self { |
| 23 | ConfigAttrib { |
| 24 | type_: cros_libva::VAConfigAttribType::VAConfigAttribRTFormat, |
| 25 | value: mask, |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 36 | pub struct SurfaceCaps { |
| 37 | |
| 38 | pub min_width: Option<u32>, |
| 39 | |
| 40 | pub max_width: Option<u32>, |
| 41 | |
| 42 | pub min_height: Option<u32>, |
| 43 | |
| 44 | pub max_height: Option<u32>, |
| 45 | |
| 46 | pub pixel_formats: Vec<u32>, |
| 47 | |
| 48 | pub memory_types: u32, |
| 49 | } |
| 50 | |
| 51 | impl SurfaceCaps { |
| 52 | |
| 53 | |
| 54 | pub fn allows(&self, width: u32, height: u32) -> bool { |
| 55 | self.min_width.is_none_or(|v| width >= v) |
| 56 | && self.max_width.is_none_or(|v| width <= v) |
| 57 | && self.min_height.is_none_or(|v| height >= v) |
| 58 | && self.max_height.is_none_or(|v| height <= v) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | pub fn supports_fourcc(&self, fourcc: u32) -> bool { |
| 63 | self.pixel_formats.contains(&fourcc) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub struct Config { |
| 69 | inner: cros_libva::Config, |
| 70 | profile: Profile, |
| 71 | entrypoint: Entrypoint, |
| 72 | } |
| 73 | |
| 74 | impl Config { |
| 75 | |
| 76 | pub fn new( |
| 77 | display: &Rc<Display>, |
| 78 | profile: Profile, |
| 79 | entrypoint: Entrypoint, |
| 80 | attribs: &[ConfigAttrib], |
| 81 | ) -> Result<Config> { |
| 82 | let attrs = attribs |
| 83 | .iter() |
| 84 | .map(|a| VAConfigAttrib { |
| 85 | type_: a.type_, |
| 86 | value: a.value, |
| 87 | }) |
| 88 | .collect(); |
| 89 | let inner = va_ok( |
| 90 | "vaCreateConfig", |
| 91 | display.create_config(attrs, profile.as_raw(), entrypoint.as_raw()), |
| 92 | )?; |
| 93 | Ok(Config { |
| 94 | inner, |
| 95 | profile, |
| 96 | entrypoint, |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | |
| 101 | pub fn profile(&self) -> Profile { |
| 102 | self.profile |
| 103 | } |
| 104 | |
| 105 | |
| 106 | pub fn entrypoint(&self) -> Entrypoint { |
| 107 | self.entrypoint |
| 108 | } |
| 109 | |
| 110 | |
| 111 | pub fn raw(&self) -> &cros_libva::Config { |
| 112 | &self.inner |
| 113 | } |
| 114 | |
| 115 | |
| 116 | pub fn raw_mut(&mut self) -> &mut cros_libva::Config { |
| 117 | &mut self.inner |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | pub fn caps_of(config: &mut Config) -> Result<SurfaceCaps> { |
| 126 | let first = |v: Vec<u32>| v.first().copied(); |
| 127 | Ok(SurfaceCaps { |
| 128 | min_width: first(attribute_ints( |
| 129 | config, |
| 130 | VASurfaceAttribType::VASurfaceAttribMinWidth, |
| 131 | )?), |
| 132 | max_width: first(attribute_ints( |
| 133 | config, |
| 134 | VASurfaceAttribType::VASurfaceAttribMaxWidth, |
| 135 | )?), |
| 136 | min_height: first(attribute_ints( |
| 137 | config, |
| 138 | VASurfaceAttribType::VASurfaceAttribMinHeight, |
| 139 | )?), |
| 140 | max_height: first(attribute_ints( |
| 141 | config, |
| 142 | VASurfaceAttribType::VASurfaceAttribMaxHeight, |
| 143 | )?), |
| 144 | pixel_formats: attribute_ints(config, VASurfaceAttribType::VASurfaceAttribPixelFormat)?, |
| 145 | memory_types: attribute_ints(config, VASurfaceAttribType::VASurfaceAttribMemoryType)? |
| 146 | .into_iter() |
| 147 | .fold(0, |acc, m| acc | m), |
| 148 | }) |
| 149 | } |
| 150 | |
| 151 | fn attribute_ints(config: &mut Config, ty: VASurfaceAttribType::Type) -> Result<Vec<u32>> { |
| 152 | let values = config |
| 153 | .raw_mut() |
| 154 | .query_surface_attributes_by_type(ty) |
| 155 | .map_err(|e| match e { |
| 156 | QuerySurfaceAttributesError::VaError(v) => Error::va("vaQuerySurfaceAttributes", &v), |
| 157 | other => Error::Protocol(other.to_string()), |
| 158 | })?; |
| 159 | Ok(values |
| 160 | .into_iter() |
| 161 | .filter_map(|v| match v { |
| 162 | GenericValue::Integer(i) => Some(i as u32), |
| 163 | _ => None, |
| 164 | }) |
| 165 | .collect()) |
| 166 | } |