Scaffold iced_tiny_skia and connect it to iced_renderer

This commit is contained in:
Héctor Ramón Jiménez 2023-02-25 15:38:25 +01:00
parent a01bc865a0
commit 8c373cd497
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
10 changed files with 318 additions and 12 deletions

View file

@ -50,6 +50,7 @@ members = [
"native",
"renderer",
"style",
"tiny_skia",
"wgpu",
"winit",
"examples/*",

View file

@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"
[features]
image = ["iced_wgpu/image"]
svg = ["iced_wgpu/svg"]
image = ["iced_wgpu/image", "iced_tiny_skia/image"]
svg = ["iced_wgpu/svg", "iced_tiny_skia/svg"]
tracing = ["iced_wgpu/tracing"]
[dependencies]
@ -19,6 +19,10 @@ path = "../native"
version = "0.7"
path = "../graphics"
[dependencies.iced_tiny_skia]
version = "0.1"
path = "../tiny_skia"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced_wgpu = { version = "0.9", path = "../wgpu" }

View file

@ -7,12 +7,14 @@ use std::borrow::Cow;
pub enum Backend {
Wgpu(iced_wgpu::Backend),
TinySkia(iced_tiny_skia::Backend),
}
impl iced_graphics::Backend for Backend {
fn trim_measurements(&mut self) {
match self {
Self::Wgpu(backend) => backend.trim_measurements(),
Self::TinySkia(backend) => backend.trim_measurements(),
}
}
}
@ -25,12 +27,14 @@ impl backend::Text for Backend {
fn default_font(&self) -> Font {
match self {
Self::Wgpu(backend) => backend.default_font(),
Self::TinySkia(backend) => backend.default_font(),
}
}
fn default_size(&self) -> f32 {
match self {
Self::Wgpu(backend) => backend.default_size(),
Self::TinySkia(backend) => backend.default_size(),
}
}
@ -45,6 +49,9 @@ impl backend::Text for Backend {
Self::Wgpu(backend) => {
backend.measure(contents, size, font, bounds)
}
Self::TinySkia(backend) => {
backend.measure(contents, size, font, bounds)
}
}
}
@ -66,6 +73,14 @@ impl backend::Text for Backend {
position,
nearest_only,
),
Self::TinySkia(backend) => backend.hit_test(
contents,
size,
font,
bounds,
position,
nearest_only,
),
}
}
@ -74,6 +89,9 @@ impl backend::Text for Backend {
Self::Wgpu(backend) => {
backend.load_font(font);
}
Self::TinySkia(backend) => {
backend.load_font(font);
}
}
}
}
@ -83,6 +101,7 @@ impl backend::Image for Backend {
fn dimensions(&self, handle: &iced_native::image::Handle) -> Size<u32> {
match self {
Self::Wgpu(backend) => backend.dimensions(handle),
Self::TinySkia(backend) => backend.dimensions(handle),
}
}
}
@ -95,6 +114,7 @@ impl backend::Svg for Backend {
) -> Size<u32> {
match self {
Self::Wgpu(backend) => backend.viewport_dimensions(handle),
Self::TinySkia(backend) => backend.viewport_dimensions(handle),
}
}
}

View file

@ -6,10 +6,12 @@ pub use iced_graphics::window::compositor::{Information, SurfaceError};
pub enum Compositor<Theme> {
Wgpu(iced_wgpu::window::Compositor<Theme>),
TinySkia(iced_tiny_skia::window::Compositor<Theme>),
}
pub enum Surface {
Wgpu(iced_wgpu::window::Surface),
TinySkia(iced_tiny_skia::window::Surface),
}
impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
@ -19,21 +21,31 @@ impl<Theme> iced_graphics::window::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 {
//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,
antialiasing: settings.antialiasing,
..iced_wgpu::Settings::from_env()
},
compatible_window,
)?;
});
Ok((
Self::Wgpu(compositor),
Renderer::new(Backend::Wgpu(backend)),
Self::TinySkia(compositor),
Renderer::new(Backend::TinySkia(backend)),
))
}
@ -45,6 +57,9 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
Self::Wgpu(compositor) => {
Surface::Wgpu(compositor.create_surface(window))
}
Self::TinySkia(compositor) => {
Surface::TinySkia(compositor.create_surface(window))
}
}
}
@ -58,12 +73,17 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
(Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
compositor.configure_surface(surface, width, height);
}
(Self::TinySkia(compositor), Surface::TinySkia(surface)) => {
compositor.configure_surface(surface, width, height);
}
_ => unreachable!(),
}
}
fn fetch_information(&self) -> Information {
match self {
Self::Wgpu(compositor) => compositor.fetch_information(),
Self::TinySkia(compositor) => compositor.fetch_information(),
}
}
@ -90,6 +110,20 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
background_color,
overlay,
),
(
Self::TinySkia(compositor),
Backend::TinySkia(backend),
Surface::TinySkia(surface),
) => iced_tiny_skia::window::compositor::present(
compositor,
backend,
surface,
primitives,
viewport,
background_color,
overlay,
),
_ => unreachable!(),
}
})
}

