Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import cpu-sparse prototype #818

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
481 changes: 418 additions & 63 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
resolver = "2"
members = [
"vello",
"vello_api",
"vello_encoding",
"vello_hybrid",
"vello_shaders",
"vello_tests",

Expand Down
23 changes: 23 additions & 0 deletions vello_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "vello_api"
version = "0.1.0"
authors = ["Raph Levien <raph.levien@gmail.com>"]
description = "A testbend for next-generation 2D renderer ideas"
keywords = ["graphics", "2d"]
categories = ["graphics"]
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true

[package.metadata.docs.rs]
all-features = true
# There are no platform specific docs.
default-target = "x86_64-unknown-linux-gnu"
targets = []

[lints]
workspace = true

[dependencies]
peniko = "0.3.1"
5 changes: 5 additions & 0 deletions vello_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# vello_api

Experiment in API abstraction for 2D rendering.

TODO: explain
254 changes: 254 additions & 0 deletions vello_api/src/any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
// Copyright 2024 the Vello Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![allow(unused, reason = "prototyping")]

use std::{any::Any, sync::Arc};

use peniko::{kurbo::Affine, BrushRef};

use crate::{Id, Path, Record, RenderCtx, ResourceCtx};

#[derive(Clone)]
pub struct AnyImage {
// TODO: move id into trait
id: Id,
body: Arc<dyn Any + Send + Sync>,
}

pub trait AnyRecord: Send {
fn as_any(&mut self) -> &mut dyn std::any::Any;

fn dyn_finish(&mut self) -> Arc<dyn Any + Send>;
}

impl<R: Record + Send + 'static> AnyRecord for R
where
<<R as RenderCtx>::Resource as ResourceCtx>::Recording: Sync,
{
fn as_any(&mut self) -> &mut dyn std::any::Any {
self
}

fn dyn_finish(&mut self) -> Arc<dyn Any + Send> {
let recording = self.finish();
Arc::new(recording)
}
}

pub trait AnyRenderCtx {
fn as_any(&mut self) -> &mut dyn std::any::Any;

fn dyn_playback(&mut self, recording: &Arc<dyn Any + Send>);

fn dyn_fill(&mut self, path: &Path, brush: BrushRef<'_>);
}

impl<RC: RenderCtx + 'static> AnyRenderCtx for RC {
fn as_any(&mut self) -> &mut dyn std::any::Any {
self
}

fn dyn_playback(&mut self, recording: &Arc<dyn Any + Send>) {
if let Some(recording) = recording.downcast_ref() {
self.playback(recording);
} else {
panic!("downcast error on playback");
}
}

fn dyn_fill(&mut self, path: &Path, brush: BrushRef<'_>) {
self.fill(path, brush);
}
}

pub type BoxedRenderCtx = Box<dyn AnyRenderCtx>;

impl RenderCtx for BoxedRenderCtx {
type Resource = Box<dyn AnyResourceCtx>;

fn playback(&mut self, recording: &Arc<<Self::Resource as ResourceCtx>::Recording>) {
self.dyn_playback(recording);
}

fn fill(&mut self, path: &Path, brush: BrushRef<'_>) {
self.dyn_fill(path, brush);
}

fn stroke(&mut self, path: &Path, stroke: &peniko::kurbo::Stroke, brush: BrushRef<'_>) {
todo!()
}

fn draw_image(
&mut self,
image: &<Self::Resource as ResourceCtx>::Image,
dst_rect: peniko::kurbo::Rect,
interp: crate::InterpolationMode,
) {
todo!()
}

fn clip(&mut self, path: &Path) {
todo!()
}

fn save(&mut self) {
todo!()
}

fn restore(&mut self) {
todo!()
}

fn transform(&mut self, affine: Affine) {
todo!()
}

fn begin_draw_glyphs(&mut self, font: &peniko::Font) {
todo!()
}

fn font_size(&mut self, size: f32) {
todo!()
}

fn hint(&mut self, hint: bool) {
todo!()
}

fn glyph_brush(&mut self, brush: BrushRef<'_>) {
todo!()
}

fn draw_glyphs(
&mut self,
style: peniko::StyleRef<'_>,
glyphs: &dyn Iterator<Item = crate::Glyph>,
) {
todo!()
}

fn end_draw_glyphs(&mut self) {
todo!()
}
}

pub trait AnyResourceCtx {
fn as_any(&mut self) -> &mut dyn std::any::Any;

fn dyn_record(&mut self) -> Box<dyn AnyRecord + Send>;

fn dyn_make_image_with_stride(
&mut self,
width: usize,
height: usize,
stride: usize,
buf: &[u8],
format: crate::ImageFormat,
) -> Result<AnyImage, crate::Error>;
}

impl ResourceCtx for Box<dyn AnyResourceCtx> {
type Image = AnyImage;

type Recording = dyn Any + Send;

type Record = Box<dyn AnyRecord>;

fn record(&mut self) -> Self::Record {
self.dyn_record()
}

fn make_image_with_stride(
&mut self,
width: usize,
height: usize,
stride: usize,
buf: &[u8],
format: crate::ImageFormat,
) -> Result<Self::Image, crate::Error> {
let image = self.dyn_make_image_with_stride(width, height, stride, buf, format)?;
let id = Id::get();
Ok(AnyImage {
id,
body: Arc::new(image),
})
}
}

pub struct BoxedAnyRecord(Option<Box<dyn AnyRecord>>);

impl RenderCtx for Box<dyn AnyRecord> {
type Resource = Box<dyn AnyResourceCtx>;

fn playback(&mut self, recording: &Arc<<Self::Resource as ResourceCtx>::Recording>) {
self.dyn_playback(recording);
}

fn fill(&mut self, path: &Path, brush: BrushRef<'_>) {
self.dyn_fill(path, brush);
}

fn stroke(&mut self, path: &Path, stroke: &peniko::kurbo::Stroke, brush: BrushRef<'_>) {
todo!()
}

fn draw_image(
&mut self,
image: &<Self::Resource as ResourceCtx>::Image,
dst_rect: peniko::kurbo::Rect,
interp: crate::InterpolationMode,
) {
todo!()
}

fn clip(&mut self, path: &Path) {
todo!()
}

fn save(&mut self) {
todo!()
}

fn restore(&mut self) {
todo!()
}

fn transform(&mut self, affine: Affine) {
todo!()
}

fn begin_draw_glyphs(&mut self, font: &peniko::Font) {
todo!()
}

fn font_size(&mut self, size: f32) {
todo!()
}

fn hint(&mut self, hint: bool) {
todo!()
}

fn glyph_brush(&mut self, brush: BrushRef<'_>) {
todo!()
}

fn draw_glyphs(
&mut self,
style: peniko::StyleRef<'_>,
glyphs: &dyn Iterator<Item = crate::Glyph>,
) {
todo!()
}

fn end_draw_glyphs(&mut self) {
todo!()
}
}

impl Record for Box<dyn AnyRecord> {
fn finish(&mut self) -> Arc<<Self::Resource as ResourceCtx>::Recording> {
self.dyn_finish()
}
}
Loading
Loading