Skip to content

Commit

Permalink
Switch to BitBlt mode when screen orientation is changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Nov 24, 2020
1 parent 80e3d08 commit fc42790
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions Desktop.Win/Services/ScreenCapturerWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public Bitmap GetNextFrame()
// on the screen. I've observed this when a laptop lid is closed, or
// on some machines that aren't connected to a monitor. This will
// have it fall back to BitBlt in those cases.
if (_directxScreens.ContainsKey(SelectedScreen))
// TODO: Make DX capture work with changed screen orientation.
if (_directxScreens.ContainsKey(SelectedScreen) &&
SystemInformation.ScreenOrientation != ScreenOrientation.Angle270 &&
SystemInformation.ScreenOrientation != ScreenOrientation.Angle90)
{
var (result, frame) = GetDirectXFrame();

Expand Down Expand Up @@ -224,7 +227,7 @@ private enum GetDirectXFrameResult
return (GetDirectXFrameResult.Failure, null);
}

var currentFrame = new Bitmap(CurrentScreenBounds.Width, CurrentScreenBounds.Height, PixelFormat.Format32bppArgb);
var currentFrame = new Bitmap(texture2D.Description.Width, texture2D.Description.Height, PixelFormat.Format32bppArgb);

// Copy resource into memory that can be accessed by the CPU
using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
Expand Down Expand Up @@ -290,44 +293,46 @@ private void InitDirectX()
using (var factory = new Factory1())
{
foreach (var adapter in factory.Adapters1.Where(x => (x.Outputs?.Length ?? 0) > 0))
foreach (var output in adapter.Outputs)
{
try
foreach (var output in adapter.Outputs)
{
var device = new SharpDX.Direct3D11.Device(adapter);
var output1 = output.QueryInterface<Output1>();

var bounds = output1.Description.DesktopBounds;
var width = bounds.Right - bounds.Left;
var height = bounds.Bottom - bounds.Top;

// Create Staging texture CPU-accessible
var textureDesc = new Texture2DDescription
try
{
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm,
Width = width,
Height = height,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
ArraySize = 1,
SampleDescription = { Count = 1, Quality = 0 },
Usage = ResourceUsage.Staging
};

var texture2D = new Texture2D(device, textureDesc);

_directxScreens.Add(
output1.Description.DeviceName,
new DirectXOutput(adapter,
device,
output1.DuplicateOutput(device),
texture2D));
}
catch (Exception ex)
{
Logger.Write(ex);
var device = new SharpDX.Direct3D11.Device(adapter);
var output1 = output.QueryInterface<Output1>();

var bounds = output1.Description.DesktopBounds;
var width = bounds.Right - bounds.Left;
var height = bounds.Bottom - bounds.Top;

// Create Staging texture CPU-accessible
var textureDesc = new Texture2DDescription
{
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm,
Width = width,
Height = height,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
ArraySize = 1,
SampleDescription = { Count = 1, Quality = 0 },
Usage = ResourceUsage.Staging
};

var texture2D = new Texture2D(device, textureDesc);

_directxScreens.Add(
output1.Description.DeviceName,
new DirectXOutput(adapter,
device,
output1.DuplicateOutput(device),
texture2D));
}
catch (Exception ex)
{
Logger.Write(ex);
}
}
}
}
Expand Down

0 comments on commit fc42790

Please sign in to comment.