-
Notifications
You must be signed in to change notification settings - Fork 781
femtovg/skia: Fix rendering of children outside a layer's intrinsic bounding rect #10109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tronical
wants to merge
9
commits into
master
Choose a base branch
from
simon/layer-transform
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−104
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ebdc50
femtovg: Switch to Edition 2024
tronical f484fba
femtovg: Fix rendering of children outside a layer's intrinsic boundi…
tronical 1b5f443
Better debug for the ItemRc
ogoffart 95bc34e
femtovg: fix warning about unused variable
ogoffart 8fe4ff3
skia: Fix rendering of children outside a layer's intrinsic bounding …
tronical 0cd1112
skia: Remove unnecessary union with the layer's own geometry
tronical 0a7dbff
Fix skia build
tronical 56d136e
femtovg: Add doc comment for origin field in TextureWithOrigin
tronical 5ff952c
femtovg: Rename tuple field for ItemGraphicsCacheEntry::Texture match…
tronical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,14 +22,14 @@ use i_slint_core::lengths::{ | |
| LogicalBorderRadius, LogicalLength, LogicalPoint, LogicalRect, LogicalSize, LogicalVector, | ||
| RectLengths, ScaleFactor, | ||
| }; | ||
| use i_slint_core::textlayout::sharedparley::{self, parley, GlyphRenderer}; | ||
| use i_slint_core::textlayout::sharedparley::{self, GlyphRenderer, parley}; | ||
| use i_slint_core::{Brush, Color, ImageInner, SharedString}; | ||
|
|
||
| use crate::images::TextureImporter; | ||
|
|
||
| use super::images::{Texture, TextureCacheKey}; | ||
| use super::PhysicalSize; | ||
| use super::{font_cache, PhysicalBorderRadius, PhysicalLength, PhysicalPoint, PhysicalRect}; | ||
| use super::images::{Texture, TextureCacheKey}; | ||
| use super::{PhysicalBorderRadius, PhysicalLength, PhysicalPoint, PhysicalRect, font_cache}; | ||
|
|
||
| type FemtovgBoxShadowCache<R> = BoxShadowCache<ItemGraphicsCacheEntry<R>>; | ||
|
|
||
|
|
@@ -38,6 +38,10 @@ pub type CanvasRc<R> = Rc<RefCell<Canvas<R>>>; | |
|
|
||
| pub enum ItemGraphicsCacheEntry<R: femtovg::Renderer + TextureImporter> { | ||
| Texture(Rc<Texture<R>>), | ||
| TextureWithOrigin { | ||
| texture: Rc<Texture<R>>, | ||
| origin: PhysicalPoint, | ||
| }, | ||
| ColorizedImage { | ||
| // This original image Rc is kept here to keep the image in the shared image cache, so that | ||
| // changes to the colorization brush will not require re-uploading the image. | ||
|
|
@@ -50,6 +54,9 @@ impl<R: femtovg::Renderer + TextureImporter> Clone for ItemGraphicsCacheEntry<R> | |
| fn clone(&self) -> Self { | ||
| match self { | ||
| Self::Texture(arg0) => Self::Texture(arg0.clone()), | ||
| Self::TextureWithOrigin { texture, origin } => { | ||
| Self::TextureWithOrigin { texture: texture.clone(), origin: origin.clone() } | ||
| } | ||
| Self::ColorizedImage { _original_image, colorized_image } => Self::ColorizedImage { | ||
| _original_image: _original_image.clone(), | ||
| colorized_image: colorized_image.clone(), | ||
|
|
@@ -62,6 +69,7 @@ impl<R: femtovg::Renderer + TextureImporter> ItemGraphicsCacheEntry<R> { | |
| fn as_texture(&self) -> &Rc<Texture<R>> { | ||
| match self { | ||
| ItemGraphicsCacheEntry::Texture(image) => image, | ||
| ItemGraphicsCacheEntry::TextureWithOrigin { texture, .. } => texture, | ||
| ItemGraphicsCacheEntry::ColorizedImage { colorized_image, .. } => colorized_image, | ||
| } | ||
| } | ||
|
|
@@ -670,7 +678,9 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer | |
| let border_width = clip_item.border_width(); | ||
|
|
||
| if !radius.is_zero() { | ||
| if let Some(layer_image) = self.render_layer(item_rc, &|| item_rc.geometry()) { | ||
| if let Some((layer_origin, layer_image)) = | ||
| self.render_layer(item_rc, &|| item_rc.geometry()) | ||
| { | ||
| let layer_image_paint = layer_image.as_paint(); | ||
|
|
||
| let layer_path = clip_path_for_rect_alike_item( | ||
|
|
@@ -680,7 +690,10 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer | |
| self.scale_factor, | ||
| ); | ||
|
|
||
| self.canvas.borrow_mut().fill_path(&layer_path, &layer_image_paint); | ||
| self.canvas.borrow_mut().save_with(|canvas| { | ||
| canvas.translate(layer_origin.x, layer_origin.y); | ||
| canvas.fill_path(&layer_path, &layer_image_paint); | ||
| }); | ||
| } | ||
|
|
||
| RenderingResult::ContinueRenderingWithoutChildren | ||
|
|
@@ -769,6 +782,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer | |
| }); | ||
| let image_id = match cache_entry { | ||
| Some(ItemGraphicsCacheEntry::Texture(image)) => image.id, | ||
|
||
| Some(ItemGraphicsCacheEntry::TextureWithOrigin { texture, .. }) => texture.id, | ||
| Some(ItemGraphicsCacheEntry::ColorizedImage { .. }) => unreachable!(), | ||
| None => return, | ||
| }; | ||
|
|
@@ -1039,114 +1053,110 @@ impl<'a, R: femtovg::Renderer + TextureImporter> GLItemRenderer<'a, R> { | |
| &mut self, | ||
| item_rc: &ItemRc, | ||
| layer_bounding_rect_fn: &dyn Fn() -> LogicalRect, | ||
| ) -> Option<Rc<Texture<R>>> { | ||
| ) -> Option<(PhysicalPoint, Rc<Texture<R>>)> { | ||
| let existing_layer_texture = | ||
| self.graphics_cache.with_entry(item_rc, |cache_entry| match cache_entry { | ||
| Some(ItemGraphicsCacheEntry::Texture(texture)) => Some(texture.clone()), | ||
| Some(ItemGraphicsCacheEntry::TextureWithOrigin { texture, .. }) => { | ||
| Some(texture.clone()) | ||
| } | ||
| _ => None, | ||
| }); | ||
|
|
||
| let cache_entry = self.graphics_cache.get_or_update_cache_entry(item_rc, || { | ||
| ItemGraphicsCacheEntry::Texture({ | ||
| let bounding_rect = layer_bounding_rect_fn(); | ||
| let size = (bounding_rect.size * self.scale_factor).ceil().try_cast()?; | ||
|
|
||
| let layer_image = existing_layer_texture | ||
| .and_then(|layer_texture| { | ||
| // If we have an existing layer texture, there must be only one reference from within | ||
| // the existing cache entry and one through the `existing_layer_texture` variable. | ||
| // Then it is safe to render new content into it in this callback and when we return | ||
| // into `get_or_update_cache_entry` the first ref is dropped. | ||
| debug_assert_eq!(Rc::strong_count(&layer_texture), 2); | ||
| if layer_texture.size() == Some(size.to_untyped()) { | ||
| Some(layer_texture) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .or_else(|| { | ||
| *self.metrics.layers_created.as_mut().unwrap() += 1; | ||
| Texture::new_empty_on_gpu(&self.canvas, size.width, size.height) | ||
| })?; | ||
| let bounding_rect = layer_bounding_rect_fn(); | ||
| let origin = bounding_rect.origin * self.scale_factor; | ||
| let size = (bounding_rect.size * self.scale_factor).ceil().try_cast()?; | ||
|
|
||
| let layer_image = existing_layer_texture | ||
| .and_then(|layer_texture| { | ||
| // If we have an existing layer texture, there must be only one reference from within | ||
| // the existing cache entry and one through the `existing_layer_texture` variable. | ||
| // Then it is safe to render new content into it in this callback and when we return | ||
| // into `get_or_update_cache_entry` the first ref is dropped. | ||
| debug_assert_eq!(Rc::strong_count(&layer_texture), 2); | ||
| if layer_texture.size() == Some(size.to_untyped()) { | ||
| Some(layer_texture) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .or_else(|| { | ||
| *self.metrics.layers_created.as_mut().unwrap() += 1; | ||
| Texture::new_empty_on_gpu(&self.canvas, size.width, size.height) | ||
| })?; | ||
|
|
||
| let previous_render_target = self.current_render_target(); | ||
| let previous_render_target = self.current_render_target(); | ||
|
|
||
| { | ||
| let mut canvas = self.canvas.borrow_mut(); | ||
| canvas.save(); | ||
| { | ||
| let mut canvas = self.canvas.borrow_mut(); | ||
| canvas.save(); | ||
|
|
||
| canvas.set_render_target(layer_image.as_render_target()); | ||
| canvas.set_render_target(layer_image.as_render_target()); | ||
|
|
||
| canvas.reset(); | ||
| canvas.reset(); | ||
|
|
||
| canvas.clear_rect( | ||
| 0, | ||
| 0, | ||
| size.width, | ||
| size.height, | ||
| femtovg::Color::rgba(0, 0, 0, 0), | ||
| ); | ||
| canvas.clear_rect(0, 0, size.width, size.height, femtovg::Color::rgba(0, 0, 0, 0)); | ||
|
|
||
| let origin = bounding_rect.origin * self.scale_factor; | ||
| canvas.translate(-origin.x, -origin.y); | ||
| } | ||
| canvas.translate(-origin.x, -origin.y); | ||
| } | ||
|
|
||
| *self.state.last_mut().unwrap() = State { | ||
| scissor: LogicalRect::new(LogicalPoint::default(), bounding_rect.size), | ||
| global_alpha: 1., | ||
| current_render_target: layer_image.as_render_target(), | ||
| }; | ||
| *self.state.last_mut().unwrap() = State { | ||
| scissor: bounding_rect, | ||
| global_alpha: 1., | ||
| current_render_target: layer_image.as_render_target(), | ||
| }; | ||
|
|
||
| let window_adapter = self.window().window_adapter(); | ||
| let window_adapter = self.window().window_adapter(); | ||
|
|
||
| i_slint_core::item_rendering::render_item_children( | ||
| self, | ||
| item_rc.item_tree(), | ||
| item_rc.index() as isize, | ||
| &window_adapter, | ||
| ); | ||
| i_slint_core::item_rendering::render_item_children( | ||
| self, | ||
| item_rc.item_tree(), | ||
| item_rc.index() as isize, | ||
| &window_adapter, | ||
| ); | ||
|
|
||
| { | ||
| let mut canvas = self.canvas.borrow_mut(); | ||
| canvas.restore(); | ||
| { | ||
| let mut canvas = self.canvas.borrow_mut(); | ||
| canvas.restore(); | ||
|
|
||
| canvas.set_render_target(previous_render_target); | ||
| } | ||
| canvas.set_render_target(previous_render_target); | ||
| } | ||
|
|
||
| layer_image | ||
| }) | ||
| .into() | ||
| Some(ItemGraphicsCacheEntry::TextureWithOrigin { texture: layer_image, origin }) | ||
| }); | ||
|
|
||
| cache_entry.map(|item_cache_entry| item_cache_entry.as_texture().clone()) | ||
| cache_entry.and_then(|item_cache_entry| match item_cache_entry { | ||
| ItemGraphicsCacheEntry::TextureWithOrigin { texture, origin } => { | ||
| Some((origin, texture.clone())) | ||
| } | ||
| _ => None, | ||
| }) | ||
| } | ||
|
|
||
| fn render_and_blend_layer(&mut self, alpha_tint: f32, item_rc: &ItemRc) -> RenderingResult { | ||
| let current_clip = self.get_current_clip(); | ||
| if let Some((layer_image, layer_size)) = self | ||
| .render_layer(item_rc, &|| { | ||
| // We don't need to include the size of the opacity item itself, since it has no content. | ||
| let children_rect = i_slint_core::properties::evaluate_no_tracking(|| { | ||
| item_rc.geometry().union( | ||
| &i_slint_core::item_rendering::item_children_bounding_rect( | ||
| item_rc.item_tree(), | ||
| item_rc.index() as isize, | ||
| ¤t_clip, | ||
| ), | ||
| ) | ||
| }); | ||
| children_rect | ||
| if let Some((layer_origin, layer_image)) = self.render_layer(item_rc, &|| { | ||
| // We don't need to include the size of the opacity item itself, since it has no content. | ||
| i_slint_core::properties::evaluate_no_tracking(|| { | ||
| i_slint_core::item_rendering::item_children_bounding_rect( | ||
| item_rc.item_tree(), | ||
| item_rc.index() as isize, | ||
| ¤t_clip, | ||
| ) | ||
| }) | ||
| .and_then(|image| image.size().map(|size| (image, size))) | ||
| }) && let Some(layer_size) = layer_image.size() | ||
| { | ||
| let mut layer_path = femtovg::Path::new(); | ||
| // On the paint for the layer, we don't need anti-aliasing on the fringes, | ||
| // since we are just blitting a texture. This saves a triangle strip for the stroke. | ||
| let layer_image_paint = | ||
| layer_image.as_paint_with_alpha(alpha_tint).with_anti_alias(false); | ||
|
|
||
| layer_path.rect(0., 0., layer_size.width as _, layer_size.height as _); | ||
| self.canvas.borrow_mut().fill_path(&layer_path, &layer_image_paint); | ||
| self.canvas.borrow_mut().save_with(|canvas| { | ||
| canvas.translate(layer_origin.x, layer_origin.y); | ||
| layer_path.rect(0., 0., layer_size.width as _, layer_size.height as _); | ||
| canvas.fill_path(&layer_path, &layer_image_paint); | ||
| }); | ||
| } | ||
| RenderingResult::ContinueRenderingWithoutChildren | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could do with a line of documentation here