use std::any::Any;
use std::sync::Arc;
use crate::image_codecs::{ImageError, ImageFuture, PixelBuffer};
pub trait HardwareBufferAllocator: Send + Sync {
fn allocate(
&self,
width: u32,
height: u32,
format: HardwareBufferFormat,
usage: HardwareBufferUsage,
) -> Result<Arc<dyn HardwareBuffer>, ImageError>;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct HardwareBufferUsage {
pub gpu_sampled: bool,
pub gpu_color_output: bool,
pub composer_overlay: bool,
pub cpu_read: bool,
}
impl HardwareBufferUsage {
pub const GPU_SAMPLED: HardwareBufferUsage = HardwareBufferUsage {
gpu_sampled: true,
gpu_color_output: false,
composer_overlay: false,
cpu_read: false,
};
pub const ALL: HardwareBufferUsage = HardwareBufferUsage {
gpu_sampled: true,
gpu_color_output: true,
composer_overlay: true,
cpu_read: true,
};
}
pub trait HardwareBuffer: Any + Send + Sync {
fn width(&self) -> u32;
fn height(&self) -> u32;
fn usage(&self) -> HardwareBufferUsage;
fn format(&self) -> HardwareBufferFormat;
fn read_pixels(&self) -> ImageFuture<PixelBuffer>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HardwareBufferFormat {
Bgra8,
Rgba8,
Nv12,
P010,
}
impl HardwareBufferFormat {
pub fn plane_count(self) -> usize {
match self {
HardwareBufferFormat::Bgra8 | HardwareBufferFormat::Rgba8 => 1,
HardwareBufferFormat::Nv12 | HardwareBufferFormat::P010 => 2,
}
}
pub fn plane_format(self, plane: usize) -> Option<wgpu::TextureFormat> {
let formats: &[wgpu::TextureFormat] = match self {
HardwareBufferFormat::Bgra8 => &[wgpu::TextureFormat::Bgra8Unorm],
HardwareBufferFormat::Rgba8 => &[wgpu::TextureFormat::Rgba8Unorm],
HardwareBufferFormat::Nv12 => {
&[wgpu::TextureFormat::R8Unorm, wgpu::TextureFormat::Rg8Unorm]
}
HardwareBufferFormat::P010 => &[
wgpu::TextureFormat::R16Unorm,
wgpu::TextureFormat::Rg16Unorm,
],
};
formats.get(plane).copied()
}
pub fn plane_size(self, plane: usize, size: [u32; 2]) -> Option<[u32; 2]> {
match plane {
0 => Some(size),
1 if self.plane_count() == 2 => Some(size.map(|side| side.div_ceil(2))),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum GpuId {
Luid { low: u32, high: i32 },
DrmRenderNode { major: u32, minor: u32 },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_decoders_format_has_a_brightness_plane_then_a_colour_plane_at_half_size() {
let format = HardwareBufferFormat::Nv12;
assert_eq!(format.plane_count(), 2);
assert_eq!(format.plane_format(0), Some(wgpu::TextureFormat::R8Unorm));
assert_eq!(format.plane_format(1), Some(wgpu::TextureFormat::Rg8Unorm));
assert_eq!(format.plane_format(2), None);
assert_eq!(format.plane_size(0, [1919, 1081]), Some([1919, 1081]));
assert_eq!(format.plane_size(1, [1919, 1081]), Some([960, 541]));
assert_eq!(format.plane_size(2, [1919, 1081]), None);
assert_eq!(
HardwareBufferFormat::P010.plane_format(1),
Some(wgpu::TextureFormat::Rg16Unorm)
);
}
#[test]
fn a_format_of_one_plane_is_that_plane_whole() {
let format = HardwareBufferFormat::Bgra8;
assert_eq!(format.plane_count(), 1);
assert_eq!(
format.plane_format(0),
Some(wgpu::TextureFormat::Bgra8Unorm)
);
assert_eq!(format.plane_format(1), None);
assert_eq!(format.plane_size(1, [8, 8]), None);
}
}