Skip to content

Commit

Permalink
Add a tiny_skia rendering backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Oct 10, 2023
1 parent 5dd6c86 commit 0119578
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)

Check warning on line 70 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L70

Added line #L70 was not covered by tests
{
Ok(vger) => return Self::Vger(vger),
Err(vger_err) => vger_err,

Check warning on line 73 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L72-L73

Added lines #L72 - L73 were not covered by tests
};

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}")

Check warning on line 82 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L76-L82

Added lines #L76 - L82 were not covered by tests
}

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),

Check warning on line 89 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L89

Added line #L89 was not covered by tests
}
}

pub fn set_scale(&mut self, scale: f64) {
match self {
Renderer::Vger(r) => r.set_scale(scale),
Renderer::TinySkia(r) => r.set_scale(scale),

Check warning on line 96 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L96

Added line #L96 was not covered by tests
}
}
}
Expand All @@ -86,6 +104,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(r) => {
r.begin();
}
Renderer::TinySkia(r) => {
r.begin();
}

Check warning on line 109 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L107-L109

Added lines #L107 - L109 were not covered by tests
}
}

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

Check warning on line 120 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L118-L120

Added lines #L118 - L120 were not covered by tests
}
}

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

Check warning on line 131 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L129-L131

Added lines #L129 - L131 were not covered by tests
}
}

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);
}

Check warning on line 142 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L140-L142

Added lines #L140 - L142 were not covered by tests
}
}

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);
}

Check warning on line 158 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L156-L158

Added lines #L156 - L158 were not covered by tests
}
}

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);
}

Check warning on line 169 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L167-L169

Added lines #L167 - L169 were not covered by tests
}
}

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);
}

Check warning on line 180 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L178-L180

Added lines #L178 - L180 were not covered by tests
}
}

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);
}

Check warning on line 196 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L194-L196

Added lines #L194 - L196 were not covered by tests
}
}

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

Check warning on line 207 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L205-L207

Added lines #L205 - L207 were not covered by tests
}
}

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);
}

Check warning on line 218 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L216-L218

Added lines #L216 - L218 were not covered by tests
}
}

Expand All @@ -176,6 +224,9 @@ impl floem_renderer::Renderer for Renderer {
Renderer::Vger(r) => {
r.finish();
}
Renderer::TinySkia(r) => {
r.finish();
}

Check warning on line 229 in src/renderer.rs

View check run for this annotation

Codecov / codecov/patch

src/renderer.rs#L227-L229

Added lines #L227 - L229 were not covered by tests
}
}
}
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 0119578

Please sign in to comment.