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.
8 use crate::display::Display;
12 /// An owned VA surface that is tied to the lifetime of a particular VADisplay
15 id: bindings::VASurfaceID,
21 /// Create `Surfaces` by wrapping around a `vaCreateSurfaces` call. This is just a helper for
22 /// [`Display::create_surfaces`].
26 va_fourcc: Option<u32>,
29 usage_hint: Option<UsageHint>,
31 ) -> VAResult<Vec<Self>> {
32 let mut attrs = vec![];
34 if let Some(usage_hint) = usage_hint {
35 let attr = bindings::VASurfaceAttrib {
36 type_: bindings::VASurfaceAttribType::VASurfaceAttribUsageHint,
37 flags: bindings::constants::VA_SURFACE_ATTRIB_SETTABLE,
38 value: bindings::VAGenericValue {
39 type_: bindings::VAGenericValueType::VAGenericValueTypeInteger,
40 value: bindings::_VAGenericValue__bindgen_ty_1 {
41 i: usage_hint.bits() as i32,
49 if let Some(fourcc) = va_fourcc {
50 let attr = bindings::VASurfaceAttrib {
51 type_: bindings::VASurfaceAttribType::VASurfaceAttribPixelFormat,
52 flags: bindings::constants::VA_DISPLAY_ATTRIB_SETTABLE,
53 value: bindings::VAGenericValue {
54 type_: bindings::VAGenericValueType::VAGenericValueTypeInteger,
55 value: bindings::_VAGenericValue__bindgen_ty_1 { i: fourcc as i32 },
62 let mut surfaces = Vec::with_capacity(num_surfaces as usize);
64 // Safe because `self` represents a valid VADisplay. The `surface` and `attrs` vectors are
65 // properly initialized and valid sizes are passed to the C function, so it is impossible to
66 // write past the end of their storage by mistake.
68 bindings::vaCreateSurfaces(
73 surfaces.as_mut_ptr(),
81 // Safe because the C function will have written to exactly `num_surfaces` entries, which is
82 // known to be within the vector's capacity.
84 surfaces.set_len(num_surfaces as usize);
87 let va_surfaces = surfaces
90 display: Rc::clone(&display),
100 /// Blocks until all pending operations on the render target have been completed. Upon return it
101 /// is safe to use the render target for a different picture.
102 pub fn sync(&self) -> VAResult<()> {
103 // Safe because `self` represents a valid VASurface.
104 (unsafe { bindings::vaSyncSurface(self.display.handle(), self.id) }).check()
107 /// Convenience function to return a VASurfaceID vector. Useful to interface with the C API
108 /// where a surface array might be needed.
109 pub fn as_id_vec(surfaces: &[Self]) -> Vec<bindings::VASurfaceID> {
110 surfaces.iter().map(|surface| surface.id).collect()
113 /// Wrapper over `vaQuerySurfaceStatus` to find out any pending ops on the render target.
114 pub fn query_status(&self) -> VAResult<bindings::VASurfaceStatus::Type> {
115 let mut status: bindings::VASurfaceStatus::Type = 0;
116 // Safe because `self` represents a valid VASurface.
118 bindings::vaQuerySurfaceStatus(self.display.handle(), self.id, &mut status)
124 /// Returns the ID of this surface.
125 pub fn id(&self) -> bindings::VASurfaceID {
129 /// Returns the dimensions of this surface.
130 pub fn size(&self) -> (u32, u32) {
131 (self.width, self.height)
135 impl Drop for Surface {
137 // Safe because `self` represents a valid VASurface.
138 unsafe { bindings::vaDestroySurfaces(self.display.handle(), &mut self.id, 1) };