Skip to content

Commit

Permalink
fix: scale problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Feb 16, 2024
1 parent f2aa2b6 commit b1334c6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
25 changes: 8 additions & 17 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 libwayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ thiserror = "1"
wayland-client = "0.31.1"
wayland-protocols = { version = "0.31.0", features = ["client", "unstable"] }
wayland-protocols-wlr = { version = "0.2.0", features = ["client"] }
tempfile = "3.10.0"
51 changes: 44 additions & 7 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod screencopy;
use std::{
collections::HashSet,
fs::File,
io::Write,
os::fd::AsFd,
sync::atomic::{AtomicBool, Ordering},
thread,
Expand Down Expand Up @@ -457,8 +458,9 @@ impl WayshotConnection {
));
}
};
let shm = self.globals.bind::<WlShm, _, _>(&qh, 1..=1, ())?;

for (frame_copy, frame_guard, output_info) in frames {
for (frame_copy, _frame_guard, output_info) in frames {
tracing::span!(
tracing::Level::DEBUG,
"overlay_frames::surface",
Expand All @@ -476,11 +478,37 @@ impl WayshotConnection {
output_info.wl_output.clone(),
);

let file = tempfile::tempfile().unwrap();
let image: DynamicImage = frame_copy.try_into().unwrap();
let image = image::imageops::resize(
&image,
output_info.region.size.width,
output_info.region.size.height,
image::imageops::FilterType::Nearest,
);
init_overlay(&image, &file);
let pool = shm.create_pool(
file.as_fd(),
(output_info.region.size.width * output_info.region.size.height * 4) as i32,
&qh,
(),
);

let buffer = pool.create_buffer(
0,
output_info.region.size.width as i32,
output_info.region.size.height as i32,
(output_info.region.size.height * 4) as i32,
wl_shm::Format::Argb8888,
&qh,
(),
);

layer_surface.set_exclusive_zone(-1);
layer_surface.set_anchor(Anchor::Top | Anchor::Left);
layer_surface.set_size(
frame_copy.frame_format.size.width,
frame_copy.frame_format.size.height,
output_info.region.size.width,
output_info.region.size.height,
);

debug!("Committing surface creation changes.");
Expand All @@ -493,7 +521,7 @@ impl WayshotConnection {

surface.set_buffer_transform(output_info.transform);
surface.set_buffer_scale(output_info.scale);
surface.attach(Some(&frame_guard.buffer), 0, 0);
surface.attach(Some(&buffer), 0, 0);

debug!("Committing surface with attached buffer.");
surface.commit();
Expand Down Expand Up @@ -565,15 +593,15 @@ impl WayshotConnection {
thread::scope(|scope| {
let rotate_join_handles = frames
.into_iter()
.map(|(frame_copy, _, _)| {
.map(|(frame_copy, _, output_info)| {
scope.spawn(move || {
let image = (&frame_copy).try_into()?;
Ok((
image_util::rotate_image_buffer(
image,
frame_copy.transform,
frame_copy.frame_format.size.width,
frame_copy.frame_format.size.height,
output_info.region.size.width,
output_info.region.size.height,
),
frame_copy,
))
Expand Down Expand Up @@ -675,3 +703,12 @@ impl WayshotConnection {
self.screenshot_outputs(self.get_all_outputs(), cursor_overlay)
}
}

fn init_overlay(origin_image: &image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, tmp: &File) {
let mut buf = std::io::BufWriter::new(tmp);

for index in origin_image.pixels() {
buf.write_all(&index.0).unwrap();
}
buf.flush().unwrap();
}

0 comments on commit b1334c6

Please sign in to comment.