Skip to content

Commit

Permalink
Remove SceneBuilder/Fragment (#432)
Browse files Browse the repository at this point in the history
This removes both `SceneBuilder` and `SceneFragment` while moving all of the "builder" methods directly to `Scene`.

Based on #426 which must land first.
  • Loading branch information
dfrg authored Feb 13, 2024
1 parent b8f09b6 commit d16df77
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 310 deletions.
10 changes: 4 additions & 6 deletions examples/headless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use vello::{
block_on_wgpu,
kurbo::{Affine, Vec2},
util::RenderContext,
RendererOptions, Scene, SceneBuilder, SceneFragment,
RendererOptions, Scene,
};
use wgpu::{
BufferDescriptor, BufferUsages, CommandEncoderDescriptor, Extent3d, ImageCopyBuffer,
Expand Down Expand Up @@ -94,8 +94,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
},
)
.or_else(|_| bail!("Got non-Send/Sync error from creating renderer"))?;
let mut fragment = SceneFragment::new();
let mut builder = SceneBuilder::for_fragment(&mut fragment);
let mut fragment = Scene::new();
let example_scene = &mut scenes.scenes[index];
let mut text = SimpleText::new();
let mut images = ImageCache::new();
Expand All @@ -110,7 +109,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
};
example_scene
.function
.render(&mut builder, &mut scene_params);
.render(&mut fragment, &mut scene_params);
let mut transform = Affine::IDENTITY;
let (width, height) = if let Some(resolution) = scene_params.resolution {
let ratio = resolution.x / resolution.y;
Expand Down Expand Up @@ -143,8 +142,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
antialiasing_method: vello::AaConfig::Area,
};
let mut scene = Scene::new();
let mut builder = SceneBuilder::for_scene(&mut scene);
builder.append(&fragment, Some(transform));
scene.append(&fragment, Some(transform));
let size = Extent3d {
width,
height,
Expand Down
10 changes: 5 additions & 5 deletions examples/scenes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use simple_text::SimpleText;
pub use svg::{default_scene, scene_from_files};
pub use test_scenes::test_scenes;

use vello::{kurbo::Vec2, peniko::Color, SceneBuilder};
use vello::{kurbo::Vec2, peniko::Color, Scene};

pub struct SceneParams<'a> {
pub time: f64,
Expand All @@ -43,12 +43,12 @@ pub struct ExampleScene {
}

pub trait TestScene {
fn render(&mut self, sb: &mut SceneBuilder, params: &mut SceneParams);
fn render(&mut self, scene: &mut Scene, params: &mut SceneParams);
}

impl<F: FnMut(&mut SceneBuilder, &mut SceneParams)> TestScene for F {
fn render(&mut self, sb: &mut SceneBuilder, params: &mut SceneParams) {
self(sb, params);
impl<F: FnMut(&mut Scene, &mut SceneParams)> TestScene for F {
fn render(&mut self, scene: &mut Scene, params: &mut SceneParams) {
self(scene, params);
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/scenes/src/mmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rand::{seq::SliceRandom, Rng};
use vello::peniko::Color;
use vello::{
kurbo::{Affine, BezPath, CubicBez, Line, ParamCurve, PathSeg, Point, QuadBez, Stroke},
SceneBuilder,
Scene,
};

use crate::{SceneParams, TestScene};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl MMark {
}

impl TestScene for MMark {
fn render(&mut self, sb: &mut SceneBuilder, params: &mut SceneParams) {
fn render(&mut self, scene: &mut Scene, params: &mut SceneParams) {
let c = params.complexity;
let n = if c < 10 {
(c + 1) * 1000
Expand All @@ -90,7 +90,7 @@ impl TestScene for MMark {
if element.is_split || i == len {
// This gets color and width from the last element, original
// gets it from the first, but this should not matter.
sb.stroke(
scene.stroke(
&Stroke::new(element.width),
Affine::IDENTITY,
element.color,
Expand All @@ -105,7 +105,7 @@ impl TestScene for MMark {
}
let label = format!("mmark test: {} path elements (up/down to adjust)", n);
params.text.add(
sb,
scene,
None,
40.0,
None,
Expand Down
14 changes: 7 additions & 7 deletions examples/scenes/src/simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use vello::{
kurbo::Affine,
peniko::{Blob, Brush, BrushRef, Font, StyleRef},
skrifa::{raw::FontRef, MetadataProvider},
SceneBuilder,
Scene,
};

// This is very much a hack to get things working.
Expand All @@ -46,7 +46,7 @@ impl SimpleText {
#[allow(clippy::too_many_arguments)]
pub fn add_run<'a>(
&mut self,
builder: &mut SceneBuilder,
scene: &mut Scene,
font: Option<&Font>,
size: f32,
brush: impl Into<BrushRef<'a>>,
Expand All @@ -56,7 +56,7 @@ impl SimpleText {
text: &str,
) {
self.add_var_run(
builder,
scene,
font,
size,
&[],
Expand All @@ -71,7 +71,7 @@ impl SimpleText {
#[allow(clippy::too_many_arguments)]
pub fn add_var_run<'a>(
&mut self,
builder: &mut SceneBuilder,
scene: &mut Scene,
font: Option<&Font>,
size: f32,
variations: &[(&str, f32)],
Expand Down Expand Up @@ -99,7 +99,7 @@ impl SimpleText {
let glyph_metrics = font_ref.glyph_metrics(font_size, &var_loc);
let mut pen_x = 0f32;
let mut pen_y = 0f32;
builder
scene
.draw_glyphs(font)
.font_size(size)
.transform(transform)
Expand Down Expand Up @@ -129,7 +129,7 @@ impl SimpleText {

pub fn add(
&mut self,
builder: &mut SceneBuilder,
scene: &mut Scene,
font: Option<&Font>,
size: f32,
brush: Option<&Brush>,
Expand All @@ -139,7 +139,7 @@ impl SimpleText {
use vello::peniko::{Color, Fill};
let brush = brush.unwrap_or(&Brush::Solid(Color::WHITE));
self.add_run(
builder,
scene,
font,
size,
brush,
Expand Down
21 changes: 10 additions & 11 deletions examples/scenes/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use anyhow::{Ok, Result};
use instant::Instant;
use vello::{kurbo::Vec2, SceneBuilder, SceneFragment};
use vello::{kurbo::Vec2, Scene};
use vello_svg::usvg;
use vello_svg::usvg::TreeParsing;

Expand Down Expand Up @@ -88,16 +88,15 @@ fn example_scene_of(file: PathBuf) -> ExampleScene {
pub fn svg_function_of<R: AsRef<str>>(
name: String,
contents: impl FnOnce() -> R + Send + 'static,
) -> impl FnMut(&mut SceneBuilder, &mut SceneParams) {
fn render_svg_contents(name: &str, contents: &str) -> (SceneFragment, Vec2) {
) -> impl FnMut(&mut Scene, &mut SceneParams) {
fn render_svg_contents(name: &str, contents: &str) -> (Scene, Vec2) {
let start = Instant::now();
let svg = usvg::Tree::from_str(contents, &usvg::Options::default())
.expect("failed to parse svg file");
eprintln!("Parsed svg {name} in {:?}", start.elapsed());
let start = Instant::now();
let mut new_scene = SceneFragment::new();
let mut builder = SceneBuilder::for_fragment(&mut new_scene);
vello_svg::render_tree(&mut builder, &svg);
let mut new_scene = Scene::new();
vello_svg::render_tree(&mut new_scene, &svg);
let resolution = Vec2::new(svg.size.width() as f64, svg.size.height() as f64);
eprintln!("Encoded svg {name} in {:?}", start.elapsed());
(new_scene, resolution)
Expand All @@ -110,17 +109,17 @@ pub fn svg_function_of<R: AsRef<str>>(
#[cfg(not(target_arch = "wasm32"))]
let mut has_started_parse = false;
let mut contents = Some(contents);
move |builder, params| {
move |scene, params| {
if let Some((scene_frag, resolution)) = cached_scene.as_mut() {
builder.append(scene_frag, None);
scene.append(scene_frag, None);
params.resolution = Some(*resolution);
return;
}
if cfg!(target_arch = "wasm32") || !params.interactive {
let contents = contents.take().unwrap();
let contents = contents();
let (scene_frag, resolution) = render_svg_contents(&name, contents.as_ref());
builder.append(&scene_frag, None);
scene.append(&scene_frag, None);
params.resolution = Some(resolution);
cached_scene = Some((scene_frag, resolution));
return;
Expand All @@ -145,12 +144,12 @@ pub fn svg_function_of<R: AsRef<str>>(
use std::sync::mpsc::RecvTimeoutError;
match recv {
Result::Ok((scene_frag, resolution)) => {
builder.append(&scene_frag, None);
scene.append(&scene_frag, None);
params.resolution = Some(resolution);
cached_scene = Some((scene_frag, resolution));
}
Err(RecvTimeoutError::Timeout) => params.text.add(
builder,
scene,
None,
48.,
None,
Expand Down
Loading

0 comments on commit d16df77

Please sign in to comment.