From 9cab8b81316f462cdc142a6f5e20bb949da0c2f4 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Wed, 18 Aug 2021 12:04:43 -0700 Subject: [PATCH 1/5] Switch to swash Use swash instead of ttf-parser. We can definitely do higher-level use of the swash crate, but this leaves the integration pretty much as-is. --- Cargo.lock | 34 ++++++--- piet-gpu/Cargo.toml | 2 +- piet-gpu/bin/android.rs | 3 +- piet-gpu/src/text.rs | 155 ++++++++++++++++++++++------------------ 4 files changed, 114 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d3c1aa..59604da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ab_glyph_rasterizer" version = "0.1.4" @@ -795,7 +797,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f923fb806c46266c02ab4a5b239735c144bdeda724a50ed058e5226f594cde3" dependencies = [ - "ttf-parser 0.6.2", + "ttf-parser", ] [[package]] @@ -855,7 +857,7 @@ dependencies = [ "rand", "raw-window-handle", "roxmltree", - "ttf-parser 0.12.0", + "swash", "winit", ] @@ -1102,6 +1104,16 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" +[[package]] +name = "swash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1bdb2004b76b5f5f3afe722d70b1aea160d2362cb7e40716a7106197ee7f82d" +dependencies = [ + "yazi", + "zeno", +] + [[package]] name = "syn" version = "1.0.48" @@ -1157,12 +1169,6 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc" -[[package]] -name = "ttf-parser" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85e00391c1f3d171490a3f8bd79999b0002ae38d3da0d6a3a306c754b053d71b" - [[package]] name = "unic-bidi" version = "0.9.0" @@ -1459,3 +1465,15 @@ name = "xmlparser" version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "114ba2b24d2167ef6d67d7d04c8cc86522b87f490025f39f0303b7db5bf5e3d8" + +[[package]] +name = "yazi" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03b3e19c937b5b9bd8e52b1c88f30cce5c0d33d676cf174866175bb794ff658" + +[[package]] +name = "zeno" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c110ba09c9b3a43edd4803d570df0da2414fed6e822e22b976a4e3ef50860701" diff --git a/piet-gpu/Cargo.toml b/piet-gpu/Cargo.toml index dd342a1..3d18d48 100644 --- a/piet-gpu/Cargo.toml +++ b/piet-gpu/Cargo.toml @@ -32,7 +32,7 @@ rand = "0.7.3" roxmltree = "0.13" winit = "0.23" clap = "2.33" -ttf-parser = "0.12" +swash = "0.1.4" [target.'cfg(target_os = "android")'.dependencies] ndk = "0.3" diff --git a/piet-gpu/bin/android.rs b/piet-gpu/bin/android.rs index 3a697d1..6c7af35 100644 --- a/piet-gpu/bin/android.rs +++ b/piet-gpu/bin/android.rs @@ -100,8 +100,7 @@ impl GfxState { ) -> Result { unsafe { let device = instance.device(surface)?; - let mut swapchain = - instance.swapchain(width, height, &device, surface.unwrap())?; + let mut swapchain = instance.swapchain(width, height, &device, surface.unwrap())?; let session = Session::new(device); let mut current_frame = 0; let present_semaphores = (0..NUM_FRAMES) diff --git a/piet-gpu/src/text.rs b/piet-gpu/src/text.rs index 9980e20..64ee88a 100644 --- a/piet-gpu/src/text.rs +++ b/piet-gpu/src/text.rs @@ -1,6 +1,8 @@ use std::ops::RangeBounds; -use ttf_parser::{Face, GlyphId, OutlineBuilder}; +use swash::scale::ScaleContext; +use swash::zeno::{Vector, Verb}; +use swash::{FontRef, GlyphId}; use piet::kurbo::{Point, Rect, Size}; use piet::{ @@ -18,7 +20,9 @@ const FONT_DATA: &[u8] = include_bytes!("../third-party/Roboto-Regular.ttf"); #[derive(Clone)] pub struct Font { - face: Face<'static>, + // Storing the font_ref is ok for static font data, but the better way to do + // this is to store the CacheKey. + font_ref: FontRef<'static>, } #[derive(Clone)] @@ -48,8 +52,6 @@ struct Glyph { #[derive(Default)] pub struct PathEncoder { - start_pt: [f32; 2], - cur_pt: [f32; 2], elements: Vec, } @@ -112,14 +114,79 @@ impl TextLayout for PietGpuTextLayout { impl Font { pub fn new() -> Font { - let face = Face::from_slice(FONT_DATA, 0).expect("error parsing font"); - Font { face } + let font_ref = FontRef::from_index(FONT_DATA, 0).expect("error parsing font"); + Font { font_ref } } fn make_path(&self, glyph_id: GlyphId) -> PathEncoder { + /* let mut encoder = PathEncoder::default(); self.face.outline_glyph(glyph_id, &mut encoder); encoder + */ + // Should the scale context be in the font? In the RenderCtx? + let mut scale_context = ScaleContext::new(); + let mut scaler = scale_context.builder(self.font_ref).size(2048.).build(); + let mut encoder = PathEncoder::default(); + if let Some(outline) = scaler.scale_outline(glyph_id) { + let verbs = outline.verbs(); + let points = outline.points(); + let elements = &mut encoder.elements; + let mut i = 0; + let mut start_pt = [0.0f32; 2]; + let mut last_pt = [0.0f32; 2]; + for verb in verbs { + match verb { + Verb::MoveTo => { + start_pt = convert_swash_point(points[i]); + last_pt = start_pt; + i += 1; + } + Verb::LineTo => { + let p1 = convert_swash_point(points[i]); + elements.push(Element::Line(LineSeg { + p0: last_pt, + p1, + })); + last_pt = p1; + i += 1; + } + Verb::QuadTo => { + let p1 = convert_swash_point(points[i]); + let p2 = convert_swash_point(points[i + 1]); + elements.push(Element::Quad(QuadSeg { + p0: last_pt, + p1, + p2, + })); + last_pt = p2; + i += 2; + } + Verb::CurveTo => { + let p1 = convert_swash_point(points[i]); + let p2 = convert_swash_point(points[i + 1]); + let p3 = convert_swash_point(points[i + 2]); + elements.push(Element::Cubic(CubicSeg { + p0: last_pt, + p1, + p2, + p3, + })); + last_pt = p3; + i += 3; + } + Verb::Close => { + if start_pt != last_pt { + elements.push(Element::Line(LineSeg { + p0: last_pt, + p1: start_pt, + })); + } + } + } + } + } + encoder } } @@ -129,24 +196,23 @@ impl PietGpuTextLayout { let mut x = 0.0; let y = 0.0; for c in text.chars() { - if let Some(glyph_id) = font.face.glyph_index(c) { - let glyph = Glyph { glyph_id, x, y }; - glyphs.push(glyph); - if let Some(adv) = font.face.glyph_hor_advance(glyph_id) { - x += adv as f32; - } - } + let glyph_id = font.font_ref.charmap().map(c); + let glyph = Glyph { glyph_id, x, y }; + glyphs.push(glyph); + let adv = font.font_ref.glyph_metrics(&[]).advance_width(glyph_id); + x += adv; } PietGpuTextLayout { glyphs, font: font.clone(), - size: size, + size, } } pub(crate) fn draw_text(&self, ctx: &mut PietGpuRenderContext, pos: Point) { - const DEFAULT_UPEM: u16 = 1024; - let scale = self.size as f32 / self.font.face.units_per_em().unwrap_or(DEFAULT_UPEM) as f32; + // Should we use ppem from font, or let swash scale? + const DEFAULT_UPEM: u16 = 2048; + let scale = self.size as f32 / DEFAULT_UPEM as f32; let mut inv_transform = None; // TODO: handle y offsets also let mut last_x = 0.0; @@ -237,61 +303,12 @@ impl TextLayoutBuilder for PietGpuTextLayoutBuilder { } } -impl OutlineBuilder for PathEncoder { - fn move_to(&mut self, x: f32, y: f32) { - self.start_pt = [x, y]; - self.cur_pt = [x, y]; - } - - fn line_to(&mut self, x: f32, y: f32) { - let p1 = [x, y]; - let seg = LineSeg { - p0: self.cur_pt, - p1: p1, - }; - self.cur_pt = p1; - self.elements.push(Element::Line(seg)); - } - - fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { - let p1 = [x1, y1]; - let p2 = [x, y]; - let seg = QuadSeg { - p0: self.cur_pt, - p1: p1, - p2: p2, - }; - self.cur_pt = p2; - self.elements.push(Element::Quad(seg)); - } - - fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { - let p1 = [x1, y1]; - let p2 = [x2, y2]; - let p3 = [x, y]; - let seg = CubicSeg { - p0: self.cur_pt, - p1: p1, - p2: p2, - p3: p3, - }; - self.cur_pt = p3; - self.elements.push(Element::Cubic(seg)); - } - - fn close(&mut self) { - if self.cur_pt != self.start_pt { - let seg = LineSeg { - p0: self.cur_pt, - p1: self.start_pt, - }; - self.elements.push(Element::Line(seg)); - } - } -} - impl PathEncoder { pub(crate) fn elements(&self) -> &[Element] { &self.elements } } + +fn convert_swash_point(v: Vector) -> [f32; 2] { + [v.x, v.y] +} From 02ee369428c19875f52aafed49e9acca673a93c5 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Fri, 20 Aug 2021 12:20:27 -0700 Subject: [PATCH 2/5] Start work on emoji WIP --- piet-gpu/src/lib.rs | 2 +- piet-gpu/src/render_ctx.rs | 2 +- piet-gpu/src/text.rs | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/piet-gpu/src/lib.rs b/piet-gpu/src/lib.rs index 096f84e..c171c62 100644 --- a/piet-gpu/src/lib.rs +++ b/piet-gpu/src/lib.rs @@ -192,7 +192,7 @@ fn render_text_test(rc: &mut impl RenderContext) { //rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0])); let layout = rc .text() - .new_text_layout("hello piet-gpu text!") + .new_text_layout("\u{1f600}hello piet-gpu text!") .default_attribute(TextAttribute::FontSize(100.0)) .build() .unwrap(); diff --git a/piet-gpu/src/render_ctx.rs b/piet-gpu/src/render_ctx.rs index 43762fa..eb8fbea 100644 --- a/piet-gpu/src/render_ctx.rs +++ b/piet-gpu/src/render_ctx.rs @@ -492,7 +492,7 @@ impl PietGpuRenderContext { pub(crate) fn append_path_encoder(&mut self, path: &PathEncoder) { let elements = path.elements(); self.elements.extend(elements.iter().cloned()); - self.pathseg_count += elements.len(); + self.pathseg_count += path.n_segs(); } pub(crate) fn fill_glyph(&mut self, rgba_color: u32) { diff --git a/piet-gpu/src/text.rs b/piet-gpu/src/text.rs index 64ee88a..db8b547 100644 --- a/piet-gpu/src/text.rs +++ b/piet-gpu/src/text.rs @@ -16,7 +16,7 @@ use crate::render_ctx::{self, FillMode}; use crate::PietGpuRenderContext; // This is very much a hack to get things working. -const FONT_DATA: &[u8] = include_bytes!("../third-party/Roboto-Regular.ttf"); +const FONT_DATA: &[u8] = include_bytes!("c:\\Windows\\Fonts\\seguiemj.ttf"); #[derive(Clone)] pub struct Font { @@ -53,6 +53,9 @@ struct Glyph { #[derive(Default)] pub struct PathEncoder { elements: Vec, + n_segs: usize, + // If this is zero, then it's a text glyph and should be followed by a fill + n_colr_layers: usize, } impl PietGpuText { @@ -128,6 +131,7 @@ impl Font { let mut scale_context = ScaleContext::new(); let mut scaler = scale_context.builder(self.font_ref).size(2048.).build(); let mut encoder = PathEncoder::default(); + println!("glyph {} has_color_outlines {}", glyph_id, scaler.has_color_outlines()); if let Some(outline) = scaler.scale_outline(glyph_id) { let verbs = outline.verbs(); let points = outline.points(); @@ -186,6 +190,7 @@ impl Font { } } } + encoder.n_segs = encoder.elements.len(); encoder } } @@ -307,6 +312,10 @@ impl PathEncoder { pub(crate) fn elements(&self) -> &[Element] { &self.elements } + + pub(crate) fn n_segs(&self) -> usize { + self.n_segs + } } fn convert_swash_point(v: Vector) -> [f32; 2] { From ef5ef2745ceb247e258c2df5875678821eb5ddbe Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Wed, 25 Aug 2021 13:59:26 -0700 Subject: [PATCH 3/5] Render color emoji layers A bit hacky still, but does render color in Segoe color emoji. --- piet-gpu-types/src/scene.rs | 3 +- piet-gpu/src/render_ctx.rs | 5 ++ piet-gpu/src/text.rs | 153 ++++++++++++++++++++++-------------- 3 files changed, 101 insertions(+), 60 deletions(-) diff --git a/piet-gpu-types/src/scene.rs b/piet-gpu-types/src/scene.rs index bd500d1..9591f04 100644 --- a/piet-gpu-types/src/scene.rs +++ b/piet-gpu-types/src/scene.rs @@ -1,7 +1,8 @@ use piet_gpu_derive::piet_gpu; pub use self::scene::{ - Clip, CubicSeg, Element, FillColor, FillLinGradient, LineSeg, QuadSeg, SetFillMode, SetLineWidth, Transform, + Clip, CubicSeg, Element, FillColor, FillLinGradient, LineSeg, QuadSeg, SetFillMode, + SetLineWidth, Transform, }; piet_gpu! { diff --git a/piet-gpu/src/render_ctx.rs b/piet-gpu/src/render_ctx.rs index eb8fbea..1bddb2e 100644 --- a/piet-gpu/src/render_ctx.rs +++ b/piet-gpu/src/render_ctx.rs @@ -501,6 +501,11 @@ impl PietGpuRenderContext { self.path_count += 1; } + /// Bump the path count when rendering a color emoji. + pub(crate) fn bump_n_paths(&mut self, n_paths: usize) { + self.path_count += n_paths; + } + pub(crate) fn encode_transform(&mut self, transform: Transform) { self.elements.push(Element::Transform(transform)); self.trans_count += 1; diff --git a/piet-gpu/src/text.rs b/piet-gpu/src/text.rs index db8b547..8d8066c 100644 --- a/piet-gpu/src/text.rs +++ b/piet-gpu/src/text.rs @@ -10,13 +10,14 @@ use piet::{ TextLayoutBuilder, TextStorage, }; -use piet_gpu_types::scene::{CubicSeg, Element, LineSeg, QuadSeg, Transform}; +use piet_gpu_types::scene::{CubicSeg, Element, FillColor, LineSeg, QuadSeg, Transform}; use crate::render_ctx::{self, FillMode}; use crate::PietGpuRenderContext; // This is very much a hack to get things working. -const FONT_DATA: &[u8] = include_bytes!("c:\\Windows\\Fonts\\seguiemj.ttf"); +// On Windows, can set this to "c:\\Windows\\Fonts\\seguiemj.ttf" to get color emoji +const FONT_DATA: &[u8] = include_bytes!("../third-party/Roboto-Regular.ttf"); #[derive(Clone)] pub struct Font { @@ -118,6 +119,12 @@ impl TextLayout for PietGpuTextLayout { impl Font { pub fn new() -> Font { let font_ref = FontRef::from_index(FONT_DATA, 0).expect("error parsing font"); + for palette in font_ref.color_palettes() { + println!("palette, len={}", palette.len()); + for i in 0..palette.len() { + println!("{}: {:?}", i, palette.get(i)); + } + } Font { font_ref } } @@ -131,66 +138,27 @@ impl Font { let mut scale_context = ScaleContext::new(); let mut scaler = scale_context.builder(self.font_ref).size(2048.).build(); let mut encoder = PathEncoder::default(); - println!("glyph {} has_color_outlines {}", glyph_id, scaler.has_color_outlines()); - if let Some(outline) = scaler.scale_outline(glyph_id) { - let verbs = outline.verbs(); - let points = outline.points(); - let elements = &mut encoder.elements; - let mut i = 0; - let mut start_pt = [0.0f32; 2]; - let mut last_pt = [0.0f32; 2]; - for verb in verbs { - match verb { - Verb::MoveTo => { - start_pt = convert_swash_point(points[i]); - last_pt = start_pt; - i += 1; - } - Verb::LineTo => { - let p1 = convert_swash_point(points[i]); - elements.push(Element::Line(LineSeg { - p0: last_pt, - p1, - })); - last_pt = p1; - i += 1; - } - Verb::QuadTo => { - let p1 = convert_swash_point(points[i]); - let p2 = convert_swash_point(points[i + 1]); - elements.push(Element::Quad(QuadSeg { - p0: last_pt, - p1, - p2, - })); - last_pt = p2; - i += 2; - } - Verb::CurveTo => { - let p1 = convert_swash_point(points[i]); - let p2 = convert_swash_point(points[i + 1]); - let p3 = convert_swash_point(points[i + 2]); - elements.push(Element::Cubic(CubicSeg { - p0: last_pt, - p1, - p2, - p3, - })); - last_pt = p3; - i += 3; - } - Verb::Close => { - if start_pt != last_pt { - elements.push(Element::Line(LineSeg { - p0: last_pt, - p1: start_pt, - })); - } + if scaler.has_color_outlines() { + if let Some(outline) = scaler.scale_color_outline(glyph_id) { + // TODO: be more sophisticated choosing a palette + let palette = self.font_ref.color_palettes().next().unwrap(); + println!("is_color {}", outline.is_color()); + let mut i = 0; + while let Some(layer) = outline.get(i) { + if let Some(color_ix) = layer.color_index() { + let color = palette.get(color_ix); + encoder.append_outline(layer.verbs(), layer.points()); + encoder.append_solid_fill(color); + println!("layer {} color {:?}", i, color); } + i += 1; } + return encoder; } } - encoder.n_segs = encoder.elements.len(); + if let Some(outline) = scaler.scale_outline(glyph_id) { + encoder.append_outline(outline.verbs(), outline.points()); + } encoder } } @@ -255,7 +223,11 @@ impl PietGpuTextLayout { ctx.encode_transform(transform); let path = self.font.make_path(glyph.glyph_id); ctx.append_path_encoder(&path); - ctx.fill_glyph(0xff_ff_ff_ff); + if path.n_colr_layers == 0 { + ctx.fill_glyph(0xff_ff_ff_ff); + } else { + ctx.bump_n_paths(path.n_colr_layers); + } } if let Some(transform) = inv_transform { ctx.encode_transform(transform); @@ -316,6 +288,69 @@ impl PathEncoder { pub(crate) fn n_segs(&self) -> usize { self.n_segs } + + fn append_outline(&mut self, verbs: &[Verb], points: &[Vector]) { + let elements = &mut self.elements; + let old_len = elements.len(); + let mut i = 0; + let mut start_pt = [0.0f32; 2]; + let mut last_pt = [0.0f32; 2]; + for verb in verbs { + match verb { + Verb::MoveTo => { + start_pt = convert_swash_point(points[i]); + last_pt = start_pt; + i += 1; + } + Verb::LineTo => { + let p1 = convert_swash_point(points[i]); + elements.push(Element::Line(LineSeg { p0: last_pt, p1 })); + last_pt = p1; + i += 1; + } + Verb::QuadTo => { + let p1 = convert_swash_point(points[i]); + let p2 = convert_swash_point(points[i + 1]); + elements.push(Element::Quad(QuadSeg { + p0: last_pt, + p1, + p2, + })); + last_pt = p2; + i += 2; + } + Verb::CurveTo => { + let p1 = convert_swash_point(points[i]); + let p2 = convert_swash_point(points[i + 1]); + let p3 = convert_swash_point(points[i + 2]); + elements.push(Element::Cubic(CubicSeg { + p0: last_pt, + p1, + p2, + p3, + })); + last_pt = p3; + i += 3; + } + Verb::Close => { + if start_pt != last_pt { + elements.push(Element::Line(LineSeg { + p0: last_pt, + p1: start_pt, + })); + } + } + } + } + self.n_segs += elements.len() - old_len; + } + + fn append_solid_fill(&mut self, color: [u8; 4]) { + let rgba_color = u32::from_be_bytes(color); + self.elements + .push(Element::FillColor(FillColor { rgba_color })); + self.n_colr_layers += 1; + } } fn convert_swash_point(v: Vector) -> [f32; 2] { From 4b2a720289c25fade122379ddfc9777ccee324b4 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Tue, 31 Aug 2021 12:46:28 -0700 Subject: [PATCH 4/5] Animating scene Make the scene dependent on timing. This commit patches the HAL to reuse command buffers; this works well on Vulkan and prevents a leak, but breaks the other back-ends. That will require a solution, possibly including plumbing up the resource lifetime responsibilities to the client. Other things might be hacky as well. --- piet-gpu-hal/src/hub.rs | 6 +- piet-gpu/bin/android.rs | 12 +- piet-gpu/bin/cli.rs | 6 +- piet-gpu/bin/winit.rs | 15 ++- piet-gpu/src/lib.rs | 187 +------------------------------- piet-gpu/src/render_ctx.rs | 3 + piet-gpu/src/test_scenes.rs | 211 ++++++++++++++++++++++++++++++++++++ piet-gpu/src/text.rs | 8 -- 8 files changed, 245 insertions(+), 203 deletions(-) create mode 100644 piet-gpu/src/test_scenes.rs diff --git a/piet-gpu-hal/src/hub.rs b/piet-gpu-hal/src/hub.rs index 5df54a2..f4a7938 100644 --- a/piet-gpu-hal/src/hub.rs +++ b/piet-gpu-hal/src/hub.rs @@ -173,7 +173,7 @@ impl Session { // Reuse of command buffers works on Vulkan, but not at all on // Metal and is problematic on DX12 (the allocator is returned) // to the pool. Punt for now. - /* + let mut pool = self.0.cmd_buf_pool.lock().unwrap(); pool.push((item.cmd_buf, item.fence)); std::mem::drop(item.resources); @@ -181,7 +181,7 @@ impl Session { pool.push((staging_cmd_buf.cmd_buf, staging_cmd_buf.fence)); std::mem::drop(staging_cmd_buf.resources); } - */ + } else { i += 1; } @@ -569,13 +569,11 @@ impl SubmittedCmdBuf { session.device.wait_and_reset(vec![&mut item.fence])?; } // See discussion in `poll_cleanup` - /* session .cmd_buf_pool .lock() .unwrap() .push((item.cmd_buf, item.fence)); - */ std::mem::drop(item.resources); } // else session dropped error? diff --git a/piet-gpu/bin/android.rs b/piet-gpu/bin/android.rs index 6c7af35..c008704 100644 --- a/piet-gpu/bin/android.rs +++ b/piet-gpu/bin/android.rs @@ -16,7 +16,7 @@ use piet_gpu_hal::{ Swapchain, }; -use piet_gpu::{render_scene, PietGpuRenderContext, Renderer}; +use piet_gpu::{test_scenes, PietGpuRenderContext, Renderer}; #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))] fn main() { @@ -62,7 +62,7 @@ fn my_main() -> Result<(), Error> { } Event::WindowRedrawNeeded => { if let Some(gfx_state) = gfx_state.as_mut() { - for _ in 0..10 { + for _ in 0..1000 { gfx_state.redraw(); } } @@ -111,7 +111,7 @@ impl GfxState { .collect::, Error>>()?; let mut ctx = PietGpuRenderContext::new(); - render_scene(&mut ctx); + test_scenes::render_anim_frame(&mut ctx, 0); let mut renderer = Renderer::new(&session, width, height)?; renderer.upload_render_ctx(&mut ctx)?; @@ -138,6 +138,12 @@ impl GfxState { if let Some(submitted) = self.submitted.take() { submitted.wait().unwrap(); + let mut ctx = PietGpuRenderContext::new(); + test_scenes::render_anim_frame(&mut ctx, self.current_frame); + if let Err(e) = self.renderer.upload_render_ctx(&mut ctx) { + println!("error in uploading: {}", e); + } + let ts = self .session .fetch_query_pool(&self.query_pools[self.last_frame_idx]) diff --git a/piet-gpu/bin/cli.rs b/piet-gpu/bin/cli.rs index 3165fe0..f67afe7 100644 --- a/piet-gpu/bin/cli.rs +++ b/piet-gpu/bin/cli.rs @@ -6,7 +6,7 @@ use clap::{App, Arg}; use piet_gpu_hal::{BufferUsage, Error, Instance, Session}; -use piet_gpu::{render_scene, render_svg, PietGpuRenderContext, Renderer}; +use piet_gpu::{test_scenes, PietGpuRenderContext, Renderer}; const WIDTH: usize = 2048; const HEIGHT: usize = 1536; @@ -243,9 +243,9 @@ fn main() -> Result<(), Error> { if matches.is_present("flip") { scale = -scale; } - render_svg(&mut ctx, input, scale); + test_scenes::render_svg(&mut ctx, input, scale); } else { - render_scene(&mut ctx); + test_scenes::render_scene(&mut ctx); } let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?; diff --git a/piet-gpu/bin/winit.rs b/piet-gpu/bin/winit.rs index 2da6fe7..c5f9bef 100644 --- a/piet-gpu/bin/winit.rs +++ b/piet-gpu/bin/winit.rs @@ -1,6 +1,7 @@ +use piet::RenderContext; use piet_gpu_hal::{Error, ImageLayout, Instance, Session, SubmittedCmdBuf}; -use piet_gpu::{render_scene, render_svg, PietGpuRenderContext, Renderer}; +use piet_gpu::{test_scenes, PietGpuRenderContext, Renderer}; use clap::{App, Arg}; @@ -60,9 +61,10 @@ fn main() -> Result<(), Error> { if matches.is_present("flip") { scale = -scale; } - render_svg(&mut ctx, input, scale); + test_scenes::render_svg(&mut ctx, input, scale); } else { - render_scene(&mut ctx); + test_scenes::render_scene(&mut ctx); + //test_scenes::render_anim_frame(&mut ctx, 0); } let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?; @@ -96,6 +98,13 @@ fn main() -> Result<(), Error> { // Getting this right will take some thought. if let Some(submitted) = submitted.take() { submitted.wait().unwrap(); + if matches.value_of("INPUT").is_none() { + let mut ctx = PietGpuRenderContext::new(); + test_scenes::render_anim_frame(&mut ctx, current_frame); + if let Err(e) = renderer.upload_render_ctx(&mut ctx) { + println!("error in uploading: {}", e); + } + } let ts = session.fetch_query_pool(&query_pools[last_frame_idx]).unwrap(); window.set_title(&format!( diff --git a/piet-gpu/src/lib.rs b/piet-gpu/src/lib.rs index c171c62..6c9a82d 100644 --- a/piet-gpu/src/lib.rs +++ b/piet-gpu/src/lib.rs @@ -1,6 +1,7 @@ mod gradient; mod pico_svg; mod render_ctx; +pub mod test_scenes; mod text; use std::convert::TryInto; @@ -31,188 +32,6 @@ const PTCL_INITIAL_ALLOC: usize = 1024; const MAX_BLEND_STACK: usize = 128; -const N_CIRCLES: usize = 0; - -pub fn render_svg(rc: &mut impl RenderContext, filename: &str, scale: f64) { - let xml_str = std::fs::read_to_string(filename).unwrap(); - let start = std::time::Instant::now(); - let svg = PicoSvg::load(&xml_str, scale).unwrap(); - println!("parsing time: {:?}", start.elapsed()); - - let start = std::time::Instant::now(); - svg.render(rc); - println!("flattening and encoding time: {:?}", start.elapsed()); -} - -pub fn render_scene(rc: &mut impl RenderContext) { - const WIDTH: usize = 2048; - const HEIGHT: usize = 1536; - let mut rng = rand::thread_rng(); - for _ in 0..N_CIRCLES { - let color = Color::from_rgba32_u32(rng.next_u32()); - let center = Point::new( - rng.gen_range(0.0, WIDTH as f64), - rng.gen_range(0.0, HEIGHT as f64), - ); - let radius = rng.gen_range(0.0, 50.0); - let circle = Circle::new(center, radius); - rc.fill(circle, &color); - } - let _ = rc.save(); - let mut path = BezPath::new(); - path.move_to((200.0, 150.0)); - path.line_to((100.0, 200.0)); - path.line_to((150.0, 250.0)); - path.close_path(); - rc.clip(path); - - let mut path = BezPath::new(); - path.move_to((100.0, 150.0)); - path.line_to((200.0, 200.0)); - path.line_to((150.0, 250.0)); - path.close_path(); - rc.fill(path, &Color::rgb8(128, 0, 128)); - let _ = rc.restore(); - rc.stroke( - piet::kurbo::Line::new((100.0, 100.0), (200.0, 150.0)), - &Color::WHITE, - 5.0, - ); - //render_cardioid(rc); - render_clip_test(rc); - render_alpha_test(rc); - render_gradient_test(rc); - render_text_test(rc); - //render_tiger(rc); -} - -#[allow(unused)] -fn render_cardioid(rc: &mut impl RenderContext) { - let n = 601; - let dth = std::f64::consts::PI * 2.0 / (n as f64); - let center = Point::new(1024.0, 768.0); - let r = 750.0; - let mut path = BezPath::new(); - for i in 1..n { - let p0 = center + Vec2::from_angle(i as f64 * dth) * r; - let p1 = center + Vec2::from_angle(((i * 2) % n) as f64 * dth) * r; - //rc.fill(&Circle::new(p0, 8.0), &Color::WHITE); - path.move_to(p0); - path.line_to(p1); - //rc.stroke(Line::new(p0, p1), &Color::BLACK, 2.0); - } - rc.stroke(&path, &Color::BLACK, 2.0); -} - -#[allow(unused)] -fn render_clip_test(rc: &mut impl RenderContext) { - const N: usize = 16; - const X0: f64 = 50.0; - const Y0: f64 = 450.0; - // Note: if it gets much larger, it will exceed the 1MB scratch buffer. - // But this is a pretty demanding test. - const X1: f64 = 550.0; - const Y1: f64 = 950.0; - let step = 1.0 / ((N + 1) as f64); - for i in 0..N { - let t = ((i + 1) as f64) * step; - rc.save(); - let mut path = BezPath::new(); - path.move_to((X0, Y0)); - path.line_to((X1, Y0)); - path.line_to((X1, Y0 + t * (Y1 - Y0))); - path.line_to((X1 + t * (X0 - X1), Y1)); - path.line_to((X0, Y1)); - path.close_path(); - rc.clip(path); - } - let rect = piet::kurbo::Rect::new(X0, Y0, X1, Y1); - rc.fill(rect, &Color::BLACK); - for _ in 0..N { - rc.restore(); - } -} - -#[allow(unused)] -fn render_alpha_test(rc: &mut impl RenderContext) { - // Alpha compositing tests. - rc.fill( - diamond(Point::new(1024.0, 100.0)), - &Color::Rgba32(0xff0000ff), - ); - rc.fill( - diamond(Point::new(1024.0, 125.0)), - &Color::Rgba32(0x00ff0080), - ); - rc.save(); - rc.clip(diamond(Point::new(1024.0, 150.0))); - rc.fill( - diamond(Point::new(1024.0, 175.0)), - &Color::Rgba32(0x0000ff80), - ); - rc.restore(); -} - -#[allow(unused)] -fn render_gradient_test(rc: &mut impl RenderContext) { - let stops = vec![ - GradientStop { - color: Color::rgb8(0, 255, 0), - pos: 0.0, - }, - GradientStop { - color: Color::BLACK, - pos: 1.0, - }, - ]; - let lin = FixedLinearGradient { - start: Point::new(0.0, 100.0), - end: Point::new(0.0, 300.0), - stops, - }; - let brush = FixedGradient::Linear(lin); - //let brush = Color::rgb8(0, 128, 0); - rc.fill(Rect::new(100.0, 100.0, 300.0, 300.0), &brush); -} - -fn diamond(origin: Point) -> impl Shape { - let mut path = BezPath::new(); - const SIZE: f64 = 50.0; - path.move_to((origin.x, origin.y - SIZE)); - path.line_to((origin.x + SIZE, origin.y)); - path.line_to((origin.x, origin.y + SIZE)); - path.line_to((origin.x - SIZE, origin.y)); - path.close_path(); - return path; -} - -#[allow(unused)] -fn render_text_test(rc: &mut impl RenderContext) { - rc.save(); - //rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0])); - let layout = rc - .text() - .new_text_layout("\u{1f600}hello piet-gpu text!") - .default_attribute(TextAttribute::FontSize(100.0)) - .build() - .unwrap(); - rc.draw_text(&layout, Point::new(110.0, 600.0)); - rc.draw_text(&layout, Point::new(110.0, 700.0)); - rc.restore(); -} - -#[allow(unused)] -fn render_tiger(rc: &mut impl RenderContext) { - let xml_str = std::str::from_utf8(include_bytes!("../Ghostscript_Tiger.svg")).unwrap(); - let start = std::time::Instant::now(); - let svg = PicoSvg::load(xml_str, 8.0).unwrap(); - println!("parsing time: {:?}", start.elapsed()); - - let start = std::time::Instant::now(); - svg.render(rc); - println!("flattening and encoding time: {:?}", start.elapsed()); -} - #[allow(unused)] fn dump_scene(buf: &[u8]) { for i in 0..(buf.len() / 4) { @@ -478,6 +297,10 @@ impl Renderer { } pub unsafe fn record(&self, cmd_buf: &mut CmdBuf, query_pool: &QueryPool) { + //cmd_buf.clear_buffer(&self.memory_buf_dev, None); + //cmd_buf.memory_barrier(); + // Only need to copy the first few words; need to upgrade HAL to be able to + // express sub-buffer copies. cmd_buf.copy_buffer(&self.memory_buf_host, &self.memory_buf_dev); cmd_buf.clear_buffer(&self.state_buf, None); cmd_buf.memory_barrier(); diff --git a/piet-gpu/src/render_ctx.rs b/piet-gpu/src/render_ctx.rs index 1bddb2e..4847051 100644 --- a/piet-gpu/src/render_ctx.rs +++ b/piet-gpu/src/render_ctx.rs @@ -102,6 +102,9 @@ impl PietGpuRenderContext { } pub fn get_scene_buf(&mut self) -> &[u8] { + const ALIGN: usize = 128; + let padded_size = (self.elements.len() + (ALIGN - 1)) & ALIGN.wrapping_neg(); + self.elements.resize(padded_size, Element::Nop()); self.elements.encode(&mut self.encoder); self.encoder.buf() } diff --git a/piet-gpu/src/test_scenes.rs b/piet-gpu/src/test_scenes.rs new file mode 100644 index 0000000..fa4c8e3 --- /dev/null +++ b/piet-gpu/src/test_scenes.rs @@ -0,0 +1,211 @@ +//! Various synthetic scenes for exercising the renderer. + +use rand::{Rng, RngCore}; + +use piet::{Color, FixedGradient, FixedLinearGradient, GradientStop, Text, TextAttribute, TextLayoutBuilder}; +use piet::kurbo::{BezPath, Circle, Line, Point, Rect, Shape}; + +use crate::{RenderContext, PicoSvg, Vec2}; + +const N_CIRCLES: usize = 0; + +pub fn render_svg(rc: &mut impl RenderContext, filename: &str, scale: f64) { + let xml_str = std::fs::read_to_string(filename).unwrap(); + let start = std::time::Instant::now(); + let svg = PicoSvg::load(&xml_str, scale).unwrap(); + println!("parsing time: {:?}", start.elapsed()); + + let start = std::time::Instant::now(); + svg.render(rc); + println!("flattening and encoding time: {:?}", start.elapsed()); +} + +pub fn render_scene(rc: &mut impl RenderContext) { + const WIDTH: usize = 2048; + const HEIGHT: usize = 1536; + let mut rng = rand::thread_rng(); + for _ in 0..N_CIRCLES { + let color = Color::from_rgba32_u32(rng.next_u32()); + let center = Point::new( + rng.gen_range(0.0, WIDTH as f64), + rng.gen_range(0.0, HEIGHT as f64), + ); + let radius = rng.gen_range(0.0, 50.0); + let circle = Circle::new(center, radius); + rc.fill(circle, &color); + } + let _ = rc.save(); + let mut path = BezPath::new(); + path.move_to((200.0, 150.0)); + path.line_to((100.0, 200.0)); + path.line_to((150.0, 250.0)); + path.close_path(); + rc.clip(path); + + let mut path = BezPath::new(); + path.move_to((100.0, 150.0)); + path.line_to((200.0, 200.0)); + path.line_to((150.0, 250.0)); + path.close_path(); + rc.fill(path, &Color::rgb8(128, 0, 128)); + let _ = rc.restore(); + rc.stroke( + piet::kurbo::Line::new((100.0, 100.0), (200.0, 150.0)), + &Color::WHITE, + 5.0, + ); + //render_cardioid(rc); + render_clip_test(rc); + render_alpha_test(rc); + render_gradient_test(rc); + render_text_test(rc); + //render_tiger(rc); +} + +#[allow(unused)] +fn render_cardioid(rc: &mut impl RenderContext) { + let n = 601; + let dth = std::f64::consts::PI * 2.0 / (n as f64); + let center = Point::new(1024.0, 768.0); + let r = 750.0; + let mut path = BezPath::new(); + for i in 1..n { + let p0 = center + Vec2::from_angle(i as f64 * dth) * r; + let p1 = center + Vec2::from_angle(((i * 2) % n) as f64 * dth) * r; + //rc.fill(&Circle::new(p0, 8.0), &Color::WHITE); + path.move_to(p0); + path.line_to(p1); + //rc.stroke(Line::new(p0, p1), &Color::BLACK, 2.0); + } + rc.stroke(&path, &Color::BLACK, 2.0); +} + +#[allow(unused)] +fn render_clip_test(rc: &mut impl RenderContext) { + const N: usize = 16; + const X0: f64 = 50.0; + const Y0: f64 = 450.0; + // Note: if it gets much larger, it will exceed the 1MB scratch buffer. + // But this is a pretty demanding test. + const X1: f64 = 550.0; + const Y1: f64 = 950.0; + let step = 1.0 / ((N + 1) as f64); + for i in 0..N { + let t = ((i + 1) as f64) * step; + rc.save(); + let mut path = BezPath::new(); + path.move_to((X0, Y0)); + path.line_to((X1, Y0)); + path.line_to((X1, Y0 + t * (Y1 - Y0))); + path.line_to((X1 + t * (X0 - X1), Y1)); + path.line_to((X0, Y1)); + path.close_path(); + rc.clip(path); + } + let rect = piet::kurbo::Rect::new(X0, Y0, X1, Y1); + rc.fill(rect, &Color::BLACK); + for _ in 0..N { + rc.restore(); + } +} + +#[allow(unused)] +fn render_alpha_test(rc: &mut impl RenderContext) { + // Alpha compositing tests. + rc.fill( + diamond(Point::new(1024.0, 100.0)), + &Color::Rgba32(0xff0000ff), + ); + rc.fill( + diamond(Point::new(1024.0, 125.0)), + &Color::Rgba32(0x00ff0080), + ); + rc.save(); + rc.clip(diamond(Point::new(1024.0, 150.0))); + rc.fill( + diamond(Point::new(1024.0, 175.0)), + &Color::Rgba32(0x0000ff80), + ); + rc.restore(); +} + +#[allow(unused)] +fn render_gradient_test(rc: &mut impl RenderContext) { + let stops = vec![ + GradientStop { + color: Color::rgb8(0, 255, 0), + pos: 0.0, + }, + GradientStop { + color: Color::BLACK, + pos: 1.0, + }, + ]; + let lin = FixedLinearGradient { + start: Point::new(0.0, 100.0), + end: Point::new(0.0, 300.0), + stops, + }; + let brush = FixedGradient::Linear(lin); + //let brush = Color::rgb8(0, 128, 0); + rc.fill(Rect::new(100.0, 100.0, 300.0, 300.0), &brush); +} + +fn diamond(origin: Point) -> impl Shape { + let mut path = BezPath::new(); + const SIZE: f64 = 50.0; + path.move_to((origin.x, origin.y - SIZE)); + path.line_to((origin.x + SIZE, origin.y)); + path.line_to((origin.x, origin.y + SIZE)); + path.line_to((origin.x - SIZE, origin.y)); + path.close_path(); + return path; +} + +#[allow(unused)] +fn render_text_test(rc: &mut impl RenderContext) { + rc.save(); + //rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0])); + let layout = rc + .text() + .new_text_layout("\u{1f600}hello piet-gpu text!") + .default_attribute(TextAttribute::FontSize(100.0)) + .build() + .unwrap(); + rc.draw_text(&layout, Point::new(110.0, 600.0)); + rc.draw_text(&layout, Point::new(110.0, 700.0)); + rc.restore(); +} + +#[allow(unused)] +fn render_tiger(rc: &mut impl RenderContext) { + let xml_str = std::str::from_utf8(include_bytes!("../Ghostscript_Tiger.svg")).unwrap(); + let start = std::time::Instant::now(); + let svg = PicoSvg::load(xml_str, 8.0).unwrap(); + println!("parsing time: {:?}", start.elapsed()); + + let start = std::time::Instant::now(); + svg.render(rc); + println!("flattening and encoding time: {:?}", start.elapsed()); +} + +pub fn render_anim_frame(rc: &mut impl RenderContext, i: usize) { + rc.fill(Rect::new(0.0, 0.0, 1000.0, 1000.0), &Color::rgb8(128, 128, 128)); + let text_size = 60.0 + 40.0 * (0.01 * i as f64).sin(); + rc.save().unwrap(); + //rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0])); + let layout = rc + .text() + .new_text_layout("\u{1f600}hello piet-gpu text!") + .default_attribute(TextAttribute::FontSize(text_size)) + .build() + .unwrap(); + rc.draw_text(&layout, Point::new(110.0, 600.0)); + rc.draw_text(&layout, Point::new(110.0, 700.0)); + rc.restore().unwrap(); + let th = (std::f64::consts::PI / 180.0) * (i as f64); + let center = Point::new(500.0, 500.0); + let p1 = center + 400.0 * Vec2::from_angle(th); + let line = Line::new(center, p1); + rc.stroke(line, &Color::rgb8(128, 0, 0), 5.0); +} \ No newline at end of file diff --git a/piet-gpu/src/text.rs b/piet-gpu/src/text.rs index 8d8066c..2287237 100644 --- a/piet-gpu/src/text.rs +++ b/piet-gpu/src/text.rs @@ -119,12 +119,6 @@ impl TextLayout for PietGpuTextLayout { impl Font { pub fn new() -> Font { let font_ref = FontRef::from_index(FONT_DATA, 0).expect("error parsing font"); - for palette in font_ref.color_palettes() { - println!("palette, len={}", palette.len()); - for i in 0..palette.len() { - println!("{}: {:?}", i, palette.get(i)); - } - } Font { font_ref } } @@ -142,14 +136,12 @@ impl Font { if let Some(outline) = scaler.scale_color_outline(glyph_id) { // TODO: be more sophisticated choosing a palette let palette = self.font_ref.color_palettes().next().unwrap(); - println!("is_color {}", outline.is_color()); let mut i = 0; while let Some(layer) = outline.get(i) { if let Some(color_ix) = layer.color_index() { let color = palette.get(color_ix); encoder.append_outline(layer.verbs(), layer.points()); encoder.append_solid_fill(color); - println!("layer {} color {:?}", i, color); } i += 1; } From 056446c23d752c462721f2b4a04a06b00d0689ac Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Mon, 6 Sep 2021 10:19:55 -0700 Subject: [PATCH 5/5] Cargo fmt --- piet-gpu-hal/src/hub.rs | 3 +-- piet-gpu/src/test_scenes.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/piet-gpu-hal/src/hub.rs b/piet-gpu-hal/src/hub.rs index f4a7938..8312d4e 100644 --- a/piet-gpu-hal/src/hub.rs +++ b/piet-gpu-hal/src/hub.rs @@ -173,7 +173,7 @@ impl Session { // Reuse of command buffers works on Vulkan, but not at all on // Metal and is problematic on DX12 (the allocator is returned) // to the pool. Punt for now. - + let mut pool = self.0.cmd_buf_pool.lock().unwrap(); pool.push((item.cmd_buf, item.fence)); std::mem::drop(item.resources); @@ -181,7 +181,6 @@ impl Session { pool.push((staging_cmd_buf.cmd_buf, staging_cmd_buf.fence)); std::mem::drop(staging_cmd_buf.resources); } - } else { i += 1; } diff --git a/piet-gpu/src/test_scenes.rs b/piet-gpu/src/test_scenes.rs index fa4c8e3..47ace66 100644 --- a/piet-gpu/src/test_scenes.rs +++ b/piet-gpu/src/test_scenes.rs @@ -2,10 +2,12 @@ use rand::{Rng, RngCore}; -use piet::{Color, FixedGradient, FixedLinearGradient, GradientStop, Text, TextAttribute, TextLayoutBuilder}; use piet::kurbo::{BezPath, Circle, Line, Point, Rect, Shape}; +use piet::{ + Color, FixedGradient, FixedLinearGradient, GradientStop, Text, TextAttribute, TextLayoutBuilder, +}; -use crate::{RenderContext, PicoSvg, Vec2}; +use crate::{PicoSvg, RenderContext, Vec2}; const N_CIRCLES: usize = 0; @@ -190,7 +192,10 @@ fn render_tiger(rc: &mut impl RenderContext) { } pub fn render_anim_frame(rc: &mut impl RenderContext, i: usize) { - rc.fill(Rect::new(0.0, 0.0, 1000.0, 1000.0), &Color::rgb8(128, 128, 128)); + rc.fill( + Rect::new(0.0, 0.0, 1000.0, 1000.0), + &Color::rgb8(128, 128, 128), + ); let text_size = 60.0 + 40.0 * (0.01 * i as f64).sin(); rc.save().unwrap(); //rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0])); @@ -208,4 +213,4 @@ pub fn render_anim_frame(rc: &mut impl RenderContext, i: usize) { let p1 = center + 400.0 * Vec2::from_angle(th); let line = Line::new(center, p1); rc.stroke(line, &Color::rgb8(128, 0, 0), 5.0); -} \ No newline at end of file +}