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