Ejemplo n.º 1
0
            public void PresentSurface(IDirect3DSurface surface)
            {
                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(surface))
                {
                    if (!_isSwapChainSized)
                    {
                        var description = sourceTexture.Description;

                        _swapChain.ResizeBuffers(
                            2,
                            description.Width,
                            description.Height,
                            SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                            SharpDX.DXGI.SwapChainFlags.None);

                        _isSwapChainSized = true;
                    }

                    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);
            }
Ejemplo n.º 2
0
        public Encoder(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            _device      = device;
            _d3dDevice   = Direct3D11Helpers.CreateSharpDXDevice(device);
            _captureItem = item;
            _isRecording = false;
            _previewLock = new object();

            CreateMediaObjects();
        }
Ejemplo n.º 3
0
        public CaptureFrameWait(
            IDirect3DDevice device,
            GraphicsCaptureItem item,
            SizeInt32 size)
        {
            _device      = device;
            _d3dDevice   = Direct3D11Helpers.CreateSharpDXDevice(device);
            _multithread = _d3dDevice.QueryInterface <SharpDX.Direct3D11.Multithread>();
            _multithread.SetMultithreadProtected(true);
            _item        = item;
            _frameEvent  = new ManualResetEvent(false);
            _closedEvent = new ManualResetEvent(false);
            _events      = new[] { _closedEvent, _frameEvent };

            InitializeBlankTexture(size);
            InitializeCapture(size);
        }
        public SurfaceWithInfo WaitForNewFrame()
        {
            // Let's get a fresh one.
            _currentFrame?.Dispose();
            _frameEvent.Reset();

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

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

            var result = new SurfaceWithInfo();

            //System.Diagnostics.Debug.WriteLine("Using _currentFrame");
            result.SystemRelativeTime = _currentFrame.SystemRelativeTime;
            //System.Diagnostics.Debug.WriteLine("Done using _currentFrame");
            //result.Surface = _currentFrame.Surface; return result;
            using (var multithreadLock = new MultithreadLock(_multithread))
                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(_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(_d3dDevice, description))
                    {
                        var width  = Math.Clamp(_currentFrame.ContentSize.Width, 0, _currentFrame.Surface.Description.Width);
                        var height = Math.Clamp(_currentFrame.ContentSize.Height, 0, _currentFrame.Surface.Description.Height);

                        var region = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, width, height, 1);

                        _d3dDevice.ImmediateContext.CopyResource(_blankTexture, copyTexture);
                        _d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, copyTexture, 0);
                        result.Surface = Direct3D11Helpers.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture);
                    }
                }

            return(result);
        }
Ejemplo n.º 5
0
        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)
                {
                    // The thing we have been capturing has changed size.
                    // We need to resize our swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate our frame pool.
                    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);
                        }
            } // retire the frame

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

            if (newSize)
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
        public CaptureFrameWait(
            Microsoft.Graphics.Canvas.CanvasDevice canvasDevice,
            GraphicsCaptureItem item,
            Windows.UI.Composition.CompositionDrawingSurface surface,
            SizeInt32 size)
        {
            // _device = device;
            _canvasDevice = canvasDevice;
            _d3dDevice    = Direct3D11Helpers.CreateSharpDXDevice(_canvasDevice);
            _multithread  = _d3dDevice.QueryInterface <SharpDX.Direct3D11.Multithread>();
            _multithread.SetMultithreadProtected(true);
            _item        = item;
            _surface     = surface;
            _frameEvent  = new ManualResetEvent(false);
            _closedEvent = new ManualResetEvent(false);
            _events      = new[] { _closedEvent, _frameEvent };

            InitializeBlankTexture(size);
            InitializeCapture(size);

            _frameCounter = 0;
        }
Ejemplo n.º 7
0
        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;
        }