Skip to content

Commit

Permalink
Femtovg&Skia: fix drawing of tiled svg image
Browse files Browse the repository at this point in the history
We will render them with the final size in the buffer, so account for
that difference when rendering
  • Loading branch information
ogoffart committed Feb 27, 2024
1 parent c4e0f4a commit 06174c1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
80 changes: 42 additions & 38 deletions internal/renderers/femtovg/itemrenderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,23 +1289,23 @@ impl<'a> GLItemRenderer<'a> {
let image_inner: &ImageInner = (&image).into();

let target_size_for_scalable_source = if image_inner.is_svg() {
let image_size = image.size();
let image_size = image.size().cast::<f32>();
if image_size.is_empty() {
return None;
}
let t = item.target_size() * self.scale_factor;
Some(
i_slint_core::graphics::fit(
item.image_fit(),
t,
IntRect::from_size(image_size.cast()),
self.scale_factor,
Default::default(), // We only care about the size, so alignments don't matter
item.tiling(),
)
.size
.cast(),
)
let fit = i_slint_core::graphics::fit(
item.image_fit(),
t,
IntRect::from_size(image_size.cast()),
self.scale_factor,
Default::default(), // We only care about the size, so alignments don't matter
item.tiling(),
);
Some(euclid::size2(
(image_size.width * fit.source_to_target_x) as u32,
(image_size.height * fit.source_to_target_y) as u32,
))
} else {
None
};
Expand Down Expand Up @@ -1360,11 +1360,12 @@ impl<'a> GLItemRenderer<'a> {
break cached_image.as_texture().clone();
};

let image = item.source();
let image_id = cached_image.id;
let image_size = cached_image.size().unwrap_or_default();
let source_clip_rect = item.source_clip().unwrap_or(IntRect::from_size(image_size.cast()));
let orig_size = image.size().cast::<f32>();
let buf_size = cached_image.size().unwrap_or_default().cast::<f32>();
let source_clip_rect = item.source_clip().unwrap_or(IntRect::from_size(orig_size.cast()));

let image = item.source();
let image_inner: &ImageInner = (&image).into();
let fits = if let ImageInner::NineSlice(nine) = image_inner {
i_slint_core::graphics::fit9slice(
Expand All @@ -1387,21 +1388,29 @@ impl<'a> GLItemRenderer<'a> {
)]
};

let scale_w = buf_size.width / orig_size.width;
let scale_h = buf_size.height / orig_size.height;

for fit in fits {
let (image_id, origin, image_size) =
if fit.tiled.is_some() && fit.clip_rect.size.cast() != image_size {
let (image_id, origin, texture_size) =
if fit.tiled.is_some() && fit.clip_rect.size.cast() != orig_size {
let scaling_flags = match item.rendering() {
ImageRendering::Smooth => femtovg::ImageFlags::empty(),
ImageRendering::Pixelated => {
femtovg::ImageFlags::empty() | femtovg::ImageFlags::NEAREST
}
};
let texture_size = euclid::size2(
scale_w * fit.clip_rect.width() as f32,
scale_h * fit.clip_rect.height() as f32,
);

let clipped_image = self
.canvas
.borrow_mut()
.create_image_empty(
fit.clip_rect.width() as usize,
fit.clip_rect.height() as usize,
texture_size.width as usize,
texture_size.height as usize,
femtovg::PixelFormat::Rgba8,
femtovg::ImageFlags::PREMULTIPLIED
| femtovg::ImageFlags::REPEAT_X
Expand All @@ -1411,26 +1420,21 @@ impl<'a> GLItemRenderer<'a> {
.expect("internal error allocating temporary texture for image tiling");

let mut image_rect = femtovg::Path::new();
image_rect.rect(
0.,
0.,
fit.clip_rect.width() as f32,
fit.clip_rect.height() as f32,
);
image_rect.rect(0., 0., texture_size.width, texture_size.height);
self.canvas.borrow_mut().save_with(|canvas| {
canvas.reset();
canvas.scale(1., -1.); // Image are rendered upside down
canvas.translate(0., -fit.clip_rect.height() as f32);
canvas.translate(0., -scale_h * (fit.clip_rect.height() as f32));
canvas.set_render_target(femtovg::RenderTarget::Image(clipped_image));
canvas.global_composite_operation(femtovg::CompositeOperation::Copy);
canvas.fill_path(
&image_rect,
&femtovg::Paint::image(
image_id,
-fit.clip_rect.origin.x as f32,
-fit.clip_rect.origin.y as f32,
image_size.cast().width,
image_size.cast().height,
-scale_w * fit.clip_rect.origin.x as f32,
-scale_h * fit.clip_rect.origin.y as f32,
buf_size.cast().width,
buf_size.cast().height,
0.,
1.0,
),
Expand All @@ -1440,17 +1444,17 @@ impl<'a> GLItemRenderer<'a> {
self.textures_to_delete_after_flush
.borrow_mut()
.push(Texture::adopt(&self.canvas, clipped_image));
(clipped_image, Default::default(), fit.clip_rect.size.cast())
(clipped_image, Default::default(), texture_size)
} else {
(image_id, fit.clip_rect.origin.cast::<f32>(), image_size.cast())
(image_id, fit.clip_rect.origin.cast::<f32>(), buf_size)
};
let tiled = fit.tiled.unwrap_or_default();
let fill_paint = femtovg::Paint::image(
image_id,
-origin.x - tiled.x as f32,
-origin.y - tiled.y as f32,
image_size.width,
image_size.height,
texture_size.width,
texture_size.height,
0.0,
1.0,
)
Expand All @@ -1460,13 +1464,13 @@ impl<'a> GLItemRenderer<'a> {
path.rect(
0.,
0.,
fit.size.width / fit.source_to_target_x,
fit.size.height / fit.source_to_target_y,
scale_w * fit.size.width / fit.source_to_target_x,
scale_h * fit.size.height / fit.source_to_target_y,
);

self.canvas.borrow_mut().save_with(|canvas| {
canvas.translate(fit.offset.x, fit.offset.y);
canvas.scale(fit.source_to_target_x, fit.source_to_target_y);
canvas.scale(fit.source_to_target_x / scale_w, fit.source_to_target_y / scale_h);
canvas.fill_path(&path, &fill_paint);
})
}
Expand Down
13 changes: 9 additions & 4 deletions internal/renderers/skia/cached_image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

use crate::PhysicalSize;
#[cfg(skia_backend_opengl)]
use i_slint_core::graphics::BorrowedOpenGLTexture;
use i_slint_core::graphics::{
Expand Down Expand Up @@ -53,15 +54,19 @@ pub(crate) fn as_skia_image(
}
ImageInner::Svg(svg) => {
// Query target_width/height here again to ensure that changes will invalidate the item rendering cache.
let target_size = i_slint_core::graphics::fit(
let svg_size = svg.size();
let fit = i_slint_core::graphics::fit(
image_fit,
target_size_fn() * scale_factor,
IntRect::from_size(svg.size().cast()),
IntRect::from_size(svg_size.cast()),
scale_factor,
Default::default(), // We only care about the size, so alignments don't matter
Default::default(),
)
.size;
);
let target_size = PhysicalSize::new(
svg_size.cast::<f32>().width * fit.source_to_target_x,
svg_size.cast::<f32>().height * fit.source_to_target_y,
);
let pixels = match svg.render(Some(target_size.cast())).ok()? {
SharedImageBuffer::RGB8(_) => unreachable!(),
SharedImageBuffer::RGBA8(_) => unreachable!(),
Expand Down
25 changes: 15 additions & 10 deletions internal/renderers/skia/itemrenderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ impl<'a> SkiaItemRenderer<'a> {
});

let skia_image = if let Some(img) = skia_image { img } else { return };

let fits = if let &i_slint_core::ImageInner::NineSlice(ref nine) = (&item.source()).into() {
let source = item.source();
let source_size = source.size();
let fits = if let &i_slint_core::ImageInner::NineSlice(ref nine) = (&source).into() {
i_slint_core::graphics::fit9slice(
euclid::size2(skia_image.width(), skia_image.height()).cast(),
source_size.cast(),
nine.1,
dest_rect.size,
self.scale_factor,
Expand All @@ -194,8 +195,7 @@ impl<'a> SkiaItemRenderer<'a> {
vec![i_slint_core::graphics::fit(
item.image_fit(),
dest_rect.size,
item.source_clip()
.unwrap_or_else(|| euclid::rect(0, 0, skia_image.width(), skia_image.height())),
item.source_clip().unwrap_or_else(|| euclid::Rect::from_size(source_size.cast())),
self.scale_factor,
item.alignment(),
tiling,
Expand All @@ -208,10 +208,10 @@ impl<'a> SkiaItemRenderer<'a> {
let dst = to_skia_rect(&PhysicalRect::new(fit.offset, fit.size));
self.canvas.clip_rect(dst, None, None);
let src = skia_safe::IRect::from_xywh(
fit.clip_rect.origin.x,
fit.clip_rect.origin.y,
fit.clip_rect.size.width,
fit.clip_rect.size.height,
skia_image.width() * fit.clip_rect.origin.x / source_size.width as i32,
skia_image.height() * fit.clip_rect.origin.y / source_size.height as i32,
skia_image.width() * fit.clip_rect.size.width / source_size.width as i32,
skia_image.height() * fit.clip_rect.size.height / source_size.height as i32,
);

let filter_mode: skia_safe::sampling_options::SamplingOptions =
Expand All @@ -223,7 +223,12 @@ impl<'a> SkiaItemRenderer<'a> {

if let Some(tiled_offset) = fit.tiled {
let matrix = Matrix::translate(((fit.offset.x as i32), (fit.offset.y as i32)))
* Matrix::scale((fit.source_to_target_x, fit.source_to_target_y))
* Matrix::scale((
fit.source_to_target_x * source_size.width as f32
/ skia_image.width() as f32,
fit.source_to_target_y * source_size.height as f32
/ skia_image.height() as f32,
))
* Matrix::translate((-(tiled_offset.x as i32), -(tiled_offset.y as i32)));
if let Some(shader) = skia_image.make_subset(None, &src).and_then(|i| {
i.to_shader((TileMode::Repeat, TileMode::Repeat), filter_mode, &matrix)
Expand Down

0 comments on commit 06174c1

Please sign in to comment.