Skip to content

Commit

Permalink
Add a tiny_skia rendering backend (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc authored Oct 10, 2023
1 parent 5dd6c86 commit 18bb6aa
Show file tree
Hide file tree
Showing 5 changed files with 626 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ clipboard = "0.5.0"
smallvec = "1.10.0"
educe = "0.4.20"
taffy = "0.3.13"
rfd = { version = "0.11.4", default-features = false, features = ["xdg-portal"] }
rfd = { version = "0.11.4", default-features = false, features = [
"xdg-portal",
] }
raw-window-handle = "0.5.1"
kurbo = { version = "0.9.5", features = ["serde"] }
unicode-segmentation = "1.10.0"
Expand All @@ -24,6 +26,7 @@ im = "15.1.0"
parking_lot = { version = "0.12.1" }
floem_renderer = { path = "renderer" }
floem_vger = { path = "vger" }
floem_tiny_skia = { path = "tiny_skia" }
floem_reactive = { path = "reactive" }
winit = { git = "https://github.com/lapce/winit", rev = "25edc72fa4869d0fa83c61c26f0e38d7d7be9b0d" }
# winit = { path = "../winit" }
Expand All @@ -33,7 +36,7 @@ image = { version = "0.24", features = ["jpeg", "png"] }
serde = ["winit/serde"]

[workspace]
members = ["renderer", "vger", "reactive", "examples/*"]
members = ["renderer", "vger", "tiny_skia", "reactive", "examples/*"]

[workspace.package]
license = "MIT"
53 changes: 52 additions & 1 deletion src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@
//!
use crate::cosmic_text::TextLayout;
use floem_renderer::Img;
use floem_tiny_skia::TinySkiaRenderer;
use floem_vger::VgerRenderer;
use kurbo::{Affine, Rect, Shape, Size};
use peniko::BrushRef;

#[allow(clippy::large_enum_variant)]
pub enum Renderer {
Vger(VgerRenderer),
TinySkia(TinySkiaRenderer),
}

impl Renderer {
Expand All @@ -63,19 +66,34 @@ impl Renderer {
W: raw_window_handle::HasRawDisplayHandle + raw_window_handle::HasRawWindowHandle,
{
let size = Size::new(size.width.max(1.0), size.height.max(1.0));
Self::Vger(VgerRenderer::new(window, size.width as u32, size.height as u32, scale).unwrap())

let vger_err = match VgerRenderer::new(window, size.width as u32, size.height as u32, scale)
{
Ok(vger) => return Self::Vger(vger),
Err(vger_err) => vger_err,
};

let tiny_skia_err =
match TinySkiaRenderer::new(window, size.width as u32, size.height as u32, scale) {
Ok(tiny_skia) => return Self::TinySkia(tiny_skia),
Err(vger_err) => vger_err,
};

panic!("Failed to create VgerRenderer: {vger_err}\nFailed to create TinySkiaRenderer: {tiny_skia_err}")
}

pub fn resize(&mut self, scale: f64, size: Size) {
let size = Size::new(size.width.max(1.0), size.height.max(1.0));
match self {
Renderer::Vger(r) => r.resize(size.width as u32, size.height as u32, scale),
Renderer::TinySkia(r) => r.resize(size.width as u32, size.height as u32, scale),
}
}

pub fn set_scale(&mut self, scale: f64) {
match self {
Renderer::Vger(r) => r.set_scale(scale),
Renderer::TinySkia(r) => r.set_scale(scale),
}
}
}
Expand All @@ -86,6 +104,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(r) => {
r.begin();
}
Renderer::TinySkia(r) => {
r.begin();
}
}
}

Expand All @@ -94,6 +115,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.clip(shape);
}
Renderer::TinySkia(v) => {
v.clip(shape);
}
}
}

Expand All @@ -102,6 +126,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.clear_clip();
}
Renderer::TinySkia(v) => {
v.clear_clip();
}
}
}

Expand All @@ -110,6 +137,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.stroke(shape, brush, width);
}
Renderer::TinySkia(v) => {
v.stroke(shape, brush, width);
}
}
}

Expand All @@ -123,6 +153,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.fill(path, brush, blur_radius);
}
Renderer::TinySkia(v) => {
v.fill(path, brush, blur_radius);
}
}
}

Expand All @@ -131,6 +164,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.draw_text(layout, pos);
}
Renderer::TinySkia(v) => {
v.draw_text(layout, pos);
}
}
}

Expand All @@ -139,6 +175,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.draw_img(img, width, height, rect);
}
Renderer::TinySkia(v) => {
v.draw_img(img, width, height, rect);
}
}
}

Expand All @@ -152,6 +191,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.draw_svg(svg, rect, brush);
}
Renderer::TinySkia(v) => {
v.draw_svg(svg, rect, brush);
}
}
}

Expand All @@ -160,6 +202,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.transform(transform);
}
Renderer::TinySkia(v) => {
v.transform(transform);
}
}
}

Expand All @@ -168,6 +213,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(v) => {
v.set_z_index(z_index);
}
Renderer::TinySkia(v) => {
v.set_z_index(z_index);
}
}
}

Expand All @@ -176,6 +224,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(r) => {
r.finish();
}
Renderer::TinySkia(r) => {
r.finish();
}
}
}
}
17 changes: 17 additions & 0 deletions tiny_skia/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "floem_tiny_skia"
version = "0.1.0"
edition = "2021"
license.workspace = true

[dependencies]
resvg = "0.33.0"
raw-window-handle = "0.5.1"
futures = "0.3.26"
anyhow = "1.0.69"
peniko = { git = "https://github.com/linebender/peniko", rev = "cafdac9a211a0fb2fec5656bd663d1ac770bcc81" }
swash = "0.1.8"
floem_renderer = { path = "../renderer" }
softbuffer = "0.3.1"
bytemuck = "1.12"
image = { version = "0.24", features = ["jpeg", "png"] }
Loading

0 comments on commit 18bb6aa

Please sign in to comment.