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;
9 use crate::formats::{RTFormat, VAFourcc};
11 use crate::UsageHints;
13 /// An owned VA surface that is tied to the lifetime of a particular VADisplay
16 id: bindings::VASurfaceID,
22 /// Create `Surfaces` by wrapping around a `vaCreateSurfaces` call. This is just a helper for
23 /// [`Display::create_surfaces`].
27 va_fourcc: Option<VAFourcc>,
30 usage_hints: Option<UsageHints>,
32 ) -> VAResult<Vec<Self>> {
33 let mut attrs = vec![];
35 if let Some(usage_hints) = usage_hints {
36 let attr = bindings::VASurfaceAttrib {
37 type_: bindings::VASurfaceAttribType::VASurfaceAttribUsageHint,
38 flags: bindings::constants::VA_SURFACE_ATTRIB_SETTABLE,
39 value: bindings::VAGenericValue {
40 type_: bindings::VAGenericValueType::VAGenericValueTypeInteger,
41 value: bindings::_VAGenericValue__bindgen_ty_1 {
42 i: usage_hints.bits() as i32,
50 if let Some(fourcc) = va_fourcc {
51 let attr = bindings::VASurfaceAttrib {
52 type_: bindings::VASurfaceAttribType::VASurfaceAttribPixelFormat,
53 flags: bindings::constants::VA_DISPLAY_ATTRIB_SETTABLE,
54 value: bindings::VAGenericValue {
55 type_: bindings::VAGenericValueType::VAGenericValueTypeInteger,
56 value: bindings::_VAGenericValue__bindgen_ty_1 { i: fourcc as i32 },
63 let mut surfaces = Vec::with_capacity(num_surfaces as usize);
65 // Safe because `self` represents a valid VADisplay. The `surface` and `attrs` vectors are
66 // properly initialized and valid sizes are passed to the C function, so it is impossible to
67 // write past the end of their storage by mistake.
69 bindings::vaCreateSurfaces(
74 surfaces.as_mut_ptr(),
82 // Safe because the C function will have written to exactly `num_surfaces` entries, which is
83 // known to be within the vector's capacity.
85 surfaces.set_len(num_surfaces as usize);
88 let va_surfaces = surfaces
91 display: Rc::clone(&display),
101 /// Blocks until all pending operations on the render target have been completed. Upon return it
102 /// is safe to use the render target for a different picture.
103 pub fn sync(&self) -> VAResult<()> {
104 // Safe because `self` represents a valid VASurface.
105 (unsafe { bindings::vaSyncSurface(self.display.handle(), self.id) }).check()
108 /// Convenience function to return a VASurfaceID vector. Useful to interface with the C API
109 /// where a surface array might be needed.
110 pub fn as_id_vec(surfaces: &[Self]) -> Vec<bindings::VASurfaceID> {
111 surfaces.iter().map(|surface| surface.id).collect()
114 /// Wrapper over `vaQuerySurfaceStatus` to find out any pending ops on the render target.
115 pub fn query_status(&self) -> VAResult<VASurfaceStatus> {
116 let mut status: bindings::VASurfaceStatus::Type = 0;
117 // Safe because `self` represents a valid VASurface.
119 bindings::vaQuerySurfaceStatus(self.display.handle(), self.id, &mut status)
123 bindings::VASurfaceStatus::VASurfaceRendering => Ok(VASurfaceStatus::Rendering),
124 bindings::VASurfaceStatus::VASurfaceDisplaying => Ok(VASurfaceStatus::Displaying),
125 bindings::VASurfaceStatus::VASurfaceReady => Ok(VASurfaceStatus::Ready),
126 bindings::VASurfaceStatus::VASurfaceSkipped => Ok(VASurfaceStatus::Skipped),
127 _ => Err(VAError::Unknown),
131 /// Returns the ID of this surface.
132 pub fn id(&self) -> bindings::VASurfaceID {
136 /// Returns the dimensions of this surface.
137 pub fn size(&self) -> (u32, u32) {
138 (self.width, self.height)
142 impl Drop for Surface {
144 // Safe because `self` represents a valid VASurface.
145 unsafe { bindings::vaDestroySurfaces(self.display.handle(), &mut self.id, 1) };