From 3e9781033a362c92db41b17f4942fcde1488df2e Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 5 Sep 2020 14:06:56 -0700 Subject: [PATCH] Create windows with SDL_WINDOW_METAL on macOS This prevents the window from being recreated when initializing a Metal renderer. --- app/backend/systemproperties.cpp | 20 +++++++++---- app/streaming/session.cpp | 51 ++++++++++++++++++++++---------- app/streaming/streamutils.cpp | 9 ++++++ app/streaming/streamutils.h | 3 ++ 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/app/backend/systemproperties.cpp b/app/backend/systemproperties.cpp index a220a50d..1e2720e4 100644 --- a/app/backend/systemproperties.cpp +++ b/app/backend/systemproperties.cpp @@ -120,13 +120,21 @@ void SystemProperties::querySdlVideoInfo() } } - SDL_Window* testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_HIDDEN); + SDL_Window* testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, + SDL_WINDOW_HIDDEN | StreamUtils::getPlatformWindowFlags()); if (!testWindow) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Failed to create window for hardware decode test: %s", - SDL_GetError()); - SDL_QuitSubSystem(SDL_INIT_VIDEO); - return; + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Failed to create test window with platform flags: %s", + SDL_GetError()); + + testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_HIDDEN); + if (!testWindow) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Failed to create window for hardware decode test: %s", + SDL_GetError()); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + return; + } } Session::getDecoderInfo(testWindow, hasHardwareAcceleration, rendererAlwaysFullScreen, maximumResolution); diff --git a/app/streaming/session.cpp b/app/streaming/session.cpp index 930e35d9..947acd3e 100644 --- a/app/streaming/session.cpp +++ b/app/streaming/session.cpp @@ -372,13 +372,21 @@ bool Session::initialize() } // Create a hidden window to use for decoder initialization tests - SDL_Window* testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_HIDDEN); + SDL_Window* testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, + SDL_WINDOW_HIDDEN | StreamUtils::getPlatformWindowFlags()); if (!testWindow) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Failed to create window for hardware decode test: %s", - SDL_GetError()); - SDL_QuitSubSystem(SDL_INIT_VIDEO); - return false; + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Failed to create test window with platform flags: %s", + SDL_GetError()); + + testWindow = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_HIDDEN); + if (!testWindow) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Failed to create window for hardware decode test: %s", + SDL_GetError()); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + return false; + } } qInfo() << "Server GPU:" << m_Computer->gpuModel; @@ -1104,16 +1112,29 @@ void Session::exec(int displayOriginX, int displayOriginY) y, width, height, - SDL_WINDOW_ALLOW_HIGHDPI); + SDL_WINDOW_ALLOW_HIGHDPI | StreamUtils::getPlatformWindowFlags()); if (!m_Window) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "SDL_CreateWindow() failed: %s", - SDL_GetError()); - delete m_InputHandler; - m_InputHandler = nullptr; - SDL_QuitSubSystem(SDL_INIT_VIDEO); - QThreadPool::globalInstance()->start(new DeferredSessionCleanupTask(this)); - return; + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "SDL_CreateWindow() failed with platform flags: %s", + SDL_GetError()); + + m_Window = SDL_CreateWindow("Moonlight", + x, + y, + width, + height, + SDL_WINDOW_ALLOW_HIGHDPI); + if (!m_Window) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "SDL_CreateWindow() failed: %s", + SDL_GetError()); + + delete m_InputHandler; + m_InputHandler = nullptr; + SDL_QuitSubSystem(SDL_INIT_VIDEO); + QThreadPool::globalInstance()->start(new DeferredSessionCleanupTask(this)); + return; + } } m_InputHandler->setWindow(m_Window); diff --git a/app/streaming/streamutils.cpp b/app/streaming/streamutils.cpp index c8c8c8e6..964b5d8b 100644 --- a/app/streaming/streamutils.cpp +++ b/app/streaming/streamutils.cpp @@ -6,6 +6,15 @@ #include #endif +Uint32 StreamUtils::getPlatformWindowFlags() +{ +#ifdef Q_OS_DARWIN + return SDL_WINDOW_METAL; +#else + return 0; +#endif +} + void StreamUtils::scaleSourceToDestinationSurface(SDL_Rect* src, SDL_Rect* dst) { int dstH = dst->w * src->h / src->w; diff --git a/app/streaming/streamutils.h b/app/streaming/streamutils.h index ce6fc6f5..db6bc779 100644 --- a/app/streaming/streamutils.h +++ b/app/streaming/streamutils.h @@ -5,6 +5,9 @@ class StreamUtils { public: + static + Uint32 getPlatformWindowFlags(); + static void scaleSourceToDestinationSurface(SDL_Rect* src, SDL_Rect* dst);