mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
make map_async function slim and handle the other stuff inside the download_and_handle_internal fn which is only called from a seperate thread
This commit is contained in:
parent
db9eca031c
commit
dfb8715aed
@ -1,5 +1,6 @@
|
||||
use super::super::pipelines::blit;
|
||||
use common_base::prof_span;
|
||||
use crossbeam_channel;
|
||||
use tracing::error;
|
||||
|
||||
pub type ScreenshotFn = Box<dyn FnOnce(Result<image::RgbImage, String>) + Send>;
|
||||
@ -135,6 +136,7 @@ impl TakeScreenshot {
|
||||
}
|
||||
}
|
||||
|
||||
/// Don't call this from the main loop, it will block for a while
|
||||
fn download_and_handle_internal(self) {
|
||||
prof_span!("download_and_handle_internal");
|
||||
// Calculate padded bytes per row
|
||||
@ -144,7 +146,22 @@ impl TakeScreenshot {
|
||||
let buffer = std::sync::Arc::new(self.buffer);
|
||||
let buffer2 = std::sync::Arc::clone(&buffer);
|
||||
let buffer_slice = buffer.slice(..);
|
||||
let (map_result_sender, map_result_receiver) = crossbeam_channel::bounded(1);
|
||||
buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
|
||||
map_result_sender
|
||||
.send(result)
|
||||
.expect("seems like the receiver broke, which should not happen");
|
||||
});
|
||||
let result = match map_result_receiver.recv() {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
error!(
|
||||
?e,
|
||||
"map_async never send the result for the screenshot mapping"
|
||||
);
|
||||
return;
|
||||
},
|
||||
};
|
||||
let padded_buffer;
|
||||
let buffer_slice = buffer2.slice(..);
|
||||
let rows = match result {
|
||||
@ -213,13 +230,11 @@ impl TakeScreenshot {
|
||||
}
|
||||
.map(|pixel_bytes| {
|
||||
image::RgbImage::from_vec(self.width, self.height, pixel_bytes).expect(
|
||||
"Failed to create ImageBuffer! Buffer was not large enough. This should not \
|
||||
occur",
|
||||
"Failed to create ImageBuffer! Buffer was not large enough. This should not occur",
|
||||
)
|
||||
});
|
||||
// Call supplied handler
|
||||
(self.screenshot_fn)(image);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user