2020-09-08 00:35:17 +02:00
|
|
|
use crate::{Color, Error, Viewport};
|
2020-05-20 20:28:35 +02:00
|
|
|
use iced_native::mouse;
|
2020-02-09 03:25:13 +01:00
|
|
|
use raw_window_handle::HasRawWindowHandle;
|
|
|
|
|
|
2020-05-19 20:01:55 +02:00
|
|
|
/// A graphics compositor that can draw to windows.
|
|
|
|
|
pub trait Compositor: Sized {
|
2020-02-09 03:25:13 +01:00
|
|
|
/// The settings of the backend.
|
2020-05-21 04:27:31 +02:00
|
|
|
type Settings: Default;
|
2020-02-09 03:25:13 +01:00
|
|
|
|
|
|
|
|
/// The iced renderer of the backend.
|
2020-05-20 20:28:35 +02:00
|
|
|
type Renderer: iced_native::Renderer;
|
2020-02-09 03:25:13 +01:00
|
|
|
|
|
|
|
|
/// The surface of the backend.
|
|
|
|
|
type Surface;
|
|
|
|
|
|
|
|
|
|
/// The swap chain of the backend.
|
|
|
|
|
type SwapChain;
|
|
|
|
|
|
2020-11-25 07:11:27 +01:00
|
|
|
/// Creates a new [`Compositor`].
|
2021-05-05 14:33:03 +07:00
|
|
|
fn new<W: HasRawWindowHandle>(
|
|
|
|
|
settings: Self::Settings,
|
|
|
|
|
compatible_window: Option<&W>,
|
|
|
|
|
) -> Result<(Self, Self::Renderer), Error>;
|
2020-02-09 03:25:13 +01:00
|
|
|
|
|
|
|
|
/// Crates a new [`Surface`] for the given window.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Surface`]: Self::Surface
|
2020-02-09 03:25:13 +01:00
|
|
|
fn create_surface<W: HasRawWindowHandle>(
|
|
|
|
|
&mut self,
|
|
|
|
|
window: &W,
|
|
|
|
|
) -> Self::Surface;
|
|
|
|
|
|
|
|
|
|
/// Crates a new [`SwapChain`] for the given [`Surface`].
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`SwapChain`]: Self::SwapChain
|
|
|
|
|
/// [`Surface`]: Self::Surface
|
2020-02-09 03:25:13 +01:00
|
|
|
fn create_swap_chain(
|
|
|
|
|
&mut self,
|
|
|
|
|
surface: &Self::Surface,
|
|
|
|
|
width: u32,
|
|
|
|
|
height: u32,
|
|
|
|
|
) -> Self::SwapChain;
|
|
|
|
|
|
|
|
|
|
/// Draws the output primitives to the next frame of the given [`SwapChain`].
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`SwapChain`]: Self::SwapChain
|
2020-02-09 03:25:13 +01:00
|
|
|
fn draw<T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
renderer: &mut Self::Renderer,
|
|
|
|
|
swap_chain: &mut Self::SwapChain,
|
2020-05-20 20:28:35 +02:00
|
|
|
viewport: &Viewport,
|
2020-06-12 22:12:15 +02:00
|
|
|
background_color: Color,
|
2020-05-20 20:28:35 +02:00
|
|
|
output: &<Self::Renderer as iced_native::Renderer>::Output,
|
2020-02-09 03:25:13 +01:00
|
|
|
overlay: &[T],
|
2021-07-22 13:08:13 -05:00
|
|
|
) -> Result<mouse::Interaction, CompositorDrawError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Result of an unsuccessful call to [`Compositor::draw`].
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum CompositorDrawError {
|
|
|
|
|
/// The swapchain is outdated. Try rendering again next frame.
|
|
|
|
|
SwapchainOutdated(Box<dyn std::error::Error>),
|
|
|
|
|
/// A fatal swapchain error occured. Rendering cannot continue.
|
|
|
|
|
FatalSwapchainError(Box<dyn std::error::Error>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::error::Error for CompositorDrawError {}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for CompositorDrawError {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
CompositorDrawError::SwapchainOutdated(e) => write!(
|
|
|
|
|
f,
|
|
|
|
|
"Swapchain is outdated: {}. Try rendering next frame.",
|
|
|
|
|
e
|
|
|
|
|
),
|
|
|
|
|
CompositorDrawError::FatalSwapchainError(e) => write!(
|
|
|
|
|
f,
|
|
|
|
|
"Fatal swapchain error: {}. Rendering cannot continue.",
|
|
|
|
|
e
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-09 03:25:13 +01:00
|
|
|
}
|