async improvement

This commit is contained in:
Boki 2026-02-15 18:39:55 -05:00
parent 3fe7c0b37d
commit 9de6293b1a
4 changed files with 101 additions and 34 deletions

View file

@ -83,16 +83,26 @@ public sealed class DesktopDuplication : IScreenCapture
var mapped = _context.Map(_staging!, 0, MapMode.Read);
var mat = Mat.FromPixelData(h, w, MatType.CV_8UC4, mapped.DataPointer, (int)mapped.RowPitch);
// GPU copy is complete once Map returns — release DXGI frame immediately
// so the DWM compositor can recycle the buffer (~0.5ms hold vs ~2.5ms before).
srcTexture.Dispose();
resource.Dispose();
_duplication!.ReleaseFrame();
var duplication = _duplication!;
return new ScreenFrame(mat, () =>
// CPU copy from our staging texture (no DXGI dependency, ~1.5ms for 2560×1440)
var mat = new Mat(h, w, MatType.CV_8UC4);
unsafe
{
_context.Unmap(_staging!, 0);
srcTexture.Dispose();
resource.Dispose();
duplication.ReleaseFrame();
});
var src = (byte*)mapped.DataPointer;
var dst = (byte*)mat.Data;
var rowBytes = w * 4;
var pitch = (int)mapped.RowPitch;
for (var row = 0; row < h; row++)
Buffer.MemoryCopy(src + row * pitch, dst + row * rowBytes, rowBytes, rowBytes);
}
_context.Unmap(_staging!, 0);
return new ScreenFrame(mat, () => mat.Dispose());
}
public unsafe Mat? CaptureRegion(Region region)