Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"crates/anyrender",
"crates/anyrender_vello",
"crates/anyrender_vello_cpu",
"crates/anyrender_vello_hybrid",
"crates/anyrender_svg",
"crates/wgpu_context",
"crates/pixels_window_renderer",
Expand All @@ -26,6 +27,7 @@ rust-version = "1.86.0"
anyrender = { version = "0.6.0", path = "./crates/anyrender" }
anyrender_vello = { version = "0.6.0", path = "./crates/anyrender_vello" }
anyrender_vello_cpu = { version = "0.7.0", path = "./crates/anyrender_vello_cpu" }
anyrender_vello_hybrid = { version = "0.1.0", path = "./crates/anyrender_vello_hybrid" }
anyrender_svg = { version = "0.6.0", path = "./crates/anyrender_svg" }
wgpu_context = { version = "0.1.0", path = "./crates/wgpu_context" }
pixels_window_renderer = { version = "0.1.0", path = "./crates/pixels_window_renderer" }
Expand All @@ -37,7 +39,9 @@ linebender_resource_handle = "0.1"
peniko = "0.5.0"
kurbo = "0.12"
vello = { version = "0.6", features = [ "wgpu" ] }
vello_cpu = { version = "0.0.3", default-features = false, features = ["std", "text"]}
vello_cpu = { version = "0.0.4", default-features = false, features = ["std", "text"] }
vello_hybrid = { version = "0.0.4" }
vello_common = { version = "0.0.4" }

# Rendering
wgpu = "26"
Expand Down
27 changes: 27 additions & 0 deletions crates/anyrender_vello_hybrid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "anyrender_vello_hybrid"
description = "vello_hybrid backend for anyrender"
version = "0.1.0"
documentation = "https://docs.rs/anyrender_vello_hybrid"
homepage.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
publish = false

[features]
log_frame_times = ["debug_timer/enable"]

[dependencies]
anyrender = { workspace = true }
debug_timer = { workspace = true }
kurbo = { workspace = true }
peniko = { workspace = true }
vello_hybrid = { workspace = true }
vello_common = { workspace = true }
raw-window-handle = { workspace = true }
wgpu = { workspace = true }
futures-intrusive = { workspace = true }
pollster = { workspace = true }
rustc-hash = { workspace = true }
wgpu_context = { workspace = true }
8 changes: 8 additions & 0 deletions crates/anyrender_vello_hybrid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! An Anyrender backend using the vello_cpu crate
#![cfg_attr(docsrs, feature(doc_cfg))]

mod scene;
mod window_renderer;

pub use scene::VelloHybridScenePainter;
pub use window_renderer::*;
211 changes: 211 additions & 0 deletions crates/anyrender_vello_hybrid/src/scene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
use anyrender::{NormalizedCoord, Paint, PaintRef, PaintScene};
use kurbo::{Affine, Rect, Shape, Stroke};
use peniko::{BlendMode, Color, Fill, FontData, ImageBrush, ImageData, StyleRef};
use rustc_hash::FxHashMap;
use vello_common::paint::{ImageId, ImageSource, PaintType};
use vello_hybrid::Renderer;
use wgpu::{CommandEncoder, Device, Queue};

const DEFAULT_TOLERANCE: f64 = 0.1;

fn anyrender_paint_to_vello_hybrid_paint<'a>(
paint: PaintRef<'a>,
mut image_manager: &mut Option<&mut ImageManager<'_>>,
) -> PaintType {
match paint {
Paint::Solid(alpha_color) => PaintType::Solid(alpha_color),
Paint::Gradient(gradient) => PaintType::Gradient(gradient.clone()),

Paint::Image(image_brush) => {
if let Some(image_manager) = &mut image_manager {
let image_id = image_manager.upload_image(image_brush.image);
PaintType::Image(ImageBrush {
image: ImageSource::OpaqueId(image_id),
sampler: image_brush.sampler,
})
} else {
PaintType::Solid(peniko::color::palette::css::TRANSPARENT)
}
}

// TODO: custom paint
Paint::Custom(_) => PaintType::Solid(peniko::color::palette::css::TRANSPARENT),
}
}

pub(crate) struct ImageManager<'a> {
pub(crate) renderer: &'a mut Renderer,
pub(crate) device: &'a Device,
pub(crate) queue: &'a Queue,
pub(crate) encoder: &'a mut CommandEncoder,
pub(crate) cache: &'a mut FxHashMap<u64, ImageId>,
}

