Skip to content

Commit

Permalink
Performance Boost 🔥
Browse files Browse the repository at this point in the history
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   src/frame.rs
  • Loading branch information
NiiightmareXD committed Nov 23, 2023
1 parent 418cd88 commit 90b324a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 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 @@ -22,6 +22,7 @@ resolver = "2"
image = "0.24.7"
log = "0.4.20"
parking_lot = "0.12.1"
rayon = "1.8.0"
thiserror = "1.0.50"
windows = { version = "0.52.0", features = [
"Win32_System_WinRT_Graphics_Capture",
Expand Down
23 changes: 13 additions & 10 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{error::Error, path::Path, ptr, slice};

use image::{Rgb, RgbImage};
use log::trace;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use windows::Win32::Graphics::{
Direct3D11::{
ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D, D3D11_BOX, D3D11_CPU_ACCESS_READ,
Expand Down Expand Up @@ -312,27 +313,29 @@ impl<'a> FrameBuffer<'a> {

/// Get The Raw Pixel Data Without Padding
#[allow(clippy::type_complexity)]
pub fn as_raw_nopadding_buffer(
&'a mut self,
) -> Result<&'a mut Vec<u8>, Box<dyn Error + Send + Sync>> {
if self.buffer.capacity() < (self.width * self.height) as usize {
pub fn as_raw_nopadding_buffer(&'a mut self) -> Result<&'a [u8], Box<dyn Error + Send + Sync>> {
let frame_size = (self.width * self.height * 4) as usize;
if self.buffer.capacity() < frame_size {
trace!("Resizing Preallocated Buffer");
self.buffer.resize((self.width * self.height) as usize, 0);
self.buffer.resize(frame_size, 0);
}

for y in 0..self.height {
let width_size = (self.width * 4) as usize;
let buffer_address = self.buffer.as_mut_ptr() as isize;
(0..self.height).into_par_iter().for_each(|y| {
let index = (y * self.row_pitch) as usize;
let ptr = buffer_address as *mut u8;

unsafe {
ptr::copy_nonoverlapping(
self.raw_buffer.as_ptr().add(index),
self.buffer.as_mut_ptr(),
self.width as usize,
ptr.add(y as usize * width_size),
width_size,
);
}
}
});

Ok(self.buffer)
Ok(&self.buffer[0..frame_size])
}

/// Save The Frame Buffer As An Image To The Specified Path
Expand Down

0 comments on commit 90b324a

Please sign in to comment.