Introduce iced_renderer subcrate featuring runtime renderer fallback

This commit is contained in:
Héctor Ramón Jiménez 2023-02-24 23:24:48 +01:00
parent 368cadd25a
commit 5100b5d0a1
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
16 changed files with 371 additions and 72 deletions

View file

@ -13,9 +13,9 @@ categories = ["gui"]
[features]
# Enables the `Image` widget
image = ["iced_wgpu/image", "image_rs"]
image = ["iced_renderer/image", "image_rs"]
# Enables the `Svg` widget
svg = ["iced_wgpu/svg"]
svg = ["iced_renderer/svg"]
# Enables the `Canvas` widget
canvas = ["iced_graphics/canvas"]
# Enables the `QRCode` widget
@ -35,7 +35,7 @@ system = ["iced_winit/system"]
# Enables chrome traces
chrome-trace = [
"iced_winit/chrome-trace",
"iced_wgpu/tracing",
"iced_renderer/tracing",
]
[badges]
@ -48,6 +48,7 @@ members = [
"graphics",
"lazy",
"native",
"renderer",
"style",
"wgpu",
"winit",
@ -59,6 +60,7 @@ iced_core = { version = "0.8", path = "core" }
iced_futures = { version = "0.6", path = "futures" }
iced_native = { version = "0.9", path = "native" }
iced_graphics = { version = "0.7", path = "graphics" }
iced_renderer = { version = "0.1", path = "renderer" }
iced_winit = { version = "0.8", path = "winit", features = ["application"] }
thiserror = "1.0"
@ -67,12 +69,6 @@ version = "0.24"
package = "image"
optional = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced_wgpu = { version = "0.9", path = "wgpu" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
iced_wgpu = { version = "0.9", path = "wgpu", features = ["webgl"] }
[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
features = ["image", "svg", "canvas", "qr_code"]

View file

@ -46,9 +46,9 @@ pub use primitive::Primitive;
pub use renderer::Renderer;
pub use transformation::Transformation;
pub use viewport::Viewport;
pub use window::compositor;
pub use iced_native::alignment;
pub use iced_native::text;
pub use iced_native::{
Alignment, Background, Color, Font, Point, Rectangle, Size, Vector,
};

View file

@ -43,8 +43,11 @@ impl<B: Backend, T> Renderer<B, T> {
/// Runs the given closure with the [`Backend`] and the recorded primitives
/// of the [`Renderer`].
pub fn with_primitives(&mut self, f: impl FnOnce(&mut B, &[Primitive])) {
f(&mut self.backend, &self.primitives);
pub fn with_primitives<O>(
&mut self,
f: impl FnOnce(&mut B, &[Primitive]) -> O,
) -> O {
f(&mut self.backend, &self.primitives)
}
}

View file

@ -1,6 +1,6 @@
//! A compositor is responsible for initializing a renderer and managing window
//! surfaces.
use crate::compositor::Information;
use crate::window::compositor::Information;
use crate::{Color, Error, Size, Viewport};
use core::ffi::c_void;

26
renderer/Cargo.toml Normal file
View file

@ -0,0 +1,26 @@
[package]
name = "iced_renderer"
version = "0.1.0"
edition = "2021"
[features]
image = ["iced_wgpu/image"]
svg = ["iced_wgpu/svg"]
tracing = ["iced_wgpu/tracing"]
[dependencies]
raw-window-handle = "0.5"
[dependencies.iced_native]
version = "0.9"
path = "../native"
[dependencies.iced_graphics]
version = "0.7"
path = "../graphics"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced_wgpu = { version = "0.9", path = "../wgpu" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
iced_wgpu = { version = "0.9", path = "../wgpu", features = ["webgl"] }

94
renderer/src/backend.rs Normal file
View file

@ -0,0 +1,94 @@
use crate::{Font, Point, Size};
use iced_graphics::backend;
use iced_graphics::text;
use std::borrow::Cow;
pub enum Backend {
Wgpu(iced_wgpu::Backend),
}
impl iced_graphics::Backend for Backend {}
impl backend::Text for Backend {
const ICON_FONT: Font = Font::Name("Iced-Icons");
const CHECKMARK_ICON: char = '\u{f00c}';
const ARROW_DOWN_ICON: char = '\u{e800}';
fn default_font(&self) -> Font {
match self {
Self::Wgpu(backend) => backend.default_font(),
}
}
fn default_size(&self) -> f32 {
match self {
Self::Wgpu(backend) => backend.default_size(),
}
}
fn measure(
&self,
contents: &str,
size: f32,
font: Font,
bounds: Size,
) -> (f32, f32) {
match self {
Self::Wgpu(backend) => {
backend.measure(contents, size, font, bounds)
}
}
}
fn hit_test(
&self,
contents: &str,
size: f32,
font: Font,
bounds: Size,
position: Point,
nearest_only: bool,
) -> Option<text::Hit> {
match self {
Self::Wgpu(backend) => backend.hit_test(
contents,
size,
font,
bounds,
position,
nearest_only,
),
}
}
fn load_font(&mut self, font: Cow<'static, [u8]>) {
match self {
Self::Wgpu(backend) => {
backend.load_font(font);
}
}
}
}
#[cfg(feature = "image")]
impl backend::Image for Backend {
fn dimensions(&self, handle: &iced_native::image::Handle) -> Size<u32> {
match self {
Self::Wgpu(backend) => backend.dimensions(handle),
}
}
}
#[cfg(feature = "svg")]
impl backend::Svg for Backend {
fn viewport_dimensions(
&self,
handle: &iced_native::svg::Handle,
) -> Size<u32> {
match self {
Self::Wgpu(backend) => backend.viewport_dimensions(handle),
}
}
}

17
renderer/src/lib.rs Normal file
View file

@ -0,0 +1,17 @@
pub mod window;
mod backend;
mod settings;
pub use backend::Backend;
pub use settings::Settings;
pub use iced_graphics::{
Antialiasing, Color, Error, Font, Point, Size, Viewport,
};
/// The default graphics renderer for [`iced`].
///
/// [`iced`]: https://github.com/iced-rs/iced
pub type Renderer<Theme = iced_native::Theme> =
iced_graphics::Renderer<Backend, Theme>;

30
renderer/src/settings.rs Normal file
View file

@ -0,0 +1,30 @@
use crate::{Antialiasing, Font};
/// The settings of a [`Backend`].
///
/// [`Backend`]: crate::Backend
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Settings {
/// The default [`Font`] to use.
pub default_font: Font,
/// The default size of text.
///
/// By default, it will be set to `16.0`.
pub default_text_size: f32,
/// The antialiasing strategy that will be used for triangle primitives.
///
/// By default, it is `None`.
pub antialiasing: Option<Antialiasing>,
}
impl Default for Settings {
fn default() -> Settings {
Settings {
default_font: Font::SansSerif,
default_text_size: 16.0,
antialiasing: None,
}
}
}

3
renderer/src/window.rs Normal file
View file

@ -0,0 +1,3 @@
mod compositor;
pub use compositor::Compositor;

View file

@ -0,0 +1,96 @@
use crate::{Backend, Color, Error, Renderer, Settings, Viewport};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
pub use iced_graphics::window::compositor::{Information, SurfaceError};
pub enum Compositor<Theme> {
Wgpu(iced_wgpu::window::Compositor<Theme>),
}
pub enum Surface {
Wgpu(iced_wgpu::window::Surface),
}
impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
type Settings = Settings;
type Renderer = Renderer<Theme>;
type Surface = Surface;
fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
settings: Self::Settings,
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,
)?;
Ok((
Self::Wgpu(compositor),
Renderer::new(Backend::Wgpu(backend)),
))
}
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
&mut self,
window: &W,
) -> Surface {
match self {
Self::Wgpu(compositor) => {
Surface::Wgpu(compositor.create_surface(window))
}
}
}
fn configure_surface(
&mut self,
surface: &mut Surface,
width: u32,
height: u32,
) {
match (self, surface) {
(Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
compositor.configure_surface(surface, width, height);
}
}
}
fn fetch_information(&self) -> Information {
match self {
Self::Wgpu(compositor) => compositor.fetch_information(),
}
}
fn present<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
surface: &mut Self::Surface,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Result<(), SurfaceError> {
renderer.with_primitives(|backend, primitives| {
match (self, backend, surface) {
(
Self::Wgpu(compositor),
Backend::Wgpu(backend),
Surface::Wgpu(surface),
) => iced_wgpu::window::compositor::present(
compositor,
backend,
surface,
primitives,
viewport,
background_color,
overlay,
),
}
})
}
}

View file

@ -198,11 +198,11 @@ pub trait Application: Sized {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4)
Some(crate::renderer::Antialiasing::MSAAx4)
} else {
None
},
..crate::renderer::Settings::from_env()
..crate::renderer::Settings::default()
};
Ok(crate::runtime::application::run::<

View file

@ -182,7 +182,7 @@ pub mod touch;
pub mod widget;
pub mod window;
use iced_wgpu as renderer;
use iced_renderer as renderer;
use iced_winit as runtime;
pub use iced_native::theme;

View file

@ -1,4 +1,5 @@
//! Display rendering results on windows.
mod compositor;
pub mod compositor;
pub use compositor::Compositor;
pub use wgpu::Surface;

View file

@ -1,8 +1,9 @@
use crate::{Backend, Color, Error, Renderer, Settings, Viewport};
//! Connect a window with a renderer.
use crate::{Backend, Color, Error, Primitive, Renderer, Settings, Viewport};
use futures::stream::{self, StreamExt};
use iced_graphics::compositor;
use iced_graphics::window::compositor;
use iced_native::futures;
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
@ -112,6 +113,77 @@ impl<Theme> Compositor<Theme> {
}
}
/// Creates a [`Compositor`] and its [`Backend`] for the given [`Settings`] and
/// window.
pub fn new<Theme, W: HasRawWindowHandle + HasRawDisplayHandle>(
settings: Settings,
compatible_window: Option<&W>,
) -> Result<(Compositor<Theme>, Backend), Error> {
let compositor = futures::executor::block_on(Compositor::request(
settings,
compatible_window,
))
.ok_or(Error::GraphicsAdapterNotFound)?;
let backend = compositor.create_backend();
Ok((compositor, backend))
}
/// Presents the given primitives with the given [`Compositor`] and [`Backend`].
pub fn present<Theme, T: AsRef<str>>(
compositor: &mut Compositor<Theme>,
backend: &mut Backend,
surface: &mut wgpu::Surface,
primitives: &[Primitive],
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Result<(), compositor::SurfaceError> {
match surface.get_current_texture() {
Ok(frame) => {
let mut encoder = compositor.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor {
label: Some("iced_wgpu encoder"),
},
);
let view = &frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
backend.present(
&compositor.device,
&compositor.queue,
&mut encoder,
Some(background_color),
view,
primitives,
viewport,
overlay,
);
// Submit work
let _submission = compositor.queue.submit(Some(encoder.finish()));
frame.present();
Ok(())
}
Err(error) => match error {
wgpu::SurfaceError::Timeout => {
Err(compositor::SurfaceError::Timeout)
}
wgpu::SurfaceError::Outdated => {
Err(compositor::SurfaceError::Outdated)
}
wgpu::SurfaceError::Lost => Err(compositor::SurfaceError::Lost),
wgpu::SurfaceError::OutOfMemory => {
Err(compositor::SurfaceError::OutOfMemory)
}
},
}
}
impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
type Settings = Settings;
type Renderer = Renderer<Theme>;
@ -121,13 +193,7 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
settings: Self::Settings,
compatible_window: Option<&W>,
) -> Result<(Self, Self::Renderer), Error> {
let compositor = futures::executor::block_on(Self::request(
settings,
compatible_window,
))
.ok_or(Error::GraphicsAdapterNotFound)?;
let backend = compositor.create_backend();
let (compositor, backend) = new(settings, compatible_window)?;
Ok((compositor, Renderer::new(backend)))
}
@ -178,49 +244,16 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
background_color: Color,
overlay: &[T],
) -> Result<(), compositor::SurfaceError> {
match surface.get_current_texture() {
Ok(frame) => {
let mut encoder = self.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor {
label: Some("iced_wgpu encoder"),
},
);
let view = &frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
renderer.with_primitives(|backend, primitives| {
backend.present(
&self.device,
&self.queue,
&mut encoder,
Some(background_color),
view,
primitives,
viewport,
overlay,
);
});
// Submit work
let _submission = self.queue.submit(Some(encoder.finish()));
frame.present();
Ok(())
}
Err(error) => match error {
wgpu::SurfaceError::Timeout => {
Err(compositor::SurfaceError::Timeout)
}
wgpu::SurfaceError::Outdated => {
Err(compositor::SurfaceError::Outdated)
}
wgpu::SurfaceError::Lost => Err(compositor::SurfaceError::Lost),
wgpu::SurfaceError::OutOfMemory => {
Err(compositor::SurfaceError::OutOfMemory)
}
},
}
renderer.with_primitives(|backend, primitives| {
present(
self,
backend,
surface,
primitives,
viewport,
background_color,
overlay,
)
})
}
}

View file

@ -17,8 +17,8 @@ use crate::{
use iced_futures::futures;
use iced_futures::futures::channel::mpsc;
use iced_graphics::compositor;
use iced_graphics::window;
use iced_graphics::window::compositor;
use iced_native::program::Program;
use iced_native::time::Instant;
use iced_native::user_interface::{self, UserInterface};

View file

@ -2,7 +2,7 @@
use crate::command::{self, Command};
pub use iced_native::system::*;
use iced_graphics::compositor;
use iced_graphics::window::compositor;
/// Query for available system information.
pub fn fetch_information<Message>(