| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::ffi::CStr; |
| 6 | use std::fs::File; |
| 7 | use std::io; |
| 8 | use std::os::unix::io::AsRawFd; |
| 9 | use std::path::Path; |
| 10 | use std::path::PathBuf; |
| 11 | use std::rc::Rc; |
| 12 | |
| 13 | use thiserror::Error; |
| 14 | |
| 15 | use crate::bindings; |
| 16 | use crate::config::Config; |
| 17 | use crate::context::Context; |
| 18 | use crate::surface::Surface; |
| 19 | use crate::va_check; |
| 20 | use crate::SurfaceMemoryDescriptor; |
| 21 | use crate::UsageHint; |
| 22 | use crate::VaError; |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | pub struct DrmDeviceIterator { |
| 29 | cur_idx: usize, |
| 30 | } |
| 31 | |
| 32 | const DRM_NODE_DEFAULT_PREFIX: &str = "/dev/dri/renderD"; |
| 33 | const DRM_NUM_NODES: usize = 64; |
| 34 | const DRM_RENDER_NODE_START: usize = 128; |
| 35 | |
| 36 | impl Default for DrmDeviceIterator { |
| 37 | fn default() -> Self { |
| 38 | Self { |
| 39 | cur_idx: DRM_RENDER_NODE_START, |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl Iterator for DrmDeviceIterator { |
| 45 | type Item = PathBuf; |
| 46 | |
| 47 | fn next(&mut self) -> Option<Self::Item> { |
| 48 | match self.cur_idx { |
| 49 | idx if idx >= DRM_RENDER_NODE_START + DRM_NUM_NODES => None, |
| 50 | idx => { |
| 51 | let path = PathBuf::from(format!("{}{}", DRM_NODE_DEFAULT_PREFIX, idx)); |
| 52 | if !path.exists() { |
| 53 | None |
| 54 | } else { |
| 55 | self.cur_idx += 1; |
| 56 | Some(path) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | pub struct Display { |
| 73 | |
| 74 | handle: bindings::VADisplay, |
| 75 | |
| 76 | #[allow(dead_code)] |
| 77 | drm_file: File, |
| 78 | } |
| 79 | |
| 80 | |
| 81 | #[derive(Debug, Error)] |
| 82 | pub enum OpenDrmDisplayError { |
| 83 | #[error("cannot open DRM device: {0}")] |
| 84 | DeviceOpen(io::Error), |
| 85 | #[error("vaGetDisplayDRM returned NULL")] |
| 86 | VaGetDisplayDrm, |
| 87 | #[error("call to vaInitialize failed: {0}")] |
| 88 | VaInitialize(VaError), |
| 89 | } |
| 90 | |
| 91 | impl Display { |
| 92 | |
| 93 | |
| 94 | |
| 95 | pub fn open_drm_display<P: AsRef<Path>>(path: P) -> Result<Rc<Self>, OpenDrmDisplayError> { |
| 96 | let file = std::fs::File::options() |
| 97 | .read(true) |
| 98 | .write(true) |
| 99 | .open(path.as_ref()) |
| 100 | .map_err(OpenDrmDisplayError::DeviceOpen)?; |
| 101 | |
| 102 | |
| 103 | |
| 104 | let display = unsafe { bindings::vaGetDisplayDRM(file.as_raw_fd()) }; |
| 105 | if display.is_null() { |
| 106 | return Err(OpenDrmDisplayError::VaGetDisplayDrm); |
| 107 | } |
| 108 | |
| 109 | let mut major = 0i32; |
| 110 | let mut minor = 0i32; |
| 111 | |
| 112 | |
| 113 | va_check(unsafe { bindings::vaInitialize(display, &mut major, &mut minor) }) |
| 114 | .map(|()| { |
| 115 | Rc::new(Self { |
| 116 | handle: display, |
| 117 | drm_file: file, |
| 118 | }) |
| 119 | }) |
| 120 | .map_err(OpenDrmDisplayError::VaInitialize) |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | pub fn open() -> Option<Rc<Self>> { |
| 128 | let devices = DrmDeviceIterator::default(); |
| 129 | |
| 130 | |
| 131 | for device in devices { |
| 132 | if let Ok(display) = Self::open_drm_display(device) { |
| 133 | return Some(display); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | None |
| 138 | } |
| 139 | |
| 140 | |
| 141 | pub(crate) fn handle(&self) -> bindings::VADisplay { |
| 142 | self.handle |
| 143 | } |
| 144 | |
| 145 | |
| 146 | pub fn query_config_profiles(&self) -> Result<Vec<bindings::VAProfile::Type>, VaError> { |
| 147 | |
| 148 | let mut max_num_profiles = unsafe { bindings::vaMaxNumProfiles(self.handle) }; |
| 149 | let mut profiles = Vec::with_capacity(max_num_profiles as usize); |
| 150 | |
| 151 | |
| 152 | |
| 153 | va_check(unsafe { |
| 154 | bindings::vaQueryConfigProfiles( |
| 155 | self.handle, |
| 156 | profiles.as_mut_ptr(), |
| 157 | &mut max_num_profiles, |
| 158 | ) |
| 159 | })?; |
| 160 | |
| 161 | |
| 162 | |
| 163 | unsafe { |
| 164 | profiles.set_len(max_num_profiles as usize); |
| 165 | }; |
| 166 | |
| 167 | Ok(profiles) |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | pub fn query_vendor_string(&self) -> std::result::Result<String, &'static str> { |
| 177 | |
| 178 | let vendor_string = unsafe { bindings::vaQueryVendorString(self.handle) }; |
| 179 | |
| 180 | if vendor_string.is_null() { |
| 181 | return Err("vaQueryVendorString() returned NULL"); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | Ok(unsafe { CStr::from_ptr(vendor_string) } |
| 186 | .to_string_lossy() |
| 187 | .to_string()) |
| 188 | } |
| 189 | |
| 190 | |
| 191 | pub fn query_config_entrypoints( |
| 192 | &self, |
| 193 | profile: bindings::VAProfile::Type, |
| 194 | ) -> Result<Vec<bindings::VAEntrypoint::Type>, VaError> { |
| 195 | |
| 196 | let mut max_num_entrypoints = unsafe { bindings::vaMaxNumEntrypoints(self.handle) }; |
| 197 | let mut entrypoints = Vec::with_capacity(max_num_entrypoints as usize); |
| 198 | |
| 199 | |
| 200 | |
| 201 | va_check(unsafe { |
| 202 | bindings::vaQueryConfigEntrypoints( |
| 203 | self.handle, |
| 204 | profile, |
| 205 | entrypoints.as_mut_ptr(), |
| 206 | &mut max_num_entrypoints, |
| 207 | ) |
| 208 | })?; |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | unsafe { |
| 214 | entrypoints.set_len(max_num_entrypoints as usize); |
| 215 | } |
| 216 | |
| 217 | Ok(entrypoints) |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | pub fn get_config_attributes( |
| 226 | &self, |
| 227 | profile: bindings::VAProfile::Type, |
| 228 | entrypoint: bindings::VAEntrypoint::Type, |
| 229 | attributes: &mut [bindings::VAConfigAttrib], |
| 230 | ) -> Result<(), VaError> { |
| 231 | |
| 232 | |
| 233 | va_check(unsafe { |
| 234 | bindings::vaGetConfigAttributes( |
| 235 | self.handle, |
| 236 | profile, |
| 237 | entrypoint, |
| 238 | attributes.as_mut_ptr(), |
| 239 | attributes.len() as i32, |
| 240 | ) |
| 241 | }) |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | pub fn create_surfaces<D: SurfaceMemoryDescriptor>( |
| 266 | self: &Rc<Self>, |
| 267 | rt_format: u32, |
| 268 | va_fourcc: Option<u32>, |
| 269 | width: u32, |
| 270 | height: u32, |
| 271 | usage_hint: Option<UsageHint>, |
| 272 | descriptors: Vec<D>, |
| 273 | ) -> Result<Vec<Surface<D>>, VaError> { |
| 274 | Surface::new( |
| 275 | Rc::clone(self), |
| 276 | rt_format, |
| 277 | va_fourcc, |
| 278 | width, |
| 279 | height, |
| 280 | usage_hint, |
| 281 | descriptors, |
| 282 | ) |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | pub fn create_context<D: SurfaceMemoryDescriptor>( |
| 295 | self: &Rc<Self>, |
| 296 | config: &Config, |
| 297 | coded_width: u32, |
| 298 | coded_height: u32, |
| 299 | surfaces: Option<&Vec<Surface<D>>>, |
| 300 | progressive: bool, |
| 301 | ) -> Result<Rc<Context>, VaError> { |
| 302 | Context::new( |
| 303 | Rc::clone(self), |
| 304 | config, |
| 305 | coded_width, |
| 306 | coded_height, |
| 307 | surfaces, |
| 308 | progressive, |
| 309 | ) |
| 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | pub fn create_config( |
| 319 | self: &Rc<Self>, |
| 320 | attrs: Vec<bindings::VAConfigAttrib>, |
| 321 | profile: bindings::VAProfile::Type, |
| 322 | entrypoint: bindings::VAEntrypoint::Type, |
| 323 | ) -> Result<Config, VaError> { |
| 324 | Config::new(Rc::clone(self), attrs, profile, entrypoint) |
| 325 | } |
| 326 | |
| 327 | |
| 328 | pub fn query_image_formats(&self) -> Result<Vec<bindings::VAImageFormat>, VaError> { |
| 329 | |
| 330 | let mut num_image_formats = unsafe { bindings::vaMaxNumImageFormats(self.handle) }; |
| 331 | let mut image_formats = Vec::with_capacity(num_image_formats as usize); |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | va_check(unsafe { |
| 337 | bindings::vaQueryImageFormats( |
| 338 | self.handle, |
| 339 | image_formats.as_mut_ptr(), |
| 340 | &mut num_image_formats, |
| 341 | ) |
| 342 | })?; |
| 343 | |
| 344 | |
| 345 | |
| 346 | unsafe { |
| 347 | image_formats.set_len(num_image_formats as usize); |
| 348 | } |
| 349 | |
| 350 | Ok(image_formats) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | impl Drop for Display { |
| 355 | fn drop(&mut self) { |
| 356 | |
| 357 | unsafe { |
| 358 | bindings::vaTerminate(self.handle); |
| 359 | |
| 360 | } |
| 361 | } |
| 362 | } |