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:
@ -1,5 +1,6 @@
|
|||||||
use super::super::pipelines::blit;
|
use super::super::pipelines::blit;
|
||||||
use common_base::prof_span;
|
use common_base::prof_span;
|
||||||
|
use crossbeam_channel;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
pub type ScreenshotFn = Box<dyn FnOnce(Result<image::RgbImage, String>) + Send>;
|
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) {
|
fn download_and_handle_internal(self) {
|
||||||
prof_span!("download_and_handle_internal");
|
prof_span!("download_and_handle_internal");
|
||||||
// Calculate padded bytes per row
|
// Calculate padded bytes per row
|
||||||
@ -144,7 +146,22 @@ impl TakeScreenshot {
|
|||||||
let buffer = std::sync::Arc::new(self.buffer);
|
let buffer = std::sync::Arc::new(self.buffer);
|
||||||
let buffer2 = std::sync::Arc::clone(&buffer);
|
let buffer2 = std::sync::Arc::clone(&buffer);
|
||||||
let buffer_slice = buffer.slice(..);
|
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| {
|
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 padded_buffer;
|
||||||
let buffer_slice = buffer2.slice(..);
|
let buffer_slice = buffer2.slice(..);
|
||||||
let rows = match result {
|
let rows = match result {
|
||||||
@ -213,13 +230,11 @@ impl TakeScreenshot {
|
|||||||
}
|
}
|
||||||
.map(|pixel_bytes| {
|
.map(|pixel_bytes| {
|
||||||
image::RgbImage::from_vec(self.width, self.height, pixel_bytes).expect(
|
image::RgbImage::from_vec(self.width, self.height, pixel_bytes).expect(
|
||||||
"Failed to create ImageBuffer! Buffer was not large enough. This should not \
|
"Failed to create ImageBuffer! Buffer was not large enough. This should not occur",
|
||||||
occur",
|
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
// Call supplied handler
|
// Call supplied handler
|
||||||
(self.screenshot_fn)(image);
|
(self.screenshot_fn)(image);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user