diff options
Diffstat (limited to 'renderer')
-rw-r--r-- | renderer/Cargo.toml | 19 | ||||
-rw-r--r-- | renderer/src/backend.rs | 12 | ||||
-rw-r--r-- | renderer/src/compositor.rs | 110 | ||||
-rw-r--r-- | renderer/src/geometry.rs | 31 | ||||
-rw-r--r-- | renderer/src/lib.rs | 3 |
5 files changed, 87 insertions, 88 deletions
diff --git a/renderer/Cargo.toml b/renderer/Cargo.toml index 17657888..ddfb6445 100644 --- a/renderer/Cargo.toml +++ b/renderer/Cargo.toml @@ -5,11 +5,11 @@ edition = "2021" [features] wgpu = ["iced_wgpu"] -tiny-skia = ["iced_tiny_skia"] -image = ["iced_wgpu/image", "iced_tiny_skia/image"] -svg = ["iced_wgpu/svg", "iced_tiny_skia/svg"] -geometry = ["iced_graphics/geometry", "iced_wgpu?/geometry", "iced_tiny_skia?/geometry"] -tracing = ["iced_wgpu/tracing"] +image = ["iced_tiny_skia/image", "iced_wgpu?/image"] +svg = ["iced_tiny_skia/svg", "iced_wgpu?/svg"] +geometry = ["iced_graphics/geometry", "iced_tiny_skia/geometry", "iced_wgpu?/geometry"] +tracing = ["iced_wgpu?/tracing"] +web-colors = ["iced_wgpu?/web-colors"] [dependencies] raw-window-handle = "0.5" @@ -19,12 +19,11 @@ thiserror = "1" version = "0.8" path = "../graphics" -[dependencies.iced_wgpu] -version = "0.10" -path = "../wgpu" -optional = true - [dependencies.iced_tiny_skia] version = "0.1" path = "../tiny_skia" + +[dependencies.iced_wgpu] +version = "0.10" +path = "../wgpu" optional = true diff --git a/renderer/src/backend.rs b/renderer/src/backend.rs index c1bec6af..18f9f3fc 100644 --- a/renderer/src/backend.rs +++ b/renderer/src/backend.rs @@ -6,29 +6,21 @@ use std::borrow::Cow; #[allow(clippy::large_enum_variant)] pub enum Backend { + TinySkia(iced_tiny_skia::Backend), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::Backend), - #[cfg(feature = "tiny-skia")] - TinySkia(iced_tiny_skia::Backend), } macro_rules! delegate { ($backend:expr, $name:ident, $body:expr) => { match $backend { + Self::TinySkia($name) => $body, #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, - #[cfg(feature = "tiny-skia")] - Self::TinySkia($name) => $body, } }; } -impl iced_graphics::Backend for Backend { - fn trim_measurements(&mut self) { - delegate!(self, backend, backend.trim_measurements()); - } -} - impl backend::Text for Backend { const ICON_FONT: Font = Font::with_name("Iced-Icons"); const CHECKMARK_ICON: char = '\u{f00c}'; diff --git a/renderer/src/compositor.rs b/renderer/src/compositor.rs index 48ed4b1f..57317b28 100644 --- a/renderer/src/compositor.rs +++ b/renderer/src/compositor.rs @@ -7,17 +7,15 @@ use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; use std::env; pub enum Compositor<Theme> { + TinySkia(iced_tiny_skia::window::Compositor<Theme>), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::window::Compositor<Theme>), - #[cfg(feature = "tiny-skia")] - TinySkia(iced_tiny_skia::window::Compositor<Theme>), } pub enum Surface { + TinySkia(iced_tiny_skia::window::Surface), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::window::Surface), - #[cfg(feature = "tiny-skia")] - TinySkia(iced_tiny_skia::window::Surface), } impl<Theme> crate::graphics::Compositor for Compositor<Theme> { @@ -55,14 +53,13 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { height: u32, ) -> Surface { match self { + Self::TinySkia(compositor) => Surface::TinySkia( + compositor.create_surface(window, width, height), + ), #[cfg(feature = "wgpu")] Self::Wgpu(compositor) => { Surface::Wgpu(compositor.create_surface(window, width, height)) } - #[cfg(feature = "tiny-skia")] - Self::TinySkia(compositor) => Surface::TinySkia( - compositor.create_surface(window, width, height), - ), } } @@ -73,12 +70,11 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { height: u32, ) { match (self, surface) { - #[cfg(feature = "wgpu")] - (Self::Wgpu(compositor), Surface::Wgpu(surface)) => { + (Self::TinySkia(compositor), Surface::TinySkia(surface)) => { compositor.configure_surface(surface, width, height); } - #[cfg(feature = "tiny-skia")] - (Self::TinySkia(compositor), Surface::TinySkia(surface)) => { + #[cfg(feature = "wgpu")] + (Self::Wgpu(compositor), Surface::Wgpu(surface)) => { compositor.configure_surface(surface, width, height); } #[allow(unreachable_patterns)] @@ -90,10 +86,9 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { fn fetch_information(&self) -> Information { match self { + Self::TinySkia(compositor) => compositor.fetch_information(), #[cfg(feature = "wgpu")] Self::Wgpu(compositor) => compositor.fetch_information(), - #[cfg(feature = "tiny-skia")] - Self::TinySkia(compositor) => compositor.fetch_information(), } } @@ -107,13 +102,11 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { ) -> Result<(), SurfaceError> { renderer.with_primitives(|backend, primitives| { match (self, backend, surface) { - #[cfg(feature = "wgpu")] ( - Self::Wgpu(compositor), - crate::Backend::Wgpu(backend), - Surface::Wgpu(surface), - ) => iced_wgpu::window::compositor::present( - compositor, + Self::TinySkia(_compositor), + crate::Backend::TinySkia(backend), + Surface::TinySkia(surface), + ) => iced_tiny_skia::window::compositor::present( backend, surface, primitives, @@ -121,12 +114,13 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { background_color, overlay, ), - #[cfg(feature = "tiny-skia")] + #[cfg(feature = "wgpu")] ( - Self::TinySkia(_compositor), - crate::Backend::TinySkia(backend), - Surface::TinySkia(surface), - ) => iced_tiny_skia::window::compositor::present( + Self::Wgpu(compositor), + crate::Backend::Wgpu(backend), + Surface::Wgpu(surface), + ) => iced_wgpu::window::compositor::present( + compositor, backend, surface, primitives, @@ -142,6 +136,36 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { } }) } + + fn screenshot<T: AsRef<str>>( + &mut self, + renderer: &mut Self::Renderer, + surface: &mut Self::Surface, + viewport: &Viewport, + background_color: Color, + overlay: &[T], + ) -> Vec<u8> { + renderer.with_primitives(|backend, primitives| match (self, backend, surface) { + (Self::TinySkia(_compositor), crate::Backend::TinySkia(backend), Surface::TinySkia(surface)) => { + iced_tiny_skia::window::compositor::screenshot(surface, backend, primitives, viewport, background_color, overlay) + }, + #[cfg(feature = "wgpu")] + (Self::Wgpu(compositor), crate::Backend::Wgpu(backend), Surface::Wgpu(_)) => { + iced_wgpu::window::compositor::screenshot( + compositor, + backend, + primitives, + viewport, + background_color, + overlay, + ) + }, + #[allow(unreachable_patterns)] + _ => panic!( + "The provided renderer or backend are not compatible with the compositor." + ), + }) + } } enum Candidate { @@ -154,7 +178,6 @@ impl Candidate { vec![ #[cfg(feature = "wgpu")] Self::Wgpu, - #[cfg(feature = "tiny-skia")] Self::TinySkia, ] } @@ -181,6 +204,20 @@ impl Candidate { _compatible_window: Option<&W>, ) -> Result<(Compositor<Theme>, Renderer<Theme>), Error> { match self { + Self::TinySkia => { + let (compositor, backend) = + iced_tiny_skia::window::compositor::new( + iced_tiny_skia::Settings { + default_font: settings.default_font, + default_text_size: settings.default_text_size, + }, + ); + + Ok(( + Compositor::TinySkia(compositor), + Renderer::new(crate::Backend::TinySkia(backend)), + )) + } #[cfg(feature = "wgpu")] Self::Wgpu => { let (compositor, backend) = iced_wgpu::window::compositor::new( @@ -198,31 +235,10 @@ impl Candidate { Renderer::new(crate::Backend::Wgpu(backend)), )) } - #[cfg(feature = "tiny-skia")] - Self::TinySkia => { - let (compositor, backend) = - iced_tiny_skia::window::compositor::new( - iced_tiny_skia::Settings { - default_font: settings.default_font, - default_text_size: settings.default_text_size, - }, - ); - - Ok(( - Compositor::TinySkia(compositor), - Renderer::new(crate::Backend::TinySkia(backend)), - )) - } #[cfg(not(feature = "wgpu"))] Self::Wgpu => { panic!("`wgpu` feature was not enabled in `iced_renderer`") } - #[cfg(not(feature = "tiny-skia"))] - Self::TinySkia => { - panic!( - "`tiny-skia` feature was not enabled in `iced_renderer`" - ); - } } } } diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index 21ef2c06..26e2fed0 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -7,19 +7,17 @@ use crate::graphics::geometry::{Fill, Geometry, Path, Stroke, Text}; use crate::Backend; pub enum Frame { + TinySkia(iced_tiny_skia::geometry::Frame), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::geometry::Frame), - #[cfg(feature = "tiny-skia")] - TinySkia(iced_tiny_skia::geometry::Frame), } macro_rules! delegate { ($frame:expr, $name:ident, $body:expr) => { match $frame { + Self::TinySkia($name) => $body, #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, - #[cfg(feature = "tiny-skia")] - Self::TinySkia($name) => $body, } }; } @@ -27,14 +25,13 @@ macro_rules! delegate { impl Frame { pub fn new<Theme>(renderer: &crate::Renderer<Theme>, size: Size) -> Self { match renderer.backend() { + Backend::TinySkia(_) => { + Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) + } #[cfg(feature = "wgpu")] Backend::Wgpu(_) => { Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) } - #[cfg(feature = "tiny-skia")] - Backend::TinySkia(_) => { - Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) - } } } @@ -127,28 +124,26 @@ impl Frame { #[inline] pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { let mut frame = match self { + Self::TinySkia(_) => Self::TinySkia( + iced_tiny_skia::geometry::Frame::new(region.size()), + ), #[cfg(feature = "wgpu")] Self::Wgpu(_) => { Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) } - #[cfg(feature = "tiny-skia")] - Self::TinySkia(_) => Self::TinySkia( - iced_tiny_skia::geometry::Frame::new(region.size()), - ), }; f(&mut frame); - let translation = Vector::new(region.x, region.y); + let origin = Point::new(region.x, region.y); match (self, frame) { + (Self::TinySkia(target), Self::TinySkia(frame)) => { + target.clip(frame, origin); + } #[cfg(feature = "wgpu")] (Self::Wgpu(target), Self::Wgpu(frame)) => { - target.clip(frame, translation); - } - #[cfg(feature = "tiny-skia")] - (Self::TinySkia(target), Self::TinySkia(frame)) => { - target.clip(frame, translation); + target.clip(frame, origin); } #[allow(unreachable_patterns)] _ => unreachable!(), diff --git a/renderer/src/lib.rs b/renderer/src/lib.rs index ba737b11..22ec7bd1 100644 --- a/renderer/src/lib.rs +++ b/renderer/src/lib.rs @@ -1,6 +1,3 @@ -#[cfg(not(any(feature = "wgpu", feature = "tiny-skia")))] -compile_error!("No backend selected. Enable at least one backend feature: `wgpu` or `tiny-skia`."); - pub mod compositor; #[cfg(feature = "geometry")] |