| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::fmt; |
| 11 | |
| 12 | |
| 13 | pub type Result<T> = std::result::Result<T, Error>; |
| 14 | |
| 15 | |
| 16 | #[derive(Debug)] |
| 17 | #[non_exhaustive] |
| 18 | pub enum Error { |
| 19 | |
| 20 | #[cfg(target_os = "linux")] |
| 21 | Va(ec_va::Error), |
| 22 | |
| 23 | Stream(ec_core::Error), |
| 24 | |
| 25 | Unsupported { |
| 26 | |
| 27 | what: String, |
| 28 | |
| 29 | why: String, |
| 30 | }, |
| 31 | |
| 32 | |
| 33 | Config(String), |
| 34 | |
| 35 | Vk(String), |
| 36 | |
| 37 | |
| 38 | |
| 39 | HwReset(String), |
| 40 | } |
| 41 | |
| 42 | impl Error { |
| 43 | |
| 44 | pub fn unsupported(what: impl Into<String>, why: impl Into<String>) -> Error { |
| 45 | Error::Unsupported { |
| 46 | what: what.into(), |
| 47 | why: why.into(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | pub fn config(what: impl Into<String>) -> Error { |
| 53 | Error::Config(what.into()) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub fn poisoned(what: impl Into<String>) -> Error { |
| 58 | Error::Config(format!("hardware decoder poisoned: {}", what.into())) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | pub fn hw_reset(what: impl Into<String>) -> Error { |
| 63 | Error::HwReset(what.into()) |
| 64 | } |
| 65 | |
| 66 | |
| 67 | pub fn is_hw_reset(&self) -> bool { |
| 68 | matches!(self, Error::HwReset(_)) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub fn va_status(&self) -> Option<i32> { |
| 73 | match self { |
| 74 | #[cfg(target_os = "linux")] |
| 75 | Error::Va(e) => e.status(), |
| 76 | _ => None, |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg(target_os = "linux")] |
| 82 | impl From<ec_va::Error> for Error { |
| 83 | fn from(e: ec_va::Error) -> Error { |
| 84 | Error::Va(e) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl From<ec_vk::Error> for Error { |
| 89 | fn from(e: ec_vk::Error) -> Error { |
| 90 | match e { |
| 91 | ec_vk::Error::DeviceLost(call) => { |
| 92 | crate::reset::report_gpu_reset(); |
| 93 | Error::hw_reset(format!("Vulkan device lost in {call}")) |
| 94 | } |
| 95 | ec_vk::Error::Unsupported(why) => Error::unsupported("Vulkan Video", why), |
| 96 | other => Error::Vk(other.to_string()), |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl From<ec_core::Error> for Error { |
| 102 | fn from(e: ec_core::Error) -> Error { |
| 103 | Error::Stream(e) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl fmt::Display for Error { |
| 108 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 109 | match self { |
| 110 | #[cfg(target_os = "linux")] |
| 111 | Error::Va(e) => write!(f, "VA-API: {e}"), |
| 112 | Error::Stream(e) => write!(f, "bitstream: {e}"), |
| 113 | Error::Unsupported { what, why } => write!(f, "{what} is not supported: {why}"), |
| 114 | Error::Config(what) => write!(f, "{what}"), |
| 115 | Error::Vk(what) => write!(f, "Vulkan Video: {what}"), |
| 116 | Error::HwReset(what) => write!(f, "GPU reset: {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 | #[cfg(target_os = "linux")] |
| 125 | Error::Va(e) => Some(e), |
| 126 | Error::Stream(e) => Some(e), |
| 127 | _ => None, |
| 128 | } |
| 129 | } |
| 130 | } |