forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

config.rs166 lines, 5.3 KiBHistoryRawDownload
1//! VA configs: one `(profile, entrypoint)` pair plus driver attributes.
2
3use std::rc::Rc;
4
5use cros_libva::{GenericValue, QuerySurfaceAttributesError, VAConfigAttrib, VASurfaceAttribType};
6
7use crate::caps::{Entrypoint, Profile};
8use crate::display::Display;
9use crate::error::{Error, Result, va_ok};
10
11/// One `VAConfigAttrib`: a type and its value.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct ConfigAttrib {
14 /// The `VAConfigAttribType` this value answers.
15 pub type_: u32,
16 /// The value, interpreted per attribute type.
17 pub value: u32,
18}
19
20impl ConfigAttrib {
21 /// `VAConfigAttribRTFormat` with the given `VA_RT_FORMAT_*` mask.
22 pub fn rt_format(mask: u32) -> Self {
23 ConfigAttrib {
24 type_: cros_libva::VAConfigAttribType::VAConfigAttribRTFormat,
25 value: mask,
26 }
27 }
28}
29
30/// Surface constraints reported for a config.
31///
32/// The minimum sizes are the reason this type exists: radeonsi rejects
33/// surfaces below 64x64, and probing with a hardcoded 16x16 is how the
34/// incumbent stack used to panic on this GPU.
35#[derive(Debug, Clone, Default, PartialEq, Eq)]
36pub struct SurfaceCaps {
37 /// Smallest legal surface width, if reported.
38 pub min_width: Option<u32>,
39 /// Largest legal surface width, if reported.
40 pub max_width: Option<u32>,
41 /// Smallest legal surface height, if reported.
42 pub min_height: Option<u32>,
43 /// Largest legal surface height, if reported.
44 pub max_height: Option<u32>,
45 /// Pixel formats (FourCCs) the driver will allocate for this config.
46 pub pixel_formats: Vec<u32>,
47 /// `VASurfaceAttribMemoryType` bitmask, e.g. DRM PRIME 2 support.
48 pub memory_types: u32,
49}
50
51impl SurfaceCaps {
52 /// True if `width` x `height` is within the reported bounds. Unreported
53 /// bounds are treated as "no constraint".
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 /// True if the driver will allocate this FourCC for the config.
62 pub fn supports_fourcc(&self, fourcc: u32) -> bool {
63 self.pixel_formats.contains(&fourcc)
64 }
65}
66
67/// A live `VAConfigID`, remembering the profile and entrypoint it was made for.
68pub struct Config {
69 inner: cros_libva::Config,
70 profile: Profile,
71 entrypoint: Entrypoint,
72}
73
74impl Config {
75 /// `vaCreateConfig` for `profile` and `entrypoint` with the given attributes.
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 /// The profile this config decodes or encodes.
101 pub fn profile(&self) -> Profile {
102 self.profile
103 }
104
105 /// The entrypoint this config was created for.
106 pub fn entrypoint(&self) -> Entrypoint {
107 self.entrypoint
108 }
109
110 /// The cros-libva handle, for the calls that take one.
111 pub fn raw(&self) -> &cros_libva::Config {
112 &self.inner
113 }
114
115 /// Mutable cros-libva handle; surface attribute queries need one.
116 pub fn raw_mut(&mut self) -> &mut cros_libva::Config {
117 &mut self.inner
118 }
119}
120
121/// Query the surface constraints of `config` (`vaQuerySurfaceAttributes`).
122///
123/// A config's surfaces are described by attributes the driver may or may not
124/// report; each one that is missing stays `None`.
125pub 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
151fn 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}