private bool ProcessFrame(Direct3D11CaptureFrame frame, Func <IntPtr, int, int, int, int, object, bool> setPacket)
        {
            bool processed = true;

            using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
            {
                if (_dx == null)
                {
                    _d3dDevice.ImmediateContext.CopyResource(bitmap, _screenTexture);
                    var data = _d3dDevice.ImmediateContext.MapSubresource(_screenTexture, 0, MapMode.Read, MapFlags.None);

                    if (_initSize.Width * 4 > data.RowPitch)
                    {
                        ScreenCaptureManager.Instance.Logger.Warining($"Width of capture is higher then Pitch {_initSize.Width * 4} > {data.RowPitch}");
                    }

                    processed = setPacket(data.DataPointer, 4, _initSize.Width, _initSize.Height, data.RowPitch, null);
                    _d3dDevice.ImmediateContext.UnmapSubresource(_screenTexture, 0);
                }
                else
                {
                    var dxRes = _dx.CreateCopy(bitmap);
                    processed = setPacket(IntPtr.Zero, 0, 0, 0, 0, dxRes);
                }
            }
            return(processed);
        }
        public ScreenCaptureEngine(ScreenCaptureRequest request, IDirectXContext dx, Action <SizeInt32> onSizeChanged)
        {
            _dx            = dx;
            _onSizeChanged = onSizeChanged;
            _initSize      = request.InitialSize;

            ScreenCaptureManager.Instance.Logger.Info($"Opening capture {request.Id} ({_initSize.Width}x{_initSize.Height})");

            ScreenCaptureManager.Instance.MainThreadExecutor.Execute(() =>
            {
                _device    = dx == null ? Direct3D11Helper.CreateDevice() : Direct3D11Helper.CreateDirect3DDeviceFromSharpDXDevice(dx.Device);
                _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device);
                try
                {
                    _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                        _device,
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        2,
                        request.InitialSize);

                    _framePool.FrameArrived += OnFrameArrived;
                    _session = _framePool.CreateCaptureSession(request.Item);
                    if (ApiInformation.IsApiContractPresent(typeof(Windows.Foundation.UniversalApiContract).FullName, 10))
                    {
                        SetCursor(_session, request.Cursor);
                    }

                    _session.StartCapture();
                }
                catch (Exception e)
                {
                    ScreenCaptureManager.Instance.Logger.Error(e, $"Failed to create capture");
                }
            }, true);

            _screenTexture = CreateTexture(_initSize);

            ScreenCaptureManager.Instance.Logger.Info($"Opened capture {request.Id}");
        }