From dbfaf1f7c81cad229980f787dbfccc0667fedd54 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Mon, 7 Jun 2021 17:55:44 +1200 Subject: [PATCH] Initial ADL library import Using the example ADL.cs from https://github.com/GPUOpen-LibrariesAndSDKs/display-library/tree/master/Sample-Managed --- DisplayMagicianShared/AMD/ADL.cs | 501 ++++++++++++++++++ .../DisplayMagicianShared.csproj | 3 + 2 files changed, 504 insertions(+) create mode 100644 DisplayMagicianShared/AMD/ADL.cs diff --git a/DisplayMagicianShared/AMD/ADL.cs b/DisplayMagicianShared/AMD/ADL.cs new file mode 100644 index 0000000..729e0f1 --- /dev/null +++ b/DisplayMagicianShared/AMD/ADL.cs @@ -0,0 +1,501 @@ +#region Copyright + +/******************************************************************************* + Copyright(c) 2008 - 2009 Advanced Micro Devices, Inc. All Rights Reserved. + Copyright (c) 2002 - 2006 ATI Technologies Inc. All Rights Reserved. + + THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF + ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDED BUT NOT LIMITED TO + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A + PARTICULAR PURPOSE. + + File: ADL.cs + + Purpose: Implements ADL interface + + Description: Implements some of the methods defined in ADL interface. + + ********************************************************************************/ + +#endregion Copyright + +#region Using + +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Threading; +using FARPROC = System.IntPtr; +using HMODULE = System.IntPtr; + +#endregion Using + +#region ATI.ADL + +namespace ATI.ADL +{ + #region Export Delegates + /// ADL Memory allocation function allows ADL to callback for memory allocation + /// input size + /// retrun ADL Error Code + internal delegate IntPtr ADL_Main_Memory_Alloc (int size); + + // ///// ADL Create Function to create ADL Data + /// Call back functin pointer which is ised to allocate memeory + /// If it is 1, then ADL will only retuen the physical exist adapters + ///// retrun ADL Error Code + internal delegate int ADL_Main_Control_Create(ADL_Main_Memory_Alloc callback, int enumConnectedAdapters); + + /// ADL Destroy Function to free up ADL Data + /// retrun ADL Error Code + internal delegate int ADL_Main_Control_Destroy (); + + /// ADL Function to get the number of adapters + /// return number of adapters + /// retrun ADL Error Code + internal delegate int ADL_Adapter_NumberOfAdapters_Get (ref int numAdapters); + + /// ADL Function to get the GPU adapter information + /// return GPU adapter information + /// the size of the GPU adapter struct + /// retrun ADL Error Code + internal delegate int ADL_Adapter_AdapterInfo_Get (IntPtr info, int inputSize); + + /// Function to determine if the adapter is active or not. + /// The function is used to check if the adapter associated with iAdapterIndex is active + /// Adapter Index. + /// Status of the adapter. True: Active; False: Dsiabled + /// Non zero is successfull + internal delegate int ADL_Adapter_Active_Get(int adapterIndex, ref int status); + + /// Get display information based on adapter index + /// Adapter Index + /// return the total number of supported displays + /// return ADLDisplayInfo Array for supported displays' information + /// force detect or not + /// return ADL Error Code + internal delegate int ADL_Display_DisplayInfo_Get(int adapterIndex, ref int numDisplays, out IntPtr displayInfoArray, int forceDetect); + + #endregion Export Delegates + + #region Export Struct + + #region ADLAdapterInfo + /// ADLAdapterInfo Structure + [StructLayout(LayoutKind.Sequential)] + internal struct ADLAdapterInfo + { + /// The size of the structure + int Size; + /// Adapter Index + internal int AdapterIndex; + /// Adapter UDID + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string UDID; + /// Adapter Bus Number + internal int BusNumber; + /// Adapter Driver Number + internal int DriverNumber; + /// Adapter Function Number + internal int FunctionNumber; + /// Adapter Vendor ID + internal int VendorID; + /// Adapter Adapter name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string AdapterName; + /// Adapter Display name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DisplayName; + /// Adapter Present status + internal int Present; + /// Adapter Exist status + internal int Exist; + /// Adapter Driver Path + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DriverPath; + /// Adapter Driver Ext Path + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DriverPathExt; + /// Adapter PNP String + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string PNPString; + /// OS Display Index + internal int OSDisplayIndex; + } + + + /// ADLAdapterInfo Array + [StructLayout(LayoutKind.Sequential)] + internal struct ADLAdapterInfoArray + { + /// ADLAdapterInfo Array + [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)ADL.ADL_MAX_ADAPTERS)] + internal ADLAdapterInfo[] ADLAdapterInfo; + } + #endregion ADLAdapterInfo + + + #region ADLDisplayInfo + /// ADLDisplayID Structure + [StructLayout(LayoutKind.Sequential)] + internal struct ADLDisplayID + { + /// Display Logical Index + internal int DisplayLogicalIndex; + /// Display Physical Index + internal int DisplayPhysicalIndex; + /// Adapter Logical Index + internal int DisplayLogicalAdapterIndex; + /// Adapter Physical Index + internal int DisplayPhysicalAdapterIndex; + } + + /// ADLDisplayInfo Structure + [StructLayout(LayoutKind.Sequential)] + internal struct ADLDisplayInfo + { + /// Display Index + internal ADLDisplayID DisplayID; + /// Display Controller Index + internal int DisplayControllerIndex; + /// Display Name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DisplayName; + /// Display Manufacturer Name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DisplayManufacturerName; + /// Display Type : < The Display type. CRT, TV,CV,DFP are some of display types, + internal int DisplayType; + /// Display output type + internal int DisplayOutputType; + /// Connector type + internal int DisplayConnector; + /// Indicating the display info bits' mask. + internal int DisplayInfoMask; + /// Indicating the display info value. + internal int DisplayInfoValue; + } + #endregion ADLDisplayInfo + + #endregion Export Struct + + #region ADL Class + /// ADL Class + internal static class ADL + { + #region Internal Constant + /// Define the maximum path + internal const int ADL_MAX_PATH = 256; + /// Define the maximum adapters + internal const int ADL_MAX_ADAPTERS = 40 /* 150 */; + /// Define the maximum displays + internal const int ADL_MAX_DISPLAYS = 40 /* 150 */; + /// Define the maximum device name length + internal const int ADL_MAX_DEVICENAME = 32; + /// Define the successful + internal const int ADL_SUCCESS = 0; + /// Define the failure + internal const int ADL_FAIL = -1; + /// Define the driver ok + internal const int ADL_DRIVER_OK = 0; + /// Maximum number of GL-Sync ports on the GL-Sync module + internal const int ADL_MAX_GLSYNC_PORTS = 8; + /// Maximum number of GL-Sync ports on the GL-Sync module + internal const int ADL_MAX_GLSYNC_PORT_LEDS = 8; + /// Maximum number of ADLMOdes for the adapter + internal const int ADL_MAX_NUM_DISPLAYMODES = 1024; + + #endregion Internal Constant + + #region Class ADLImport + /// ADLImport class + private static class ADLImport + { + #region Internal Constant + /// Atiadlxx_FileName + internal const string Atiadlxx_FileName = "atiadlxx.dll"; + /// Kernel32_FileName + internal const string Kernel32_FileName = "kernel32.dll"; + #endregion Internal Constant + + #region DLLImport + [DllImport(Kernel32_FileName)] + internal static extern HMODULE GetModuleHandle (string moduleName); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Main_Control_Create (ADL_Main_Memory_Alloc callback, int enumConnectedAdapters); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Main_Control_Destroy (); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Main_Control_IsFunctionValid (HMODULE module, string procName); + + [DllImport(Atiadlxx_FileName)] + internal static extern FARPROC ADL_Main_Control_GetProcAddress (HMODULE module, string procName); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Adapter_NumberOfAdapters_Get (ref int numAdapters); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Adapter_AdapterInfo_Get (IntPtr info, int inputSize); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Adapter_Active_Get(int adapterIndex, ref int status); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL_Display_DisplayInfo_Get(int adapterIndex, ref int numDisplays, out IntPtr displayInfoArray, int forceDetect); + + #endregion DLLImport + } + #endregion Class ADLImport + + #region Class ADLCheckLibrary + /// ADLCheckLibrary class + private class ADLCheckLibrary + { + #region Private Members + private HMODULE ADLLibrary = System.IntPtr.Zero; + #endregion Private Members + + #region Static Members + /// new a private instance + private static ADLCheckLibrary ADLCheckLibrary_ = new ADLCheckLibrary(); + #endregion Static Members + + #region Constructor + /// Constructor + private ADLCheckLibrary () + { + try + { + if (1 == ADLImport.ADL_Main_Control_IsFunctionValid(IntPtr.Zero, "ADL_Main_Control_Create")) + { + ADLLibrary = ADLImport.GetModuleHandle(ADLImport.Atiadlxx_FileName); + } + } + catch (DllNotFoundException) { } + catch (EntryPointNotFoundException) { } + catch (Exception) { } + } + #endregion Constructor + + #region Destructor + /// Destructor to force calling ADL Destroy function before free up the ADL library + ~ADLCheckLibrary () + { + if (System.IntPtr.Zero != ADLCheckLibrary_.ADLLibrary) + { + ADLImport.ADL_Main_Control_Destroy(); + } + } + #endregion Destructor + + #region Static IsFunctionValid + /// Check the import function to see it exists or not + /// function name + /// return true, if function exists + internal static bool IsFunctionValid (string functionName) + { + bool result = false; + if (System.IntPtr.Zero != ADLCheckLibrary_.ADLLibrary) + { + if (1 == ADLImport.ADL_Main_Control_IsFunctionValid(ADLCheckLibrary_.ADLLibrary, functionName)) + { + result = true; + } + } + return result; + } + #endregion Static IsFunctionValid + + #region Static GetProcAddress + /// Get the unmanaged function pointer + /// function name + /// return function pointer, if function exists + internal static FARPROC GetProcAddress (string functionName) + { + FARPROC result = System.IntPtr.Zero; + if (System.IntPtr.Zero != ADLCheckLibrary_.ADLLibrary) + { + result = ADLImport.ADL_Main_Control_GetProcAddress(ADLCheckLibrary_.ADLLibrary, functionName); + } + return result; + } + #endregion Static GetProcAddress + } + #endregion Class ADLCheckLibrary + + #region Export Functions + + #region ADL_Main_Memory_Alloc + /// Build in memory allocation function + internal static ADL_Main_Memory_Alloc ADL_Main_Memory_Alloc = ADL_Main_Memory_Alloc_; + /// Build in memory allocation function + /// input size + /// return the memory buffer + private static IntPtr ADL_Main_Memory_Alloc_ (int size) + { + IntPtr result = Marshal.AllocCoTaskMem(size); + return result; + } + #endregion ADL_Main_Memory_Alloc + + #region ADL_Main_Memory_Free + /// Build in memory free function + /// input buffer + internal static void ADL_Main_Memory_Free (IntPtr buffer) + { + if (IntPtr.Zero != buffer) + { + Marshal.FreeCoTaskMem(buffer); + } + } + #endregion ADL_Main_Memory_Free + + #region ADL_Main_Control_Create + /// ADL_Main_Control_Create Delegates + internal static ADL_Main_Control_Create ADL_Main_Control_Create + { + get + { + if (!ADL_Main_Control_Create_Check && null == ADL_Main_Control_Create_) + { + ADL_Main_Control_Create_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Main_Control_Create")) + { + ADL_Main_Control_Create_ = ADLImport.ADL_Main_Control_Create; + } + } + return ADL_Main_Control_Create_; + } + } + /// Private Delegate + private static ADL_Main_Control_Create ADL_Main_Control_Create_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Main_Control_Create_Check = false; + #endregion ADL_Main_Control_Create + + #region ADL_Main_Control_Destroy + /// ADL_Main_Control_Destroy Delegates + internal static ADL_Main_Control_Destroy ADL_Main_Control_Destroy + { + get + { + if (!ADL_Main_Control_Destroy_Check && null == ADL_Main_Control_Destroy_) + { + ADL_Main_Control_Destroy_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Main_Control_Destroy")) + { + ADL_Main_Control_Destroy_ = ADLImport.ADL_Main_Control_Destroy; + } + } + return ADL_Main_Control_Destroy_; + } + } + /// Private Delegate + private static ADL_Main_Control_Destroy ADL_Main_Control_Destroy_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Main_Control_Destroy_Check = false; + #endregion ADL_Main_Control_Destroy + + #region ADL_Adapter_NumberOfAdapters_Get + /// ADL_Adapter_NumberOfAdapters_Get Delegates + internal static ADL_Adapter_NumberOfAdapters_Get ADL_Adapter_NumberOfAdapters_Get + { + get + { + if (!ADL_Adapter_NumberOfAdapters_Get_Check && null == ADL_Adapter_NumberOfAdapters_Get_) + { + ADL_Adapter_NumberOfAdapters_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Adapter_NumberOfAdapters_Get")) + { + ADL_Adapter_NumberOfAdapters_Get_ = ADLImport.ADL_Adapter_NumberOfAdapters_Get; + } + } + return ADL_Adapter_NumberOfAdapters_Get_; + } + } + /// Private Delegate + private static ADL_Adapter_NumberOfAdapters_Get ADL_Adapter_NumberOfAdapters_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Adapter_NumberOfAdapters_Get_Check = false; + #endregion ADL_Adapter_NumberOfAdapters_Get + + #region ADL_Adapter_AdapterInfo_Get + /// ADL_Adapter_AdapterInfo_Get Delegates + internal static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get + { + get + { + if (!ADL_Adapter_AdapterInfo_Get_Check && null == ADL_Adapter_AdapterInfo_Get_) + { + ADL_Adapter_AdapterInfo_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Adapter_AdapterInfo_Get")) + { + ADL_Adapter_AdapterInfo_Get_ = ADLImport.ADL_Adapter_AdapterInfo_Get; + } + } + return ADL_Adapter_AdapterInfo_Get_; + } + } + /// Private Delegate + private static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Adapter_AdapterInfo_Get_Check = false; + #endregion ADL_Adapter_AdapterInfo_Get + + #region ADL_Adapter_Active_Get + /// ADL_Adapter_Active_Get Delegates + internal static ADL_Adapter_Active_Get ADL_Adapter_Active_Get + { + get + { + if (!ADL_Adapter_Active_Get_Check && null == ADL_Adapter_Active_Get_) + { + ADL_Adapter_Active_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Adapter_Active_Get")) + { + ADL_Adapter_Active_Get_ = ADLImport.ADL_Adapter_Active_Get; + } + } + return ADL_Adapter_Active_Get_; + } + } + /// Private Delegate + private static ADL_Adapter_Active_Get ADL_Adapter_Active_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Adapter_Active_Get_Check = false; + #endregion ADL_Adapter_Active_Get + + #region ADL_Display_DisplayInfo_Get + /// ADL_Display_DisplayInfo_Get Delegates + internal static ADL_Display_DisplayInfo_Get ADL_Display_DisplayInfo_Get + { + get + { + if (!ADL_Display_DisplayInfo_Get_Check && null == ADL_Display_DisplayInfo_Get_) + { + ADL_Display_DisplayInfo_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Display_DisplayInfo_Get")) + { + ADL_Display_DisplayInfo_Get_ = ADLImport.ADL_Display_DisplayInfo_Get; + } + } + return ADL_Display_DisplayInfo_Get_; + } + } + /// Private Delegate + private static ADL_Display_DisplayInfo_Get ADL_Display_DisplayInfo_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Display_DisplayInfo_Get_Check = false; + #endregion ADL_Display_DisplayInfo_Get + + #endregion Export Functions + } + #endregion ADL Class +} + +#endregion ATI.ADL \ No newline at end of file diff --git a/DisplayMagicianShared/DisplayMagicianShared.csproj b/DisplayMagicianShared/DisplayMagicianShared.csproj index 0236843..d9b8ee4 100644 --- a/DisplayMagicianShared/DisplayMagicianShared.csproj +++ b/DisplayMagicianShared/DisplayMagicianShared.csproj @@ -151,6 +151,9 @@ True + + +