From 2303111e09d806ef2a652bddc2b73be6dccf6ae2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Nov 2019 01:55:32 +0100 Subject: Draft new layout API --- wgpu/src/renderer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 235eefc6..664853ba 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -423,7 +423,7 @@ impl Debugger for Renderer { fn explain( &mut self, widget: &dyn Widget, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, color: Color, ) -> Self::Output { @@ -438,7 +438,7 @@ impl Debugger for Renderer { } fn explain_layout( - layout: Layout, + layout: &Layout, color: Color, primitives: &mut Vec, ) { -- cgit From 0240c3981b716c82ecb3364945815335b420a63e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Nov 2019 06:05:20 +0100 Subject: Draft custom layout engine based on `druid` --- wgpu/src/renderer.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 664853ba..7f7b79a5 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -9,9 +9,8 @@ use wgpu::{ Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions, TextureFormat, }; -use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section}; -use std::{cell::RefCell, rc::Rc}; +use std::cell::RefCell; mod target; mod widget; @@ -24,7 +23,7 @@ pub struct Renderer { quad_pipeline: quad::Pipeline, image_pipeline: crate::image::Pipeline, - glyph_brush: Rc>>, + glyph_brush: RefCell>, } pub struct Layer<'a> { @@ -72,8 +71,10 @@ impl Renderer { .load(&[font::Family::Monospace]) .expect("Find monospace font"); + let fonts = vec![default_font, mono_font]; + let glyph_brush = - GlyphBrushBuilder::using_fonts_bytes(vec![default_font, mono_font]) + wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts) .initial_cache_size((2048, 2048)) .build(&mut device, TextureFormat::Bgra8UnormSrgb); @@ -86,7 +87,7 @@ impl Renderer { quad_pipeline, image_pipeline, - glyph_brush: Rc::new(RefCell::new(glyph_brush)), + glyph_brush: RefCell::new(glyph_brush), } } @@ -190,7 +191,7 @@ impl Renderer { } }; - layer.text.push(Section { + layer.text.push(wgpu_glyph::Section { text: &content, screen_position: ( x - layer.offset.x as f32, @@ -297,22 +298,22 @@ impl Renderer { let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 }; for (i, line) in lines.iter().enumerate() { - overlay.text.push(Section { + overlay.text.push(wgpu_glyph::Section { text: line.as_ref(), screen_position: (11.0, 11.0 + 25.0 * i as f32), color: [0.9, 0.9, 0.9, 1.0], scale, font_id, - ..Section::default() + ..wgpu_glyph::Section::default() }); - overlay.text.push(Section { + overlay.text.push(wgpu_glyph::Section { text: line.as_ref(), screen_position: (10.0, 10.0 + 25.0 * i as f32), color: [0.0, 0.0, 0.0, 1.0], scale, font_id, - ..Section::default() + ..wgpu_glyph::Section::default() }); } @@ -364,7 +365,7 @@ impl Renderer { for text in layer.text.iter() { // Target physical coordinates directly to avoid blurry text - let text = Section { + let text = wgpu_glyph::Section { screen_position: ( (text.screen_position.0 * dpi).round(), (text.screen_position.1 * dpi).round(), @@ -423,7 +424,7 @@ impl Debugger for Renderer { fn explain( &mut self, widget: &dyn Widget, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, color: Color, ) -> Self::Output { @@ -438,7 +439,7 @@ impl Debugger for Renderer { } fn explain_layout( - layout: &Layout, + layout: Layout<'_>, color: Color, primitives: &mut Vec, ) { -- cgit From 860a6923bbed57a21ce4b2cae331f6a3a51ca3fe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 11 Nov 2019 06:07:31 +0100 Subject: Split text measurements cache from rendering cache This speeds up layouting in the most common scenario considerably! :tada: --- wgpu/src/renderer.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 7f7b79a5..87e499e2 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -22,8 +22,8 @@ pub struct Renderer { queue: Queue, quad_pipeline: quad::Pipeline, image_pipeline: crate::image::Pipeline, - - glyph_brush: RefCell>, + text_pipeline: wgpu_glyph::GlyphBrush<'static, ()>, + text_measurements: RefCell>, } pub struct Layer<'a> { @@ -73,11 +73,14 @@ impl Renderer { let fonts = vec![default_font, mono_font]; - let glyph_brush = - wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts) + let text_pipeline = + wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts.clone()) .initial_cache_size((2048, 2048)) .build(&mut device, TextureFormat::Bgra8UnormSrgb); + let text_measurements = + glyph_brush::GlyphBrushBuilder::using_fonts_bytes(fonts).build(); + let quad_pipeline = quad::Pipeline::new(&mut device); let image_pipeline = crate::image::Pipeline::new(&mut device); @@ -86,8 +89,8 @@ impl Renderer { queue, quad_pipeline, image_pipeline, - - glyph_brush: RefCell::new(glyph_brush), + text_pipeline, + text_measurements: RefCell::new(text_measurements), } } @@ -293,8 +296,7 @@ impl Renderer { let first = layers.first().unwrap(); let mut overlay = Layer::new(first.bounds, Vector::new(0, 0)); - let font_id = - wgpu_glyph::FontId(self.glyph_brush.borrow().fonts().len() - 1); + let font_id = wgpu_glyph::FontId(self.text_pipeline.fonts().len() - 1); let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 }; for (i, line) in lines.iter().enumerate() { @@ -361,8 +363,6 @@ impl Renderer { } if layer.text.len() > 0 { - let mut glyph_brush = self.glyph_brush.borrow_mut(); - for text in layer.text.iter() { // Target physical coordinates directly to avoid blurry text let text = wgpu_glyph::Section { @@ -378,10 +378,10 @@ impl Renderer { ..*text }; - glyph_brush.queue(text); + self.text_pipeline.queue(text); } - glyph_brush + self.text_pipeline .draw_queued_with_transform_and_scissoring( &mut self.device, encoder, @@ -401,6 +401,25 @@ impl Renderer { impl iced_native::Renderer for Renderer { type Output = (Primitive, MouseCursor); + + fn layout<'a, Message>( + &mut self, + element: &iced_native::Element<'a, Message, Self>, + ) -> iced_native::layout::Node { + let node = element.layout(self, &iced_native::layout::Limits::NONE); + + // Trim measurements cache + // TODO: We should probably use a `GlyphCalculator` for this. However, + // it uses a lifetimed `GlyphCalculatorGuard` with side-effects on drop. + // This makes stuff quite inconvenient. A manual method for trimming the + // cache would make our lives easier. + self.text_measurements + .borrow_mut() + .process_queued(|_, _| {}, |_| {}) + .expect("Trim text measurements"); + + node + } } impl Windowed for Renderer { -- cgit From 73f3c900071f950ea914652ca3f0002c1e173f61 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 11 Nov 2019 21:33:05 +0100 Subject: Load default font only in measurement cache --- wgpu/src/renderer.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 87e499e2..e9df3550 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -71,15 +71,17 @@ impl Renderer { .load(&[font::Family::Monospace]) .expect("Find monospace font"); - let fonts = vec![default_font, mono_font]; - let text_pipeline = - wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts.clone()) - .initial_cache_size((2048, 2048)) - .build(&mut device, TextureFormat::Bgra8UnormSrgb); + wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![ + default_font.clone(), + mono_font, + ]) + .initial_cache_size((2048, 2048)) + .build(&mut device, TextureFormat::Bgra8UnormSrgb); let text_measurements = - glyph_brush::GlyphBrushBuilder::using_fonts_bytes(fonts).build(); + glyph_brush::GlyphBrushBuilder::using_font_bytes(default_font) + .build(); let quad_pipeline = quad::Pipeline::new(&mut device); let image_pipeline = crate::image::Pipeline::new(&mut device); -- cgit