impl ImageManager<'_> {
pub(crate) fn upload_image(&mut self, image: &ImageData) -> ImageId {
let peniko_id = image.data.id();

// Try to get ImageId from cache first
if let Some(atlas_id) = self.cache.get(&peniko_id) {
return *atlas_id;
};

// Convert ImageData to Pixmap
let ImageSource::Pixmap(pixmap) = ImageSource::from_peniko_image_data(image) else {
unreachable!(); // ImageSource::from_peniko_image_data always return a Pixmap
};

// Upload Pixamp
let atlas_id = self
.renderer
.upload_image(self.device, self.queue, self.encoder, &pixmap);

// Store ImageId in cache
self.cache.insert(peniko_id, atlas_id);

// Return ImageId
atlas_id
}
}

pub struct VelloHybridScenePainter<'s> {
pub(crate) scene: &'s mut vello_hybrid::Scene,
pub(crate) image_manager: Option<ImageManager<'s>>,
}

impl VelloHybridScenePainter<'_> {
pub fn new<'s>(scene: &'s mut vello_hybrid::Scene) -> VelloHybridScenePainter<'s> {
VelloHybridScenePainter {
scene,
image_manager: None,
}
}
}

impl PaintScene for VelloHybridScenePainter<'_> {
fn reset(&mut self) {
self.scene.reset();
}

fn push_layer(
&mut self,
blend: impl Into<BlendMode>,
alpha: f32,
transform: Affine,
clip: &impl Shape,
) {
self.scene.set_transform(transform);
self.scene.push_layer(
Some(&clip.into_path(DEFAULT_TOLERANCE)),
Some(blend.into()),
Some(alpha),
None,
);
}

fn pop_layer(&mut self) {
self.scene.pop_layer();
}

fn stroke<'a>(
&mut self,
style: &Stroke,
transform: Affine,
paint: impl Into<PaintRef<'a>>,
brush_transform: Option<Affine>,
shape: &impl Shape,
) {
self.scene.set_transform(transform);
self.scene.set_stroke(style.clone());
let paint =
anyrender_paint_to_vello_hybrid_paint(paint.into(), &mut self.image_manager.as_mut());
self.scene.set_paint(paint);
self.scene
.set_paint_transform(brush_transform.unwrap_or(Affine::IDENTITY));
self.scene.stroke_path(&shape.into_path(DEFAULT_TOLERANCE));
}

fn fill<'a>(
&mut self,
style: Fill,
transform: Affine,
paint: impl Into<PaintRef<'a>>,
brush_transform: Option<Affine>,
shape: &impl Shape,
) {
self.scene.set_transform(transform);
self.scene.set_fill_rule(style);
let paint =
anyrender_paint_to_vello_hybrid_paint(paint.into(), &mut self.image_manager.as_mut());
self.scene.set_paint(paint);
self.scene
.set_paint_transform(brush_transform.unwrap_or(Affine::IDENTITY));
self.scene.fill_path(&shape.into_path(DEFAULT_TOLERANCE));
}

fn draw_glyphs<'a, 's: 'a>(
&'a mut self,
font: &'a FontData,
font_size: f32,
hint: bool,
normalized_coords: &'a [NormalizedCoord],
style: impl Into<StyleRef<'a>>,
paint: impl Into<PaintRef<'a>>,
_brush_alpha: f32,
transform: Affine,
glyph_transform: Option<Affine>,
glyphs: impl Iterator<Item = anyrender::Glyph>,
) {
let paint =
anyrender_paint_to_vello_hybrid_paint(paint.into(), &mut self.image_manager.as_mut());
self.scene.set_paint(paint);
self.scene.set_transform(transform);

fn into_vello_hybrid_glyph(g: anyrender::Glyph) -> vello_common::glyph::Glyph {
vello_common::glyph::Glyph {
id: g.id,
x: g.x,
y: g.y,
}
}

let style: StyleRef<'a> = style.into();
match style {
StyleRef::Fill(fill) => {
self.scene.set_fill_rule(fill);
self.scene
.glyph_run(font)
.font_size(font_size)
.hint(hint)
.normalized_coords(normalized_coords)
.glyph_transform(glyph_transform.unwrap_or_default())
.fill_glyphs(glyphs.map(into_vello_hybrid_glyph));
}
StyleRef::Stroke(stroke) => {
self.scene.set_stroke(stroke.clone());
self.scene
.glyph_run(font)
.font_size(font_size)
.hint(hint)
.normalized_coords(normalized_coords)
.glyph_transform(glyph_transform.unwrap_or_default())
.stroke_glyphs(glyphs.map(into_vello_hybrid_glyph));
}
}
}
fn draw_box_shadow(
&mut self,
_transform: Affine,
_rect: Rect,
_color: Color,
_radius: f64,
_std_dev: f64,
) {
// FIXME: implement once supported in vello_hybrid
//
// self.scene.set_transform(transform);
// self.scene.set_paint(PaintType::Solid(color));
// self.scene
// .fill_blurred_rounded_rect(&rect, radius as f32, std_dev as f32);
}
}
Loading