Introduce backend feature flags in iced_renderer
This commit is contained in:
parent
06bbcc310e
commit
9b4bcd287a
6 changed files with 147 additions and 93 deletions
|
|
@ -1,17 +1,21 @@
|
|||
use crate::core::Color;
|
||||
use crate::graphics::compositor::{Information, SurfaceError};
|
||||
use crate::graphics::{Error, Viewport};
|
||||
use crate::{Backend, Renderer, Settings};
|
||||
use crate::{Renderer, Settings};
|
||||
|
||||
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
|
||||
|
||||
pub enum 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 {
|
||||
#[cfg(feature = "wgpu")]
|
||||
Wgpu(iced_wgpu::window::Surface),
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
TinySkia(iced_tiny_skia::window::Surface),
|
||||
}
|
||||
|
||||
|
|
@ -22,32 +26,65 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
|
|||
|
||||
fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
|
||||
settings: Self::Settings,
|
||||
_compatible_window: Option<&W>,
|
||||
compatible_window: Option<&W>,
|
||||
) -> Result<(Self, Self::Renderer), Error> {
|
||||
//let (compositor, backend) = iced_wgpu::window::compositor::new(
|
||||
// iced_wgpu::Settings {
|
||||
// default_font: settings.default_font,
|
||||
// default_text_size: settings.default_text_size,
|
||||
// antialiasing: settings.antialiasing,
|
||||
// ..iced_wgpu::Settings::from_env()
|
||||
// },
|
||||
// compatible_window,
|
||||
//)?;
|
||||
#[cfg(feature = "wgpu")]
|
||||
let new_wgpu = |settings: Self::Settings, compatible_window| {
|
||||
let (compositor, backend) = iced_wgpu::window::compositor::new(
|
||||
iced_wgpu::Settings {
|
||||
default_font: settings.default_font,
|
||||
default_text_size: settings.default_text_size,
|
||||
antialiasing: settings.antialiasing,
|
||||
..iced_wgpu::Settings::from_env()
|
||||
},
|
||||
compatible_window,
|
||||
)?;
|
||||
|
||||
//Ok((
|
||||
// Self::Wgpu(compositor),
|
||||
// Renderer::new(Backend::Wgpu(backend)),
|
||||
//))
|
||||
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((
|
||||
Self::Wgpu(compositor),
|
||||
Renderer::new(crate::Backend::Wgpu(backend)),
|
||||
))
|
||||
};
|
||||
|
||||
Ok((
|
||||
Self::TinySkia(compositor),
|
||||
Renderer::new(Backend::TinySkia(backend)),
|
||||
))
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
let new_tiny_skia = |settings: Self::Settings, _compatible_window| {
|
||||
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((
|
||||
Self::TinySkia(compositor),
|
||||
Renderer::new(crate::Backend::TinySkia(backend)),
|
||||
))
|
||||
};
|
||||
|
||||
let fail = |_, _| Err(Error::GraphicsAdapterNotFound);
|
||||
|
||||
let candidates = &[
|
||||
#[cfg(feature = "wgpu")]
|
||||
new_wgpu,
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
new_tiny_skia,
|
||||
fail,
|
||||
];
|
||||
|
||||
let mut error = Error::GraphicsAdapterNotFound;
|
||||
|
||||
for candidate in candidates {
|
||||
match candidate(settings, compatible_window) {
|
||||
Ok((compositor, renderer)) => {
|
||||
return Ok((compositor, renderer))
|
||||
}
|
||||
Err(new_error) => {
|
||||
error = new_error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(error)
|
||||
}
|
||||
|
||||
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
|
||||
|
|
@ -57,9 +94,11 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
|
|||
height: u32,
|
||||
) -> Surface {
|
||||
match self {
|
||||
#[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,19 +112,26 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
|
|||
height: u32,
|
||||
) {
|
||||
match (self, surface) {
|
||||
#[cfg(feature = "wgpu")]
|
||||
(Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
|
||||
compositor.configure_surface(surface, width, height);
|
||||
}
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
(Self::TinySkia(compositor), Surface::TinySkia(surface)) => {
|
||||
compositor.configure_surface(surface, width, height);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
#[allow(unreachable_patterns)]
|
||||
_ => panic!(
|
||||
"The provided surface is not compatible with the compositor."
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_information(&self) -> Information {
|
||||
match self {
|
||||
#[cfg(feature = "wgpu")]
|
||||
Self::Wgpu(compositor) => compositor.fetch_information(),
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
Self::TinySkia(compositor) => compositor.fetch_information(),
|
||||
}
|
||||
}
|
||||
|
|
@ -100,9 +146,10 @@ 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),
|
||||
Backend::Wgpu(backend),
|
||||
crate::Backend::Wgpu(backend),
|
||||
Surface::Wgpu(surface),
|
||||
) => iced_wgpu::window::compositor::present(
|
||||
compositor,
|
||||
|
|
@ -113,9 +160,10 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
|
|||
background_color,
|
||||
overlay,
|
||||
),
|
||||
#[cfg(feature = "tiny-skia")]
|
||||
(
|
||||
Self::TinySkia(compositor),
|
||||
Backend::TinySkia(backend),
|
||||
crate::Backend::TinySkia(backend),
|
||||
Surface::TinySkia(surface),
|
||||
) => iced_tiny_skia::window::compositor::present(
|
||||
compositor,
|
||||
|
|
@ -126,7 +174,11 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
|
|||
background_color,
|
||||
overlay,
|
||||
),
|
||||
_ => unreachable!(),
|
||||
#[allow(unreachable_patterns)]
|
||||
_ => panic!(
|
||||
"The provided renderer or surface are not compatible \
|
||||
with the compositor."
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue