public CaptureFrameWait(IDirect3DDevice device, Func <GraphicsCaptureItem> itemFactory) { this.device = device; this.d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device); this.multithread = d3dDevice.QueryInterface <SharpDX.Direct3D11.Multithread>(); this.multithread.SetMultithreadProtected(true); this.frameEvent = new ManualResetEvent(false); this.closedEvent = new ManualResetEvent(false); this.events = new[] { closedEvent, frameEvent }; this.itemFactory = itemFactory; this.CurrentItem = this.itemFactory(); InitializeBlankTexture(this.CurrentItem.Size); InitializeCapture(); }
public SurfaceWithInfo WaitForNewFrame() { // Let's get a fresh one. this.currentFrame?.Dispose(); this.frameEvent.Reset(); var signaledEvent = this.events[WaitHandle.WaitAny(this.events)]; if (signaledEvent == this.closedEvent) { this.CleanupItem(); return(null); } using var multithreadLock = new MultithreadLock(this.multithread); using var sourceTexture = Direct3D11Helper.CreateSharpDXTexture2D(this.currentFrame.Surface); var description = sourceTexture.Description; description.Usage = SharpDX.Direct3D11.ResourceUsage.Default; description.BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget; description.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None; description.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None; using var copyTexture = new SharpDX.Direct3D11.Texture2D(this.d3dDevice, description); var width = Math.Clamp(this.currentFrame.ContentSize.Width, 0, this.currentFrame.Surface.Description.Width); var height = Math.Clamp(this.currentFrame.ContentSize.Height, 0, this.currentFrame.Surface.Description.Height); var region = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, width, height, 1); this.d3dDevice.ImmediateContext.CopyResource(blankTexture, copyTexture); this.d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, copyTexture, 0); var result = new SurfaceWithInfo { SystemRelativeTime = this.currentFrame.SystemRelativeTime, Surface = Direct3D11Helper.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture) }; return(result); }