Skip to main content

moq_vaapi/buffer/
proc_pipeline.rs

1// Copyright 2025 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.
4
5//! Wrappers around `VAProcPipeline` types.
6
7use crate::bindings;
8use std::{marker::PhantomData, ptr};
9
10/// Wrapper over the `VABlendState` ffi type.
11pub struct BlendState(bindings::VABlendState);
12
13impl BlendState {
14	/// Creates the bindgen field
15	pub fn new(flags: u32, global_alpha: f32, min_luma: f32, max_luma: f32) -> Self {
16		Self(bindings::VABlendState {
17			flags,
18			global_alpha,
19			min_luma,
20			max_luma,
21		})
22	}
23}
24
25/// Wrapper over the `VAProcColorProperties` ffi type.
26pub struct ProcColorProperties(bindings::VAProcColorProperties);
27
28impl ProcColorProperties {
29	/// Creates the bindgen field
30	pub fn new(
31		chroma_sample_location: u8,
32		color_range: u8,
33		colour_primaries: u8,
34		transfer_characteristics: u8,
35		matrix_coefficients: u8,
36	) -> Self {
37		Self(bindings::VAProcColorProperties {
38			chroma_sample_location,
39			color_range,
40			colour_primaries,
41			transfer_characteristics,
42			matrix_coefficients,
43			reserved: Default::default(),
44		})
45	}
46}
47
48impl Default for ProcColorProperties {
49	fn default() -> Self {
50		Self::new(0, 0, 0, 0, 0)
51	}
52}
53
54/// Wrapper over the `VAHdrMetaData` ffi type.
55pub struct HdrMetaData(bindings::VAHdrMetaData);
56
57impl<'a> HdrMetaData {
58	/// Creates the bindgen field
59	pub fn new(metadata_type: u32, metadata: Option<&'a [u8]>, metadata_size: u32) -> Self {
60		Self(bindings::VAHdrMetaData {
61			metadata_type: metadata_type as _,
62			metadata: metadata.map_or(ptr::null_mut(), |f| f.as_ptr() as *mut _),
63			metadata_size,
64			reserved: Default::default(),
65		})
66	}
67}
68
69/// Wrapper over the `VAProcPipelineParameterBuffer` FFI type.
70pub struct ProcPipelineParameterBuffer {
71	c_params: Box<bindings::VAProcPipelineParameterBuffer>,
72
73	// Fields that own the data for the pointers in `c_params`.
74	surface_region: Option<Box<bindings::VARectangle>>,
75	output_region: Option<Box<bindings::VARectangle>>,
76	filters: Option<Vec<bindings::VABufferID>>,
77	forward_references: Option<Vec<bindings::VASurfaceID>>,
78	backward_references: Option<Vec<bindings::VASurfaceID>>,
79	blend_state: Option<Vec<BlendState>>,
80	additional_outputs: Option<Vec<bindings::VASurfaceID>>,
81	output_hdr_metadata: Option<Vec<HdrMetaData>>,
82}
83
84impl ProcPipelineParameterBuffer {
85	/// Creates the wrapper.
86	#[allow(clippy::too_many_arguments)]
87	pub fn new(
88		surface: bindings::VASurfaceID,
89		surface_region: Option<bindings::VARectangle>,
90		surface_color_standard: u8,
91		output_region: Option<bindings::VARectangle>,
92		output_background_color: u32,
93		output_color_standard: u8,
94		pipeline_flags: u32,
95		filter_flags: u32,
96		filters: Option<Vec<bindings::VABufferID>>,
97		forward_references: Option<Vec<bindings::VASurfaceID>>,
98		backward_references: Option<Vec<bindings::VASurfaceID>>,
99		rotation_state: u32,
100		blend_state: Option<Vec<BlendState>>,
101		mirror_state: u32,
102		additional_outputs: Option<Vec<bindings::VASurfaceID>>,
103		input_surface_flag: u32,
104		output_surface_flag: u32,
105		input_color_properties: ProcColorProperties,
106		output_color_properties: ProcColorProperties,
107		processing_mode: u32,
108		output_hdr_metadata: Option<Vec<HdrMetaData>>,
109	) -> Self {
110		let mut slf = Self {
111			// SAFETY: The VA-API structures are C-compatible so zeroing is safe.
112			c_params: Box::new(unsafe { std::mem::zeroed() }),
113			surface_region: surface_region.map(Box::new),
114			output_region: output_region.map(Box::new),
115			filters,
116			forward_references,
117			backward_references,
118			blend_state,
119			additional_outputs,
120			output_hdr_metadata,
121		};
122
123		slf.c_params = Box::new(bindings::VAProcPipelineParameterBuffer {
124			surface,
125			surface_region: slf
126				.surface_region
127				.as_deref()
128				.map_or(ptr::null_mut(), |r| r as *const _ as *mut _),
129			surface_color_standard: surface_color_standard as _,
130			output_region: slf
131				.output_region
132				.as_deref()
133				.map_or(ptr::null_mut(), |r| r as *const _ as *mut _),
134			output_background_color,
135			output_color_standard: output_color_standard as _,
136			pipeline_flags,
137			filter_flags,
138			filters: slf.filters.as_deref().map_or(ptr::null_mut(), |f| f.as_ptr() as *mut _),
139			num_filters: slf.filters.as_ref().map_or(0, |f| f.len() as u32),
140			forward_references: slf
141				.forward_references
142				.as_deref()
143				.map_or(ptr::null_mut(), |r| r.as_ptr() as *mut _),
144			num_forward_references: slf.forward_references.as_ref().map_or(0, |f| f.len() as u32),
145			backward_references: slf
146				.backward_references
147				.as_deref()
148				.map_or(ptr::null_mut(), |r| r.as_ptr() as *mut _),
149			num_backward_references: slf.backward_references.as_ref().map_or(0, |b| b.len() as u32),
150			rotation_state,
151			blend_state: slf
152				.blend_state
153				.as_deref()
154				.map_or(ptr::null(), |b| b.as_ptr() as *const bindings::VABlendState),
155			mirror_state,
156			additional_outputs: slf
157				.additional_outputs
158				.as_deref()
159				.map_or(ptr::null_mut(), |a| a.as_ptr() as *mut _),
160			num_additional_outputs: slf.additional_outputs.as_ref().map_or(0, |a| a.len() as u32),
161			input_surface_flag,
162			output_surface_flag,
163			input_color_properties: input_color_properties.0,
164			output_color_properties: output_color_properties.0,
165			processing_mode,
166			output_hdr_metadata: slf
167				.output_hdr_metadata
168				.as_deref()
169				.map_or(ptr::null_mut(), |o| o.as_ptr() as *mut bindings::VAHdrMetaData),
170			va_reserved: Default::default(),
171		});
172
173		slf
174	}
175
176	pub(crate) fn inner_mut(&mut self) -> &mut bindings::VAProcPipelineParameterBuffer {
177		self.c_params.as_mut()
178	}
179
180	/// Returns the inner FFI type. Useful for testing purposes.
181	pub fn inner(&self) -> &bindings::VAProcPipelineParameterBuffer {
182		self.c_params.as_ref()
183	}
184}