1//! A compositor is responsible for initializing a renderer and managing window
2//! surfaces.
3use crate::core::Color;
4use crate::futures::{MaybeSend, MaybeSync};
5use crate::{Error, Settings, Viewport};
67use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
8use thiserror::Error;
910use std::borrow::Cow;
1112/// A graphics compositor that can draw to windows.
13pub trait Compositor: Sized {
14/// The iced renderer of the backend.
15type Renderer;
1617/// The surface of the backend.
18type Surface;
1920/// Creates a new [`Compositor`].
21fn new<W: Window + Clone>(
22 settings: Settings,
23 compatible_window: W,
24 ) -> impl Future<Output = Result<Self, Error>> {
25Self::with_backend(settings, compatible_window, None)
26 }
2728/// Creates a new [`Compositor`] with a backend preference.
29 ///
30 /// If the backend does not match the preference, it will return
31 /// [`Error::GraphicsAdapterNotFound`].
32fn with_backend<W: Window + Clone>(
33 _settings: Settings,
34 _compatible_window: W,
35 _backend: Option<&str>,
36 ) -> impl Future<Output = Result<Self, Error>>;
3738/// Creates a [`Self::Renderer`] for the [`Compositor`].
39fn create_renderer(&self) -> Self::Renderer;
4041/// Crates a new [`Surface`] for the given window.
42 ///
43 /// [`Surface`]: Self::Surface
44fn create_surface<W: Window + Clone>(
45&mut self,
46 window: W,
47 width: u32,
48 height: u32,
49 ) -> Self::Surface;
5051/// Configures a new [`Surface`] with the given dimensions.
52 ///
53 /// [`Surface`]: Self::Surface
54fn configure_surface(
55&mut self,
56 surface: &mut Self::Surface,
57 width: u32,
58 height: u32,
59 );
6061/// Returns [`Information`] used by this [`Compositor`].
62fn fetch_information(&self) -> Information;
6364/// Loads a font from its bytes.
65fn load_font(&mut self, font: Cow<'static, [u8]>) {
66crate::text::font_system()
67 .write()
68 .expect("Write to font system")
69 .load_font(font);
70 }
7172/// Presents the [`Renderer`] primitives to the next frame of the given [`Surface`].
73 ///
74 /// [`Renderer`]: Self::Renderer
75 /// [`Surface`]: Self::Surface
76fn present(
77&mut self,
78 renderer: &mut Self::Renderer,
79 surface: &mut Self::Surface,
80 viewport: &Viewport,
81 background_color: Color,
82 on_pre_present: impl FnOnce(),
83 ) -> Result<(), SurfaceError>;
8485/// Screenshots the current [`Renderer`] primitives to an offscreen texture, and returns the bytes of
86 /// the texture ordered as `RGBA` in the `sRGB` color space.
87 ///
88 /// [`Renderer`]: Self::Renderer
89fn screenshot(
90&mut self,
91 renderer: &mut Self::Renderer,
92 viewport: &Viewport,
93 background_color: Color,
94 ) -> Vec<u8>;
95}
9697/// A window that can be used in a [`Compositor`].
98///
99/// This is just a convenient super trait of the `raw-window-handle`
100/// traits.
101pub trait Window:
102 HasWindowHandle + HasDisplayHandle + MaybeSend + MaybeSync + 'static
103{
104}
105106impl<T> Window for T where
107T: HasWindowHandle + HasDisplayHandle + MaybeSend + MaybeSync + 'static
108{
109}
110111/// Defines the default compositor of a renderer.
112pub trait Default {
113/// The compositor of the renderer.
114type Compositor: Compositor<Renderer = Self>;
115}
116117/// Result of an unsuccessful call to [`Compositor::present`].
118#[derive(Clone, PartialEq, Eq, Debug, Error)]
119pub enum SurfaceError {
120/// A timeout was encountered while trying to acquire the next frame.
121#[error("A timeout was encountered while trying to acquire the next frame")]
122Timeout,
123/// The underlying surface has changed, and therefore the surface must be updated.
124#[error(
125"The underlying surface has changed, and therefore the surface must be updated."
126)]
127Outdated,
128/// The swap chain has been lost and needs to be recreated.
129#[error("The surface has been lost and needs to be recreated")]
130Lost,
131/// There is no more memory left to allocate a new frame.
132#[error("There is no more memory left to allocate a new frame")]
133OutOfMemory,
134/// Acquiring a texture failed with a generic error.
135#[error("Acquiring a texture failed with a generic error")]
136Other,
137}
138139/// Contains information about the graphics (e.g. graphics adapter, graphics backend).
140#[derive(Debug)]
141pub struct Information {
142/// Contains the graphics adapter.
143pub adapter: String,
144/// Contains the graphics backend.
145pub backend: String,
146}
147148#[cfg(debug_assertions)]
149impl Compositor for () {
150type Renderer = ();
151type Surface = ();
152153async fn with_backend<W: Window + Clone>(
154 _settings: Settings,
155 _compatible_window: W,
156 _preferred_backend: Option<&str>,
157 ) -> Result<Self, Error> {
158Ok(())
159 }
160161fn create_renderer(&self) -> Self::Renderer {}
162163fn create_surface<W: Window + Clone>(
164&mut self,
165 _window: W,
166 _width: u32,
167 _height: u32,
168 ) -> Self::Surface {
169 }
170171fn configure_surface(
172&mut self,
173 _surface: &mut Self::Surface,
174 _width: u32,
175 _height: u32,
176 ) {
177 }
178179fn load_font(&mut self, _font: Cow<'static, [u8]>) {}
180181fn fetch_information(&self) -> Information {
182 Information {
183 adapter: String::from("Null Renderer"),
184 backend: String::from("Null"),
185 }
186 }
187188fn present(
189&mut self,
190 _renderer: &mut Self::Renderer,
191 _surface: &mut Self::Surface,
192 _viewport: &Viewport,
193 _background_color: Color,
194 _on_pre_present: impl FnOnce(),
195 ) -> Result<(), SurfaceError> {
196Ok(())
197 }
198199fn screenshot(
200&mut self,
201 _renderer: &mut Self::Renderer,
202 _viewport: &Viewport,
203 _background_color: Color,
204 ) -> Vec<u8> {
205vec![]
206 }
207}
208209#[cfg(debug_assertions)]
210impl Default for () {
211type Compositor = ();
212}