| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::rc::Rc; |
| 6 | |
| 7 | use log::error; |
| 8 | |
| 9 | use crate::bindings; |
| 10 | use crate::buffer::Buffer; |
| 11 | use crate::buffer::BufferType; |
| 12 | use crate::display::Display; |
| 13 | use crate::va_check; |
| 14 | use crate::Config; |
| 15 | use crate::EncCodedBuffer; |
| 16 | use crate::Surface; |
| 17 | use crate::SurfaceMemoryDescriptor; |
| 18 | use crate::VaError; |
| 19 | |
| 20 | |
| 21 | pub struct Context { |
| 22 | display: Rc<Display>, |
| 23 | id: bindings::VAContextID, |
| 24 | } |
| 25 | |
| 26 | impl Context { |
| 27 | |
| 28 | |
| 29 | pub(crate) fn new<D: SurfaceMemoryDescriptor>( |
| 30 | display: Rc<Display>, |
| 31 | config: &Config, |
| 32 | coded_width: u32, |
| 33 | coded_height: u32, |
| 34 | surfaces: Option<&Vec<Surface<D>>>, |
| 35 | progressive: bool, |
| 36 | ) -> Result<Rc<Self>, VaError> { |
| 37 | let mut context_id = 0; |
| 38 | let flags = if progressive { |
| 39 | bindings::VA_PROGRESSIVE as i32 |
| 40 | } else { |
| 41 | 0 |
| 42 | }; |
| 43 | |
| 44 | let mut render_targets = match surfaces { |
| 45 | Some(surfaces) => Surface::as_id_vec(surfaces), |
| 46 | None => Default::default(), |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | va_check(unsafe { |
| 53 | bindings::vaCreateContext( |
| 54 | display.handle(), |
| 55 | config.id(), |
| 56 | coded_width as i32, |
| 57 | coded_height as i32, |
| 58 | flags, |
| 59 | render_targets.as_mut_ptr(), |
| 60 | render_targets.len() as i32, |
| 61 | &mut context_id, |
| 62 | ) |
| 63 | })?; |
| 64 | |
| 65 | Ok(Rc::new(Self { |
| 66 | display, |
| 67 | id: context_id, |
| 68 | })) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub fn display(&self) -> &Rc<Display> { |
| 73 | &self.display |
| 74 | } |
| 75 | |
| 76 | |
| 77 | pub(crate) fn id(&self) -> bindings::VAContextID { |
| 78 | self.id |
| 79 | } |
| 80 | |
| 81 | |
| 82 | pub fn create_buffer(self: &Rc<Self>, type_: BufferType) -> Result<Buffer, VaError> { |
| 83 | Buffer::new(Rc::clone(self), type_) |
| 84 | } |
| 85 | |
| 86 | |
| 87 | pub fn create_buffer_borrowed( |
| 88 | self: &Rc<Self>, |
| 89 | type_: crate::buffer::BorrowedBufferType<'_>, |
| 90 | ) -> Result<Buffer, VaError> { |
| 91 | Buffer::new_borrowed(Rc::clone(self), type_) |
| 92 | } |
| 93 | |
| 94 | |
| 95 | pub fn create_enc_coded(self: &Rc<Self>, size: usize) -> Result<EncCodedBuffer, VaError> { |
| 96 | EncCodedBuffer::new(Rc::clone(self), size) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl Drop for Context { |
| 101 | fn drop(&mut self) { |
| 102 | |
| 103 | let status = |
| 104 | va_check(unsafe { bindings::vaDestroyContext(self.display.handle(), self.id) }); |
| 105 | |
| 106 | if status.is_err() { |
| 107 | error!("vaDestroyContext failed: {}", status.unwrap_err()); |
| 108 | } |
| 109 | } |
| 110 | } |