use std::sync::Arc;
use mln_style::error::Error;
use super::shared::{Published, SessionShared, with_unlocked_wakes};
use super::{RenderSessionState, ThreadEnd};
use crate::render::frame::FrameResult;
use crate::render::target::{ConsumerSync, FrameTexture, GpuSync, SyncKind};
use crate::render::worker;
pub struct AcquiredFrame {
pub(super) shared: Arc<SessionShared>,
pub(super) thread_end: ThreadEnd,
pub(super) slot: usize,
pub(super) frame: Published,
pub(super) done: bool,
}
impl std::fmt::Debug for AcquiredFrame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AcquiredFrame").field("slot", &self.slot).field("frame", &self.frame).finish_non_exhaustive()
}
}
impl AcquiredFrame {
#[must_use]
pub fn slot(&self) -> usize {
self.slot
}
#[must_use]
pub fn result(&self) -> &FrameResult {
&self.frame.result
}
#[must_use]
pub fn producer_sync(&self) -> GpuSync {
self.frame.sync
}
#[must_use]
pub fn texture(&self) -> Option<&FrameTexture> {
self.frame.texture.as_ref()
}
#[must_use]
pub fn accepts(&self, kind: SyncKind) -> bool {
self.shared.capabilities.consumer_sync_kinds.contains(kind)
}
pub fn view(&self) -> Result<FrameView, Error> {
let mut state = self.shared.state.lock();
if !matches!(
state.lifecycle,
RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
) {
return Err(Error::target_lost("the render session failed, or was detached, abandoned, or disposed"));
}
state.views += 1;
Ok(FrameView { shared: Arc::clone(&self.shared), _thread_end: self.thread_end.clone() })
}
#[must_use]
pub fn session_live(&self) -> bool {
matches!(
self.shared.state.lock().lifecycle,
RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
)
}
#[expect(
clippy::result_large_err,
reason = "the error hands back the frame and the consumer's sync, so that the caller can release it another way"
)]
pub fn release(mut self, consumer: ConsumerSync) -> Result<(), (AcquiredFrame, ConsumerSync, Error)> {
if !self.shared.capabilities.consumer_sync_kinds.contains(consumer.sync.kind) {
return Err((self, consumer, Error::unsupported("render backend does not support this gpu sync kind")));
}
self.done = true;
give_back(&self.shared, self.slot, Some(consumer));
Ok(())
}
pub fn discard(mut self) {
self.done = true;
give_back(&self.shared, self.slot, None);
}
}
pub struct FrameView {
shared: Arc<SessionShared>,
_thread_end: ThreadEnd,
}
impl std::fmt::Debug for FrameView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FrameView").finish_non_exhaustive()
}
}
impl Drop for FrameView {
fn drop(&mut self) {
let release = {
let mut state = self.shared.state.lock();
state.views = state.views.saturating_sub(1);
state.views == 0 && std::mem::take(&mut state.viewed_teardown)
};
if release {
worker::post(&self.shared.queue, |worker, _| worker.release_viewed());
}
}
}
impl Drop for AcquiredFrame {
fn drop(&mut self) {
if !self.done {
give_back(&self.shared, self.slot, Some(ConsumerSync::of(GpuSync::of(SyncKind::CpuComplete))));
}
}
}
fn give_back(shared: &Arc<SessionShared>, slot: usize, consumer: Option<ConsumerSync>) {
with_unlocked_wakes(|| {
let mut state = shared.state.lock();
state.acquired_frames = state.acquired_frames.saturating_sub(1);
let live = matches!(
state.lifecycle,
RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
);
if !live {
if let Some(entry) = state.ring.get_mut(slot) {
entry.acquired = false;
entry.quarantined |=
consumer.as_ref().is_none_or(|consumer| consumer.sync.kind != SyncKind::CpuComplete);
}
return;
}
if let Some(sync) = consumer {
worker::post(&shared.queue, move |worker, outbox| worker.release_slot(slot, sync, outbox));
} else {
if let Some(entry) = state.ring.get_mut(slot) {
entry.acquired = false;
entry.quarantined = !entry.retired;
entry.retired = false;
}
worker::post(&shared.queue, move |worker, outbox| worker.discard_slot(slot, outbox));
}
});
}