Skip to content

Commit 509c977

Browse files
committed
Enabling Hardware Acceleration on RDP
1 parent f5be928 commit 509c977

File tree

6 files changed

+112
-9
lines changed

6 files changed

+112
-9
lines changed

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/common/Display.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class CDisplaySet
243243
}
244244

245245
public:
246+
bool IsGraphicHWAccelerationForRDPEnabled() const;
246247
static HRESULT InspectLastError();
247248

248249
void AddRef() const {InterlockedIncrement(&m_cRef);}

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/common/d3dutils.cpp

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,19 @@ GetMinimalTextureDesc(
232232
}
233233

234234
// If format is ok, restore HR from width/height check
235-
if (hr == S_OK)
235+
if (::IsGraphicHWAccelerationForRDPEnabled())
236236
{
237-
hr = hrWH;
237+
if(SUCCEEDED(hr))
238+
{
239+
hr = hrWH;
240+
}
241+
} else {
242+
if (hr == S_OK)
243+
{
244+
hr = hrWH;
245+
}
238246
}
247+
239248
}
240249

241250
#if DBG
@@ -316,7 +325,84 @@ GetSuperiorSurfaceFormat(
316325
return D3DFMT_UNKNOWN;
317326
}
318327

328+
//+------------------------------------------------------------------------
329+
//
330+
// Function: IsGraphicHWAccelerationForRDPEnabled
331+
//
332+
// Synopsis:
333+
// Checks to see if user has enabled graphich hardware acceleration in remote connection.
334+
// An app can opt-in to enable HW acceleration by setting a regkey in
335+
// HKCU\Software\Microsoft\Avalon.Graphics\EnableRemoteHWAcceleration
336+
// or
337+
// HKLM\Software\Microsoft\Avalon.Graphics\EnableRemoteHWAcceleration
338+
// whose name is the full path to the .exe that wants to opt-in, and whose DWORD value is 1.
339+
//
340+
// The regkey is checked in the first call to this function, and the
341+
// value is cached for all future calls in this process.
342+
//
343+
//-------------------------------------------------------------------------
344+
bool
345+
IsGraphicHWAccelerationForRDPEnabled()
346+
{
347+
enum IsRemoteHWAccelerationEnabledState
348+
{
349+
HWAcceleration_Uninitialized,
350+
HWAcceleration_False,
351+
HWAcceleration_True,
352+
};
353+
static IsRemoteHWAccelerationEnabledState s_hwAccelerationFlag = IsRemoteHWAccelerationEnabledState::HWAcceleration_Uninitialized;
354+
if (s_hwAccelerationFlag == IsRemoteHWAccelerationEnabledState::HWAcceleration_Uninitialized)
355+
{
356+
TCHAR szFullPath[MAX_PATH];
357+
DWORD cFullPath = GetModuleFileName(NULL, szFullPath, sizeof(szFullPath) / sizeof(szFullPath[0]));
319358

320-
321-
322-
359+
if (cFullPath > 0)
360+
{
361+
// First check for a value in HKCU (which should override any HKLM setting).
362+
HKEY hKey = NULL;
363+
LONG r = RegOpenKeyEx(
364+
HKEY_CURRENT_USER,
365+
_T("Software\\Microsoft\\Avalon.Graphics\\EnableRemoteHWAcceleration"),
366+
0,
367+
KEY_QUERY_VALUE,
368+
&hKey
369+
);
370+
if (r == ERROR_SUCCESS)
371+
{
372+
DWORD dwValue = 0; // default to false (don't ignore errors)
373+
if (RegGetDword(hKey, szFullPath, &dwValue))
374+
{
375+
s_hwAccelerationFlag = (dwValue == 0) ? IsRemoteHWAccelerationEnabledState::HWAcceleration_False : IsRemoteHWAccelerationEnabledState::HWAcceleration_True;
376+
}
377+
RegCloseKey(hKey);
378+
}
379+
// If we didn't set a value from HKCU, try HKLM.
380+
if (s_hwAccelerationFlag == IsRemoteHWAccelerationEnabledState::HWAcceleration_Uninitialized)
381+
{
382+
r = RegOpenKeyEx(
383+
HKEY_LOCAL_MACHINE,
384+
_T("Software\\Microsoft\\Avalon.Graphics\\EnableRemoteHWAcceleration"),
385+
0,
386+
KEY_QUERY_VALUE,
387+
&hKey
388+
);
389+
if (r == ERROR_SUCCESS)
390+
{
391+
DWORD dwValue = 0; // default to false (don't ignore errors)
392+
if (RegGetDword(hKey, szFullPath, &dwValue))
393+
{
394+
s_hwAccelerationFlag = (dwValue == 0) ? IsRemoteHWAccelerationEnabledState::HWAcceleration_False : IsRemoteHWAccelerationEnabledState::HWAcceleration_True;
395+
}
396+
RegCloseKey(hKey);
397+
}
398+
}
399+
}
400+
// If we still haven't set a value, then no value was set in the registry.
401+
// Default to false (disable HW acceleration in remote connection).
402+
if (s_hwAccelerationFlag == IsRemoteHWAccelerationEnabledState::HWAcceleration_Uninitialized)
403+
{
404+
s_hwAccelerationFlag = IsRemoteHWAccelerationEnabledState::HWAcceleration_False;
405+
}
406+
}
407+
return (s_hwAccelerationFlag == IsRemoteHWAccelerationEnabledState::HWAcceleration_True);
408+
}

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/common/d3dutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ D3DFORMAT GetSuperiorSurfaceFormat(
105105
BOOL fPalUsesAlpha
106106
);
107107

