sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 641 forks source link

SharpDx - DesktopDuplication with multiple outputs #1016

Open AndrejHafner opened 6 years ago

AndrejHafner commented 6 years ago

I'm having a problem when trying to duplicate the non-primary desktop output. When I try to duplicate the primary monitor it works normally, but when trying to output a secondary output (other monitor) I get a timeout error, also the dimensions of the screen are weird.

This works in SharpDX 2.6.3, butnot on 4.0.1.

In the adapter there are 2 outputs (which is correct, i have a 1920x1080 display and a 1366x768 on a laptop). The first (0th - primary) output is shown as a 1920x1080 (which is correct), but the second one is show as 3386x1080. This means that the second output are both screens combined. When I try to view this image I get back, it's all black and nothing is shown. I wanted to cut the second output from this bigger image to duplicate only that part, but the problem is that its all black, no information. In SharpDx 2.6.3 it normally returns a second output as a 1366x768 with the correct image.

The code I used:

public DesktopDuplicator(int whichGraphicsCardAdapter, int whichOutputDevice)
        {
            this.mWhichOutputDevice = whichOutputDevice;
            Adapter1 adapter = null;
            Factory1 factory;
            try
            {
                factory = new Factory1();

                adapter = factory.GetAdapter1(whichGraphicsCardAdapter);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified graphics card adapter.");
            }
            this.mDevice = new Device(adapter);
            Output output = null;
            try
            {
                output = adapter.GetOutput(whichOutputDevice);

            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified output device.");
            }

            var output1 = output.QueryInterface<Output1>();
            this.mOutputDesc = output.Description;
            this.mTextureDesc = new Texture2DDescription()
            {
                CpuAccessFlags = CpuAccessFlags.Read,
                BindFlags = BindFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                Width = this.mOutputDesc.DesktopBounds.Right,
                Height = this.mOutputDesc.DesktopBounds.Bottom,
                OptionFlags = ResourceOptionFlags.None,
                MipLevels = 1,
                ArraySize = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage = ResourceUsage.Staging,

            };

            try
            {
                this.mDeskDupl = output1.DuplicateOutput(mDevice);
            }
            catch (SharpDXException ex)
            {
                if (ex.ResultCode.Code == SharpDX.DXGI.ResultCode.NotCurrentlyAvailable.Result.Code)
                {
                    throw new DesktopDuplicationException("There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.");
                }
            }
        }

The error I get is: HRESULT: [0x887A0027], Module: [SharpDX.DXGI], ApiCode: [DXGI_ERROR_WAIT_TIMEOUT/WaitTimeout], Message: The timeout value has elapsed and the resource is not yet available.

posnetek zaslona 21

BEEden commented 5 years ago

Is this problem still ongoing for you? I can test it out if you like, I have four Outputs.