Example #1
0
        private void Application_SlideShowBegin(SlideShowWindow Wn)
        {
            if (!PresentationToNDIAddIn.Properties.Settings.Default.NDIDynamic)
            {
                return;
            }

            _window = Wn;
            _wasFullscreenBefore = IsFullscreen;
            _xOrig = Wn.Presentation.SlideMaster.Width;
            _yOrig = Wn.Presentation.SlideMaster.Height;

            _device    = Direct3D11Helper.CreateDevice(!PresentationToNDIAddIn.Properties.Settings.Default.UseHw);
            _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device);
            _factory   = new Factory2();

            _item = GetItem();
            CreateCaptureItemDependendStuff();

            _ndiSender = new Thread(SendNdi)
            {
                Priority = ThreadPriority.Normal, Name = "DynamicNdiSenderThread", IsBackground = true
            };
            _ndiSender.Start();
        }
        public BasicSampleApplication(Compositor compositor)
        {
            _compositor = compositor;
            _device     = Direct3D11Helper.CreateDevice();

            // Setup our root
            _root = _compositor.CreateContainerVisual();
            _root.RelativeSizeAdjustment = Vector2.One;

            // Setup our content
            _brush = _compositor.CreateSurfaceBrush();
            _brush.HorizontalAlignmentRatio = 0.5f;
            _brush.VerticalAlignmentRatio   = 0.5f;
            _brush.Stretch = CompositionStretch.Uniform;

            var shadow = _compositor.CreateDropShadow();

            shadow.Mask = _brush;

            _content             = _compositor.CreateSpriteVisual();
            _content.AnchorPoint = new Vector2(0.5f);
            _content.RelativeOffsetAdjustment = new Vector3(0.5f, 0.5f, 0);
            _content.RelativeSizeAdjustment   = Vector2.One;
            _content.Size   = new Vector2(-80, -80);
            _content.Brush  = _brush;
            _content.Shadow = shadow;
            _root.Children.InsertAtTop(_content);
        }
        public OverlayScreenCaptureCaptureHandler(Compositor c)
        {
            __compositor = c;
            __device     = Direct3D11Helper.CreateDevice();

            // Setup the root.
            __root = __compositor.CreateContainerVisual();
            __root.RelativeSizeAdjustment = Vector2.One;

            // Setup the content.
            __brush = __compositor.CreateSurfaceBrush();
            __brush.HorizontalAlignmentRatio = 0.5f;
            __brush.VerticalAlignmentRatio   = 0.5f;
            __brush.Stretch = CompositionStretch.Uniform;

            var shadow = __compositor.CreateDropShadow();

            shadow.Mask = __brush;

            __content             = __compositor.CreateSpriteVisual();
            __content.AnchorPoint = new Vector2(0.5f);
            __content.RelativeOffsetAdjustment = new Vector3(0.5f, 0.5f, 0);
            __content.RelativeSizeAdjustment   = Vector2.One;
            __content.Brush  = __brush;
            __content.Shadow = shadow;
            __root.Children.InsertAtTop(__content);
        }
Example #4
0
        protected virtual void ResetFramePool(SizeInt32 size, bool recreateDevice)
        {
            do
            {
                try
                {
                    if (recreateDevice)
                    {
                        _device = Direct3D11Helper.CreateDevice(!PresentationToNDIAddIn.Properties.Settings.Default.UseHw);
                    }
                }
                catch
                {
                    _device        = null;
                    recreateDevice = true;
                }
            } while (_device == null);

            // detect change from or to fullscreen --> captureItem needs to be recreated
            if ((_wasFullscreenBefore && !IsFullscreen) || (!_wasFullscreenBefore && IsFullscreen))
            {
                _wasFullscreenBefore = IsFullscreen;
                DisposeCaptureItemDependentStuff();
                _item = GetItem();
                CreateCaptureItemDependendStuff();
            }
            else
            {
                _framePool.Recreate(_device, PixelFormat, 2, size);
            }
        }
        public BasicSampleApplication(Compositor c)
        {
            compositor = c;
            device     = Direct3D11Helper.CreateDevice(false);

            // Setup the root.
            root = compositor.CreateContainerVisual();
            root.RelativeSizeAdjustment = Vector2.One;

            // Setup the content.
            brush = compositor.CreateSurfaceBrush();
            brush.HorizontalAlignmentRatio = 0.5f;
            brush.VerticalAlignmentRatio   = 0.5f;
            brush.Stretch = CompositionStretch.Uniform;

            var shadow = compositor.CreateDropShadow();

            shadow.Mask = brush;

            content             = compositor.CreateSpriteVisual();
            content.AnchorPoint = new Vector2(0.5f);
            content.RelativeOffsetAdjustment = new Vector3(0.5f, 0.5f, 0);
            content.RelativeSizeAdjustment   = Vector2.One;
            content.Size   = new Vector2(-80, -80);
            content.Brush  = brush;
            content.Shadow = shadow;
            root.Children.InsertAtTop(content);
        }
Example #6
0
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.

        #endregion

        #region Initialization

        private void InitializeDevices()
        {
            var createdDevice = Direct3D11Helper.CreateDevice();

            device = createdDevice ?? throw new Exception("Failed to create Direct3D device.");

            var createdSharpDXDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            sharpDXDevice = createdSharpDXDevice ?? throw new Exception("Failed to create SharpDX device.");

            multithread = sharpDXDevice.QueryInterface <Multithread>();
            multithread.SetMultithreadProtected(true);
        }
