Split text measurements cache from rendering cache
This speeds up layouting in the most common scenario considerably!
🎉
This commit is contained in:
parent
d4d14b68f4
commit
860a6923bb
9 changed files with 53 additions and 24 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
//! Display images in your user interface.
|
//! Display images in your user interface.
|
||||||
|
|
||||||
use crate::{Align, Length, Rectangle};
|
use crate::{Length, Rectangle};
|
||||||
|
|
||||||
/// A frame that displays an image while keeping aspect ratio.
|
/// A frame that displays an image while keeping aspect ratio.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
button, scrollable, Align, Application, Button, Column, Container, Element,
|
button, scrollable, Align, Application, Button, Container, Element, Image,
|
||||||
Image, Length, Scrollable, Text,
|
Length, Scrollable, Text,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,15 @@ mod windowed;
|
||||||
pub use debugger::Debugger;
|
pub use debugger::Debugger;
|
||||||
pub use windowed::{Target, Windowed};
|
pub use windowed::{Target, Windowed};
|
||||||
|
|
||||||
pub trait Renderer {
|
use crate::{layout, Element};
|
||||||
|
|
||||||
|
pub trait Renderer: Sized {
|
||||||
type Output;
|
type Output;
|
||||||
|
|
||||||
|
fn layout<'a, Message>(
|
||||||
|
&mut self,
|
||||||
|
element: &Element<'a, Message, Self>,
|
||||||
|
) -> layout::Node {
|
||||||
|
element.layout(self, &layout::Limits::NONE)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ where
|
||||||
pub fn build<E: Into<Element<'a, Message, Renderer>>>(
|
pub fn build<E: Into<Element<'a, Message, Renderer>>>(
|
||||||
root: E,
|
root: E,
|
||||||
cache: Cache,
|
cache: Cache,
|
||||||
renderer: &Renderer,
|
renderer: &mut Renderer,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let root = root.into();
|
let root = root.into();
|
||||||
|
|
||||||
|
|
@ -110,7 +110,7 @@ where
|
||||||
cache.layout
|
cache.layout
|
||||||
} else {
|
} else {
|
||||||
let layout_start = std::time::Instant::now();
|
let layout_start = std::time::Instant::now();
|
||||||
let layout = root.layout(renderer, &layout::Limits::NONE);
|
let layout = renderer.layout(&root);
|
||||||
dbg!(std::time::Instant::now() - layout_start);
|
dbg!(std::time::Instant::now() - layout_start);
|
||||||
|
|
||||||
layout
|
layout
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ repository = "https://github.com/hecrj/iced"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced_native = { version = "0.1.0-alpha", path = "../native" }
|
iced_native = { version = "0.1.0-alpha", path = "../native" }
|
||||||
wgpu = "0.4"
|
wgpu = "0.4"
|
||||||
|
glyph_brush = "0.6"
|
||||||
wgpu_glyph = { version = "0.5", git = "https://github.com/hecrj/wgpu_glyph", branch = "feature/scissoring" }
|
wgpu_glyph = { version = "0.5", git = "https://github.com/hecrj/wgpu_glyph", branch = "feature/scissoring" }
|
||||||
raw-window-handle = "0.3"
|
raw-window-handle = "0.3"
|
||||||
image = "0.22"
|
image = "0.22"
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,8 @@ pub struct Renderer {
|
||||||
queue: Queue,
|
queue: Queue,
|
||||||
quad_pipeline: quad::Pipeline,
|
quad_pipeline: quad::Pipeline,
|
||||||
image_pipeline: crate::image::Pipeline,
|
image_pipeline: crate::image::Pipeline,
|
||||||
|
text_pipeline: wgpu_glyph::GlyphBrush<'static, ()>,
|
||||||
glyph_brush: RefCell<wgpu_glyph::GlyphBrush<'static, ()>>,
|
text_measurements: RefCell<glyph_brush::GlyphBrush<'static, ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Layer<'a> {
|
pub struct Layer<'a> {
|
||||||
|
|
@ -73,11 +73,14 @@ impl Renderer {
|
||||||
|
|
||||||
let fonts = vec![default_font, mono_font];
|
let fonts = vec![default_font, mono_font];
|
||||||
|
|
||||||
let glyph_brush =
|
let text_pipeline =
|
||||||
wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts)
|
wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(fonts.clone())
|
||||||
.initial_cache_size((2048, 2048))
|
.initial_cache_size((2048, 2048))
|
||||||
.build(&mut device, TextureFormat::Bgra8UnormSrgb);
|
.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 quad_pipeline = quad::Pipeline::new(&mut device);
|
||||||
let image_pipeline = crate::image::Pipeline::new(&mut device);
|
let image_pipeline = crate::image::Pipeline::new(&mut device);
|
||||||
|
|
||||||
|
|
@ -86,8 +89,8 @@ impl Renderer {
|
||||||
queue,
|
queue,
|
||||||
quad_pipeline,
|
quad_pipeline,
|
||||||
image_pipeline,
|
image_pipeline,
|
||||||
|
text_pipeline,
|
||||||
glyph_brush: RefCell::new(glyph_brush),
|
text_measurements: RefCell::new(text_measurements),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -293,8 +296,7 @@ impl Renderer {
|
||||||
let first = layers.first().unwrap();
|
let first = layers.first().unwrap();
|
||||||
let mut overlay = Layer::new(first.bounds, Vector::new(0, 0));
|
let mut overlay = Layer::new(first.bounds, Vector::new(0, 0));
|
||||||
|
|
||||||
let font_id =
|
let font_id = wgpu_glyph::FontId(self.text_pipeline.fonts().len() - 1);
|
||||||
wgpu_glyph::FontId(self.glyph_brush.borrow().fonts().len() - 1);
|
|
||||||
let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };
|
let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };
|
||||||
|
|
||||||
for (i, line) in lines.iter().enumerate() {
|
for (i, line) in lines.iter().enumerate() {
|
||||||
|
|
@ -361,8 +363,6 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if layer.text.len() > 0 {
|
if layer.text.len() > 0 {
|
||||||
let mut glyph_brush = self.glyph_brush.borrow_mut();
|
|
||||||
|
|
||||||
for text in layer.text.iter() {
|
for text in layer.text.iter() {
|
||||||
// Target physical coordinates directly to avoid blurry text
|
// Target physical coordinates directly to avoid blurry text
|
||||||
let text = wgpu_glyph::Section {
|
let text = wgpu_glyph::Section {
|
||||||
|
|
@ -378,10 +378,10 @@ impl Renderer {
|
||||||
..*text
|
..*text
|
||||||
};
|
};
|
||||||
|
|
||||||
glyph_brush.queue(text);
|
self.text_pipeline.queue(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
glyph_brush
|
self.text_pipeline
|
||||||
.draw_queued_with_transform_and_scissoring(
|
.draw_queued_with_transform_and_scissoring(
|
||||||
&mut self.device,
|
&mut self.device,
|
||||||
encoder,
|
encoder,
|
||||||
|
|
@ -401,6 +401,25 @@ impl Renderer {
|
||||||
|
|
||||||
impl iced_native::Renderer for Renderer {
|
impl iced_native::Renderer for Renderer {
|
||||||
type Output = (Primitive, MouseCursor);
|
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 {
|
impl Windowed for Renderer {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ impl text::Renderer for Renderer {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (width, height) = if let Some(bounds) =
|
let (width, height) = if let Some(bounds) =
|
||||||
self.glyph_brush.borrow_mut().glyph_bounds(§ion)
|
self.text_measurements.borrow_mut().glyph_bounds(§ion)
|
||||||
{
|
{
|
||||||
(bounds.width().ceil(), bounds.height().ceil())
|
(bounds.width().ceil(), bounds.height().ceil())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ impl text_input::Renderer for Renderer {
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let mut text_value_width = self
|
let mut text_value_width = self
|
||||||
.glyph_brush
|
.text_measurements
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.glyph_bounds(Section {
|
.glyph_bounds(Section {
|
||||||
text: text_before_cursor,
|
text: text_before_cursor,
|
||||||
|
|
@ -94,7 +94,7 @@ impl text_input::Renderer for Renderer {
|
||||||
|
|
||||||
if spaces_at_the_end > 0 {
|
if spaces_at_the_end > 0 {
|
||||||
let space_width = {
|
let space_width = {
|
||||||
let glyph_brush = self.glyph_brush.borrow();
|
let glyph_brush = self.text_measurements.borrow();
|
||||||
|
|
||||||
// TODO: Select appropriate font
|
// TODO: Select appropriate font
|
||||||
let font = &glyph_brush.fonts()[0];
|
let font = &glyph_brush.fonts()[0];
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ pub trait Application {
|
||||||
let user_interface = UserInterface::build(
|
let user_interface = UserInterface::build(
|
||||||
document(&mut self, size, &mut debug),
|
document(&mut self, size, &mut debug),
|
||||||
Cache::default(),
|
Cache::default(),
|
||||||
&renderer,
|
&mut renderer,
|
||||||
);
|
);
|
||||||
debug.layout_finished();
|
debug.layout_finished();
|
||||||
|
|
||||||
|
|
@ -87,7 +87,7 @@ pub trait Application {
|
||||||
let mut user_interface = UserInterface::build(
|
let mut user_interface = UserInterface::build(
|
||||||
document(&mut self, size, &mut debug),
|
document(&mut self, size, &mut debug),
|
||||||
cache.take().unwrap(),
|
cache.take().unwrap(),
|
||||||
&renderer,
|
&mut renderer,
|
||||||
);
|
);
|
||||||
debug.layout_finished();
|
debug.layout_finished();
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ pub trait Application {
|
||||||
let user_interface = UserInterface::build(
|
let user_interface = UserInterface::build(
|
||||||
document(&mut self, size, &mut debug),
|
document(&mut self, size, &mut debug),
|
||||||
temp_cache,
|
temp_cache,
|
||||||
&renderer,
|
&mut renderer,
|
||||||
);
|
);
|
||||||
debug.layout_finished();
|
debug.layout_finished();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue