From 47329ebd89dbad9eb26eb2a029072c57f1ca6de2 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 27 Oct 2023 11:21:58 +1100 Subject: [PATCH] [host] dxgi: move utility functions into `util.c` --- host/platform/Windows/capture/DXGI/src/dxgi.c | 139 +----------------- .../Windows/capture/DXGI/src/dxgi_capture.h | 3 - host/platform/Windows/capture/DXGI/src/util.c | 135 ++++++++++++++++- host/platform/Windows/capture/DXGI/src/util.h | 32 ++++ 4 files changed, 170 insertions(+), 139 deletions(-) create mode 100644 host/platform/Windows/capture/DXGI/src/util.h diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index 5bce58c9..8250b0c5 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -40,11 +40,11 @@ #include #include #include -#include #include #include #include "dxgi_capture.h" +#include "util.h" #define LOCKED(...) INTERLOCKED_SECTION(this->deviceContextLock, __VA_ARGS__) @@ -259,133 +259,6 @@ static bool dxgi_create(CaptureGetPointerBuffer getPointerBufferFn, CapturePostP return true; } -static bool getDisplayPathInfo(HMONITOR monitor, - DISPLAYCONFIG_PATH_INFO * info) -{ - bool result = false; - UINT32 numPath, numMode; - - MONITORINFOEXW viewInfo = { .cbSize = sizeof(viewInfo) }; - if (!GetMonitorInfoW(monitor, (MONITORINFO*)&viewInfo)) - { - DEBUG_ERROR("Failed to get the monitor info"); - goto err; - } - -err_retry: - if (FAILED(GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPath, &numMode))) - goto err; - - DISPLAYCONFIG_PATH_INFO * pathInfo = calloc(sizeof(*pathInfo), numPath); - if (!pathInfo) - goto err_mem_pathInfo; - - DISPLAYCONFIG_MODE_INFO * modeInfo = calloc(sizeof(*modeInfo), numMode); - if (!modeInfo) - goto err_mem_modeInfo; - - LONG status = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, - &numPath, pathInfo, - &numMode, modeInfo, - NULL); - - if (status != ERROR_SUCCESS) - { - if (status == ERROR_INSUFFICIENT_BUFFER) - { - free(modeInfo); - free(pathInfo); - goto err_retry; - } - - DEBUG_ERROR("QueryDisplayConfig failed with 0x%lx", status); - goto err_queryDisplay; - } - - for(unsigned i = 0; i < numPath; ++i) - { - DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName = - { - .header = - { - .type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME, - .size = sizeof(sourceName), - .adapterId = pathInfo[i].sourceInfo.adapterId, - .id = pathInfo[i].sourceInfo.id, - } - }; - - if (FAILED(DisplayConfigGetDeviceInfo(&sourceName.header))) - continue; - - if (wcscmp(viewInfo.szDevice, sourceName.viewGdiDeviceName) != 0) - continue; - - *info = pathInfo[i]; - result = true; - break; - } - -err_queryDisplay: - free(modeInfo); - -err_mem_modeInfo: - free(pathInfo); - -err_mem_pathInfo: - -err: - return result; -} - -static float getSDRWhiteLevel(void) -{ - float nits = 80.0f; - DISPLAYCONFIG_SDR_WHITE_LEVEL level = - { - .header = - { - .type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL, - .size = sizeof(level), - .adapterId = this->displayPathInfo.targetInfo.adapterId, - .id = this->displayPathInfo.targetInfo.id, - } - }; - - if (SUCCEEDED(DisplayConfigGetDeviceInfo(&level.header))) - nits = level.SDRWhiteLevel / 1000.0f * 80.0f; - - return nits; -} - -static bool compileShader(ID3DBlob ** dst, const char * entry, - const char * target, const char * code) -{ - ID3DBlob * errors; - HRESULT status = D3DCompile( - code, - strlen(code), - NULL, - NULL, - NULL, - entry, - target, - D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, - 0, - dst, - &errors); - - if (FAILED(status)) - { - DEBUG_ERROR("Failed to compile the shader"); - DEBUG_ERROR("%s", (const char *)ID3D10Blob_GetBufferPointer(errors)); - ID3D10Blob_Release(errors); - return false; - } - - return true; -} - static bool dxgi_init(void) { DEBUG_ASSERT(this); @@ -737,10 +610,10 @@ next_output: IDXGIOutput6_Release(output6); goto fail; } - this->sdrWhiteLevel = getSDRWhiteLevel(); + this->sdrWhiteLevel = getSDRWhiteLevel(&this->displayPathInfo); DEBUG_INFO("Bits Per Color : %u" , desc1.BitsPerColor); - DEBUG_INFO("Color Space : %s" , GetDXGIColorSpaceTypeStr(this->dxgiColorSpace)); + DEBUG_INFO("Color Space : %s" , getDXGIColorSpaceTypeStr(this->dxgiColorSpace)); DEBUG_INFO("Min/Max Luminance : %f/%f", desc1.MinLuminance, desc1.MaxLuminance); DEBUG_INFO("Frame Luminance : %f" , desc1.MaxFullFrameLuminance); DEBUG_INFO("SDR White Level : %f" , this->sdrWhiteLevel); @@ -753,7 +626,7 @@ next_output: { DXGI_OUTDUPL_DESC dupDesc; IDXGIOutputDuplication_GetDesc(this->dup, &dupDesc); - DEBUG_INFO("Source Format : %s", GetDXGIFormatStr(dupDesc.ModeDesc.Format)); + DEBUG_INFO("Source Format : %s", getDXGIFormatStr(dupDesc.ModeDesc.Format)); DXGI_OUTDUPL_FRAME_INFO frameInfo; IDXGIResource * res; @@ -779,7 +652,7 @@ next_output: this->dxgiSrcFormat = desc.Format; this->dxgiFormat = desc.Format; - DEBUG_INFO("Capture Format : %s", GetDXGIFormatStr(desc.Format)); + DEBUG_INFO("Capture Format : %s", getDXGIFormatStr(desc.Format)); ID3D11Texture2D_Release(src); IDXGIResource_Release(res); @@ -1413,7 +1286,7 @@ static CaptureResult dxgi_capture(void) } } - float nits = getSDRWhiteLevel(); + float nits = getSDRWhiteLevel(&this->displayPathInfo); if (nits != this->sdrWhiteLevel) { this->sdrWhiteLevel = nits; diff --git a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h index e54ec5f6..842b53a9 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h +++ b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h @@ -129,6 +129,3 @@ struct DXGICopyBackend void (*unmapTexture)(Texture * tex); void (*preRelease)(void); }; - -const char * GetDXGIFormatStr(DXGI_FORMAT format); -const char * GetDXGIColorSpaceTypeStr(DXGI_COLOR_SPACE_TYPE type); diff --git a/host/platform/Windows/capture/DXGI/src/util.c b/host/platform/Windows/capture/DXGI/src/util.c index b67c4d0a..e91fb788 100644 --- a/host/platform/Windows/capture/DXGI/src/util.c +++ b/host/platform/Windows/capture/DXGI/src/util.c @@ -18,7 +18,10 @@ * Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "dxgi_capture.h" +#include "common/debug.h" + +#include +#include static const char * DXGI_FORMAT_STR[] = { "DXGI_FORMAT_UNKNOWN", @@ -145,7 +148,7 @@ static const char * DXGI_FORMAT_STR[] = { "DXGI_FORMAT_V408" }; -const char * GetDXGIFormatStr(DXGI_FORMAT format) +const char * getDXGIFormatStr(DXGI_FORMAT format) { if (format > sizeof(DXGI_FORMAT_STR) / sizeof(const char *)) return DXGI_FORMAT_STR[0]; @@ -181,7 +184,7 @@ static const char * DXGI_COLOR_SPACE_TYPE_STR[] = "DXGI_COLOR_SPACE_YCBCR_STUDIO_G24_TOPLEFT_P2020" }; -const char * GetDXGIColorSpaceTypeStr(DXGI_COLOR_SPACE_TYPE type) +const char * getDXGIColorSpaceTypeStr(DXGI_COLOR_SPACE_TYPE type) { if (type == DXGI_COLOR_SPACE_CUSTOM) return "DXGI_COLOR_SPACE_CUSTOM"; @@ -191,3 +194,129 @@ const char * GetDXGIColorSpaceTypeStr(DXGI_COLOR_SPACE_TYPE type) return DXGI_COLOR_SPACE_TYPE_STR[type]; } + +bool compileShader(ID3DBlob ** dst, const char * entry, const char * target, + const char * code) +{ + ID3DBlob * errors; + HRESULT status = D3DCompile( + code, + strlen(code), + NULL, + NULL, + NULL, + entry, + target, + D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, + 0, + dst, + &errors); + + if (FAILED(status)) + { + DEBUG_ERROR("Failed to compile the shader"); + DEBUG_ERROR("%s", (const char *)ID3D10Blob_GetBufferPointer(errors)); + ID3D10Blob_Release(errors); + return false; + } + + return true; +} + +bool getDisplayPathInfo(HMONITOR monitor, DISPLAYCONFIG_PATH_INFO * info) +{ + bool result = false; + UINT32 numPath, numMode; + + MONITORINFOEXW viewInfo = { .cbSize = sizeof(viewInfo) }; + if (!GetMonitorInfoW(monitor, (MONITORINFO*)&viewInfo)) + { + DEBUG_ERROR("Failed to get the monitor info"); + goto err; + } + +err_retry: + if (FAILED(GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPath, &numMode))) + goto err; + + DISPLAYCONFIG_PATH_INFO * pathInfo = calloc(sizeof(*pathInfo), numPath); + if (!pathInfo) + goto err_mem_pathInfo; + + DISPLAYCONFIG_MODE_INFO * modeInfo = calloc(sizeof(*modeInfo), numMode); + if (!modeInfo) + goto err_mem_modeInfo; + + LONG status = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, + &numPath, pathInfo, + &numMode, modeInfo, + NULL); + + if (status != ERROR_SUCCESS) + { + if (status == ERROR_INSUFFICIENT_BUFFER) + { + free(modeInfo); + free(pathInfo); + goto err_retry; + } + + DEBUG_ERROR("QueryDisplayConfig failed with 0x%lx", status); + goto err_queryDisplay; + } + + for(unsigned i = 0; i < numPath; ++i) + { + DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName = + { + .header = + { + .type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME, + .size = sizeof(sourceName), + .adapterId = pathInfo[i].sourceInfo.adapterId, + .id = pathInfo[i].sourceInfo.id, + } + }; + + if (FAILED(DisplayConfigGetDeviceInfo(&sourceName.header))) + continue; + + if (wcscmp(viewInfo.szDevice, sourceName.viewGdiDeviceName) != 0) + continue; + + *info = pathInfo[i]; + result = true; + break; + } + +err_queryDisplay: + free(modeInfo); + +err_mem_modeInfo: + free(pathInfo); + +err_mem_pathInfo: + +err: + return result; +} + +float getSDRWhiteLevel(const DISPLAYCONFIG_PATH_INFO * displayPathInfo) +{ + float nits = 80.0f; + DISPLAYCONFIG_SDR_WHITE_LEVEL level = + { + .header = + { + .type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL, + .size = sizeof(level), + .adapterId = displayPathInfo->targetInfo.adapterId, + .id = displayPathInfo->targetInfo.id, + } + }; + + if (SUCCEEDED(DisplayConfigGetDeviceInfo(&level.header))) + nits = level.SDRWhiteLevel / 1000.0f * 80.0f; + + return nits; +} diff --git a/host/platform/Windows/capture/DXGI/src/util.h b/host/platform/Windows/capture/DXGI/src/util.h new file mode 100644 index 00000000..059a0afd --- /dev/null +++ b/host/platform/Windows/capture/DXGI/src/util.h @@ -0,0 +1,32 @@ +/** + * Looking Glass + * Copyright © 2017-2023 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +const char * getDXGIFormatStr(DXGI_FORMAT format); +const char * getDXGIColorSpaceTypeStr(DXGI_COLOR_SPACE_TYPE type); + +bool compileShader(ID3DBlob ** dst, const char * entry, const char * target, + const char * code); + +bool getDisplayPathInfo(HMONITOR monitor, DISPLAYCONFIG_PATH_INFO * info); + +float getSDRWhiteLevel(const DISPLAYCONFIG_PATH_INFO * displayPathInfo);