Skip to content

Commit

Permalink
Mutable Buffer 🔥
Browse files Browse the repository at this point in the history
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   README.md
	modified:   src/frame.rs
	modified:   src/lib.rs
	modified:   windows-capture-python/Cargo.lock
	modified:   windows-capture-python/Cargo.toml
	modified:   windows-capture-python/pyproject.toml
	modified:   windows-capture-python/src/lib.rs
  • Loading branch information
NiiightmareXD committed Nov 30, 2023
1 parent a849ad6 commit df9c1d4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windows-capture"
version = "1.0.41"
version = "1.0.42"
authors = ["NiiightmareXD"]
edition = "2021"
description = "Fastest Windows Screen Capture Library For Rust 🔥"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add this library to your `Cargo.toml`:

```toml
[dependencies]
windows-capture = "1.0.41"
windows-capture = "1.0.42"
```
or run this command

Expand Down
45 changes: 29 additions & 16 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
use windows::Win32::Graphics::{
Direct3D11::{
ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D, D3D11_BOX, D3D11_CPU_ACCESS_READ,
D3D11_MAPPED_SUBRESOURCE, D3D11_MAP_READ, D3D11_TEXTURE2D_DESC, D3D11_USAGE_STAGING,
D3D11_CPU_ACCESS_WRITE, D3D11_MAPPED_SUBRESOURCE, D3D11_MAP_READ_WRITE,
D3D11_TEXTURE2D_DESC, D3D11_USAGE_STAGING,
},
Dxgi::Common::{DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SAMPLE_DESC},
};
Expand Down Expand Up @@ -85,7 +86,7 @@ impl<'a> Frame<'a> {
},
Usage: D3D11_USAGE_STAGING,
BindFlags: 0,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32 | D3D11_CPU_ACCESS_WRITE.0 as u32,
MiscFlags: 0,
};

Expand All @@ -105,14 +106,19 @@ impl<'a> Frame<'a> {
// Map The Texture To Enable CPU Access
let mut mapped_resource = D3D11_MAPPED_SUBRESOURCE::default();
unsafe {
self.context
.Map(&texture, 0, D3D11_MAP_READ, 0, Some(&mut mapped_resource))?;
self.context.Map(
&texture,
0,
D3D11_MAP_READ_WRITE,
0,
Some(&mut mapped_resource),
)?;
};

// Get The Mapped Resource Data Slice
let mapped_frame_data = unsafe {
slice::from_raw_parts(
mapped_resource.pData as *const u8,
slice::from_raw_parts_mut(
mapped_resource.pData.cast(),
(self.height * mapped_resource.RowPitch) as usize,
)
};
Expand Down Expand Up @@ -163,7 +169,7 @@ impl<'a> Frame<'a> {
},
Usage: D3D11_USAGE_STAGING,
BindFlags: 0,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32 | D3D11_CPU_ACCESS_WRITE.0 as u32,
MiscFlags: 0,
};

Expand Down Expand Up @@ -202,14 +208,19 @@ impl<'a> Frame<'a> {
// Map The Texture To Enable CPU Access
let mut mapped_resource = D3D11_MAPPED_SUBRESOURCE::default();
unsafe {
self.context
.Map(&texture, 0, D3D11_MAP_READ, 0, Some(&mut mapped_resource))?;
self.context.Map(
&texture,
0,
D3D11_MAP_READ_WRITE,
0,
Some(&mut mapped_resource),
)?;
};

// Get The Mapped Resource Data Slice
let mapped_frame_data = unsafe {
slice::from_raw_parts(
mapped_resource.pData as *const u8,
slice::from_raw_parts_mut(
mapped_resource.pData.cast(),
(texture_height * mapped_resource.RowPitch) as usize,
)
};
Expand Down Expand Up @@ -243,7 +254,7 @@ impl<'a> Frame<'a> {

/// Frame Buffer Struct Used To Get Raw Pixel Data
pub struct FrameBuffer<'a> {
raw_buffer: &'a [u8],
raw_buffer: &'a mut [u8],
buffer: &'a mut Vec<u8>,
width: u32,
height: u32,
Expand All @@ -256,7 +267,7 @@ impl<'a> FrameBuffer<'a> {
/// Create A New Frame Buffer
#[must_use]
pub fn new(
raw_buffer: &'a [u8],
raw_buffer: &'a mut [u8],
buffer: &'a mut Vec<u8>,
width: u32,
height: u32,
Expand Down Expand Up @@ -307,13 +318,15 @@ impl<'a> FrameBuffer<'a> {

/// Get The Raw Pixel Data Might Have Padding
#[must_use]
pub const fn as_raw_buffer(&self) -> &'a [u8] {
pub fn as_raw_buffer(&'a mut self) -> &'a mut [u8] {
self.raw_buffer
}

/// Get The Raw Pixel Data Without Padding
#[allow(clippy::type_complexity)]
pub fn as_raw_nopadding_buffer(&'a mut self) -> Result<&'a [u8], Box<dyn Error + Send + Sync>> {
pub fn as_raw_nopadding_buffer(
&'a mut self,
) -> Result<&'a mut [u8], Box<dyn Error + Send + Sync>> {
if !self.has_padding() {
return Ok(self.raw_buffer);
}
Expand All @@ -339,7 +352,7 @@ impl<'a> FrameBuffer<'a> {
}
});

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

/// Save The Frame Buffer As An Image To The Specified Path
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! ```toml
//! [dependencies]
//! windows-capture = "1.0.41"
//! windows-capture = "1.0.42"
//! ```
//! or run this command
//!
Expand Down
2 changes: 1 addition & 1 deletion windows-capture-python/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion windows-capture-python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windows-capture-python"
version = "1.0.41"
version = "1.0.42"
authors = ["NiiightmareXD"]
edition = "2021"
description = "Fastest Windows Screen Capture Library For Python 🔥"
Expand Down
2 changes: 1 addition & 1 deletion windows-capture-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "windows-capture"
version = "1.0.41"
version = "1.0.42"
description = "Fastest Windows Screen Capture Library For Python 🔥"
readme = "README.md"
requires-python = ">=3.9"
Expand Down
4 changes: 2 additions & 2 deletions windows-capture-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl NativeWindowsCapture {
}

if window_name.is_none() && monitor_index.is_none() {
monitor_index = Some(0);
monitor_index = Some(1);
}

Ok(Self {
Expand Down Expand Up @@ -399,7 +399,7 @@ impl WindowsCaptureHandler for InnerNativeWindowsCapture {
) -> Result<(), Box<(dyn Error + Send + Sync)>> {
let width = frame.width();
let height = frame.height();
let buffer = frame.buffer()?;
let mut buffer = frame.buffer()?;
let buffer = buffer.as_raw_buffer();

Python::with_gil(|py| -> PyResult<()> {
Expand Down

0 comments on commit df9c1d4

Please sign in to comment.