Skip to content

Commit bc50533

Browse files
committed
feat: CaptureBuilder + hide some fields
1 parent 7dce028 commit bc50533

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

zbl/examples/directx_interop.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Instant;
22

33
use clap::Parser;
44
use opencv::{core::UMat, highgui};
5-
use zbl::{Capturable, Capture, Display, Window};
5+
use zbl::{Capturable, CaptureBuilder, Display, Window};
66

77
#[derive(Parser, Debug)]
88
#[clap(version)]
@@ -28,9 +28,12 @@ fn main() {
2828
panic!("either --window-name or --display-id should be set!");
2929
};
3030

31-
let mut capture =
32-
Capture::new(target, false, true, false).expect("failed to initialize capture");
33-
opencv::core::initialize_context_from_d3d11_device(&mut capture.d3d.device)
31+
let mut capture = CaptureBuilder::new(target)
32+
.set_cpu_access(false)
33+
.build()
34+
.expect("failed to initialize capture");
35+
36+
opencv::core::initialize_context_from_d3d11_device(&mut capture.d3d().device)
3437
.expect("initialize d3d11");
3538

3639
capture.start().expect("failed to start capture");

zbl/examples/simple.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use opencv::{
66
highgui,
77
};
88
use windows::Win32::Foundation::HWND;
9-
use zbl::{Capturable, Capture, Display, Window};
9+
use zbl::{capture::CaptureBuilder, Capturable, Display, Window};
1010

1111
#[derive(Parser, Debug)]
1212
#[clap(version)]
@@ -39,8 +39,9 @@ fn main() {
3939
panic!("either --window-name or --display-id should be set!");
4040
};
4141

42-
let mut capture =
43-
Capture::new(target, false, true, true).expect("failed to initialize capture");
42+
let mut capture = CaptureBuilder::new(target)
43+
.build()
44+
.expect("failed to initialize capture");
4445

4546
capture.start().expect("failed to start capture");
4647

zbl/src/capture/mod.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,58 @@ pub trait Capturable {
3232
fn get_raw_handle(&self) -> isize;
3333
}
3434

35+
pub struct CaptureBuilder {
36+
capturable: Box<dyn Capturable>,
37+
is_cursor_capture_enabled: bool,
38+
is_border_required: bool,
39+
cpu_access: bool,
40+
}
41+
42+
impl CaptureBuilder {
43+
pub fn new(capturable: Box<dyn Capturable>) -> Self {
44+
Self {
45+
capturable,
46+
is_cursor_capture_enabled: false,
47+
is_border_required: true,
48+
cpu_access: true,
49+
}
50+
}
51+
52+
pub fn set_is_cursor_capture_enabled(mut self, val: bool) -> Self {
53+
self.is_cursor_capture_enabled = val;
54+
self
55+
}
56+
57+
pub fn set_is_border_required(mut self, val: bool) -> Self {
58+
self.is_border_required = val;
59+
self
60+
}
61+
62+
pub fn set_cpu_access(mut self, val: bool) -> Self {
63+
self.cpu_access = val;
64+
self
65+
}
66+
67+
pub fn build(self) -> Result<Capture> {
68+
Capture::new(
69+
self.capturable,
70+
self.is_cursor_capture_enabled,
71+
self.is_border_required,
72+
self.cpu_access,
73+
)
74+
}
75+
}
76+
77+
/// Represents a Capture session.
3578
pub struct Capture {
36-
pub d3d: D3D,
79+
d3d: D3D,
3780
capturable: Box<dyn Capturable>,
3881
capture_box: D3D11_BOX,
3982
capture_done_signal: Receiver<()>,
4083
frame_pool: Direct3D11CaptureFramePool,
4184
frame_source: Receiver<Option<Direct3D11CaptureFrame>>,
4285
session: GraphicsCaptureSession,
43-
pub cpu_access: bool,
86+
cpu_access: bool,
4487
staging_texture: Option<ID3D11Texture2D>,
4588
content_size: SizeInt32,
4689
stopped: bool,
@@ -51,7 +94,7 @@ impl Capture {
5194
/// frame pool / capture session.
5295
///
5396
/// Note that this will not start capturing yet. Call `start()` to actually start receiving frames.
54-
pub fn new(
97+
pub(crate) fn new(
5598
capturable: Box<dyn Capturable>,
5699
is_cursor_capture_enabled: bool,
57100
is_border_required: bool,
@@ -119,6 +162,16 @@ impl Capture {
119162
})
120163
}
121164

165+
/// Get D3D contexts
166+
pub fn d3d(&mut self) -> &mut D3D {
167+
&mut self.d3d
168+
}
169+
170+
/// Whether the backing buffer of this instance of `Capture` is CPU-accessible.
171+
pub fn has_cpu_access(&self) -> bool {
172+
self.cpu_access
173+
}
174+
122175
/// Get attached capturable.
123176
pub fn capturable(&self) -> &dyn Capturable {
124177
self.capturable.as_ref()

zbl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod d3d;
33
pub mod frame;
44
pub mod util;
55

6-
pub use capture::{display::Display, window::Window, Capturable, Capture};
6+
pub use capture::{display::Display, window::Window, Capturable, Capture, CaptureBuilder};
77
pub use frame::Frame;
88

99
// re-export winapi

zbl_py/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ impl Capture {
6666
cpu_access: bool,
6767
) -> Result<Self> {
6868
::zbl::init();
69-
let capture = ::zbl::Capture::new(
70-
capturable,
71-
is_cursor_capture_enabled,
72-
is_border_required,
73-
cpu_access,
74-
)?;
69+
let capture = ::zbl::CaptureBuilder::new(capturable)
70+
.set_is_cursor_capture_enabled(is_cursor_capture_enabled)
71+
.set_is_border_required(is_border_required)
72+
.set_cpu_access(cpu_access)
73+
.build()?;
7574
Ok(Self { inner: capture })
7675
}
7776

@@ -117,7 +116,7 @@ impl Capture {
117116
width: desc.Width,
118117
height: desc.Height,
119118
row_pitch: frame.mapped_ptr.RowPitch,
120-
ptr: if self.inner.cpu_access {
119+
ptr: if self.inner.has_cpu_access() {
121120
frame.mapped_ptr.pData
122121
} else {
123122
frame.texture.as_raw()

0 commit comments

Comments
 (0)