From 90b324aeeae8e5f532fc6a0ed54cd367fcb568c9 Mon Sep 17 00:00:00 2001 From: NiiightmareXD Date: Wed, 22 Nov 2023 23:05:50 -0800 Subject: [PATCH] =?UTF-8?q?Performance=20Boost=20=F0=9F=94=A5=20=09modifie?= =?UTF-8?q?d:=20=20=20Cargo.lock=20=09modified:=20=20=20Cargo.toml=20=09mo?= =?UTF-8?q?dified:=20=20=20src/frame.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + Cargo.toml | 1 + src/frame.rs | 23 +++++++++++++---------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc62cf1..cd296b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -553,6 +553,7 @@ dependencies = [ "image", "log", "parking_lot", + "rayon", "thiserror", "windows", ] diff --git a/Cargo.toml b/Cargo.toml index 66e33c4..bdf563a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/src/frame.rs b/src/frame.rs index 0b63005..f7497fc 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -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, @@ -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, Box> { - if self.buffer.capacity() < (self.width * self.height) as usize { + pub fn as_raw_nopadding_buffer(&'a mut self) -> Result<&'a [u8], Box> { + 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