From ce8b24dfa7fbc248a34d2e4d1ca486f789fb0f09 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 15 Jan 2024 13:40:13 -0600 Subject: [PATCH] Implement fast AES detection logic for Windows and macOS --- app/streaming/session.cpp | 13 ++++++++++--- app/streaming/streamutils.cpp | 29 +++++++++++++++++++++++++++++ app/streaming/streamutils.h | 3 +++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/streaming/session.cpp b/app/streaming/session.cpp index 93480dfa..61b193e4 100644 --- a/app/streaming/session.cpp +++ b/app/streaming/session.cpp @@ -604,9 +604,16 @@ bool Session::initialize() m_StreamConfig.bitrate = m_Preferences->bitrateKbps; #ifndef STEAM_LINK - // Enable audio encryption as long as we're not on Steam Link. - // That hardware can hardly handle Opus decoding at all. - m_StreamConfig.encryptionFlags = ENCFLG_AUDIO; + // Opt-in to all encryption features if we detect that the platform + // has AES cryptography acceleration instructions. + if (StreamUtils::hasFastAes()) { + m_StreamConfig.encryptionFlags = ENCFLG_ALL; + } + else { + // Enable audio encryption as long as we're not on Steam Link. + // That hardware can hardly handle Opus decoding at all. + m_StreamConfig.encryptionFlags = ENCFLG_AUDIO; + } #endif SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, diff --git a/app/streaming/streamutils.cpp b/app/streaming/streamutils.cpp index 15a38980..c9f693ef 100644 --- a/app/streaming/streamutils.cpp +++ b/app/streaming/streamutils.cpp @@ -6,6 +6,10 @@ #include #endif +#ifdef Q_OS_WINDOWS +#include +#endif + Uint32 StreamUtils::getPlatformWindowFlags() { #if defined(Q_OS_DARWIN) @@ -95,6 +99,31 @@ int StreamUtils::getDisplayRefreshRate(SDL_Window* window) return mode.refresh_rate; } +bool StreamUtils::hasFastAes() +{ +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +#if __has_builtin(__builtin_cpu_supports) && defined(Q_PROCESSOR_X86) + return __builtin_cpu_supports("aes"); +#elif defined(__BUILTIN_CPU_SUPPORTS__) && defined(Q_PROCESSOR_POWER) + return __builtin_cpu_supports("vcrypto"); +#elif defined(Q_OS_WINDOWS) && defined(Q_PROCESSOR_ARM) + return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE); +#elif defined(_MSC_VER) && defined(Q_PROCESSOR_X86) + int regs[4]; + __cpuid(regs, 1); + return regs[2] & (1 << 25); // AES-NI +#elif defined(Q_OS_DARWIN) + // Everything that runs Catalina and later has AES-NI or ARMv8 crypto instructions + return true; +#else + // Assume AES is fast if we don't recognize the OS or processor + return true; +#endif +} + bool StreamUtils::getNativeDesktopMode(int displayIndex, SDL_DisplayMode* mode) { #ifdef Q_OS_DARWIN diff --git a/app/streaming/streamutils.h b/app/streaming/streamutils.h index 3567b829..4d61a865 100644 --- a/app/streaming/streamutils.h +++ b/app/streaming/streamutils.h @@ -33,4 +33,7 @@ public: static int getDisplayRefreshRate(SDL_Window* window); + + static + bool hasFastAes(); };