108-
108+
bool IsGraphicHWAccelerationForRDPEnabled();

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/common/display.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ CDisplaySet::GetGraphicsAccelerationCaps(
21252125
*pulDisplayUniqueness = m_ulDisplayUniquenessLoader;
21262126
}
21272127

2128-
if (m_rgpDisplays.GetCount() == 0 || m_fNonLocalDevicePresent)
2128+
if (m_rgpDisplays.GetCount() == 0 || (!::IsGraphicHWAccelerationForRDPEnabled() && m_fNonLocalDevicePresent))
21292129
{
21302130
//
21312131
// No display - no acceleration

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/hw/hwbitmapcolorsource.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,20 @@ CHwBitmapColorSource::ComputeRealizationParameters(
14211421
&uLevels
14221422
);
14231423

1424-
Assert(pDevice->GetMinimalTextureDesc(
1424+
if(::IsGraphicHWAccelerationForRDPEnabled())
1425+
{
1426+
Assert(SUCCEEDED(pDevice->GetMinimalTextureDesc(
1427+
&d3dsdRequired,
1428+
TRUE,
1429+
(GMTD_CHECK_ALL |
1430+
(TextureAddressingAllowsConditionalNonPower2Usage(
1431+
oRealizationParams.dlU.d3dta,
1432+
oRealizationParams.dlV.d3dta) ?
1433+
GMTD_NONPOW2CONDITIONAL_OK : 0)
1434+
)
1435+
)));
1436+
} else {
1437+
Assert(pDevice->GetMinimalTextureDesc(
14251438
&d3dsdRequired,
14261439
TRUE,
14271440
(GMTD_CHECK_ALL |
@@ -1431,6 +1444,8 @@ CHwBitmapColorSource::ComputeRealizationParameters(
14311444
GMTD_NONPOW2CONDITIONAL_OK : 0)
14321445
)
14331446
) == S_OK);
1447+
}
1448+
14341449
}
14351450
#endif
14361451

src/Microsoft.DotNet.Wpf/src/WpfGfx/core/meta/desktoprt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
//------------------------------------------------------------------------------
2828

2929
#include "precomp.hpp"
30+
#include "common/d3dutils.h"
3031

3132
DeclareTag(tagMILRenderClearAfterPresent, "MIL", "Clear after present");
3233
DeclareTag(tagMILTraceDesktopState, "MIL", "Trace MILRender desktop state");
@@ -93,7 +94,7 @@ HRESULT CDesktopRenderTarget::Create(
9394
// check whether any adapters don't support Hw acceleration or D3D is not
9495
// available.
9596
//
96-
if ( pDisplaySet->IsNonLocalDisplayPresent()
97+
if ( (!(::IsGraphicHWAccelerationForRDPEnabled()) && pDisplaySet->IsNonLocalDisplayPresent())
9798
|| !pDisplaySet->D3DObject())
9899
{
99100
// If possible, just revert to Sw. This simply prevents trying Hw and

0 commit comments

Comments
 (0)