Bundle tiny-skia backend together with iced_renderer

This commit is contained in:
Héctor Ramón Jiménez 2023-05-10 22:21:31 +02:00
parent 422b4dedcb
commit dd04c0b070
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
7 changed files with 56 additions and 84 deletions

View file

@ -12,11 +12,9 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"] categories = ["gui"]
[features] [features]
default = ["wgpu", "tiny-skia"] default = ["wgpu"]
# Enable the `wgpu` GPU-accelerated renderer backend # Enable the `wgpu` GPU-accelerated renderer backend
wgpu = ["iced_renderer/wgpu"] wgpu = ["iced_renderer/wgpu"]
# Enable the `tiny-skia` software renderer backend
tiny-skia = ["iced_renderer/tiny-skia"]
# Enables the `Image` widget # Enables the `Image` widget
image = ["iced_widget/image", "image_rs"] image = ["iced_widget/image", "image_rs"]
# Enables the `Svg` widget # Enables the `Svg` widget

View file

@ -9,7 +9,7 @@ publish = false
iced_winit = { path = "../../winit" } iced_winit = { path = "../../winit" }
iced_wgpu = { path = "../../wgpu" } iced_wgpu = { path = "../../wgpu" }
iced_widget = { path = "../../widget" } iced_widget = { path = "../../widget" }
iced_renderer = { path = "../../renderer", features = ["wgpu", "tiny-skia"] } iced_renderer = { path = "../../renderer", features = ["wgpu"] }
env_logger = "0.10" env_logger = "0.10"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]

View file

@ -5,11 +5,10 @@ edition = "2021"
[features] [features]
wgpu = ["iced_wgpu"] wgpu = ["iced_wgpu"]
tiny-skia = ["iced_tiny_skia"] image = ["iced_tiny_skia/image", "iced_wgpu?/image"]
image = ["iced_wgpu/image", "iced_tiny_skia/image"] svg = ["iced_tiny_skia/svg", "iced_wgpu?/svg"]
svg = ["iced_wgpu/svg", "iced_tiny_skia/svg"] geometry = ["iced_graphics/geometry", "iced_tiny_skia/geometry", "iced_wgpu?/geometry"]
geometry = ["iced_graphics/geometry", "iced_wgpu?/geometry", "iced_tiny_skia?/geometry"] tracing = ["iced_wgpu?/tracing"]
tracing = ["iced_wgpu/tracing"]
[dependencies] [dependencies]
raw-window-handle = "0.5" raw-window-handle = "0.5"
@ -19,12 +18,11 @@ thiserror = "1"
version = "0.8" version = "0.8"
path = "../graphics" path = "../graphics"
[dependencies.iced_tiny_skia]
version = "0.1"
path = "../tiny_skia"
[dependencies.iced_wgpu] [dependencies.iced_wgpu]
version = "0.10" version = "0.10"
path = "../wgpu" path = "../wgpu"
optional = true optional = true
[dependencies.iced_tiny_skia]
version = "0.1"
path = "../tiny_skia"
optional = true

View file

@ -6,19 +6,17 @@ use std::borrow::Cow;
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum Backend { pub enum Backend {
TinySkia(iced_tiny_skia::Backend),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Wgpu(iced_wgpu::Backend), Wgpu(iced_wgpu::Backend),
#[cfg(feature = "tiny-skia")]
TinySkia(iced_tiny_skia::Backend),
} }
macro_rules! delegate { macro_rules! delegate {
($backend:expr, $name:ident, $body:expr) => { ($backend:expr, $name:ident, $body:expr) => {
match $backend { match $backend {
Self::TinySkia($name) => $body,
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu($name) => $body, Self::Wgpu($name) => $body,
#[cfg(feature = "tiny-skia")]
Self::TinySkia($name) => $body,
} }
}; };
} }

View file

