diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index c9a818fb..f404ec20 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,21 @@ pub trait Layer: Default { /// Clears all the layers contents and resets its bounds. fn reset(&mut self); + + /// Returns the level of the [`Layer`]. + /// + /// The level is the lowest "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 level of A is lower or equal than the level of B. + fn level(&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 +100,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.level() <= current_layer.level() + && previous_layer.bounds() == current_layer.bounds() + { + previous_layer.merge(current_layer); + } + + self.current = previous; } /// Pushes a new [`Transformation`] in the [`Stack`]. diff --git a/tiny_skia/src/layer.rs b/tiny_skia/src/layer.rs index 24e62ecb..65e8d469 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,29 @@ impl graphics::Layer for Layer { self.text.clear(); self.images.clear(); } + + fn level(&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/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..10b1431d 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,34 @@ impl graphics::Layer for Layer { self.pending_meshes.clear(); self.pending_text.clear(); } + + fn level(&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/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, ); } };