1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 use crate::display::Display;
11 use crate::generic_value::GenericValue;
12 use crate::status::Status;
14 /// A configuration for a given [`Display`].
17 id: bindings::VAConfigID,
21 /// Creates a Config by wrapping around the `vaCreateConfig` call. This is just a helper for
22 /// [`Display::create_config`].
25 mut attrs: Vec<bindings::VAConfigAttrib>,
26 profile: bindings::VAProfile::Type,
27 entrypoint: bindings::VAEntrypoint::Type,
29 let mut config_id = 0u32;
31 // Safe because `self` represents a valid `VADisplay`.
33 // The `attrs` vector is also properly initialized and its actual size is passed to
34 // `vaCreateConfig`, so it is impossible to write past the end of its storage by mistake.
36 bindings::vaCreateConfig(
53 /// Returns the ID of this config.
54 pub(crate) fn id(&self) -> bindings::VAConfigID {
58 // Queries surface attributes for this config.
60 // This function queries for all supported attributes for this configuration. In particular, if
61 // the underlying hardware supports the creation of VA surfaces in various formats, then this
62 // function will enumerate all pixel formats that are supported.
63 fn query_surface_attributes(&mut self) -> Result<Vec<bindings::VASurfaceAttrib>> {
64 // Safe because `self` represents a valid VAConfig. We first query how
65 // much space is needed by the C API by passing in NULL in the first
66 // call to `vaQuerySurfaceAttributes`.
67 let attrs_len: std::os::raw::c_uint = 0;
69 bindings::vaQuerySurfaceAttributes(
70 self.display.handle(),
73 &attrs_len as *const _ as *mut std::os::raw::c_uint,
78 let mut attrs = Vec::with_capacity(attrs_len as usize);
79 // Safe because we allocate a vector with the required capacity as
80 // returned by the initial call to vaQuerySurfaceAttributes. We then
81 // pass a valid pointer to it.
83 bindings::vaQuerySurfaceAttributes(
84 self.display.handle(),
87 &attrs_len as *const _ as *mut std::os::raw::c_uint,
92 // Safe because vaQuerySurfaceAttributes will have written to
93 // exactly attrs_len entries in the vector.
95 attrs.set_len(attrs_len as usize);
101 /// Query the surface attributes of type `attr_type`. The attribute may or may not be defined by
103 pub fn query_surface_attributes_by_type(
105 attr_type: bindings::VASurfaceAttribType::Type,
106 ) -> Result<Vec<GenericValue>> {
107 let surface_attributes = self.query_surface_attributes()?;
111 .filter(|attr| attr.type_ == attr_type)
112 .map(|attrib| GenericValue::try_from(attrib.value))
117 impl Drop for Config {
119 // Safe because `self` represents a valid Config.
121 Status(unsafe { bindings::vaDestroyConfig(self.display.handle(), self.id) }).check();
123 println!("vaDestroyConfig failed: {}", status.unwrap_err());