Example #7
0
        public ScreenCapture(GraphicsCaptureItem i)
        {
            item = i;

            device    = Direct3D11Helper.CreateDevice();
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            SizeInt32 correctedSize = item.Size;

            correctedSize.Width  = correctedSize.Width / 8 * 8;
            correctedSize.Height = correctedSize.Height / 8 * 8;

            var dxgiFactory = new Factory2();
            var description = new SwapChainDescription1()
            {
                Width             = correctedSize.Width,
                Height            = correctedSize.Height,
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = Scaling.Stretch,
                SwapEffect  = SwapEffect.FlipSequential,
                AlphaMode   = AlphaMode.Premultiplied,
                Flags       = SwapChainFlags.None
            };

            swapChain = new SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                correctedSize);
            session  = framePool.CreateCaptureSession(i);
            lastSize = correctedSize;

            framePool.FrameArrived += OnFrameArrived;
        }
Example #8
0
        public GraphicsItemD3DImage()
        {
            if (App.Current.MainWindow != null)
            {
                IntPtr window        = new WindowInteropHelper(App.Current.MainWindow).Handle;
                var    presentparams = new SharpDX.Direct3D9.PresentParameters
                {
                    Windowed             = true,
                    SwapEffect           = SharpDX.Direct3D9.SwapEffect.Discard,
                    DeviceWindowHandle   = window,
                    PresentationInterval = PresentInterval.Immediate,
                };

                const CreateFlags deviceFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve;

                this.direct3d  = new Direct3DEx();
                this.device    = Direct3D11Helper.CreateDevice();
                this.d3dDevice = Direct3D11Helper.CreateSharpDXDevice(this.device);
                this.d9device  = new DeviceEx(this.direct3d, 0, DeviceType.Hardware, IntPtr.Zero, deviceFlags, presentparams);
                this.IsFrontBufferAvailableChanged += this.GraphicItemD3DImageIsFrontBufferAvailableChanged;
            }
        }
Example #9
0
        public BasicSampleApplication(Compositor c)
        {
            compositor = c;
            device     = Direct3D11Helper.CreateDevice();

            // Setup the root.
            containerVisual = compositor.CreateContainerVisual();
            containerVisual.RelativeSizeAdjustment = Vector2.One;

            // Setup the content.
            brush = compositor.CreateSurfaceBrush();
            brush.HorizontalAlignmentRatio = 0.1f;
            brush.VerticalAlignmentRatio   = 0.1f;
            brush.Stretch            = CompositionStretch.Uniform;
            spriteVisual             = compositor.CreateSpriteVisual();
            spriteVisual.AnchorPoint = new Vector2(0.1f);
            spriteVisual.RelativeOffsetAdjustment = new Vector3(0.1f, 0.1f, 0);
            spriteVisual.RelativeSizeAdjustment   = Vector2.One;
            //spriteVisual.Size = new Vector2(-80, -80);
            spriteVisual.Brush = brush;
            containerVisual.Children.InsertAtTop(spriteVisual);
        }
Example #10
0
 public BasicSampleApplication()
 {
     device = Direct3D11Helper.CreateDevice();
 }
Example #11
0
 public VideoCapture()
 {
     this.picker = new GraphicsCapturePicker();
     this.device = Direct3D11Helper.CreateDevice();
 }
Example #12
0
 public BasicCapturer()
 {
     device = Direct3D11Helper.CreateDevice();
 }
Example #13
0
        static void Main(string[] args)
        {
            // Get a capture item that represents the primary monitor
            var monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
            var item    = CaptureHelper.CreateItemForMonitor(monitor);
            var size    = item.Size;

            // Setup D3D
            var device     = Direct3D11Helper.CreateDevice();
            var d3dDevice  = Direct3D11Helper.CreateSharpDXDevice(device);
            var d3dContext = d3dDevice.ImmediateContext;

            // Create our staging texture
            var description = new SharpDX.Direct3D11.Texture2DDescription
            {
                Width             = size.Width,
                Height            = size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Staging,
                BindFlags      = SharpDX.Direct3D11.BindFlags.None,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };
            var stagingTexture = new SharpDX.Direct3D11.Texture2D(d3dDevice, description);

            // Setup capture
            var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                1,
                size);
            var session = framePool.CreateCaptureSession(item);

            var imageNum = 0;
            var endEvent = new ManualResetEvent(false);

            framePool.FrameArrived += (sender, a) =>
            {
                using (var frame = sender.TryGetNextFrame())
                    using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                    {
                        // Copy to our staging texture
                        d3dContext.CopyResource(bitmap, stagingTexture);

                        // Map our texture and get the bits
                        var mapped       = d3dContext.MapSubresource(stagingTexture, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
                        var source       = mapped.DataPointer;
                        var sourceStride = mapped.RowPitch;

                        // Allocate some memory to hold our copy
                        var bytes = new byte[size.Width * size.Height * 4]; // 4 bytes per pixel
                        unsafe
                        {
                            fixed(byte *bytesPointer = bytes)
                            {
                                var dest       = (IntPtr)bytesPointer;
                                var destStride = size.Width * 4;

                                for (int i = 0; i < size.Height; i++)
                                {
                                    SharpDX.Utilities.CopyMemory(dest, source, destStride);

                                    source = IntPtr.Add(source, sourceStride);
                                    dest   = IntPtr.Add(dest, destStride);
                                }
                            }
                        }

                        // Don't forget to unmap when you're done!
                        d3dContext.UnmapSubresource(stagingTexture, 0);

                        // Encode it
                        // NOTE: Waiting here will stall the capture
                        EncodeBytesAsync($"image{imageNum}.png", size.Width, size.Height, bytes).Wait();

                        imageNum++;
                        if (imageNum > 10)
                        {
                            framePool.Dispose();
                            session.Dispose();
                            endEvent.Set();
                        }
                    }
            };

            // Start the capture and wait
            session.StartCapture();
            endEvent.WaitOne();
        }