edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·382.4 MiB·cdccbc2·Tahinli·13 hours ago·4263 commits
| 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 | |
| 5 | use std::rc::Rc; |
| 6 | |
| 7 | use crate::bindings; |
| 8 | use crate::va_check; |
| 9 | use crate::Display; |
| 10 | use crate::Surface; |
| 11 | use crate::SurfaceMemoryDescriptor; |
| 12 | use crate::VaError; |
| 13 | |
| 14 | /// Wrapper around `VAImage` that is tied to the lifetime of a given `Picture`. |
| 15 | /// |
| 16 | /// An image is used to either get the surface data to client memory, or to copy image data in |
| 17 | /// client memory to a surface. |
| 18 | pub struct Image<'a> { |
| 19 | /// The display from which the image was created, so we can unmap it upon destruction. |
| 20 | display: Rc<Display>, |
| 21 | /// The `VAImage` returned by libva. |
| 22 | image: bindings::VAImage, |
| 23 | /// The mapped surface data. |
| 24 | /// |
| 25 | /// The lifetime also allows us to ensure that the `Surface` we have been created from |
| 26 | /// does not drop or get changed while we exist. |
| 27 | data: &'a mut [u8], |
| 28 | /// Whether the image was derived using the `vaDeriveImage` API or created using the |
| 29 | /// `vaCreateImage` API. |
| 30 | derived: bool, |
| 31 | /// The display resolution requested by the client. The implementation is |
| 32 | /// free to enlarge this value as needed. In any case, we guarantee that an |
| 33 | /// image at least as large is returned. |
| 34 | display_resolution: (u32, u32), |
| 35 | /// Tracks whether the underlying data has possibly been written to, i.e. an encoder will create |
| 36 | /// an image and map its buffer in order to write to it, so we must writeback later. |
| 37 | dirty: bool, |
| 38 | /// The ID of the `Surface` we have been created from. |
| 39 | surface_id: u32, |
| 40 | } |
| 41 | |
| 42 | impl<'a> Image<'a> { |
| 43 | /// Helper method to map a `VAImage` using `vaMapBuffer` and return an `Image`. |
| 44 | /// |
| 45 | /// Returns an error if the mapping failed. |
| 46 | fn new<D: SurfaceMemoryDescriptor>( |
| 47 | surface: &'a Surface<D>, |
| 48 | image: bindings::VAImage, |
| 49 | derived: bool, |
| 50 | display_resolution: (u32, u32), |
| 51 | ) -> Result<Self, VaError> { |
| 52 | let mut addr = std::ptr::null_mut(); |
| 53 | |
| 54 | // Safe since `picture.inner.context` represents a valid `VAContext` and `image` has been |
| 55 | // successfully created at this point. |
| 56 | match va_check(unsafe { |
| 57 | bindings::vaMapBuffer(surface.display().handle(), image.buf, &mut addr) |
| 58 | }) { |
| 59 | Ok(_) => { |
| 60 | if u32::from(image.width) < display_resolution.0 |
| 61 | || u32::from(image.height) < display_resolution.1 |
| 62 | { |
| 63 | // Safe because the buffer was mapped above and `image` is a valid `VAImage`. |
| 64 | unsafe { |
| 65 | bindings::vaUnmapBuffer(surface.display().handle(), image.buf); |
| 66 | bindings::vaDestroyImage(surface.display().handle(), image.image_id); |
| 67 | } |
| 68 | |
| 69 | return Err(va_check(bindings::VA_STATUS_ERROR_INVALID_IMAGE as _).unwrap_err()); |
| 70 | } |
| 71 | |
| 72 | // Safe since `addr` points to data mapped onto our address space since we called |
| 73 | // `vaMapBuffer` above, which also guarantees that the data is valid for |
| 74 | // `image.data_size`. |
| 75 | let data = |
| 76 | unsafe { std::slice::from_raw_parts_mut(addr as _, image.data_size as usize) }; |
| 77 | Ok(Image { |
| 78 | display: Rc::clone(surface.display()), |
| 79 | image, |
| 80 | data, |
| 81 | derived, |
| 82 | display_resolution, |
| 83 | dirty: false, |
| 84 | surface_id: surface.id(), |
| 85 | }) |
| 86 | } |
| 87 | Err(e) => { |
| 88 | // Safe because `picture.inner.context` represents a valid `VAContext` and `image` |
| 89 | // represents a valid `VAImage`. |
| 90 | unsafe { |
| 91 | bindings::vaDestroyImage(surface.display().handle(), image.image_id); |
| 92 | } |
| 93 | |
| 94 | Err(e) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Create a new derived image from this `surface` using `vaDeriveImage`. |
| 100 | /// |
| 101 | /// Derived images are a direct view (i.e. without any copy) on the buffer content of |
| 102 | /// `surface`. On the other hand, not all `Surface`s can be derived. |
| 103 | /// |
| 104 | /// `visible_rect` is the visible rectangle inside `surface` that we want to access. |
| 105 | pub fn derive_from<D: SurfaceMemoryDescriptor>( |
| 106 | surface: &'a Surface<D>, |
| 107 | visible_rect: (u32, u32), |
| 108 | ) -> Result<Self, VaError> { |
| 109 | // An all-zero byte-pattern is a valid initial value for `VAImage`. |
| 110 | let mut image: bindings::VAImage = Default::default(); |
| 111 | |
| 112 | // Safe because `self` has a valid display handle and ID. |
| 113 | va_check(unsafe { |
| 114 | bindings::vaDeriveImage(surface.display().handle(), surface.id(), &mut image) |
| 115 | })?; |
| 116 | |
| 117 | Self::new(surface, image, true, visible_rect) |
| 118 | } |
| 119 | |
| 120 | /// Create new image from `surface` using `vaCreateImage` and `vaGetImage`. |
| 121 | /// |
| 122 | /// `visible_rect` is the visible rectangle inside `surface` that we want to access. |
| 123 | /// |
| 124 | /// The image will contain a copy of `surface`'s data' in the desired `format` and |
| 125 | /// `coded_resolution`, meaning the data can be scaled if `coded_resolution` and `visible_rect` |
| 126 | /// differ. |
| 127 | pub fn create_from<D: SurfaceMemoryDescriptor>( |
| 128 | surface: &'a Surface<D>, |
| 129 | mut format: bindings::VAImageFormat, |
| 130 | coded_resolution: (u32, u32), |
| 131 | visible_rect: (u32, u32), |
| 132 | ) -> Result<Image<'a>, VaError> { |
| 133 | // An all-zero byte-pattern is a valid initial value for `VAImage`. |
| 134 | let mut image: bindings::VAImage = Default::default(); |
| 135 | let dpy = surface.display().handle(); |
| 136 | |
| 137 | // Safe because `dpy` is a valid display handle. |
| 138 | va_check(unsafe { |
| 139 | bindings::vaCreateImage( |
| 140 | dpy, |
| 141 | &mut format, |
| 142 | coded_resolution.0 as i32, |
| 143 | coded_resolution.1 as i32, |
| 144 | &mut image, |
| 145 | ) |
| 146 | })?; |
| 147 | |
| 148 | // Safe because `dpy` is a valid display handle, `picture.surface` is a valid VASurface and |
| 149 | // `image` is a valid `VAImage`. |
| 150 | match va_check(unsafe { |
| 151 | bindings::vaGetImage( |
| 152 | dpy, |
| 153 | surface.id(), |
| 154 | 0, |
| 155 | 0, |
| 156 | coded_resolution.0, |
| 157 | coded_resolution.1, |
| 158 | image.image_id, |
| 159 | ) |
| 160 | }) { |
| 161 | Ok(()) => Self::new(surface, image, false, visible_rect), |
| 162 | |
| 163 | Err(e) => { |
| 164 | // Safe because `image` is a valid `VAImage`. |
| 165 | unsafe { |
| 166 | bindings::vaDestroyImage(dpy, image.image_id); |
| 167 | } |
| 168 | |
| 169 | Err(e) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// Get a reference to the underlying `VAImage` that describes this image. |
| 175 | pub fn image(&self) -> &bindings::VAImage { |
| 176 | &self.image |
| 177 | } |
| 178 | |
| 179 | /// Returns whether this image is directly derived from its underlying `Picture`, as opposed to |
| 180 | /// being a view/copy of said `Picture` in a guaranteed pixel format. |
| 181 | pub fn is_derived(&self) -> bool { |
| 182 | self.derived |
| 183 | } |
| 184 | |
| 185 | /// Returns the display resolution as passed in by the client. This is a |
| 186 | /// value that is less than or equal to the coded resolution. |
| 187 | pub fn display_resolution(&self) -> (u32, u32) { |
| 188 | self.display_resolution |
| 189 | } |
| 190 | |
| 191 | /// Returns the coded resolution. This value can be larger than the value |
| 192 | /// passed in when the image was created if the driver needs to. |
| 193 | pub fn coded_resolution(&self) -> (u32, u32) { |
| 194 | (self.image.width.into(), self.image.height.into()) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | impl<'a> AsRef<[u8]> for Image<'a> { |
| 199 | fn as_ref(&self) -> &[u8] { |
| 200 | self.data |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl<'a> AsMut<[u8]> for Image<'a> { |
| 205 | fn as_mut(&mut self) -> &mut [u8] { |
| 206 | self.dirty = true; |
| 207 | self.data |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl<'a> Drop for Image<'a> { |
| 212 | fn drop(&mut self) { |
| 213 | if !self.derived && self.dirty { |
| 214 | // Safe because `picture.inner.context` represents a valid `VAContext`, |
| 215 | // `picture.surface` represents a valid `VASurface` and `image` represents a valid |
| 216 | // `VAImage`. |
| 217 | unsafe { |
| 218 | bindings::vaPutImage( |
| 219 | self.display.handle(), |
| 220 | self.surface_id, |
| 221 | self.image.image_id, |
| 222 | 0, |
| 223 | 0, |
| 224 | self.image.width as u32, |
| 225 | self.image.height as u32, |
| 226 | 0, |
| 227 | 0, |
| 228 | self.image.width as u32, |
| 229 | self.image.height as u32, |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | unsafe { |
| 235 | // Safe since the buffer is mapped in `Image::new`, so `self.image.buf` points to a |
| 236 | // valid `VABufferID`. |
| 237 | bindings::vaUnmapBuffer(self.display.handle(), self.image.buf); |
| 238 | // Safe since `self.image` represents a valid `VAImage`. |
| 239 | bindings::vaDestroyImage(self.display.handle(), self.image.image_id); |
| 240 | } |
| 241 | } |
| 242 | } |