use crate::{ImportedTexture, InteropError, NativeFrame};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SyncMechanism {
None,
ImplicitGlFlush,
ExplicitExternalSemaphore,
ExplicitFence,
}
pub trait InteropSynchronizer {
fn producer_complete(
&self,
frame: &NativeFrame,
mechanism: SyncMechanism,
) -> Result<(), InteropError>;
fn consumer_ready(
&self,
texture: &ImportedTexture,
mechanism: SyncMechanism,
) -> Result<(), InteropError>;
}
#[derive(Default)]
pub struct NoopSynchronizer;
impl InteropSynchronizer for NoopSynchronizer {
fn producer_complete(
&self,
_frame: &NativeFrame,
_mechanism: SyncMechanism,
) -> Result<(), InteropError> {
Ok(())
}
fn consumer_ready(
&self,
_texture: &ImportedTexture,
_mechanism: SyncMechanism,
) -> Result<(), InteropError> {
Ok(())
}
}
#[derive(Default)]
pub struct ImplicitOnlySynchronizer;
impl InteropSynchronizer for ImplicitOnlySynchronizer {
fn producer_complete(
&self,
_frame: &NativeFrame,
mechanism: SyncMechanism,
) -> Result<(), InteropError> {
Self::validate(mechanism)
}
fn consumer_ready(
&self,
_texture: &ImportedTexture,
mechanism: SyncMechanism,
) -> Result<(), InteropError> {
Self::validate(mechanism)
}
}
impl ImplicitOnlySynchronizer {
pub(crate) fn validate(mechanism: SyncMechanism) -> Result<(), InteropError> {
match mechanism {
SyncMechanism::None | SyncMechanism::ImplicitGlFlush => Ok(()),
other => Err(InteropError::UnsupportedSynchronization(other)),
}
}
}