Skip to content

Commit

Permalink
WIP Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Aug 8, 2024
1 parent e5637fc commit 2940dcf
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rand = "0.8"
ron = { workspace = true }
sctk = { package = "smithay-client-toolkit", git = "https://github.com/smithay/client-toolkit", rev = "2e9bf9f" }
slab = "0.4.9"
tiny-skia = "0.11.4"
tracing = { workspace=true }
tracing-subscriber = "0.3.18"
walkdir = "2.4"
Expand Down
36 changes: 17 additions & 19 deletions src/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ pub fn stretch(
}

pub fn zoom(img: &image::DynamicImage, layer_width: u32, layer_height: u32) -> image::DynamicImage {
let (w, h) = (img.width(), img.height());

let ratio = (layer_width as f64 / w as f64).max(layer_height as f64 / h as f64);

let (new_width, new_height) = (
(w as f64 * ratio).round() as u32,
(h as f64 * ratio).round() as u32,
// TODO HDR?
let img = img.to_rgba8();
let pixmap = tiny_skia::PixmapRef::from_bytes(&img, img.width(), img.height()).unwrap();
let mut target_pixmap = tiny_skia::Pixmap::new(layer_width, layer_height).unwrap();
target_pixmap.draw_pixmap(
0,
0,
pixmap,
&tiny_skia::PixmapPaint::default(),
tiny_skia::Transform::from_scale(
layer_width as f32 / img.width() as f32,
layer_height as f32 / img.height() as f32,
),
None,
);

let mut new_image = image::imageops::resize(img, new_width, new_height, FilterType::Lanczos3);

image::imageops::crop(
&mut new_image,
(new_width - layer_width) / 2,
(new_height - layer_height) / 2,
layer_width,
layer_height,
)
.to_image()
.into()
image::RgbaImage::from_vec(layer_width, layer_height, target_pixmap.take())
.unwrap()
.into()
}
39 changes: 26 additions & 13 deletions src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Wallpaper {
loop_handle: calloop::LoopHandle<'static, CosmicBg>,
queue_handle: QueueHandle<CosmicBg>,
current_source: Option<Source>,
// Cache of source image, if `current_source` is a `Source::Path`
current_image: Option<image::DynamicImage>,
timer_token: Option<RegistrationToken>,
new_image: bool,
}
Expand All @@ -59,6 +61,7 @@ impl Wallpaper {
entry,
layers: Vec::new(),
current_source: None,
current_image: None,
image_queue: VecDeque::default(),
new_image: false,
timer_token: None,
Expand Down Expand Up @@ -117,21 +120,28 @@ impl Wallpaper {
};
cur_resized_img = match source {
Source::Path(ref path) => {
let img = &match ImageReader::open(&path) {
Ok(img) => {
match img.with_guessed_format().ok().and_then(|f| f.decode().ok()) {
Some(img) => img,
None => {
tracing::warn!(
"Could not decode image: {}",
path.display()
);
continue;
if self.current_image.is_none() {
self.current_image = Some(match ImageReader::open(&path) {
Ok(img) => {
match img
.with_guessed_format()
.ok()
.and_then(|f| f.decode().ok())
{
Some(img) => img,
None => {
tracing::warn!(
"Could not decode image: {}",
path.display()
);
continue;
}
}
}
}
Err(_) => continue,
};
Err(_) => continue,
});
}
let img = self.current_image.as_ref().unwrap();

match self.entry.scaling_mode {
ScalingMode::Fit(color) => {
Expand Down Expand Up @@ -260,12 +270,14 @@ impl Wallpaper {

image_queue.pop_front().map(|current_image_path| {
self.current_source = Some(Source::Path(current_image_path.clone()));
self.current_image = None;
image_queue.push_back(current_image_path);
});
}

Source::Color(ref c) => {
self.current_source = Some(Source::Color(c.clone()));
self.current_image = None;
}
};
if let Err(err) = self.save_state() {
Expand Down Expand Up @@ -328,6 +340,7 @@ impl Wallpaper {

while let Some(next) = item.image_queue.pop_front() {
item.current_source = Some(Source::Path(next.clone()));
item.current_image = None;
if let Err(err) = item.save_state() {
error!("{err}");
}
Expand Down

0 comments on commit 2940dcf

Please sign in to comment.