use super::IDrawingContextImpl;
use crate::rendering::composition::CompositionTransparencyLevel;
use crate::{PixelSize, RenderTargetCorruptedException, RenderTargetNotReadyException, Size};
use std::any::Any;
use std::error::Error;
use std::fmt;
#[derive(Clone, Debug)]
pub enum RenderTargetError {
NotReady(RenderTargetNotReadyException),
Corrupted(RenderTargetCorruptedException),
}
impl fmt::Display for RenderTargetError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RenderTargetError::NotReady(error) => error.fmt(f),
RenderTargetError::Corrupted(error) => error.fmt(f),
}
}
}
impl Error for RenderTargetError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
RenderTargetError::NotReady(error) => Some(error),
RenderTargetError::Corrupted(error) => Some(error),
}
}
}
impl From<RenderTargetNotReadyException> for RenderTargetError {
fn from(error: RenderTargetNotReadyException) -> Self {
RenderTargetError::NotReady(error)
}
}
impl From<RenderTargetCorruptedException> for RenderTargetError {
fn from(error: RenderTargetCorruptedException) -> Self {
RenderTargetError::Corrupted(error)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PlatformRenderTargetState {
pub is_ready: bool,
pub will_wake_up_render_loop_when_ready: bool,
pub is_corrupted: bool,
}
impl PlatformRenderTargetState {
pub const READY: Self = Self { is_ready: true, will_wake_up_render_loop_when_ready: false, is_corrupted: false };
pub const NOT_READY_TRY_LATER: Self =
Self { is_ready: false, will_wake_up_render_loop_when_ready: false, is_corrupted: false };
pub const CORRUPTED: Self = Self { is_ready: true, will_wake_up_render_loop_when_ready: false, is_corrupted: true };
pub const DISPOSED: Self = Self { is_ready: false, will_wake_up_render_loop_when_ready: false, is_corrupted: true };
pub const NOT_READY_WILL_WAKEUP_RENDER_LOOP: Self =
Self { is_ready: false, will_wake_up_render_loop_when_ready: true, is_corrupted: false };
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RenderTargetProperties {
pub retains_previous_frame_contents: bool,
pub is_suitable_for_direct_rendering: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RenderTargetDrawingContextProperties {
pub previous_frame_is_retained: bool,
}
#[derive(Clone)]
pub struct RenderTargetSceneInfo {
pub size: PixelSize,
pub scaling: f64,
pub logical_size: Size,
pub transparency_level: CompositionTransparencyLevel,
pub platform_specific_scene_info: Option<std::sync::Arc<dyn Any + Send + Sync>>,
}
impl RenderTargetSceneInfo {
pub fn new(size: PixelSize, scaling: f64, transparency_level: CompositionTransparencyLevel) -> Self {
Self {
size,
scaling,
logical_size: size.to_size(scaling),
transparency_level,
platform_specific_scene_info: None,
}
}
}
pub trait IRenderTarget {
fn properties(&self) -> RenderTargetProperties;
fn create_drawing_context(
&self,
scene_info: &RenderTargetSceneInfo,
) -> (Box<dyn IDrawingContextImpl>, RenderTargetDrawingContextProperties);
fn try_create_drawing_context(
&self,
scene_info: &RenderTargetSceneInfo,
) -> Result<(Box<dyn IDrawingContextImpl>, RenderTargetDrawingContextProperties), RenderTargetError> {
Ok(self.create_drawing_context(scene_info))
}
fn platform_render_target_state(&self) -> PlatformRenderTargetState {
PlatformRenderTargetState::READY
}
fn dispose(&self);
}