diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 53f59303..84d48304 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -60,8 +60,8 @@ pub trait Renderer { /// Fills a [`Quad`] with the provided [`Background`]. fn fill_quad(&mut self, quad: Quad, background: impl Into); - /// Clears all of the recorded primitives in the [`Renderer`]. - fn clear(&mut self); + /// Resets the [`Renderer`] to start drawing in the `new_bounds` from scratch. + fn reset(&mut self, new_bounds: Rectangle); } /// A polygon with four sides. diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index 2251e527..60c87a81 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -16,7 +16,7 @@ impl Renderer for () { fn end_transformation(&mut self) {} - fn clear(&mut self) {} + fn reset(&mut self, _new_bounds: Rectangle) {} fn fill_quad( &mut self, diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index c9a818fb..dbab0192 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -9,6 +9,9 @@ pub trait Layer: Default { /// Creates a new [`Layer`] with the given bounds. fn with_bounds(bounds: Rectangle) -> Self; + /// Returns the current bounds of the [`Layer`]. + fn bounds(&self) -> Rectangle; + /// Flushes and settles any pending group of primitives in the [`Layer`]. /// /// This will be called when a [`Layer`] is finished. It allows layers to efficiently @@ -20,6 +23,24 @@ pub trait Layer: Default { /// Clears all the layers contents and resets its bounds. fn reset(&mut self); + + /// Returns the start level of the [`Layer`]. + /// + /// A level is a "sublayer" index inside of a [`Layer`]. + /// + /// A [`Layer`] may draw multiple primitive types in a certain order. + /// The level represents the lowest index of the primitive types it + /// contains. + /// + /// Two layers A and B can therefore be merged if they have the same bounds, + /// and the end level of A is lower or equal than the start level of B. + fn start(&self) -> usize; + + /// Returns the end level of the [`Layer`]. + fn end(&self) -> usize; + + /// Merges a [`Layer`] with the current one. + fn merge(&mut self, _layer: &mut Self); } /// A stack of layers used for drawing. @@ -82,7 +103,19 @@ impl Stack { pub fn pop_clip(&mut self) { self.flush(); - self.current = self.previous.pop().unwrap(); + let previous = self.previous.pop().unwrap(); + + let (head, tail) = self.layers.split_at_mut(previous + 1); + let previous_layer = &mut head[previous]; + let current_layer = &mut tail[self.current - previous - 1]; + + if previous_layer.end() <= current_layer.start() + && previous_layer.bounds() == current_layer.bounds() + { + previous_layer.merge(current_layer); + } + + self.current = previous; } /// Pushes a new [`Transformation`] in the [`Stack`]. @@ -125,12 +158,15 @@ impl Stack { /// Clears the layers of the [`Stack`], allowing reuse. /// + /// It resizes the base layer bounds to the `new_bounds`. + /// /// This will normally keep layer allocations for future drawing operations. - pub fn clear(&mut self) { + pub fn reset(&mut self, new_bounds: Rectangle) { for layer in self.layers[..self.active_count].iter_mut() { layer.reset(); } + self.layers[0].resize(new_bounds); self.current = 0; self.active_count = 1; self.previous.clear(); diff --git a/renderer/src/fallback.rs b/renderer/src/fallback.rs index 79e22c77..411b5862 100644 --- a/renderer/src/fallback.rs +++ b/renderer/src/fallback.rs @@ -46,8 +46,8 @@ where delegate!(self, renderer, renderer.fill_quad(quad, background.into())); } - fn clear(&mut self) { - delegate!(self, renderer, renderer.clear()); + fn reset(&mut self, new_bounds: Rectangle) { + delegate!(self, renderer, renderer.reset(new_bounds)); } fn start_layer(&mut self, bounds: Rectangle) { diff --git a/runtime/src/user_interface.rs b/runtime/src/user_interface.rs index 95e7574f..482ffafa 100644 --- a/runtime/src/user_interface.rs +++ b/runtime/src/user_interface.rs @@ -482,10 +482,8 @@ where style: &renderer::Style, cursor: mouse::Cursor, ) { - // TODO: Move to shell level (?) - renderer.clear(); - let viewport = Rectangle::with_size(self.bounds); + renderer.reset(viewport); let base_cursor = match &self.overlay { None diff --git a/tiny_skia/src/layer.rs b/tiny_skia/src/layer.rs index 24e62ecb..4555cdfa 100644 --- a/tiny_skia/src/layer.rs +++ b/tiny_skia/src/layer.rs @@ -17,8 +17,8 @@ pub struct Layer { pub bounds: Rectangle, pub quads: Vec<(Quad, Background)>, pub primitives: Vec>, - pub text: Vec>, pub images: Vec, + pub text: Vec>, } impl Layer { @@ -284,6 +284,10 @@ impl graphics::Layer for Layer { } } + fn bounds(&self) -> Rectangle { + self.bounds + } + fn flush(&mut self) {} fn resize(&mut self, bounds: Rectangle) { @@ -298,6 +302,49 @@ impl graphics::Layer for Layer { self.text.clear(); self.images.clear(); } + + fn start(&self) -> usize { + if !self.quads.is_empty() { + return 0; + } + + if !self.primitives.is_empty() { + return 1; + } + + if !self.images.is_empty() { + return 2; + } + + if !self.text.is_empty() { + return 3; + } + + 0 + } + + fn end(&self) -> usize { + if !self.text.is_empty() { + return 3; + } + + if !self.images.is_empty() { + return 2; + } + + if !self.primitives.is_empty() { + return 1; + } + + 0 + } + + fn merge(&mut self, layer: &mut Self) { + self.quads.append(&mut layer.quads); + self.primitives.append(&mut layer.primitives); + self.text.append(&mut layer.text); + self.images.append(&mut layer.images); + } } #[derive(Debug, Clone)] diff --git a/tiny_skia/src/lib.rs b/tiny_skia/src/lib.rs index 8f343277..bc379981 100644 --- a/tiny_skia/src/lib.rs +++ b/tiny_skia/src/lib.rs @@ -225,8 +225,8 @@ impl core::Renderer for Renderer { layer.draw_quad(quad, background.into(), transformation); } - fn clear(&mut self) { - self.layers.clear(); + fn reset(&mut self, new_bounds: Rectangle) { + self.layers.reset(new_bounds); } } diff --git a/wgpu/src/image/null.rs b/wgpu/src/image/null.rs index cfcd53fc..d0d74cfc 100644 --- a/wgpu/src/image/null.rs +++ b/wgpu/src/image/null.rs @@ -11,4 +11,6 @@ impl Batch { pub fn is_empty(&self) -> bool { true } + + pub fn append(&mut self, _batch: &mut Self) {} } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 2d8fcee5..a7d8b446 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -268,6 +268,10 @@ impl graphics::Layer for Layer { } } + fn bounds(&self) -> Rectangle { + self.bounds + } + fn flush(&mut self) { self.flush_meshes(); self.flush_text(); @@ -288,6 +292,58 @@ impl graphics::Layer for Layer { self.pending_meshes.clear(); self.pending_text.clear(); } + + fn start(&self) -> usize { + if !self.quads.is_empty() { + return 0; + } + + if !self.triangles.is_empty() { + return 1; + } + + if !self.primitives.is_empty() { + return 2; + } + + if !self.images.is_empty() { + return 3; + } + + if !self.text.is_empty() { + return 4; + } + + 0 + } + + fn end(&self) -> usize { + if !self.text.is_empty() { + return 4; + } + + if !self.images.is_empty() { + return 3; + } + + if !self.primitives.is_empty() { + return 2; + } + + if !self.triangles.is_empty() { + return 1; + } + + 0 + } + + fn merge(&mut self, layer: &mut Self) { + self.quads.append(&mut layer.quads); + self.triangles.append(&mut layer.triangles); + self.primitives.append(&mut layer.primitives); + self.images.append(&mut layer.images); + self.text.append(&mut layer.text); + } } impl Default for Layer { diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index b5af87b7..4361d605 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -648,8 +648,8 @@ impl core::Renderer for Renderer { layer.draw_quad(quad, background.into(), transformation); } - fn clear(&mut self) { - self.layers.clear(); + fn reset(&mut self, new_bounds: Rectangle) { + self.layers.reset(new_bounds); } } diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index bf9d8961..31bf69cf 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -304,6 +304,12 @@ impl Batch { self.gradients.clear(); self.order.clear(); } + + pub fn append(&mut self, batch: &mut Batch) { + self.solids.append(&mut batch.solids); + self.gradients.append(&mut batch.gradients); + self.order.append(&mut batch.order); + } } #[derive(Debug, Copy, Clone, PartialEq, Eq)] diff --git a/widget/src/stack.rs b/widget/src/stack.rs index ee81e4de..16e82678 100644 --- a/widget/src/stack.rs +++ b/widget/src/stack.rs @@ -23,6 +23,7 @@ pub struct Stack<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> width: Length, height: Length, children: Vec>, + clip: bool, } impl<'a, Message, Theme, Renderer> Stack<'a, Message, Theme, Renderer> @@ -62,6 +63,7 @@ where width: Length::Shrink, height: Length::Shrink, children, + clip: false, } } @@ -114,6 +116,16 @@ where ) -> Self { children.into_iter().fold(self, Self::push) } + + /// Sets whether the [`Stack`] should clip overflowing content. + /// + /// It has a slight performance overhead during presentation. + /// + /// By default, it is set to `false`. + pub fn clip(mut self, clip: bool) -> Self { + self.clip = clip; + self + } } impl Default for Stack<'_, Message, Renderer> @@ -277,6 +289,12 @@ where viewport: &Rectangle, ) { if let Some(clipped_viewport) = layout.bounds().intersection(viewport) { + let viewport = if self.clip { + &clipped_viewport + } else { + viewport + }; + let layers_below = if cursor.is_over(layout.bounds()) { self.children .iter() @@ -312,26 +330,16 @@ where layout, cursor| { if i > 0 { - renderer.with_layer(clipped_viewport, |renderer| { + renderer.with_layer(*viewport, |renderer| { layer.as_widget().draw( - state, - renderer, - theme, - style, - layout, - cursor, - &clipped_viewport, + state, renderer, theme, style, layout, cursor, + viewport, ); }); } else { layer.as_widget().draw( - state, - renderer, - theme, - style, - layout, - cursor, - &clipped_viewport, + state, renderer, theme, style, layout, cursor, + viewport, ); } };