small refactor
This commit is contained in:
parent
5958d28ed8
commit
3fe7c0b37d
11 changed files with 11 additions and 237 deletions
|
|
@ -1,132 +0,0 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using OpenCvSharp;
|
||||
using Serilog;
|
||||
using SharpGen.Runtime;
|
||||
using Vortice.Direct3D;
|
||||
using Vortice.Direct3D11;
|
||||
using Vortice.DXGI;
|
||||
using Region = Poe2Trade.Core.Region;
|
||||
|
||||
namespace Poe2Trade.Navigation;
|
||||
|
||||
public sealed class DesktopDuplication : IScreenCapture
|
||||
{
|
||||
private readonly ID3D11Device _device;
|
||||
private readonly ID3D11DeviceContext _context;
|
||||
private IDXGIOutputDuplication? _duplication;
|
||||
private ID3D11Texture2D? _staging;
|
||||
private int _stagingW, _stagingH;
|
||||
private bool _needsRecreate;
|
||||
|
||||
public DesktopDuplication()
|
||||
{
|
||||
D3D11.D3D11CreateDevice(
|
||||
null,
|
||||
DriverType.Hardware,
|
||||
DeviceCreationFlags.BgraSupport,
|
||||
[FeatureLevel.Level_11_0],
|
||||
out _device!,
|
||||
out _context!).CheckError();
|
||||
|
||||
CreateDuplication();
|
||||
}
|
||||
|
||||
private void CreateDuplication()
|
||||
{
|
||||
using var dxgiDevice = _device.QueryInterface<IDXGIDevice>();
|
||||
using var adapter = dxgiDevice.GetAdapter();
|
||||
adapter.EnumOutputs(0, out var output);
|
||||
using var _ = output;
|
||||
using var output1 = output.QueryInterface<IDXGIOutput1>();
|
||||
_duplication = output1.DuplicateOutput(_device);
|
||||
_needsRecreate = false;
|
||||
Log.Debug("DXGI Desktop Duplication created");
|
||||
}
|
||||
|
||||
public unsafe ScreenFrame? CaptureFrame()
|
||||
{
|
||||
if (_duplication == null) return null;
|
||||
|
||||
if (_needsRecreate)
|
||||
{
|
||||
try
|
||||
{
|
||||
_duplication.Dispose();
|
||||
CreateDuplication();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex, "Failed to recreate DXGI duplication");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
IDXGIResource? resource = null;
|
||||
try
|
||||
{
|
||||
_duplication.AcquireNextFrame(100, out _, out resource);
|
||||
}
|
||||
catch (SharpGenException ex)
|
||||
{
|
||||
if (ex.ResultCode == Vortice.DXGI.ResultCode.AccessLost)
|
||||
_needsRecreate = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
var srcTexture = resource!.QueryInterface<ID3D11Texture2D>();
|
||||
var desc = srcTexture.Description;
|
||||
var w = (int)desc.Width;
|
||||
var h = (int)desc.Height;
|
||||
EnsureStaging(w, h);
|
||||
|
||||
_context.CopySubresourceRegion(_staging!, 0, 0, 0, 0, srcTexture, 0);
|
||||
|
||||
var mapped = _context.Map(_staging!, 0, MapMode.Read);
|
||||
|
||||
var mat = Mat.FromPixelData(h, w, MatType.CV_8UC4, mapped.DataPointer, (int)mapped.RowPitch);
|
||||
|
||||
var duplication = _duplication!;
|
||||
return new ScreenFrame(mat, () =>
|
||||
{
|
||||
_context.Unmap(_staging!, 0);
|
||||
srcTexture.Dispose();
|
||||
resource.Dispose();
|
||||
duplication.ReleaseFrame();
|
||||
});
|
||||
}
|
||||
|
||||
public unsafe Mat? CaptureRegion(Region region)
|
||||
{
|
||||
using var frame = CaptureFrame();
|
||||
if (frame == null) return null;
|
||||
return frame.CropBgr(region);
|
||||
}
|
||||
|
||||
private void EnsureStaging(int w, int h)
|
||||
{
|
||||
if (_staging != null && _stagingW == w && _stagingH == h) return;
|
||||
_staging?.Dispose();
|
||||
|
||||
_staging = _device.CreateTexture2D(new Texture2DDescription
|
||||
{
|
||||
Width = (uint)w,
|
||||
Height = (uint)h,
|
||||
MipLevels = 1,
|
||||
ArraySize = 1,
|
||||
Format = Format.B8G8R8A8_UNorm,
|
||||
SampleDescription = new SampleDescription(1, 0),
|
||||
Usage = ResourceUsage.Staging,
|
||||
CPUAccessFlags = CpuAccessFlags.Read,
|
||||
});
|
||||
_stagingW = w;
|
||||
_stagingH = h;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_staging?.Dispose();
|
||||
_duplication?.Dispose();
|
||||
_context?.Dispose();
|
||||
_device?.Dispose();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue