add zero copy mode hareware codec for windows (#6778)

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2024-01-02 16:58:10 +08:00
committed by GitHub
parent f47faa548b
commit 89150317e1
55 changed files with 2540 additions and 429 deletions

View File

@@ -1,4 +1,6 @@
use crate::{common::TraitCapturer, dxgi, Pixfmt};
#[cfg(feature = "gpucodec")]
use crate::AdapterDevice;
use crate::{common::TraitCapturer, dxgi, Frame, Pixfmt};
use std::{
io::{
self,
@@ -41,7 +43,7 @@ impl Capturer {
impl TraitCapturer for Capturer {
fn frame<'a>(&'a mut self, timeout: Duration) -> io::Result<Frame<'a>> {
match self.inner.frame(timeout.as_millis() as _) {
Ok(frame) => Ok(Frame::new(frame, self.width, self.height)),
Ok(frame) => Ok(frame),
Err(ref error) if error.kind() == TimedOut => Err(WouldBlock.into()),
Err(error) => Err(error),
}
@@ -54,21 +56,31 @@ impl TraitCapturer for Capturer {
fn set_gdi(&mut self) -> bool {
self.inner.set_gdi()
}
#[cfg(feature = "gpucodec")]
fn device(&self) -> AdapterDevice {
self.inner.device()
}
#[cfg(feature = "gpucodec")]
fn set_output_texture(&mut self, texture: bool) {
self.inner.set_output_texture(texture);
}
}
pub struct Frame<'a> {
pub struct PixelBuffer<'a> {
data: &'a [u8],
width: usize,
height: usize,
stride: Vec<usize>,
}
impl<'a> Frame<'a> {
impl<'a> PixelBuffer<'a> {
pub fn new(data: &'a [u8], width: usize, height: usize) -> Self {
let stride0 = data.len() / height;
let mut stride = Vec::new();
stride.push(stride0);
Frame {
PixelBuffer {
data,
width,
height,
@@ -77,7 +89,7 @@ impl<'a> Frame<'a> {
}
}
impl<'a> crate::TraitFrame for Frame<'a> {
impl<'a> crate::TraitPixelBuffer for PixelBuffer<'a> {
fn data(&self) -> &[u8] {
self.data
}
@@ -184,6 +196,11 @@ impl Display {
// https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodea
self.origin() == (0, 0)
}
#[cfg(feature = "gpucodec")]
pub fn adapter_luid(&self) -> Option<i64> {
self.0.adapter_luid()
}
}
pub struct CapturerMag {
@@ -215,11 +232,11 @@ impl CapturerMag {
impl TraitCapturer for CapturerMag {
fn frame<'a>(&'a mut self, _timeout_ms: Duration) -> io::Result<Frame<'a>> {
self.inner.frame(&mut self.data)?;
Ok(Frame::new(
Ok(Frame::PixelBuffer(PixelBuffer::new(
&self.data,
self.inner.get_rect().1,
self.inner.get_rect().2,
))
)))
}
fn is_gdi(&self) -> bool {
@@ -229,4 +246,12 @@ impl TraitCapturer for CapturerMag {
fn set_gdi(&mut self) -> bool {
false
}
#[cfg(feature = "gpucodec")]
fn device(&self) -> AdapterDevice {
AdapterDevice::default()
}
#[cfg(feature = "gpucodec")]
fn set_output_texture(&mut self, _texture: bool) {}
}