Skip to main content

moq_vaapi/
context.rs

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.
4
5use std::rc::Rc;
6use std::sync::Arc;
7
8use log::error;
9
10use crate::bindings;
11use crate::buffer::Buffer;
12use crate::buffer::BufferType;
13use crate::display::Display;
14use crate::va_check;
15use crate::Config;
16use crate::EncCodedBuffer;
17use crate::Surface;
18use crate::SurfaceMemoryDescriptor;
19use crate::VaError;
20
21/// A VA context for a particular [`Display`].
22pub struct Context {
23	display: Arc<Display>,
24	id: bindings::VAContextID,
25}
26
27impl Context {
28	/// Creates a Context by wrapping around a `vaCreateContext` call. This is just a helper for
29	/// [`Display::create_context`].
30	pub(crate) fn new<D: SurfaceMemoryDescriptor>(
31		display: Arc<Display>,
32		config: &Config,
33		coded_width: u32,
34		coded_height: u32,
35		surfaces: Option<&Vec<Surface<D>>>,
36		progressive: bool,
37	) -> Result<Rc<Self>, VaError> {
38		let mut context_id = 0;
39		let flags = if progressive {
40			bindings::VA_PROGRESSIVE as i32
41		} else {
42			0
43		};
44
45		let mut render_targets = match surfaces {
46			Some(surfaces) => Surface::as_id_vec(surfaces),
47			None => Default::default(),
48		};
49
50		// Safe because `self` represents a valid VADisplay and render_targets
51		// and ntargets are properly initialized. Note that render_targets==NULL
52		// is valid so long as ntargets==0.
53		va_check(unsafe {
54			bindings::vaCreateContext(
55				display.handle(),
56				config.id(),
57				coded_width as i32,
58				coded_height as i32,
59				flags,
60				render_targets.as_mut_ptr(),
61				render_targets.len() as i32,
62				&mut context_id,
63			)
64		})?;
65
66		Ok(Rc::new(Self {
67			display,
68			id: context_id,
69		}))
70	}
71
72	/// Returns a shared reference to the [`Display`] used by this context.
73	pub fn display(&self) -> &Arc<Display> {
74		&self.display
75	}
76
77	/// Returns the ID of this context.
78	pub(crate) fn id(&self) -> bindings::VAContextID {
79		self.id
80	}
81
82	/// Create a new buffer of type `type_`.
83	pub fn create_buffer(self: &Rc<Self>, type_: BufferType) -> Result<Buffer, VaError> {
84		Buffer::new(Rc::clone(self), type_)
85	}
86
87	/// Create a new buffer of type `type_`.
88	pub fn create_enc_coded(self: &Rc<Self>, size: usize) -> Result<EncCodedBuffer, VaError> {
89		EncCodedBuffer::new(Rc::clone(self), size)
90	}
91}
92
93impl Drop for Context {
94	fn drop(&mut self) {
95		// Safe because `self` represents a valid VAContext.
96		let status = va_check(unsafe { bindings::vaDestroyContext(self.display.handle(), self.id) });
97
98		if status.is_err() {
99			error!("vaDestroyContext failed: {}", status.unwrap_err());
100		}
101	}
102}