using System;
using System.Linq;
using System.Runtime.InteropServices;
using NvAPIWrapper.Native.Attributes;
using NvAPIWrapper.Native.General.Structures;
using NvAPIWrapper.Native.GPU;
using NvAPIWrapper.Native.GPU.Structures;
using NvAPIWrapper.Native.Interfaces;
namespace NvAPIWrapper.Native.Mosaic.Structures
{
///
/// Holds extra details about a topology
///
[StructLayout(LayoutKind.Sequential, Pack = 8)]
[StructureVersion(1)]
public struct TopologyDetails : IInitializable, IEquatable
{
///
/// Maximum number of rows in a topology detail
///
public const int MaxLayoutRows = 8;
///
/// Maximum number of columns in a topology detail
///
public const int MaxLayoutColumns = 8;
internal StructureVersion _Version;
internal readonly LogicalGPUHandle _LogicalGPUHandle;
internal readonly TopologyValidity _ValidityFlags;
internal readonly uint _Rows;
internal readonly uint _Columns;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxLayoutRows)]
internal readonly LayoutRow[] _LayoutRows;
///
public bool Equals(TopologyDetails other)
{
return _LogicalGPUHandle.Equals(other._LogicalGPUHandle) &&
_ValidityFlags == other._ValidityFlags &&
_Rows == other._Rows &&
_Columns == other._Columns &&
_LayoutRows.SequenceEqual(other._LayoutRows);
}
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is TopologyDetails details && Equals(details);
}
///
public override int GetHashCode()
{
unchecked
{
var hashCode = _LogicalGPUHandle.GetHashCode();
hashCode = (hashCode * 397) ^ (int) _ValidityFlags;
hashCode = (hashCode * 397) ^ (int) _Rows;
hashCode = (hashCode * 397) ^ (int) _Columns;
hashCode = (hashCode * 397) ^ (_LayoutRows?.GetHashCode() ?? 0);
return hashCode;
}
}
///
/// Checks for equality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are equal, otherwise false
public static bool operator ==(TopologyDetails left, TopologyDetails right)
{
return left.Equals(right);
}
///
/// Checks for inequality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are not equal, otherwise false
public static bool operator !=(TopologyDetails left, TopologyDetails right)
{
return !left.Equals(right);
}
///
/// Logical GPU for this topology
///
public LogicalGPUHandle LogicalGPUHandle
{
get => _LogicalGPUHandle;
}
///
/// Indicates topology validity. TopologyValidity.Valid means topology is valid with the current hardware.
///
public TopologyValidity ValidityFlags
{
get => _ValidityFlags;
}
///
/// Number of displays in a row
///
public int Rows
{
get => (int) _Rows;
}
///
/// Number of displays in a column
///
public int Columns
{
get => (int) _Columns;
}
///
/// Gets a 2D array of layout cells containing information about the display layout of the topology
///
public LayoutCell[][] Layout
{
get
{
var columns = (int) _Columns;
return _LayoutRows.Take((int) _Rows).Select(row => row.LayoutCells.Take(columns).ToArray()).ToArray();
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct LayoutRow : IInitializable, IEquatable
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxLayoutColumns)]
internal readonly LayoutCell[]
LayoutCells;
public bool Equals(LayoutRow other)
{
return LayoutCells.SequenceEqual(other.LayoutCells);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is LayoutRow row && Equals(row);
}
public override int GetHashCode()
{
return LayoutCells?.GetHashCode() ?? 0;
}
}
///
/// Holds information about a topology display
///
[StructLayout(LayoutKind.Sequential)]
public struct LayoutCell : IEquatable
{
internal readonly PhysicalGPUHandle _PhysicalGPUHandle;
internal readonly OutputId _DisplayOutputId;
internal readonly int _OverlapX;
internal readonly int _OverlapY;
///
public bool Equals(LayoutCell other)
{
return _PhysicalGPUHandle.Equals(other._PhysicalGPUHandle) &&
_DisplayOutputId == other._DisplayOutputId &&
_OverlapX == other._OverlapX &&
_OverlapY == other._OverlapY;
}
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is LayoutCell cell && Equals(cell);
}
///
public override int GetHashCode()
{
unchecked
{
var hashCode = _PhysicalGPUHandle.GetHashCode();
hashCode = (hashCode * 397) ^ (int) _DisplayOutputId;
hashCode = (hashCode * 397) ^ _OverlapX;
hashCode = (hashCode * 397) ^ _OverlapY;
return hashCode;
}
}
///
/// Checks for equality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are equal, otherwise false
public static bool operator ==(LayoutCell left, LayoutCell right)
{
return left.Equals(right);
}
///
/// Checks for inequality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are not equal, otherwise false
public static bool operator !=(LayoutCell left, LayoutCell right)
{
return !left.Equals(right);
}
///
/// Physical GPU to be used in the topology (0 if GPU missing)
///
public PhysicalGPUHandle PhysicalGPUHandle
{
get => _PhysicalGPUHandle;
}
///
/// Connected display target (0 if no display connected)
///
public OutputId DisplayOutputId
{
get => _DisplayOutputId;
}
///
/// Pixels of overlap on left of target: (+overlap, -gap)
///
public int OverlapX
{
get => _OverlapX;
}
///
/// Pixels of overlap on top of target: (+overlap, -gap)
///
public int OverlapY
{
get => _OverlapY;
}
}
}
}