port from perforce
This commit is contained in:
144
aiwaz/Aiwaz.Core/DeviceEnumerator/DeviceEnumerator.cs
Normal file
144
aiwaz/Aiwaz.Core/DeviceEnumerator/DeviceEnumerator.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Aiwaz.Contracts;
|
||||
using SlimDX;
|
||||
|
||||
namespace Aiwaz.Core
|
||||
{
|
||||
public class DeviceEnumerator : IDisposable
|
||||
{
|
||||
private SlimDX.DXGI.Factory Factory;
|
||||
internal SlimDX.Direct3D10.Device Device;
|
||||
|
||||
public bool HasEnumerated
|
||||
{
|
||||
get { return EnumeratedAdapters.Count > 0; }
|
||||
}
|
||||
|
||||
public List<DisplayAdapter> EnumeratedAdapters { get; private set;}
|
||||
|
||||
public uint DesiredWidth = 0;
|
||||
public uint DesiredHeight = 0;
|
||||
public uint DesiredRefreshRate = 0;
|
||||
public SlimDX.DXGI.Format DesiredFormat = SlimDX.DXGI.Format.Unknown;
|
||||
|
||||
public DeviceEnumerator(SlimDX.Direct3D10.Device argDevice)
|
||||
{
|
||||
Factory = new SlimDX.DXGI.Factory();
|
||||
Device = argDevice;
|
||||
EnumeratedAdapters = new List<DisplayAdapter>();
|
||||
}
|
||||
|
||||
~DeviceEnumerator()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public bool TryEnumerate()
|
||||
{
|
||||
for (var i = 0; i < Factory.GetAdapterCount(); ++i)
|
||||
{
|
||||
SlimDX.DXGI.Adapter adapter = Factory.GetAdapter(i);
|
||||
if (adapter.IsInterfaceSupported(typeof(SlimDX.Direct3D10.Device)))
|
||||
{
|
||||
DisplayAdapter displayAdapter = new DisplayAdapter(this, adapter);
|
||||
if (displayAdapter.TryEnumerate())
|
||||
EnumeratedAdapters.Add(displayAdapter);
|
||||
else
|
||||
adapter.Dispose();
|
||||
}
|
||||
else
|
||||
adapter.Dispose();
|
||||
}
|
||||
|
||||
return HasEnumerated;
|
||||
}
|
||||
|
||||
public DisplayAdapter FindBestAdapter(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return EnumeratedAdapters.Last();
|
||||
}
|
||||
|
||||
public OutputDevice FindBestOutput(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return EnumeratedAdapters.Last().FindBestOutput(argAllowToEnumerate);
|
||||
}
|
||||
|
||||
public DisplayMode FindBestDisplayMode(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return EnumeratedAdapters.Last().FindBestDisplayMode(argAllowToEnumerate);
|
||||
}
|
||||
|
||||
public virtual void Verbose()
|
||||
{
|
||||
if (!HasEnumerated)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Enumeration was not executed or no results available.");
|
||||
return;
|
||||
}
|
||||
|
||||
DisplayAdapter displayAdapter = this.FindBestAdapter(false);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.WriteLine("Adapter: " + displayAdapter.Name);
|
||||
Console.WriteLine("Dedicated System Memory: " + Math.Round(displayAdapter.DedicatedSystemMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
||||
Console.WriteLine("Dedicated Video Memory: " + Math.Round(displayAdapter.DedicatedVideoMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
||||
Console.WriteLine("Shared System Memory: " + Math.Round(displayAdapter.SharedSystemMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
||||
Console.WriteLine();
|
||||
|
||||
for (int i = 0; i < displayAdapter.OutputDevices.Count; ++i)
|
||||
{
|
||||
OutputDevice outputDevice = displayAdapter.OutputDevices[i];
|
||||
if (i == 0)
|
||||
Console.Write("Primary Output: ");
|
||||
else
|
||||
Console.Write("Secondary Output: ");
|
||||
|
||||
Console.WriteLine(outputDevice.Name);
|
||||
|
||||
DisplayMode displayMode = outputDevice.FindBestDisplayMode(false);
|
||||
uint bpp = displayMode.Format.GetByteSize() * 8;
|
||||
|
||||
Console.WriteLine("Display: " + displayMode.Width.ToString() + "x" + displayMode.Height.ToString() + "x" + bpp.ToString() + " @" + displayMode.RefreshRate + "Hz");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Factory != null)
|
||||
Factory.Dispose();
|
||||
Factory = null;
|
||||
|
||||
if (EnumeratedAdapters != null)
|
||||
foreach (var adapter in EnumeratedAdapters)
|
||||
adapter.Dispose();
|
||||
EnumeratedAdapters = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
108
aiwaz/Aiwaz.Core/DeviceEnumerator/DisplayAdapter.cs
Normal file
108
aiwaz/Aiwaz.Core/DeviceEnumerator/DisplayAdapter.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Aiwaz.Contracts;
|
||||
using SlimDX;
|
||||
|
||||
namespace Aiwaz.Core
|
||||
{
|
||||
public class DisplayAdapter : IDisposable
|
||||
{
|
||||
private DeviceEnumerator deviceEnumerator;
|
||||
|
||||
public SlimDX.DXGI.Adapter Adapter { get; private set; }
|
||||
|
||||
public bool HasEnumerated
|
||||
{
|
||||
get { return OutputDevices.Count > 0; }
|
||||
}
|
||||
|
||||
public long DedicatedSystemMemory
|
||||
{
|
||||
get { return Adapter.Description.DedicatedSystemMemory; }
|
||||
}
|
||||
|
||||
public long DedicatedVideoMemory
|
||||
{
|
||||
get { return Adapter.Description.DedicatedVideoMemory; }
|
||||
}
|
||||
|
||||
public long SharedSystemMemory
|
||||
{
|
||||
get { return Adapter.Description.SharedSystemMemory; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return Adapter.Description.Description; }
|
||||
}
|
||||
|
||||
public List<OutputDevice> OutputDevices { get; private set; }
|
||||
|
||||
public DisplayAdapter(DeviceEnumerator argDeviceEnumerator, SlimDX.DXGI.Adapter argAdapter)
|
||||
{
|
||||
this.deviceEnumerator = argDeviceEnumerator;
|
||||
this.Adapter = argAdapter;
|
||||
this.OutputDevices = new List<OutputDevice>();
|
||||
}
|
||||
|
||||
~DisplayAdapter()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public bool TryEnumerate()
|
||||
{
|
||||
for (var i = 0; i < Adapter.GetOutputCount(); ++i)
|
||||
{
|
||||
var output = Adapter.GetOutput(i);
|
||||
OutputDevice device = new OutputDevice(deviceEnumerator, output);
|
||||
if (device.TryEnumerate())
|
||||
OutputDevices.Add(device);
|
||||
else
|
||||
output.Dispose();
|
||||
}
|
||||
|
||||
return HasEnumerated;
|
||||
}
|
||||
|
||||
public OutputDevice FindBestOutput(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return OutputDevices.Last();
|
||||
}
|
||||
|
||||
public DisplayMode FindBestDisplayMode(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return OutputDevices.Last().FindBestDisplayMode(argAllowToEnumerate);
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Adapter != null)
|
||||
Adapter.Dispose();
|
||||
Adapter = null;
|
||||
|
||||
if (OutputDevices != null)
|
||||
foreach (var output in OutputDevices)
|
||||
output.Dispose();
|
||||
OutputDevices = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
26
aiwaz/Aiwaz.Core/DeviceEnumerator/DisplayMode.cs
Normal file
26
aiwaz/Aiwaz.Core/DeviceEnumerator/DisplayMode.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Aiwaz.Contracts;
|
||||
using SlimDX;
|
||||
|
||||
|
||||
namespace Aiwaz.Core
|
||||
{
|
||||
public class DisplayMode
|
||||
{
|
||||
public int Width { get; protected set; }
|
||||
public int Height { get; protected set; }
|
||||
public int RefreshRate { get; protected set; }
|
||||
public SlimDX.DXGI.Format Format { get; protected set; }
|
||||
|
||||
public DisplayMode(SlimDX.DXGI.ModeDescription description)
|
||||
{
|
||||
this.Width = description.Width;
|
||||
this.Height = description.Height;
|
||||
this.Format = description.Format;
|
||||
this.RefreshRate = description.RefreshRate.Numerator == 0 ? 0 : (int)Math.Round(description.RefreshRate.Numerator / (double)description.RefreshRate.Denominator, 0, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
}
|
||||
}
|
||||
104
aiwaz/Aiwaz.Core/DeviceEnumerator/OutputDevice.cs
Normal file
104
aiwaz/Aiwaz.Core/DeviceEnumerator/OutputDevice.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Aiwaz.Contracts;
|
||||
using SlimDX;
|
||||
|
||||
namespace Aiwaz.Core
|
||||
{
|
||||
public class OutputDevice : IDisposable
|
||||
{
|
||||
private SlimDX.DXGI.OutputDescription outputDesc;
|
||||
private DeviceEnumerator deviceEnumerator;
|
||||
|
||||
public SlimDX.DXGI.Output Output { get; private set; }
|
||||
|
||||
public bool HasEnumerated
|
||||
{
|
||||
get { return DisplayModes.Count > 0; }
|
||||
}
|
||||
|
||||
public List<DisplayMode> DisplayModes { get; private set; }
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return outputDesc.Name; }
|
||||
}
|
||||
|
||||
public OutputDevice(DeviceEnumerator argDeviceEnumerator, SlimDX.DXGI.Output argOutput)
|
||||
{
|
||||
this.deviceEnumerator = argDeviceEnumerator;
|
||||
this.Output = argOutput;
|
||||
this.DisplayModes = new List<DisplayMode>();
|
||||
}
|
||||
|
||||
~OutputDevice()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public bool TryEnumerate()
|
||||
{
|
||||
for ( int formatIndex = (int)deviceEnumerator.DesiredFormat;
|
||||
formatIndex <= (deviceEnumerator.DesiredFormat == SlimDX.DXGI.Format.Unknown ? (int)SlimDX.DXGI.Format.B8G8R8X8_UNorm : (int)deviceEnumerator.DesiredFormat);
|
||||
++formatIndex
|
||||
)
|
||||
{
|
||||
var format = (SlimDX.DXGI.Format)formatIndex;
|
||||
var flags = SlimDX.DXGI.DisplayModeEnumerationFlags.Scaling;
|
||||
var displayModes = Output.GetDisplayModeList(format, flags);
|
||||
if (displayModes.Count > 0)
|
||||
{
|
||||
foreach (var displayModeRaw in displayModes)
|
||||
{
|
||||
var keepDisplayMode = false;
|
||||
var refreshRate = displayModeRaw.RefreshRate.Numerator == 0 ? 0 : (int)(displayModeRaw.RefreshRate.Numerator / displayModeRaw.RefreshRate.Denominator);
|
||||
if ((deviceEnumerator.DesiredWidth == 0 || deviceEnumerator.DesiredWidth <= displayModeRaw.Width) &&
|
||||
(deviceEnumerator.DesiredHeight == 0 || deviceEnumerator.DesiredHeight <= displayModeRaw.Height) &&
|
||||
(deviceEnumerator.DesiredRefreshRate == 0 || deviceEnumerator.DesiredRefreshRate <= refreshRate) &&
|
||||
(deviceEnumerator.DesiredFormat == SlimDX.DXGI.Format.Unknown || (int)deviceEnumerator.DesiredFormat == (int)displayModeRaw.Format))
|
||||
{
|
||||
if (deviceEnumerator.Device != null)
|
||||
{
|
||||
var supported = deviceEnumerator.Device.CheckFormatSupport(format);
|
||||
|
||||
if ((supported & SlimDX.Direct3D10.FormatSupport.FormatDisplaySupport) != 0)
|
||||
keepDisplayMode = true;
|
||||
}
|
||||
else
|
||||
keepDisplayMode = true;
|
||||
}
|
||||
if (keepDisplayMode)
|
||||
DisplayModes.Add(new DisplayMode(displayModeRaw));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return HasEnumerated;
|
||||
}
|
||||
|
||||
public DisplayMode FindBestDisplayMode(bool argAllowToEnumerate)
|
||||
{
|
||||
if (argAllowToEnumerate && !this.TryEnumerate())
|
||||
return null;
|
||||
|
||||
if (!HasEnumerated)
|
||||
return null;
|
||||
|
||||
return DisplayModes.Last();
|
||||
}
|
||||
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Output != null)
|
||||
Output.Dispose();
|
||||
Output = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user