Beispiel #1
0
        public CaptureEncode(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            _device      = device; _lastSize = item.Size;
            _d3dDevice   = Direct3D11Helpers.CreateSharpDXDevice(device);
            _captureItem = item;
            _isRecording = false;
            //_multithread = _d3dDevice.QueryInterface<SharpDX.Direct3D11.Multithread>();
            //_multithread.SetMultithreadProtected(true);
            //_frameEvent = new ManualResetEvent(false);
            //_closedEvent = new ManualResetEvent(false);
            //_events = new[] { _closedEvent, _frameEvent };

            CreateMediaObjects();
        }
Beispiel #2
0
        private SurfaceWithInfo WaitForNewFrame()
        {
            _currentFrame?.Dispose();
            _frameEvent.Reset();//获取新帧
            var whiteRegion = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, 1920, 1080, 1);

            var signaledEvent = _events[WaitHandle.WaitAny(_events)];

            if (signaledEvent == _closedEvent)
            {
                Cleanup();
                return(null);
            }

            var result = new SurfaceWithInfo {
                SystemRelativeTime = _currentFrame.SystemRelativeTime
            };

            using (var multithreadLock = new MultithreadLock(_multithread))
                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(_currentFrame.Surface))
                {
                    _d3dDevice.ImmediateContext.ClearRenderTargetView(_composeRenderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));

                    var region = new SharpDX.Direct3D11.ResourceRegion(LocationX > 0?LocationX:0, LocationY > 0?LocationY:0, 0,
                                                                       (LocationX + 1920 > sourceTexture.Description.Width ? sourceTexture.Description.Width : LocationX + 1920),
                                                                       (LocationY + 1080 > sourceTexture.Description.Height ? sourceTexture.Description.Height : LocationY + 1080), 1);
                    _d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, whiteRegion, _blankComposeTexture, 0);                 //覆盖掉原本区域
                    _d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, _tarComposeTexture, LocationX >= 0?0:(1920 + LocationX > 0? 1920 + LocationX - 1:0),
                                                                      LocationY >= 0?0:(1080 + LocationY - 1 > 0? 1080 + LocationY - 1:0), 0); //_targetTexture为剪切后的区域
                    //https://stackoverflow.com/questions/64130136/texture2d-from-byte-array-sharpdx
                    //实现空白区域
                    var description = _tarComposeTexture.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(_d3dDevice, description))
                    {
                        _d3dDevice.ImmediateContext.CopyResource(_tarComposeTexture, copyTexture);

                        result.Surface = Direct3D11Helpers.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture);
                    }
                }
            return(result);
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    // 源已改变,故需要变换抓取大小,首先改变swap chain,之后是Texture
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.ResizeBuffers(
                        2,
                        _lastSize.Width,
                        _lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }

                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
                    using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                        using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
                        {
                            _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
                            _d3dDevice.ImmediateContext.CopyResource(sourceTexture, backBuffer);
                        }
            }

            _swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);

            if (newSize)//帧池重构
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
        public CapturePreview(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            _item      = item;
            _device    = device;
            _d3dDevice = Direct3D11Helpers.CreateSharpDXDevice(device);

            var dxgiDevice = _d3dDevice.QueryInterface <SharpDX.DXGI.Device>();
            var adapter    = dxgiDevice.GetParent <SharpDX.DXGI.Adapter>();
            var factory    = adapter.GetParent <SharpDX.DXGI.Factory2>();

            var description = new SharpDX.DXGI.SwapChainDescription1 {
                Width             = item.Size.Width,
                Height            = item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied
            };

            _swapChain = new SharpDX.DXGI.SwapChain1(factory, dxgiDevice, ref description);

            _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                item.Size);
            _session  = _framePool.CreateCaptureSession(item);
            _lastSize = item.Size;

            _framePool.FrameArrived += OnFrameArrived;//新帧到达事件
        }