use std::{
any::Any,
sync::{
Arc,
atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering},
},
};
use parking_lot::{Mutex, RwLock};
use super::YuvColors;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CardSurface {
Nv12(isize),
Planes { gray: isize, color: isize },
Rgba(isize),
}
impl CardSurface {
pub fn is_rgba(&self) -> bool {
matches!(self, Self::Rgba(_))
}
pub fn handles(&self) -> (isize, Option<isize>) {
match *self {
Self::Nv12(handle) | Self::Rgba(handle) => (handle, None),
Self::Planes { gray, color } => (gray, Some(color)),
}
}
fn same_kind(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CardCrop {
pub left: u32,
pub top: u32,
pub width: u32,
pub height: u32,
}
#[cfg(any(feature = "wgpu", feature = "dx12"))]
pub(crate) fn part_of(
crop: Option<CardCrop>,
picture: (u32, u32),
(wide, high): (u32, u32),
rgba: bool,
min: [f32; 2],
max: [f32; 2],
) -> ([f32; 2], [f32; 2]) {
let crop = crop.unwrap_or(CardCrop {
left: 0,
top: 0,
width: picture.0.min(wide),
height: picture.1.min(high),
});
if (crop.left, crop.top, crop.width, crop.height) == (0, 0, wide, high) || wide == 0 || high == 0 {
return (min, max);
}
let within = if rgba { 0.5 } else { 1.0 };
let edge = |at: u32, end: u32| if at > 0 && at < end { within } else { 0.0 };
let (left, top) = (
crop.left as f32 + edge(crop.left, wide),
crop.top as f32 + edge(crop.top, high),
);
let (right, bottom) = (crop.left + crop.width, crop.top + crop.height);
let (right, bottom) = (right as f32 - edge(right, wide), bottom as f32 - edge(bottom, high));
let (from, to) = (
[left / wide as f32, top / high as f32],
[right / wide as f32, bottom / high as f32],
);
let at = |part: [f32; 2]| {
[
from[0] + part[0] * (to[0] - from[0]),
from[1] + part[1] * (to[1] - from[1]),
]
};
(at(min), at(max))
}
pub type CardLease = Arc<dyn Any + Send + Sync>;
#[derive(Clone)]
pub struct CardFrame {
pub(crate) surface: usize,
pub(crate) crop: Option<CardCrop>,
pub(crate) ready: Option<u64>,
pub(crate) lease: Option<CardLease>,
pub(crate) colors: YuvColors,
}
impl CardFrame {
pub fn of(surface: usize) -> Self {
Self {
surface,
crop: None,
ready: None,
lease: None,
colors: YuvColors::BT709,
}
}
pub fn crop(mut self, crop: CardCrop) -> Self {
self.crop = Some(crop);
self
}
pub fn ready_at(mut self, value: u64) -> Self {
self.ready = Some(value);
self
}
pub fn lease(mut self, lease: impl Any + Send + Sync) -> Self {
self.lease = Some(Arc::new(lease));
self
}
pub fn colors(mut self, colors: YuvColors) -> Self {
self.colors = colors;
self
}
pub fn surface(&self) -> usize {
self.surface
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CardFramesWay {
Card,
Memory,
Nowhere,
}
#[derive(Clone)]
pub struct CardFrames {
state: Arc<State>,
}
struct State {
surfaces: Vec<CardSurface>,
fence: Option<isize>,
shown: Mutex<Option<(u64, CardFrame)>>,
count: AtomicU64,
wakers: super::FrameWakers,
way: AtomicU8,
}
impl Drop for State {
fn drop(&mut self) {
let close = |handle: isize| {
unsafe {
let _ = windows::Win32::Foundation::CloseHandle(windows::Win32::Foundation::HANDLE(handle as _));
}
};
for surface in &self.surfaces {
let (first, second) = surface.handles();
close(first);
if let Some(second) = second {
close(second);
}
}
if let Some(fence) = self.fence {
close(fence);
}
}
}
impl CardFrames {
pub unsafe fn new(surfaces: Vec<CardSurface>) -> Self {
Self::made(surfaces, None)
}
pub unsafe fn with_fence(surfaces: Vec<CardSurface>, fence: isize) -> Self {
Self::made(surfaces, Some(fence))
}
fn made(surfaces: Vec<CardSurface>, fence: Option<isize>) -> Self {
assert!(
surfaces.windows(2).all(|pair| pair[0].same_kind(&pair[1])),
"the surfaces of a video's frames are all of one kind"
);
Self {
state: Arc::new(State {
surfaces,
fence,
shown: Mutex::new(None),
count: AtomicU64::new(0),
wakers: super::FrameWakers::default(),
way: AtomicU8::new(0),
}),
}
}
pub fn surfaces(&self) -> &[CardSurface] {
&self.state.surfaces
}
pub fn fence(&self) -> Option<isize> {
self.state.fence
}
pub fn is_rgba(&self) -> bool {
self.state.surfaces.first().is_some_and(CardSurface::is_rgba)
}
pub fn show(&self, frame: CardFrame) {
let count = self.state.count.fetch_add(1, Ordering::AcqRel) + 1;
let before = self.state.shown.lock().replace((count, frame));
drop(before);
self.state.wakers.wake();
}
pub(crate) fn wakers(&self) -> &super::FrameWakers {
&self.state.wakers
}
pub fn shown(&self) -> Option<CardFrame> {
self.state.shown.lock().as_ref().map(|(_, frame)| frame.clone())
}
pub(crate) fn count(&self) -> u64 {
self.state.count.load(Ordering::Acquire)
}
pub(crate) fn latest(&self) -> Option<(u64, CardFrame)> {
self.state.shown.lock().clone()
}
pub fn way(&self) -> Option<CardFramesWay> {
match self.state.way.load(Ordering::Acquire) {
1 => Some(CardFramesWay::Card),
2 => Some(CardFramesWay::Memory),
3 => Some(CardFramesWay::Nowhere),
_ => None,
}
}
pub(crate) fn found(&self, way: CardFramesWay) {
let way = match way {
CardFramesWay::Card => 1,
CardFramesWay::Memory => 2,
CardFramesWay::Nowhere => 3,
};
self.state.way.store(way, Ordering::Release);
}
pub(crate) fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.state, &other.state)
}
}
static ADAPTER: RwLock<Option<(u32, i32)>> = RwLock::new(None);
pub fn set_card_frames_adapter(adapter: Option<(u32, i32)>) {
*ADAPTER.write() = adapter;
}
pub fn card_frames_adapter() -> Option<(u32, i32)> {
*ADAPTER.read()
}
static NV12: AtomicBool = AtomicBool::new(true);
pub fn set_card_frames_nv12(drawn: bool) {
NV12.store(drawn, Ordering::Release);
}
pub fn card_frames_nv12() -> bool {
NV12.load(Ordering::Acquire)
}