| 1 | |
| 2 | |
| 3 | |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use crate::caps::{Entrypoint, Profile}; |
| 7 | use crate::display::Display; |
| 8 | use crate::error::{Error, Result, check}; |
| 9 | use crate::sys; |
| 10 | |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | pub struct ConfigAttrib { |
| 14 | |
| 15 | pub type_: sys::VAConfigAttribType, |
| 16 | |
| 17 | pub value: u32, |
| 18 | } |
| 19 | |
| 20 | impl ConfigAttrib { |
| 21 | |
| 22 | pub fn rt_format(mask: u32) -> Self { |
| 23 | ConfigAttrib { |
| 24 | type_: sys::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 | |
| 69 | |
| 70 | pub struct Config { |
| 71 | display: Arc<Display>, |
| 72 | id: sys::VAConfigID, |
| 73 | profile: Profile, |
| 74 | entrypoint: Entrypoint, |
| 75 | } |
| 76 | |
| 77 | impl Config { |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | pub fn new( |
| 84 | display: &Arc<Display>, |
| 85 | profile: Profile, |
| 86 | entrypoint: Entrypoint, |
| 87 | attribs: &[ConfigAttrib], |
| 88 | ) -> Result<Arc<Config>> { |
| 89 | let mut raw: Vec<sys::VAConfigAttrib> = attribs |
| 90 | .iter() |
| 91 | .map(|a| sys::VAConfigAttrib { |
| 92 | type_: a.type_, |
| 93 | value: a.value, |
| 94 | }) |
| 95 | .collect(); |
| 96 | let mut id: sys::VAConfigID = sys::VA_INVALID_ID; |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | let status = unsafe { |
| 102 | sys::vaCreateConfig( |
| 103 | display.handle(), |
| 104 | profile.as_raw(), |
| 105 | entrypoint.as_raw(), |
| 106 | raw.as_mut_ptr(), |
| 107 | raw.len() as i32, |
| 108 | &mut id, |
| 109 | ) |
| 110 | }; |
| 111 | check("vaCreateConfig", status)?; |
| 112 | if id == sys::VA_INVALID_ID { |
| 113 | return Err(Error::Protocol( |
| 114 | "vaCreateConfig succeeded but returned VA_INVALID_ID".to_string(), |
| 115 | )); |
| 116 | } |
| 117 | Ok(Arc::new(Config { |
| 118 | display: Arc::clone(display), |
| 119 | id, |
| 120 | profile, |
| 121 | entrypoint, |
| 122 | })) |
| 123 | } |
| 124 | |
| 125 | |
| 126 | pub fn surface_caps(&self) -> Result<SurfaceCaps> { |
| 127 | |
| 128 | |
| 129 | let mut count: u32 = 0; |
| 130 | |
| 131 | |
| 132 | let status = unsafe { |
| 133 | sys::vaQuerySurfaceAttributes( |
| 134 | self.display.handle(), |
| 135 | self.id, |
| 136 | std::ptr::null_mut(), |
| 137 | &mut count, |
| 138 | ) |
| 139 | }; |
| 140 | check("vaQuerySurfaceAttributes", status)?; |
| 141 | |
| 142 | let mut attrs = vec![ |
| 143 | sys::VASurfaceAttrib { |
| 144 | type_: sys::VASurfaceAttribNone, |
| 145 | flags: 0, |
| 146 | value: sys::VAGenericValue { |
| 147 | type_: sys::VAGenericValueTypeInteger, |
| 148 | value: sys::VAGenericValueUnion { i: 0 }, |
| 149 | }, |
| 150 | }; |
| 151 | count as usize |
| 152 | ]; |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | let status = unsafe { |
| 158 | sys::vaQuerySurfaceAttributes( |
| 159 | self.display.handle(), |
| 160 | self.id, |
| 161 | attrs.as_mut_ptr(), |
| 162 | &mut count, |
| 163 | ) |
| 164 | }; |
| 165 | check("vaQuerySurfaceAttributes", status)?; |
| 166 | attrs.truncate(count as usize); |
| 167 | |
| 168 | let mut caps = SurfaceCaps::default(); |
| 169 | for attr in &attrs { |
| 170 | if attr.flags & sys::VA_SURFACE_ATTRIB_GETTABLE == 0 { |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | if attr.value.type_ != sys::VAGenericValueTypeInteger { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | let value = unsafe { attr.value.value.i }; |
| 183 | match attr.type_ { |
| 184 | sys::VASurfaceAttribPixelFormat => caps.pixel_formats.push(value as u32), |
| 185 | sys::VASurfaceAttribMinWidth => caps.min_width = Some(value.max(0) as u32), |
| 186 | sys::VASurfaceAttribMaxWidth => caps.max_width = Some(value.max(0) as u32), |
| 187 | sys::VASurfaceAttribMinHeight => caps.min_height = Some(value.max(0) as u32), |
| 188 | sys::VASurfaceAttribMaxHeight => caps.max_height = Some(value.max(0) as u32), |
| 189 | sys::VASurfaceAttribMemoryType => caps.memory_types = value as u32, |
| 190 | _ => {} |
| 191 | } |
| 192 | } |
| 193 | Ok(caps) |
| 194 | } |
| 195 | |
| 196 | |
| 197 | pub fn profile(&self) -> Profile { |
| 198 | self.profile |
| 199 | } |
| 200 | |
| 201 | |
| 202 | pub fn entrypoint(&self) -> Entrypoint { |
| 203 | self.entrypoint |
| 204 | } |
| 205 | |
| 206 | |
| 207 | pub fn display(&self) -> &Arc<Display> { |
| 208 | &self.display |
| 209 | } |
| 210 | |
| 211 | pub(crate) fn id(&self) -> sys::VAConfigID { |
| 212 | self.id |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | impl std::fmt::Debug for Config { |
| 217 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 218 | f.debug_struct("Config") |
| 219 | .field("id", &self.id) |
| 220 | .field("profile", &self.profile) |
| 221 | .field("entrypoint", &self.entrypoint) |
| 222 | .finish() |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl Drop for Config { |
| 227 | fn drop(&mut self) { |
| 228 | |
| 229 | |
| 230 | |
| 231 | unsafe { sys::vaDestroyConfig(self.display.handle(), self.id) }; |
| 232 | } |
| 233 | } |