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 +- wgpu/src/renderer/widget/button.rs | 26 +++++++------ wgpu/src/renderer/widget/checkbox.rs | 31 +++++++-------- wgpu/src/renderer/widget/column.rs | 2 +- wgpu/src/renderer/widget/image.rs | 30 +++++--------- wgpu/src/renderer/widget/radio.rs | 29 +++++++------- wgpu/src/renderer/widget/row.rs | 2 +- wgpu/src/renderer/widget/scrollable.rs | 6 +-- wgpu/src/renderer/widget/slider.rs | 25 +++++++----- wgpu/src/renderer/widget/text.rs | 71 +++++----------------------------- 10 files changed, 86 insertions(+), 140 deletions(-) (limited to 'wgpu') 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, ) { diff --git a/wgpu/src/renderer/widget/button.rs b/wgpu/src/renderer/widget/button.rs index 0ac1c0a6..e8a97385 100644 --- a/wgpu/src/renderer/widget/button.rs +++ b/wgpu/src/renderer/widget/button.rs @@ -1,25 +1,27 @@ use crate::{Primitive, Renderer}; use iced_native::{ - button, Align, Background, Button, Layout, Length, MouseCursor, - Node, Point, Rectangle, Style, + button, layout, Background, Button, Layout, MouseCursor, Point, Rectangle, }; impl button::Renderer for Renderer { - fn node(&self, button: &Button) -> Node { - let style = Style::default() - .width(button.width) - .padding(button.padding) - .min_width(Length::Units(100)) - .align_self(button.align_self) - .align_items(Align::Stretch); - - Node::with_children(style, vec![button.content.node(self)]) + fn layout( + &self, + button: &Button, + limits: &layout::Limits, + ) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, + }) } fn draw( &mut self, button: &Button, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index 1594c769..de921e6b 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -1,31 +1,30 @@ use crate::{Primitive, Renderer}; use iced_native::{ - checkbox, text, text::HorizontalAlignment, text::VerticalAlignment, Align, - Background, Checkbox, Column, Layout, Length, MouseCursor, Node, - Point, Rectangle, Row, Text, Widget, + checkbox, layout, text, text::HorizontalAlignment, text::VerticalAlignment, + Background, Checkbox, Layout, MouseCursor, Point, Rectangle, Text, }; const SIZE: f32 = 28.0; impl checkbox::Renderer for Renderer { - fn node(&self, checkbox: &Checkbox) -> Node { - Row::<(), Self>::new() - .width(Length::Fill) - .spacing(15) - .align_items(Align::Center) - .push( - Column::new() - .width(Length::Units(SIZE as u16)) - .height(Length::Units(SIZE as u16)), - ) - .push(Text::new(&checkbox.label)) - .node(self) + fn layout( + &self, + checkbox: &Checkbox, + limits: &layout::Limits, + ) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, + }) } fn draw( &mut self, checkbox: &Checkbox, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/column.rs b/wgpu/src/renderer/widget/column.rs index cac6da77..6355f000 100644 --- a/wgpu/src/renderer/widget/column.rs +++ b/wgpu/src/renderer/widget/column.rs @@ -5,7 +5,7 @@ impl column::Renderer for Renderer { fn draw( &mut self, column: &Column<'_, Message, Self>, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let mut mouse_cursor = MouseCursor::OutOfBounds; diff --git a/wgpu/src/renderer/widget/image.rs b/wgpu/src/renderer/widget/image.rs index 0e312706..ea9d19c7 100644 --- a/wgpu/src/renderer/widget/image.rs +++ b/wgpu/src/renderer/widget/image.rs @@ -1,28 +1,18 @@ use crate::{Primitive, Renderer}; -use iced_native::{image, Image, Layout, Length, MouseCursor, Node, Style}; +use iced_native::{image, layout, Image, Layout, MouseCursor, Rectangle}; impl image::Renderer for Renderer { - fn node(&self, image: &Image) -> Node { - let (width, height) = self.image_pipeline.dimensions(&image.path); - - let aspect_ratio = width as f32 / height as f32; - - let mut style = Style::default().align_self(image.align_self); - - // TODO: Deal with additional cases - style = match (image.width, image.height) { - (Length::Units(width), _) => style.width(image.width).height( - Length::Units((width as f32 / aspect_ratio).round() as u16), - ), - (_, _) => style - .width(Length::Units(width as u16)) - .height(Length::Units(height as u16)), - }; - - Node::new(style) + fn layout(&self, image: &Image, limits: &layout::Limits) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, + }) } - fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output { + fn draw(&mut self, image: &Image, layout: &Layout) -> Self::Output { ( Primitive::Image { path: image.path.clone(), diff --git a/wgpu/src/renderer/widget/radio.rs b/wgpu/src/renderer/widget/radio.rs index 61f5ce47..9f3c528f 100644 --- a/wgpu/src/renderer/widget/radio.rs +++ b/wgpu/src/renderer/widget/radio.rs @@ -1,30 +1,31 @@ use crate::{Primitive, Renderer}; use iced_native::{ - radio, text, Align, Background, Column, Layout, Length, MouseCursor, - Node, Point, Radio, Rectangle, Row, Text, Widget, + layout, radio, text, Background, Layout, MouseCursor, Point, Radio, + Rectangle, Text, }; const SIZE: f32 = 28.0; const DOT_SIZE: f32 = SIZE / 2.0; impl radio::Renderer for Renderer { - fn node(&self, radio: &Radio) -> Node { - Row::<(), Self>::new() - .spacing(15) - .align_items(Align::Center) - .push( - Column::new() - .width(Length::Units(SIZE as u16)) - .height(Length::Units(SIZE as u16)), - ) - .push(Text::new(&radio.label)) - .node(self) + fn layout( + &self, + radio: &Radio, + limits: &layout::Limits, + ) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, + }) } fn draw( &mut self, radio: &Radio, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/row.rs b/wgpu/src/renderer/widget/row.rs index bbfef9a1..4f7cb10e 100644 --- a/wgpu/src/renderer/widget/row.rs +++ b/wgpu/src/renderer/widget/row.rs @@ -5,7 +5,7 @@ impl row::Renderer for Renderer { fn draw( &mut self, row: &Row<'_, Message, Self>, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let mut mouse_cursor = MouseCursor::OutOfBounds; diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs index 5eadf275..4b318d6d 100644 --- a/wgpu/src/renderer/widget/scrollable.rs +++ b/wgpu/src/renderer/widget/scrollable.rs @@ -1,7 +1,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ - scrollable, Background, Layout, MouseCursor, Point, Rectangle, - Scrollable, Vector, Widget, + scrollable, Background, Layout, MouseCursor, Point, Rectangle, Scrollable, + Vector, Widget, }; const SCROLLBAR_WIDTH: u16 = 10; @@ -32,7 +32,7 @@ impl scrollable::Renderer for Renderer { &mut self, scrollable: &Scrollable<'_, Message, Self>, bounds: Rectangle, - content: Layout<'_>, + content: &Layout, cursor_position: Point, ) -> Self::Output { let is_mouse_over = bounds.contains(cursor_position); diff --git a/wgpu/src/renderer/widget/slider.rs b/wgpu/src/renderer/widget/slider.rs index 789e7bd4..a3423785 100644 --- a/wgpu/src/renderer/widget/slider.rs +++ b/wgpu/src/renderer/widget/slider.rs @@ -1,26 +1,31 @@ use crate::{Primitive, Renderer}; use iced_native::{ - slider, Background, Color, Layout, Length, MouseCursor, Node, Point, - Rectangle, Slider, Style, + layout, slider, Background, Color, Layout, MouseCursor, Point, Rectangle, + Slider, }; const HANDLE_WIDTH: f32 = 8.0; const HANDLE_HEIGHT: f32 = 22.0; impl slider::Renderer for Renderer { - fn node(&self, slider: &Slider) -> Node { - let style = Style::default() - .width(slider.width) - .height(Length::Units(HANDLE_HEIGHT as u16)) - .min_width(Length::Units(100)); - - Node::new(style) + fn layout( + &self, + slider: &Slider, + limits: &layout::Limits, + ) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, + }) } fn draw( &mut self, slider: &Slider, - layout: Layout<'_>, + layout: &Layout, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 29e07ff7..d824c07c 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -1,76 +1,25 @@ use crate::{Primitive, Renderer}; -use iced_native::{text, Color, Layout, MouseCursor, Node, Style, Text}; +use iced_native::{layout, text, Color, Layout, MouseCursor, Rectangle, Text}; -use wgpu_glyph::{GlyphCruncher, Section}; +//use wgpu_glyph::{GlyphCruncher, Section}; -use std::cell::RefCell; use std::f32; // TODO: Obtain from renderer configuration const DEFAULT_TEXT_SIZE: f32 = 20.0; impl text::Renderer for Renderer { - fn node(&self, text: &Text) -> Node { - let glyph_brush = self.glyph_brush.clone(); - let content = text.content.clone(); - - // TODO: Investigate why stretch tries to measure this MANY times - // with every ancestor's bounds. - // Bug? Using the library wrong? I should probably open an issue on - // the stretch repository. - // I noticed that the first measure is the one that matters in - // practice. Here, we use a RefCell to store the cached measurement. - let measure = RefCell::new(None); - let size = text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE); - - let style = Style::default().width(text.width); - - iced_native::Node::with_measure(style, move |bounds| { - let mut measure = measure.borrow_mut(); - - if measure.is_none() { - let bounds = ( - match bounds.width { - iced_native::Number::Undefined => f32::INFINITY, - iced_native::Number::Defined(w) => w, - }, - match bounds.height { - iced_native::Number::Undefined => f32::INFINITY, - iced_native::Number::Defined(h) => h, - }, - ); - - let text = Section { - text: &content, - scale: wgpu_glyph::Scale { x: size, y: size }, - bounds, - ..Default::default() - }; - - let (width, height) = if let Some(bounds) = - glyph_brush.borrow_mut().glyph_bounds(&text) - { - (bounds.width().ceil(), bounds.height().ceil()) - } else { - (0.0, 0.0) - }; - - let size = iced_native::Size { width, height }; - - // If the text has no width boundary we avoid caching as the - // layout engine may just be measuring text in a row. - if bounds.0 == f32::INFINITY { - return size; - } else { - *measure = Some(size); - } - } - - measure.unwrap() + fn layout(&self, text: &Text, limits: &layout::Limits) -> Layout { + // TODO + Layout::new(Rectangle { + x: 0.0, + y: 0.0, + width: 0.0, + height: 0.0, }) } - fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output { + fn draw(&mut self, text: &Text, layout: &Layout) -> Self::Output { ( Primitive::Text { content: text.content.clone(), -- 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 +++++++++++++------------ wgpu/src/renderer/widget/button.rs | 29 +++++++++++++++++--------- wgpu/src/renderer/widget/checkbox.rs | 24 +++++++++++++--------- wgpu/src/renderer/widget/column.rs | 2 +- wgpu/src/renderer/widget/image.rs | 33 +++++++++++++++++++++--------- wgpu/src/renderer/widget/radio.rs | 25 +++++++++++++---------- wgpu/src/renderer/widget/row.rs | 2 +- wgpu/src/renderer/widget/scrollable.rs | 2 +- wgpu/src/renderer/widget/slider.rs | 19 ++++++++--------- wgpu/src/renderer/widget/text.rs | 37 ++++++++++++++++++++++++---------- 10 files changed, 121 insertions(+), 79 deletions(-) (limited to 'wgpu') 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, ) { diff --git a/wgpu/src/renderer/widget/button.rs b/wgpu/src/renderer/widget/button.rs index e8a97385..3d5e42ba 100644 --- a/wgpu/src/renderer/widget/button.rs +++ b/wgpu/src/renderer/widget/button.rs @@ -1,6 +1,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ - button, layout, Background, Button, Layout, MouseCursor, Point, Rectangle, + button, layout, Background, Button, Layout, Length, MouseCursor, Point, + Rectangle, }; impl button::Renderer for Renderer { @@ -8,20 +9,28 @@ impl button::Renderer for Renderer { &self, button: &Button, limits: &layout::Limits, - ) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + ) -> layout::Node { + let padding = f32::from(button.padding); + let limits = limits + .min_width(100) + .width(button.width) + .height(Length::Shrink) + .pad(padding); + + let mut content = button.content.layout(self, &limits); + + content.bounds.x = padding; + content.bounds.y = padding; + + let size = limits.resolve(content.size()).pad(padding); + + layout::Node::with_children(size, vec![content]) } fn draw( &mut self, button: &Button, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index de921e6b..c2d7911c 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -1,7 +1,8 @@ use crate::{Primitive, Renderer}; use iced_native::{ checkbox, layout, text, text::HorizontalAlignment, text::VerticalAlignment, - Background, Checkbox, Layout, MouseCursor, Point, Rectangle, Text, + Align, Background, Checkbox, Column, Layout, Length, MouseCursor, Point, + Rectangle, Row, Text, Widget, }; const SIZE: f32 = 28.0; @@ -11,20 +12,23 @@ impl checkbox::Renderer for Renderer { &self, checkbox: &Checkbox, limits: &layout::Limits, - ) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + ) -> layout::Node { + Row::<(), Self>::new() + .spacing(15) + .align_items(Align::Center) + .push( + Column::new() + .width(Length::Units(SIZE as u16)) + .height(Length::Units(SIZE as u16)), + ) + .push(Text::new(&checkbox.label)) + .layout(self, limits) } fn draw( &mut self, checkbox: &Checkbox, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/column.rs b/wgpu/src/renderer/widget/column.rs index 6355f000..cac6da77 100644 --- a/wgpu/src/renderer/widget/column.rs +++ b/wgpu/src/renderer/widget/column.rs @@ -5,7 +5,7 @@ impl column::Renderer for Renderer { fn draw( &mut self, column: &Column<'_, Message, Self>, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let mut mouse_cursor = MouseCursor::OutOfBounds; diff --git a/wgpu/src/renderer/widget/image.rs b/wgpu/src/renderer/widget/image.rs index ea9d19c7..0afb11e3 100644 --- a/wgpu/src/renderer/widget/image.rs +++ b/wgpu/src/renderer/widget/image.rs @@ -1,18 +1,31 @@ use crate::{Primitive, Renderer}; -use iced_native::{image, layout, Image, Layout, MouseCursor, Rectangle}; +use iced_native::{image, layout, Image, Layout, Length, MouseCursor, Size}; impl image::Renderer for Renderer { - fn layout(&self, image: &Image, limits: &layout::Limits) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + fn layout(&self, image: &Image, limits: &layout::Limits) -> layout::Node { + let (width, height) = self.image_pipeline.dimensions(&image.path); + + let aspect_ratio = width as f32 / height as f32; + + // TODO: Deal with additional cases + let (width, height) = match (image.width, image.height) { + (Length::Units(width), _) => ( + image.width, + Length::Units((width as f32 / aspect_ratio).round() as u16), + ), + (_, _) => { + (Length::Units(width as u16), Length::Units(height as u16)) + } + }; + + let mut size = limits.width(width).height(height).resolve(Size::ZERO); + + size.height = size.width / aspect_ratio; + + layout::Node::new(size) } - fn draw(&mut self, image: &Image, layout: &Layout) -> Self::Output { + fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output { ( Primitive::Image { path: image.path.clone(), diff --git a/wgpu/src/renderer/widget/radio.rs b/wgpu/src/renderer/widget/radio.rs index 9f3c528f..1f17ba28 100644 --- a/wgpu/src/renderer/widget/radio.rs +++ b/wgpu/src/renderer/widget/radio.rs @@ -1,7 +1,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ - layout, radio, text, Background, Layout, MouseCursor, Point, Radio, - Rectangle, Text, + layout, radio, text, Align, Background, Column, Layout, Length, + MouseCursor, Point, Radio, Rectangle, Row, Text, Widget, }; const SIZE: f32 = 28.0; @@ -12,20 +12,23 @@ impl radio::Renderer for Renderer { &self, radio: &Radio, limits: &layout::Limits, - ) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + ) -> layout::Node { + Row::<(), Self>::new() + .spacing(15) + .align_items(Align::Center) + .push( + Column::new() + .width(Length::Units(SIZE as u16)) + .height(Length::Units(SIZE as u16)), + ) + .push(Text::new(&radio.label)) + .layout(self, limits) } fn draw( &mut self, radio: &Radio, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/row.rs b/wgpu/src/renderer/widget/row.rs index 4f7cb10e..bbfef9a1 100644 --- a/wgpu/src/renderer/widget/row.rs +++ b/wgpu/src/renderer/widget/row.rs @@ -5,7 +5,7 @@ impl row::Renderer for Renderer { fn draw( &mut self, row: &Row<'_, Message, Self>, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let mut mouse_cursor = MouseCursor::OutOfBounds; diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs index 4b318d6d..dd6ebcc1 100644 --- a/wgpu/src/renderer/widget/scrollable.rs +++ b/wgpu/src/renderer/widget/scrollable.rs @@ -32,7 +32,7 @@ impl scrollable::Renderer for Renderer { &mut self, scrollable: &Scrollable<'_, Message, Self>, bounds: Rectangle, - content: &Layout, + content: Layout<'_>, cursor_position: Point, ) -> Self::Output { let is_mouse_over = bounds.contains(cursor_position); diff --git a/wgpu/src/renderer/widget/slider.rs b/wgpu/src/renderer/widget/slider.rs index a3423785..98065bc9 100644 --- a/wgpu/src/renderer/widget/slider.rs +++ b/wgpu/src/renderer/widget/slider.rs @@ -1,7 +1,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ - layout, slider, Background, Color, Layout, MouseCursor, Point, Rectangle, - Slider, + layout, slider, Background, Color, Layout, Length, MouseCursor, Point, + Rectangle, Size, Slider, }; const HANDLE_WIDTH: f32 = 8.0; @@ -12,20 +12,17 @@ impl slider::Renderer for Renderer { &self, slider: &Slider, limits: &layout::Limits, - ) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + ) -> layout::Node { + let limits = limits.width(slider.width).height(Length::Units(30)); + let size = limits.resolve(Size::ZERO); + + layout::Node::new(size) } fn draw( &mut self, slider: &Slider, - layout: &Layout, + layout: Layout<'_>, cursor_position: Point, ) -> Self::Output { let bounds = layout.bounds(); diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index d824c07c..713b514f 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -1,7 +1,7 @@ use crate::{Primitive, Renderer}; -use iced_native::{layout, text, Color, Layout, MouseCursor, Rectangle, Text}; +use iced_native::{layout, text, Color, Layout, MouseCursor, Size, Text}; -//use wgpu_glyph::{GlyphCruncher, Section}; +use wgpu_glyph::{GlyphCruncher, Section}; use std::f32; @@ -9,17 +9,32 @@ use std::f32; const DEFAULT_TEXT_SIZE: f32 = 20.0; impl text::Renderer for Renderer { - fn layout(&self, text: &Text, limits: &layout::Limits) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + fn layout(&self, text: &Text, limits: &layout::Limits) -> layout::Node { + let limits = limits.width(text.width).height(text.height); + let size = text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE); + let bounds = limits.max(); + + let section = Section { + text: &text.content, + scale: wgpu_glyph::Scale { x: size, y: size }, + bounds: (bounds.width, bounds.height), + ..Default::default() + }; + + let (width, height) = if let Some(bounds) = + self.glyph_brush.borrow_mut().glyph_bounds(§ion) + { + (bounds.width().ceil(), bounds.height().ceil()) + } else { + (0.0, 0.0) + }; + + let size = limits.resolve(Size::new(width, height)); + + layout::Node::new(size) } - fn draw(&mut self, text: &Text, layout: &Layout) -> Self::Output { + fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output { ( Primitive::Text { content: text.content.clone(), -- 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/Cargo.toml | 1 + wgpu/src/renderer.rs | 43 ++++++++++++++++++++++++---------- wgpu/src/renderer/widget/text.rs | 2 +- wgpu/src/renderer/widget/text_input.rs | 4 ++-- 4 files changed, 35 insertions(+), 15 deletions(-) (limited to 'wgpu') diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 032f4ae0..3ebfa7ea 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/hecrj/iced" [dependencies] iced_native = { version = "0.1.0-alpha", path = "../native" } wgpu = "0.4" +glyph_brush = "0.6" wgpu_glyph = { version = "0.5", git = "https://github.com/hecrj/wgpu_glyph", branch = "feature/scissoring" } raw-window-handle = "0.3" image = "0.22" 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 { diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 713b514f..b9ccd787 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -22,7 +22,7 @@ impl text::Renderer for Renderer { }; 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()) } else { diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index b5f6c5f6..75eb20f7 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -78,7 +78,7 @@ impl text_input::Renderer for Renderer { .to_string(); let mut text_value_width = self - .glyph_brush + .text_measurements .borrow_mut() .glyph_bounds(Section { text: text_before_cursor, @@ -94,7 +94,7 @@ impl text_input::Renderer for Renderer { if spaces_at_the_end > 0 { let space_width = { - let glyph_brush = self.glyph_brush.borrow(); + let glyph_brush = self.text_measurements.borrow(); // TODO: Select appropriate font let font = &glyph_brush.fonts()[0]; -- 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') 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