21
tiny_skia/Cargo.toml Normal file
View file

@ -0,0 +1,21 @@
[package]
name = "iced_tiny_skia"
version = "0.1.0"
edition = "2021"
[features]
image = []
svg = []
[dependencies]
raw-window-handle = "0.5"
softbuffer = "0.2"
tiny-skia = "0.8"
[dependencies.iced_native]
version = "0.9"
path = "../native"
[dependencies.iced_graphics]
version = "0.7"
path = "../graphics"

87
tiny_skia/src/backend.rs Normal file
View file

@ -0,0 +1,87 @@
use crate::{Font, Settings, Size};
use iced_graphics::backend;
use iced_graphics::text;
use std::borrow::Cow;
pub struct Backend {
default_font: Font,
default_text_size: f32,
}
impl Backend {
pub fn new(settings: Settings) -> Self {
Self {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
}
}
}
impl iced_graphics::Backend for Backend {
fn trim_measurements(&mut self) {
// TODO
}
}
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 {
self.default_font
}
fn default_size(&self) -> f32 {
self.default_text_size
}
fn measure(
&self,
_contents: &str,
_size: f32,
_font: Font,
_bounds: Size,
) -> (f32, f32) {
// TODO
(0.0, 0.0)
}
fn hit_test(
&self,
_contents: &str,
_size: f32,
_font: Font,
_bounds: Size,
_point: iced_native::Point,
_nearest_only: bool,
) -> Option<text::Hit> {
// TODO
None
}
fn load_font(&mut self, _font: Cow<'static, [u8]>) {
// TODO
}
}
#[cfg(feature = "image")]
impl backend::Image for Backend {
fn dimensions(&self, _handle: &iced_native::image::Handle) -> Size<u32> {
// TODO
Size::new(0, 0)
}
}
#[cfg(feature = "svg")]
impl backend::Svg for Backend {
fn viewport_dimensions(
&self,
_handle: &iced_native::svg::Handle,
) -> Size<u32> {
// TODO
Size::new(0, 0)
}
}

16
tiny_skia/src/lib.rs Normal file
View file

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

24
tiny_skia/src/settings.rs Normal file
View file

@ -0,0 +1,24 @@
use crate::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,
}
impl Default for Settings {
fn default() -> Settings {
Settings {
default_font: Font::SansSerif,
default_text_size: 16.0,
}
}
}

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

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

View file

@ -0,0 +1,96 @@
use crate::{Backend, Color, Error, Renderer, Settings, Viewport};
use iced_graphics::window::compositor::{self, Information, SurfaceError};
use iced_graphics::Primitive;
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::marker::PhantomData;
pub struct Compositor<Theme> {
_theme: PhantomData<Theme>,
}
pub struct 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) = new(settings);
Ok((compositor, Renderer::new(backend)))
}
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
&mut self,
_window: &W,
) -> Surface {
// TODO
Surface
}
fn configure_surface(
&mut self,
_surface: &mut Surface,
_width: u32,
_height: u32,
) {
// TODO
}
fn fetch_information(&self) -> Information {
Information {
adapter: String::from("CPU"),
backend: String::from("tiny-skia"),
}
}
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| {
present(
self,
backend,
surface,
primitives,
viewport,
background_color,
overlay,
)
})
}
}
pub fn new<Theme>(settings: Settings) -> (Compositor<Theme>, Backend) {
// TODO
(
Compositor {
_theme: PhantomData,
},
Backend::new(settings),
)
}
pub fn present<Theme, T: AsRef<str>>(
_compositor: &mut Compositor<Theme>,
_backend: &mut Backend,
_surface: &mut Surface,
_primitives: &[Primitive],
_viewport: &Viewport,
_background_color: Color,
_overlay: &[T],
) -> Result<(), compositor::SurfaceError> {
// TODO
Ok(())
}