@ -7,17 +7,15 @@ use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::env; use std::env;
pub enum Compositor<Theme> { pub enum Compositor<Theme> {
TinySkia(iced_tiny_skia::window::Compositor<Theme>),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Wgpu(iced_wgpu::window::Compositor<Theme>), Wgpu(iced_wgpu::window::Compositor<Theme>),
#[cfg(feature = "tiny-skia")]
TinySkia(iced_tiny_skia::window::Compositor<Theme>),
} }
pub enum Surface { pub enum Surface {
TinySkia(iced_tiny_skia::window::Surface),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Wgpu(iced_wgpu::window::Surface), Wgpu(iced_wgpu::window::Surface),
#[cfg(feature = "tiny-skia")]
TinySkia(iced_tiny_skia::window::Surface),
} }
impl<Theme> crate::graphics::Compositor for Compositor<Theme> { impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
@ -55,14 +53,13 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
height: u32, height: u32,
) -> Surface { ) -> Surface {
match self { match self {
Self::TinySkia(compositor) => Surface::TinySkia(
compositor.create_surface(window, width, height),
),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu(compositor) => { Self::Wgpu(compositor) => {
Surface::Wgpu(compositor.create_surface(window, width, height)) 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, height: u32,
) { ) {
match (self, surface) { match (self, surface) {
#[cfg(feature = "wgpu")] (Self::TinySkia(compositor), Surface::TinySkia(surface)) => {
(Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
compositor.configure_surface(surface, width, height); compositor.configure_surface(surface, width, height);
} }
#[cfg(feature = "tiny-skia")] #[cfg(feature = "wgpu")]
(Self::TinySkia(compositor), Surface::TinySkia(surface)) => { (Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
compositor.configure_surface(surface, width, height); compositor.configure_surface(surface, width, height);
} }
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
@ -90,10 +86,9 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
fn fetch_information(&self) -> Information { fn fetch_information(&self) -> Information {
match self { match self {
Self::TinySkia(compositor) => compositor.fetch_information(),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu(compositor) => compositor.fetch_information(), 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> { ) -> Result<(), SurfaceError> {
renderer.with_primitives(|backend, primitives| { renderer.with_primitives(|backend, primitives| {
match (self, backend, surface) { match (self, backend, surface) {
#[cfg(feature = "wgpu")]
( (
Self::Wgpu(compositor), Self::TinySkia(_compositor),
crate::Backend::Wgpu(backend), crate::Backend::TinySkia(backend),
Surface::Wgpu(surface), Surface::TinySkia(surface),
) => iced_wgpu::window::compositor::present( ) => iced_tiny_skia::window::compositor::present(
compositor,
backend, backend,
surface, surface,
primitives, primitives,
@ -121,12 +114,13 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
background_color, background_color,
overlay, overlay,
), ),
#[cfg(feature = "tiny-skia")] #[cfg(feature = "wgpu")]
( (
Self::TinySkia(_compositor), Self::Wgpu(compositor),
crate::Backend::TinySkia(backend), crate::Backend::Wgpu(backend),
Surface::TinySkia(surface), Surface::Wgpu(surface),
) => iced_tiny_skia::window::compositor::present( ) => iced_wgpu::window::compositor::present(
compositor,
backend, backend,
surface, surface,
primitives, primitives,
@ -154,7 +148,6 @@ impl Candidate {
vec![ vec![
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu, Self::Wgpu,
#[cfg(feature = "tiny-skia")]
Self::TinySkia, Self::TinySkia,
] ]
} }
@ -181,6 +174,20 @@ impl Candidate {
_compatible_window: Option<&W>, _compatible_window: Option<&W>,
) -> Result<(Compositor<Theme>, Renderer<Theme>), Error> { ) -> Result<(Compositor<Theme>, Renderer<Theme>), Error> {
match self { 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")] #[cfg(feature = "wgpu")]
Self::Wgpu => { Self::Wgpu => {
let (compositor, backend) = iced_wgpu::window::compositor::new( let (compositor, backend) = iced_wgpu::window::compositor::new(
@ -198,31 +205,10 @@ impl Candidate {
Renderer::new(crate::Backend::Wgpu(backend)), 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"))] #[cfg(not(feature = "wgpu"))]
Self::Wgpu => { Self::Wgpu => {
panic!("`wgpu` feature was not enabled in `iced_renderer`") 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`"
);
}
} }
} }
} }

View file

@ -7,19 +7,17 @@ use crate::graphics::geometry::{Fill, Geometry, Path, Stroke, Text};
use crate::Backend; use crate::Backend;
pub enum Frame { pub enum Frame {
TinySkia(iced_tiny_skia::geometry::Frame),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Wgpu(iced_wgpu::geometry::Frame), Wgpu(iced_wgpu::geometry::Frame),
#[cfg(feature = "tiny-skia")]
TinySkia(iced_tiny_skia::geometry::Frame),
} }
macro_rules! delegate { macro_rules! delegate {
($frame:expr, $name:ident, $body:expr) => { ($frame:expr, $name:ident, $body:expr) => {
match $frame { match $frame {
Self::TinySkia($name) => $body,
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu($name) => $body, Self::Wgpu($name) => $body,
#[cfg(feature = "tiny-skia")]
Self::TinySkia($name) => $body,
} }
}; };
} }
@ -27,14 +25,13 @@ macro_rules! delegate {
impl Frame { impl Frame {
pub fn new<Theme>(renderer: &crate::Renderer<Theme>, size: Size) -> Self { pub fn new<Theme>(renderer: &crate::Renderer<Theme>, size: Size) -> Self {
match renderer.backend() { match renderer.backend() {
Backend::TinySkia(_) => {
Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size))
}
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Backend::Wgpu(_) => { Backend::Wgpu(_) => {
Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) Frame::Wgpu(iced_wgpu::geometry::Frame::new(size))
} }
#[cfg(feature = "tiny-skia")]
Backend::TinySkia(_) => {
Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size))
}
} }
} }
@ -127,14 +124,13 @@ impl Frame {
#[inline] #[inline]
pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) {
let mut frame = match self { let mut frame = match self {
Self::TinySkia(_) => Self::TinySkia(
iced_tiny_skia::geometry::Frame::new(region.size()),
),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
Self::Wgpu(_) => { Self::Wgpu(_) => {
Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) 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); f(&mut frame);
@ -142,12 +138,11 @@ impl Frame {
let translation = Vector::new(region.x, region.y); let translation = Vector::new(region.x, region.y);
match (self, frame) { match (self, frame) {
#[cfg(feature = "wgpu")] (Self::TinySkia(target), Self::TinySkia(frame)) => {
(Self::Wgpu(target), Self::Wgpu(frame)) => {
target.clip(frame, translation); target.clip(frame, translation);
} }
#[cfg(feature = "tiny-skia")] #[cfg(feature = "wgpu")]
(Self::TinySkia(target), Self::TinySkia(frame)) => { (Self::Wgpu(target), Self::Wgpu(frame)) => {
target.clip(frame, translation); target.clip(frame, translation);
} }
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]

View file

@ -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; pub mod compositor;
#[cfg(feature = "geometry")] #[cfg(feature = "geometry")]