Set HDR of screen according to client request

This commit is contained in:
Yukino Song
2024-09-06 05:42:20 +08:00
parent 6f20cf91da
commit f22e02cf14
5 changed files with 99 additions and 64 deletions

View File

@@ -108,25 +108,21 @@ bool setPrimaryDisplay(const wchar_t* primaryDeviceName) {
return true;
}
bool ensureDisplayHDR(const wchar_t* displayName) {
UINT32 pathCount = 0, modeCount = 0;
if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount) != ERROR_SUCCESS) {
printf("Failed to query display configuration.\n");
bool findDisplayIds(const wchar_t* displayName, LUID& adapterId, uint32_t& targetId) {
UINT pathCount;
UINT modeCount;
if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount)) {
return false;
}
std::vector<DISPLAYCONFIG_PATH_INFO> pathArray(pathCount);
std::vector<DISPLAYCONFIG_MODE_INFO> modeArray(modeCount);
if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathArray.data(), &modeCount, modeArray.data(), NULL) != ERROR_SUCCESS) {
printf("Failed to query display paths.\n");
std::vector<DISPLAYCONFIG_PATH_INFO> paths(pathCount);
std::vector<DISPLAYCONFIG_MODE_INFO> modes(modeCount);
if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, paths.data(), &modeCount, modes.data(), nullptr)) {
return false;
}
for (const auto& path : pathArray) {
DISPLAYCONFIG_PATH_SOURCE_INFO sourceInfo = path.sourceInfo;
auto path = std::find_if(paths.begin(), paths.end(), [&displayName](DISPLAYCONFIG_PATH_INFO _path) {
DISPLAYCONFIG_PATH_SOURCE_INFO sourceInfo = _path.sourceInfo;
DISPLAYCONFIG_SOURCE_DEVICE_NAME deviceName = {};
deviceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
@@ -135,54 +131,67 @@ bool ensureDisplayHDR(const wchar_t* displayName) {
deviceName.header.id = sourceInfo.id;
if (DisplayConfigGetDeviceInfo(&deviceName.header) != ERROR_SUCCESS) {
continue;
}
if (std::wstring_view(displayName) == deviceName.viewGdiDeviceName) {
DISPLAYCONFIG_GET_ADVANCED_COLOR_INFO hdrInfo = {};
hdrInfo.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_ADVANCED_COLOR_INFO;
hdrInfo.header.size = sizeof(hdrInfo);
hdrInfo.header.adapterId = path.targetInfo.adapterId;
hdrInfo.header.id = path.targetInfo.id;
if (DisplayConfigGetDeviceInfo(&hdrInfo.header) != ERROR_SUCCESS) {
wprintf(L"Failed to get HDR info for display: %ls\n", std::wstring(deviceName.viewGdiDeviceName));
return false;
}
if (hdrInfo.advancedColorSupported) {
if (hdrInfo.advancedColorEnabled) {
DISPLAYCONFIG_SET_ADVANCED_COLOR_STATE setHdrInfo = {};
setHdrInfo.header.type = DISPLAYCONFIG_DEVICE_INFO_SET_ADVANCED_COLOR_STATE;
setHdrInfo.header.size = sizeof(setHdrInfo);
setHdrInfo.header.adapterId = path.targetInfo.adapterId;
setHdrInfo.header.id = path.targetInfo.id;
setHdrInfo.enableAdvancedColor = FALSE; // Disable HDR
if (DisplayConfigSetDeviceInfo(&setHdrInfo.header) == ERROR_SUCCESS) {
wprintf(L"HDR toggled off for display: %ls\n", displayName);
} else {
wprintf(L"Failed to toggle HDR off for display: %ls\n", std::wstring(deviceName.viewGdiDeviceName));
}
setHdrInfo.enableAdvancedColor = TRUE; // Enable HDR back on
if (DisplayConfigSetDeviceInfo(&setHdrInfo.header) == ERROR_SUCCESS) {
wprintf(L"HDR toggled on for display: %ls\n", displayName);
return true;
} else {
wprintf(L"Failed to toggle HDR on for display: %ls\n", std::wstring(deviceName.viewGdiDeviceName));
}
} else {
wprintf(L"HDR is not enabled on display: %ls\n", displayName);
}
}
return false;
}
return std::wstring_view(displayName) == deviceName.viewGdiDeviceName;
});
if (path == paths.end()) {
return false;
}
wprintf(L"Display not found or HDR not supported: %ls\n", displayName);
return false;
adapterId = path->sourceInfo.adapterId;
targetId = path->targetInfo.id;
return true;
}
bool getDisplayHDR(const LUID& adapterId, const uint32_t& targetId) {
DISPLAYCONFIG_GET_ADVANCED_COLOR_INFO hdrInfo = {};
hdrInfo.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_ADVANCED_COLOR_INFO;
hdrInfo.header.size = sizeof(hdrInfo);
hdrInfo.header.adapterId = adapterId;
hdrInfo.header.id = targetId;
if (DisplayConfigGetDeviceInfo(&hdrInfo.header) != ERROR_SUCCESS) {
return false;
}
return hdrInfo.advancedColorSupported && hdrInfo.advancedColorEnabled;
}
bool setDisplayHDR(const LUID& adapterId, const uint32_t& targetId, bool enableAdvancedColor) {
DISPLAYCONFIG_SET_ADVANCED_COLOR_STATE setHdrInfo = {};
setHdrInfo.header.type = DISPLAYCONFIG_DEVICE_INFO_SET_ADVANCED_COLOR_STATE;
setHdrInfo.header.size = sizeof(setHdrInfo);
setHdrInfo.header.adapterId = adapterId;
setHdrInfo.header.id = targetId;
setHdrInfo.enableAdvancedColor = enableAdvancedColor;
return DisplayConfigSetDeviceInfo(&setHdrInfo.header) == ERROR_SUCCESS;
}
bool getDisplayHDRByName(const wchar_t* displayName) {
LUID adapterId;
uint32_t targetId;
if (!findDisplayIds(displayName, adapterId, targetId)) {
return false;
}
return getDisplayHDR(adapterId, targetId);
}
bool setDisplayHDRByName(const wchar_t* displayName, bool enableAdvancedColor) {
LUID adapterId;
uint32_t targetId;
if (!findDisplayIds(displayName, adapterId, targetId)) {
return false;
}
return setDisplayHDR(adapterId, targetId, enableAdvancedColor);
}
void closeVDisplayDevice() {

View File

@@ -25,7 +25,8 @@ namespace VDISPLAY {
LONG changeDisplaySettings(const wchar_t* deviceName, int width, int height, int refresh_rate);
std::wstring getPrimaryDisplay();
bool setPrimaryDisplay(const wchar_t* primaryDeviceName);
bool ensureDisplayHDR(const wchar_t* displayName);
bool getDisplayHDRByName(const wchar_t* displayName);
bool setDisplayHDRByName(const wchar_t* displayName, bool enableAdvancedColor);
void closeVDisplayDevice();
DRIVER_STATUS openVDisplayDevice();