| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::ffi::CStr; |
| 7 | use std::fmt; |
| 8 | |
| 9 | use crate::sys; |
| 10 | |
| 11 | |
| 12 | pub type Result<T> = std::result::Result<T, Error>; |
| 13 | |
| 14 | |
| 15 | #[derive(Debug)] |
| 16 | #[non_exhaustive] |
| 17 | pub enum Error { |
| 18 | |
| 19 | Va { |
| 20 | |
| 21 | op: &'static str, |
| 22 | |
| 23 | status: sys::VAStatus, |
| 24 | |
| 25 | message: String, |
| 26 | }, |
| 27 | |
| 28 | Drm { |
| 29 | |
| 30 | path: String, |
| 31 | |
| 32 | source: std::io::Error, |
| 33 | }, |
| 34 | |
| 35 | NoDevice, |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | Version { |
| 42 | |
| 43 | major: i32, |
| 44 | |
| 45 | minor: i32, |
| 46 | }, |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | Protocol(String), |
| 52 | } |
| 53 | |
| 54 | impl Error { |
| 55 | |
| 56 | pub(crate) fn va(op: &'static str, status: sys::VAStatus) -> Self { |
| 57 | Error::Va { |
| 58 | op, |
| 59 | status, |
| 60 | message: status_str(status), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | pub fn status(&self) -> Option<sys::VAStatus> { |
| 66 | match self { |
| 67 | Error::Va { status, .. } => Some(*status), |
| 68 | _ => None, |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | pub(crate) fn status_str(status: sys::VAStatus) -> String { |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | let ptr = unsafe { sys::vaErrorStr(status) }; |
| 82 | if ptr.is_null() { |
| 83 | return format!("VAStatus 0x{status:08x}"); |
| 84 | } |
| 85 | |
| 86 | unsafe { CStr::from_ptr(ptr) } |
| 87 | .to_string_lossy() |
| 88 | .into_owned() |
| 89 | } |
| 90 | |
| 91 | |
| 92 | pub(crate) fn check(op: &'static str, status: sys::VAStatus) -> Result<()> { |
| 93 | if status == sys::VA_STATUS_SUCCESS { |
| 94 | Ok(()) |
| 95 | } else { |
| 96 | Err(Error::va(op, status)) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl fmt::Display for Error { |
| 101 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 102 | match self { |
| 103 | Error::Va { |
| 104 | op, |
| 105 | status, |
| 106 | message, |
| 107 | } => write!(f, "{op} failed: {message} (VAStatus 0x{status:08x})"), |
| 108 | Error::Drm { path, source } => write!(f, "opening DRM node {path}: {source}"), |
| 109 | Error::NoDevice => write!(f, "no usable /dev/dri/renderD* VA display"), |
| 110 | Error::Version { major, minor } => write!( |
| 111 | f, |
| 112 | "libva {major}.{minor} is older than the {}.{} ABI this build transcribes", |
| 113 | crate::MIN_VA_MAJOR, |
| 114 | crate::MIN_VA_MINOR |
| 115 | ), |
| 116 | Error::Protocol(what) => write!(f, "libva returned an uninterpretable value: {what}"), |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl std::error::Error for Error { |
| 122 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 123 | match self { |
| 124 | Error::Drm { source, .. } => Some(source), |
| 125 | _ => None, |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #[cfg(test)] |
| 131 | mod tests { |
| 132 | use super::*; |
| 133 | |
| 134 | #[test] |
| 135 | fn status_strings_come_from_libva() { |
| 136 | assert_eq!(status_str(sys::VA_STATUS_SUCCESS), "success (no error)"); |
| 137 | |
| 138 | assert!(!status_str(0x7fff_0000).is_empty()); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn check_maps_success_and_failure() { |
| 143 | assert!(check("vaFake", sys::VA_STATUS_SUCCESS).is_ok()); |
| 144 | let err = check("vaFake", sys::VA_STATUS_ERROR_UNIMPLEMENTED).unwrap_err(); |
| 145 | assert_eq!(err.status(), Some(sys::VA_STATUS_ERROR_UNIMPLEMENTED)); |
| 146 | assert!(err.to_string().contains("vaFake")); |
| 147 | } |
| 148 | } |