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.
11 use crate::display::Display;
12 use crate::generic_value::GenericValue;
13 use crate::status::Status;
15 /// A configuration for a given [`Display`].
18 id: bindings::VAConfigID,
22 /// Creates a Config by wrapping around the `vaCreateConfig` call. This is just a helper for
23 /// [`Display::create_config`].
26 mut attrs: Vec<bindings::VAConfigAttrib>,
27 profile: bindings::VAProfile::Type,
28 entrypoint: bindings::VAEntrypoint::Type,
30 let mut config_id = 0u32;
32 // Safe because `self` represents a valid `VADisplay`.
34 // The `attrs` vector is also properly initialized and its actual size is passed to
35 // `vaCreateConfig`, so it is impossible to write past the end of its storage by mistake.
37 bindings::vaCreateConfig(
54 /// Returns the ID of this config.
55 pub(crate) fn id(&self) -> bindings::VAConfigID {
59 // Queries surface attributes for this config.
61 // This function queries for all supported attributes for this configuration. In particular, if
62 // the underlying hardware supports the creation of VA surfaces in various formats, then this
63 // function will enumerate all pixel formats that are supported.
64 fn query_surface_attributes(&mut self) -> Result<Vec<bindings::VASurfaceAttrib>> {
65 // Safe because `self` represents a valid VAConfig. We first query how
66 // much space is needed by the C API by passing in NULL in the first
67 // call to `vaQuerySurfaceAttributes`.
68 let attrs_len: std::os::raw::c_uint = 0;
70 bindings::vaQuerySurfaceAttributes(
71 self.display.handle(),
74 &attrs_len as *const _ as *mut std::os::raw::c_uint,
79 let mut attrs = Vec::with_capacity(attrs_len as usize);
80 // Safe because we allocate a vector with the required capacity as
81 // returned by the initial call to vaQuerySurfaceAttributes. We then
82 // pass a valid pointer to it.
84 bindings::vaQuerySurfaceAttributes(
85 self.display.handle(),
88 &attrs_len as *const _ as *mut std::os::raw::c_uint,
93 // Safe because vaQuerySurfaceAttributes will have written to
94 // exactly attrs_len entries in the vector.
96 attrs.set_len(attrs_len as usize);
102 /// Query the surface attributes of type `attr_type`. The attribute may or may not be defined by
104 pub fn query_surface_attributes_by_type(
106 attr_type: bindings::VASurfaceAttribType::Type,
107 ) -> Result<Vec<GenericValue>> {
108 let surface_attributes = self.query_surface_attributes()?;
112 .filter(|attr| attr.type_ == attr_type)
113 .map(|attrib| GenericValue::try_from(attrib.value))
118 impl Drop for Config {
120 // Safe because `self` represents a valid Config.
122 Status(unsafe { bindings::vaDestroyConfig(self.display.handle(), self.id) }).check();
124 error!("vaDestroyConfig failed: {}", status.unwrap_err());