Skip to content

Commit

Permalink
nvapi: Add NvAPI_GetLogicalGPUFromDisplay
Browse files Browse the repository at this point in the history
Also add a few missing nullptr checks.
  • Loading branch information
jp7677 committed May 27, 2024
1 parent b7e018f commit 2b67af4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
29 changes: 25 additions & 4 deletions src/nvapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ extern "C" {
if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (hPhysicalGpu == nullptr)
return InvalidArgument(n);

NvapiAdapter* adapter = nullptr;
for (auto i = 0U; i < nvapiAdapterRegistry->GetAdapterCount(); i++)
if (nvapiAdapterRegistry->GetAdapter(i)->GetBoardId() == gpuId)
Expand Down Expand Up @@ -130,7 +133,7 @@ extern "C" {
if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (hPhysicalGPU == nullptr)
if (hPhysicalGPU == nullptr || pLogicalGPU == nullptr)
return InvalidArgument(n);

auto adapter = reinterpret_cast<NvapiAdapter*>(hPhysicalGPU);
Expand All @@ -142,13 +145,31 @@ extern "C" {
return Ok(n);
}

NvAPI_Status __cdecl NvAPI_GetLogicalGPUFromDisplay(NvDisplayHandle hNvDisp, NvLogicalGpuHandle* pLogicalGPU) {
constexpr auto n = __func__;

if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (hNvDisp == nullptr || pLogicalGPU == nullptr)
return InvalidArgument(n);

auto output = reinterpret_cast<NvapiOutput*>(hNvDisp);
if (!nvapiAdapterRegistry->IsOutput(output))
return ExpectedDisplayHandle(n);

*pLogicalGPU = reinterpret_cast<NvLogicalGpuHandle>(output->GetParent());

return Ok(n);
}

NvAPI_Status __cdecl NvAPI_GetPhysicalGPUsFromLogicalGPU(NvLogicalGpuHandle hLogicalGPU, NvPhysicalGpuHandle hPhysicalGPU[NVAPI_MAX_PHYSICAL_GPUS], NvU32* pGpuCount) {
constexpr auto n = __func__;

if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (hLogicalGPU == nullptr)
if (hLogicalGPU == nullptr || hPhysicalGPU == nullptr || pGpuCount == nullptr)
return InvalidArgument(n);

auto adapter = reinterpret_cast<NvapiAdapter*>(hLogicalGPU);
Expand Down Expand Up @@ -209,7 +230,7 @@ extern "C" {
if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (NvDispHandle == nullptr)
if (NvDispHandle == nullptr || szDisplayName == nullptr)
return InvalidArgument(n);

auto output = reinterpret_cast<NvapiOutput*>(NvDispHandle);
Expand All @@ -227,7 +248,7 @@ extern "C" {
if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (szDisplayName == nullptr)
if (szDisplayName == nullptr || pNvDispHandle == nullptr)
return InvalidArgument(n);

auto output = nvapiAdapterRegistry->FindOutput(szDisplayName);
Expand Down
1 change: 1 addition & 0 deletions src/nvapi_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ extern "C" {
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GetDisplayDriverVersion)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GetPhysicalGPUsFromDisplay)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GetLogicalGPUFromPhysicalGPU)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GetLogicalGPUFromDisplay)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GetPhysicalGPUsFromLogicalGPU)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_EnumNvidiaDisplayHandle)
INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_EnumNvidiaUnAttachedDisplayHandle)
Expand Down
33 changes: 31 additions & 2 deletions tests/nvapi_sysinfo_topo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ TEST_CASE("Topology methods succeed", "[.sysinfo-topo]") {
REQUIRE(logicalhandle == reinterpret_cast<NvLogicalGpuHandle>(handles[1]));
}

SECTION("GetLogicalGPUFromDisplay succeeds") {
NvLogicalGpuHandle logicalhandles[NVAPI_MAX_LOGICAL_GPUS]{};
NvU32 count;
REQUIRE(NvAPI_EnumLogicalGPUs(logicalhandles, &count) == NVAPI_OK);

NvDisplayHandle handle1 = nullptr;
NvDisplayHandle handle2 = nullptr;
NvDisplayHandle handle3 = nullptr;
REQUIRE(NvAPI_EnumNvidiaDisplayHandle(0U, &handle1) == NVAPI_OK);
REQUIRE(NvAPI_EnumNvidiaDisplayHandle(1U, &handle2) == NVAPI_OK);
REQUIRE(NvAPI_EnumNvidiaDisplayHandle(2U, &handle3) == NVAPI_OK);

NvLogicalGpuHandle logicalhandle1;
REQUIRE(NvAPI_GetLogicalGPUFromDisplay(handle1, &logicalhandle1) == NVAPI_OK);
REQUIRE(logicalhandle1 == reinterpret_cast<NvLogicalGpuHandle>(logicalhandles[0]));

NvLogicalGpuHandle logicalhandle2;
REQUIRE(NvAPI_GetLogicalGPUFromDisplay(handle2, &logicalhandle2) == NVAPI_OK);
REQUIRE(logicalhandle2 == reinterpret_cast<NvLogicalGpuHandle>(logicalhandles[0]));

NvLogicalGpuHandle logicalhandle3;
REQUIRE(NvAPI_GetLogicalGPUFromDisplay(handle3, &logicalhandle3) == NVAPI_OK);
REQUIRE(logicalhandle3 == reinterpret_cast<NvLogicalGpuHandle>(logicalhandles[1]));

auto invalid = reinterpret_cast<NvDisplayHandle>(0x1);
NvLogicalGpuHandle logicalhandle4;
REQUIRE(NvAPI_GetLogicalGPUFromDisplay(invalid, &logicalhandle4) == NVAPI_EXPECTED_DISPLAY_HANDLE);
}

SECTION("GetPhysicalGPUsFromLogicalGPU succeeds") {
NvLogicalGpuHandle handles[NVAPI_MAX_LOGICAL_GPUS]{};
NvU32 count;
Expand Down Expand Up @@ -208,9 +237,9 @@ TEST_CASE("Topology methods succeed", "[.sysinfo-topo]") {
REQUIRE(NvAPI_GetAssociatedNvidiaDisplayName(handle3, name3) == NVAPI_OK);
REQUIRE_THAT(name3, Equals("Output3"));

auto invalid = reinterpret_cast<NvDisplayHandle>(0x1);
NvAPI_ShortString name4;
NvDisplayHandle handle4 = nullptr;
REQUIRE(NvAPI_GetAssociatedNvidiaDisplayName(handle4, name4) == NVAPI_INVALID_ARGUMENT);
REQUIRE(NvAPI_GetAssociatedNvidiaDisplayName(invalid, name4) == NVAPI_EXPECTED_DISPLAY_HANDLE);
}

SECTION("GetAssociatedNvidiaDisplayHandle succeeds") {
Expand Down

0 comments on commit 2b67af4

Please sign in to comment.