| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::borrow::Borrow; |
| 6 | use std::cell::RefCell; |
| 7 | use std::collections::BTreeMap; |
| 8 | use std::collections::VecDeque; |
| 9 | use std::rc::Rc; |
| 10 | use std::rc::Weak; |
| 11 | |
| 12 | use libva::Display; |
| 13 | use libva::Surface; |
| 14 | use libva::SurfaceMemoryDescriptor; |
| 15 | use libva::VASurfaceID; |
| 16 | |
| 17 | use crate::decoder::FramePool; |
| 18 | use crate::Resolution; |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | pub struct PooledVaSurface<M: SurfaceMemoryDescriptor> { |
| 25 | surface: Option<Surface<M>>, |
| 26 | pool: Weak<RefCell<VaSurfacePoolInner<M>>>, |
| 27 | } |
| 28 | |
| 29 | impl<M: SurfaceMemoryDescriptor> PooledVaSurface<M> { |
| 30 | fn new(surface: Surface<M>, pool: &Rc<RefCell<VaSurfacePoolInner<M>>>) -> Self { |
| 31 | Self { surface: Some(surface), pool: Rc::downgrade(pool) } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
| 36 | pub fn detach_from_pool(mut self) -> Surface<M> { |
| 37 | |
| 38 | let surface = self.surface.take().unwrap(); |
| 39 | |
| 40 | if let Some(pool) = self.pool.upgrade() { |
| 41 | (*pool).borrow_mut().managed_surfaces.remove(&surface.id()); |
| 42 | } |
| 43 | |
| 44 | surface |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<M: SurfaceMemoryDescriptor> Borrow<Surface<M>> for PooledVaSurface<M> { |
| 49 | fn borrow(&self) -> &Surface<M> { |
| 50 | |
| 51 | self.surface.as_ref().unwrap() |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl<M: SurfaceMemoryDescriptor> AsRef<M> for PooledVaSurface<M> { |
| 56 | fn as_ref(&self) -> &M { |
| 57 | <Self as Borrow<Surface<M>>>::borrow(self).as_ref() |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl<M: SurfaceMemoryDescriptor> Drop for PooledVaSurface<M> { |
| 62 | fn drop(&mut self) { |
| 63 | |
| 64 | if let Some(surface) = self.surface.take() { |
| 65 | |
| 66 | if let Some(pool) = self.pool.upgrade() { |
| 67 | let mut pool_borrowed = (*pool).borrow_mut(); |
| 68 | |
| 69 | if pool_borrowed.managed_surfaces.contains_key(&surface.id()) { |
| 70 | pool_borrowed.surfaces.push_back(surface); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | log::debug!("Dropping stale surface: {}, ({:?})", surface.id(), surface.size()) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | struct VaSurfacePoolInner<M: SurfaceMemoryDescriptor> { |
| 82 | display: Rc<Display>, |
| 83 | rt_format: u32, |
| 84 | usage_hint: Option<libva::UsageHint>, |
| 85 | coded_resolution: Resolution, |
| 86 | surfaces: VecDeque<Surface<M>>, |
| 87 | |
| 88 | |
| 89 | |
| 90 | managed_surfaces: BTreeMap<VASurfaceID, Resolution>, |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | pub struct VaSurfacePool<M: SurfaceMemoryDescriptor> { |
| 103 | inner: Rc<RefCell<VaSurfacePoolInner<M>>>, |
| 104 | } |
| 105 | |
| 106 | impl<M: SurfaceMemoryDescriptor> VaSurfacePool<M> { |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #[allow(dead_code)] |
| 115 | fn add_surface(&mut self, surface: Surface<M>) -> Result<(), Surface<M>> { |
| 116 | let mut inner = (*self.inner).borrow_mut(); |
| 117 | |
| 118 | if Resolution::from(surface.size()).can_contain(inner.coded_resolution) { |
| 119 | inner.managed_surfaces.insert(surface.id(), surface.size().into()); |
| 120 | inner.surfaces.push_back(surface); |
| 121 | Ok(()) |
| 122 | } else { |
| 123 | Err(surface) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | pub fn new( |
| 136 | display: Rc<Display>, |
| 137 | rt_format: u32, |
| 138 | usage_hint: Option<libva::UsageHint>, |
| 139 | coded_resolution: Resolution, |
| 140 | ) -> Self { |
| 141 | Self { |
| 142 | inner: Rc::new(RefCell::new(VaSurfacePoolInner { |
| 143 | display, |
| 144 | rt_format, |
| 145 | usage_hint, |
| 146 | coded_resolution, |
| 147 | surfaces: VecDeque::new(), |
| 148 | managed_surfaces: Default::default(), |
| 149 | })), |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | pub fn get_surface(&mut self) -> Option<PooledVaSurface<M>> { |
| 155 | let mut inner = (*self.inner).borrow_mut(); |
| 156 | let surface = inner.surfaces.pop_front(); |
| 157 | |
| 158 | |
| 159 | |
| 160 | debug_assert!({ |
| 161 | match surface.as_ref() { |
| 162 | Some(s) => Resolution::from(s.size()).can_contain(inner.coded_resolution), |
| 163 | None => true, |
| 164 | } |
| 165 | }); |
| 166 | |
| 167 | surface.map(|s| PooledVaSurface::new(s, &self.inner)) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl<M: SurfaceMemoryDescriptor> FramePool for VaSurfacePool<M> { |
| 172 | type Descriptor = M; |
| 173 | |
| 174 | fn coded_resolution(&self) -> Resolution { |
| 175 | (*self.inner).borrow().coded_resolution |
| 176 | } |
| 177 | |
| 178 | fn set_coded_resolution(&mut self, resolution: Resolution) { |
| 179 | let mut inner = (*self.inner).borrow_mut(); |
| 180 | |
| 181 | inner.coded_resolution = resolution; |
| 182 | inner.managed_surfaces.retain(|_, res| res.can_contain(resolution)); |
| 183 | inner.surfaces.retain(|s| Resolution::from(s.size()).can_contain(resolution)); |
| 184 | } |
| 185 | |
| 186 | fn add_frames(&mut self, descriptors: Vec<Self::Descriptor>) -> Result<(), anyhow::Error> { |
| 187 | let mut inner = (*self.inner).borrow_mut(); |
| 188 | |
| 189 | let surfaces = inner |
| 190 | .display |
| 191 | .create_surfaces( |
| 192 | inner.rt_format, |
| 193 | |
| 194 | |
| 195 | None, |
| 196 | inner.coded_resolution.width, |
| 197 | inner.coded_resolution.height, |
| 198 | inner.usage_hint, |
| 199 | descriptors, |
| 200 | ) |
| 201 | .map_err(|e| anyhow::anyhow!(e))?; |
| 202 | |
| 203 | for surface in &surfaces { |
| 204 | inner.managed_surfaces.insert(surface.id(), surface.size().into()); |
| 205 | } |
| 206 | inner.surfaces.extend(surfaces); |
| 207 | |
| 208 | Ok(()) |
| 209 | } |
| 210 | |
| 211 | fn num_free_frames(&self) -> usize { |
| 212 | (*self.inner).borrow().surfaces.len() |
| 213 | } |
| 214 | |
| 215 | fn num_managed_frames(&self) -> usize { |
| 216 | (*self.inner).borrow().managed_surfaces.len() |
| 217 | } |
| 218 | |
| 219 | fn clear(&mut self) { |
| 220 | let mut pool = (*self.inner).borrow_mut(); |
| 221 | |
| 222 | pool.surfaces.clear(); |
| 223 | pool.managed_surfaces.clear(); |
| 224 | } |
| 225 | } |