Skip to content

Commit

Permalink
Merge pull request #208 from dfrg/peniko
Browse files Browse the repository at this point in the history
Update piet-scene to depend on peniko
  • Loading branch information
dfrg authored Nov 23, 2022
2 parents 0755e8b + eccce74 commit 5dbeb99
Show file tree
Hide file tree
Showing 29 changed files with 443 additions and 1,438 deletions.
21 changes: 14 additions & 7 deletions Cargo.lock

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

58 changes: 30 additions & 28 deletions pgpu-render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

mod render;

use piet_scene::{Brush, Color, Fill, PathElement};
use piet_scene::kurbo::{Affine, PathEl, Point};
use piet_scene::{Brush, Color, Fill};
use render::*;
use std::ffi::c_void;
use std::mem::transmute;
Expand Down Expand Up @@ -145,7 +146,7 @@ pub struct PgpuPathElement {
pub points: [PgpuPoint; 3],
}

#[derive(Clone)]
#[derive(Copy, Clone)]
#[repr(C)]
pub struct PgpuPathIter {
pub context: *mut c_void,
Expand Down Expand Up @@ -197,16 +198,16 @@ pub struct PgpuTransform {
pub dy: f32,
}

impl From<PgpuTransform> for piet_scene::Affine {
impl From<PgpuTransform> for Affine {
fn from(xform: PgpuTransform) -> Self {
Self {
xx: xform.xx,
yx: xform.yx,
xy: xform.xy,
yy: xform.yy,
dx: xform.dx,
dy: xform.dy,
}
Affine::new([
xform.xx as f64,
xform.yx as f64,
xform.xy as f64,
xform.yy as f64,
xform.dx as f64,
xform.dy as f64,
])
}
}

Expand Down Expand Up @@ -239,27 +240,26 @@ pub unsafe extern "C" fn pgpu_scene_builder_add_glyph(
}

impl Iterator for PgpuPathIter {
type Item = PathElement;
type Item = PathEl;

fn next(&mut self) -> Option<Self::Item> {
let mut el = PgpuPathElement {
verb: PgpuPathVerb::MoveTo,
points: [PgpuPoint::default(); 3],
};
fn conv_pt(pt: PgpuPoint) -> Point {
Point::new(pt.x as f64, pt.y as f64)
}
if (self.next_element)(self.context, &mut el as _) {
let p = &el.points;
Some(match el.verb {
PgpuPathVerb::MoveTo => PathElement::MoveTo((p[0].x, p[0].y).into()),
PgpuPathVerb::LineTo => PathElement::LineTo((p[0].x, p[0].y).into()),
PgpuPathVerb::QuadTo => {
PathElement::QuadTo((p[0].x, p[0].y).into(), (p[1].x, p[1].y).into())
PgpuPathVerb::MoveTo => PathEl::MoveTo(conv_pt(p[0])),
PgpuPathVerb::LineTo => PathEl::LineTo(conv_pt(p[0])),
PgpuPathVerb::QuadTo => PathEl::QuadTo(conv_pt(p[0]), conv_pt(p[1])),
PgpuPathVerb::CurveTo => {
PathEl::CurveTo(conv_pt(p[0]), conv_pt(p[1]), conv_pt(p[2]))
}
PgpuPathVerb::CurveTo => PathElement::CurveTo(
(p[0].x, p[0].y).into(),
(p[1].x, p[1].y).into(),
(p[2].x, p[2].y).into(),
),
PgpuPathVerb::Close => PathElement::Close,
PgpuPathVerb::Close => PathEl::ClosePath,
})
} else {
None
Expand Down Expand Up @@ -308,12 +308,13 @@ pub unsafe extern "C" fn pgpu_scene_builder_fill_path(
} else {
Some((*brush_transform).into())
};
let path_els = (*path).collect::<Vec<_>>();
(*builder).builder.fill(
fill,
(*builder).transform,
&brush,
brush_transform,
(*path).clone(),
&&path_els[..],
);
}

Expand Down Expand Up @@ -445,13 +446,14 @@ pub unsafe extern "C" fn pgpu_glyph_bbox(
glyph: *const PgpuGlyph,
transform: &[f32; 6],
) -> PgpuRect {
let transform = piet_scene::Affine::new(transform);
let transform: PgpuTransform = std::mem::transmute(*transform);
let transform = transform.into();
let rect = (*glyph).bbox(Some(transform));
PgpuRect {
x0: rect.min.x,
y0: rect.min.y,
x1: rect.max.x,
y1: rect.max.y,
x0: rect.min_x() as f32,
y0: rect.min_y() as f32,
x1: rect.max_x() as f32,
y1: rect.max_y() as f32,
}
}

Expand Down
29 changes: 20 additions & 9 deletions pgpu-render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use piet_gpu::{PixelFormat, RenderConfig};
use piet_gpu_hal::{QueryPool, Session};
use piet_scene::glyph::pinot::{types::Tag, FontDataRef};
use piet_scene::glyph::{GlyphContext, GlyphProvider};
use piet_scene::{Affine, Rect, Scene, SceneFragment};
use piet_scene::kurbo::{Affine, Point, Rect};
use piet_scene::{Scene, SceneFragment};

/// State and resources for rendering a scene.
pub struct PgpuRenderer {
Expand Down Expand Up @@ -141,7 +142,7 @@ pub struct PgpuSceneBuilder<'a> {
}

impl<'a> PgpuSceneBuilder<'a> {
pub fn add_glyph(&mut self, glyph: &PgpuGlyph, transform: &piet_scene::Affine) {
pub fn add_glyph(&mut self, glyph: &PgpuGlyph, transform: &Affine) {
self.builder.append(&glyph.fragment, Some(*transform));
}

Expand Down Expand Up @@ -214,15 +215,25 @@ pub struct PgpuGlyph {

impl PgpuGlyph {
pub fn bbox(&self, transform: Option<Affine>) -> Rect {
let points = self.fragment.points();
if points.is_empty() {
return Rect::default();
}
let mut points = points
.iter()
.map(|pt| Point::new(pt[0] as f64, pt[1] as f64));
if let Some(transform) = &transform {
Rect::from_points(
self.fragment
.points()
.iter()
.map(|p| p.transform(transform)),
)
let mut rect = Rect::from_center_size(points.next().unwrap(), (0.0, 0.0));
for point in points {
rect = rect.union_pt(*transform * point);
}
rect
} else {
Rect::from_points(self.fragment.points())
let mut rect = Rect::from_center_size(points.next().unwrap(), (0.0, 0.0));
for point in points {
rect = rect.union_pt(point);
}
rect
}
}
}
2 changes: 0 additions & 2 deletions piet-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ path = "../piet-gpu-types"

[dependencies.piet-scene]
path = "../piet-scene"
features = ["kurbo"]

[dependencies]
png = "0.17.6"
Expand All @@ -38,7 +37,6 @@ winit = {version = "0.27.3", default-features = false, features = ["x11", "wayl
raw-window-handle = "0.5"
clap = "3.2.22"
bytemuck = { version = "1.7.2", features = ["derive"] }
kurbo = "0.8.3"

[target.'cfg(target_os = "android")'.dependencies]
ndk = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion piet-gpu/bin/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn render_info(simple_text: &mut SimpleText, sb: &mut SceneBuilder, info: &str)
None,
40.0,
None,
piet_scene::Affine::translate(110.0, 50.0),
piet_scene::kurbo::Affine::translate((110.0, 50.0)),
info,
);
}
3 changes: 1 addition & 2 deletions piet-gpu/src/pico_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::str::FromStr;

use roxmltree::{Document, Node};

use kurbo::{Affine, BezPath};

use piet_scene::kurbo::{Affine, BezPath};
use piet_scene::Color;

pub struct PicoSvg {
Expand Down
8 changes: 4 additions & 4 deletions piet-gpu/src/ramp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use piet_scene::{Color, GradientStop, GradientStops};
use piet_scene::{Color, ColorStop, ColorStops};

use std::collections::HashMap;

Expand All @@ -8,7 +8,7 @@ const RETAINED_COUNT: usize = 64;
#[derive(Default)]
pub struct RampCache {
epoch: u64,
map: HashMap<GradientStops, (u32, u64)>,
map: HashMap<ColorStops, (u32, u64)>,
data: Vec<u32>,
}

Expand All @@ -22,7 +22,7 @@ impl RampCache {
}
}

pub fn add(&mut self, stops: &[GradientStop]) -> u32 {
pub fn add(&mut self, stops: &[ColorStop]) -> u32 {
if let Some(entry) = self.map.get_mut(stops) {
entry.1 = self.epoch;
entry.0
Expand Down Expand Up @@ -64,7 +64,7 @@ impl RampCache {
}
}

fn make_ramp<'a>(stops: &'a [GradientStop]) -> impl Iterator<Item = u32> + 'a {
fn make_ramp<'a>(stops: &'a [ColorStop]) -> impl Iterator<Item = u32> + 'a {
let mut last_u = 0.0;
let mut last_c = ColorF64::from_color(stops[0].color);
let mut this_u = last_u;
Expand Down
Loading

0 comments on commit 5dbeb99

Please sign in to comment.