From fb91482282f1481af84884d67288b667bddfec5e Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 28 May 2018 17:40:44 -1000 Subject: [PATCH] Do fullscreen set on main thread --- lib/win/ApplicationWin32.cpp | 66 +++++++++++++++++++++++++++++++----- lib/win/Win32Common.hpp | 51 ++++++++-------------------- lib/win/WindowWin32.cpp | 4 +-- 3 files changed, 74 insertions(+), 47 deletions(-) diff --git a/lib/win/ApplicationWin32.cpp b/lib/win/ApplicationWin32.cpp index e9f41f5..ae7f8b2 100644 --- a/lib/win/ApplicationWin32.cpp +++ b/lib/win/ApplicationWin32.cpp @@ -26,6 +26,8 @@ PFN_GetScaleFactorForMonitor MyGetScaleFactorForMonitor = nullptr; #endif DWORD g_mainThreadId = 0; +std::mutex g_nwmt; +std::condition_variable g_nwcv; static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); pD3DCompile D3DCompilePROC = nullptr; @@ -345,6 +347,47 @@ public: } } + template + static void DoSetFullscreen(W& win, bool fs) + { + std::lock_guard lk(g_nwmt); + if (fs) + { + win.m_fsStyle = GetWindowLong(win.m_hwnd, GWL_STYLE); + win.m_fsExStyle = GetWindowLong(win.m_hwnd, GWL_EXSTYLE); + GetWindowRect(win.m_hwnd, &win.m_fsRect); + + SetWindowLong(win.m_hwnd, GWL_STYLE, + win.m_fsStyle & ~(WS_CAPTION | WS_THICKFRAME)); + SetWindowLong(win.m_hwnd, GWL_EXSTYLE, + win.m_fsExStyle & ~(WS_EX_DLGMODALFRAME | + WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); + + MONITORINFO monitor_info; + monitor_info.cbSize = sizeof(monitor_info); + GetMonitorInfo(MonitorFromWindow(win.m_hwnd, MONITOR_DEFAULTTONEAREST), + &monitor_info); + SetWindowPos(win.m_hwnd, NULL, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, + monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, + monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + + win.m_fs = true; + } + else + { + SetWindowLong(win.m_hwnd, GWL_STYLE, win.m_fsStyle); + SetWindowLong(win.m_hwnd, GWL_EXSTYLE, win.m_fsExStyle); + + SetWindowPos(win.m_hwnd, NULL, win.m_fsRect.left, win.m_fsRect.top, + win.m_fsRect.right - win.m_fsRect.left, win.m_fsRect.bottom - win.m_fsRect.top, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + + win.m_fs = false; + } + g_nwcv.notify_one(); + } + int run() { g_mainThreadId = GetCurrentThreadId(); @@ -357,7 +400,7 @@ public: logvisor::RegisterThreadName(thrName.c_str()); CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); clientReturn = m_callback.appMain(this); - PostThreadMessage(g_mainThreadId, WM_USER+1, 0, 0); + PostThreadMessageW(g_mainThreadId, WM_USER+1, 0, 0); }); /* Pump messages */ @@ -372,11 +415,10 @@ public: case WM_USER: { /* New-window message (coalesced onto main thread) */ - std::unique_lock lk(m_nwmt); + std::lock_guard lk(g_nwmt); SystemStringView* title = reinterpret_cast(msg.wParam); m_mwret = newWindow(*title); - lk.unlock(); - m_nwcv.notify_one(); + g_nwcv.notify_one(); continue; } case WM_USER+1: @@ -395,6 +437,14 @@ public: /* ImmSetCompositionWindow call from client thread */ ImmSetCompositionWindow(HIMC(msg.wParam), LPCOMPOSITIONFORM(msg.lParam)); continue; + case WM_USER+5: + /* SetFullscreen call for OpenGL window */ + DoSetFullscreen(*reinterpret_cast(msg.wParam), msg.lParam); + continue; + case WM_USER+6: + /* SetFullscreen call for Vulkan window */ + DoSetFullscreen(*reinterpret_cast(msg.wParam), msg.lParam); + continue; default: break; } } @@ -434,17 +484,15 @@ public: return m_args; } - std::mutex m_nwmt; - std::condition_variable m_nwcv; std::shared_ptr m_mwret; std::shared_ptr newWindow(SystemStringView title) { if (GetCurrentThreadId() != g_mainThreadId) { - std::unique_lock lk(m_nwmt); - if (!PostThreadMessage(g_mainThreadId, WM_USER, WPARAM(&title), 0)) + std::unique_lock lk(g_nwmt); + if (!PostThreadMessageW(g_mainThreadId, WM_USER, WPARAM(&title), 0)) Log.report(logvisor::Fatal, "PostThreadMessage error"); - m_nwcv.wait(lk); + g_nwcv.wait(lk); std::shared_ptr ret = std::move(m_mwret); m_mwret.reset(); return ret; diff --git a/lib/win/Win32Common.hpp b/lib/win/Win32Common.hpp index 1874d6f..c77650e 100644 --- a/lib/win/Win32Common.hpp +++ b/lib/win/Win32Common.hpp @@ -16,6 +16,8 @@ #include "boo/graphicsdev/GL.hpp" extern DWORD g_mainThreadId; +extern std::mutex g_nwmt; +extern std::condition_variable g_nwcv; #if _WIN32_WINNT_WINBLUE && !WINDOWS_STORE #include @@ -47,45 +49,22 @@ struct OGLContext boo::GLContext m_glCtx; }; -template -static inline void SetFullscreen(W& win, bool fs) +#if !WINDOWS_STORE +static inline void SetFullscreen(OGLContext::Window& win, bool fs) { - if (fs) - { - win.m_fsStyle = GetWindowLong(win.m_hwnd, GWL_STYLE); - win.m_fsExStyle = GetWindowLong(win.m_hwnd, GWL_EXSTYLE); - GetWindowRect(win.m_hwnd, &win.m_fsRect); - - SetWindowLong(win.m_hwnd, GWL_STYLE, - win.m_fsStyle & ~(WS_CAPTION | WS_THICKFRAME)); - SetWindowLong(win.m_hwnd, GWL_EXSTYLE, - win.m_fsExStyle & ~(WS_EX_DLGMODALFRAME | - WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); - - MONITORINFO monitor_info; - monitor_info.cbSize = sizeof(monitor_info); - GetMonitorInfo(MonitorFromWindow(win.m_hwnd, MONITOR_DEFAULTTONEAREST), - &monitor_info); - SetWindowPos(win.m_hwnd, NULL, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, - monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, - monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, - SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS); - - win.m_fs = true; - } - else - { - SetWindowLong(win.m_hwnd, GWL_STYLE, win.m_fsStyle); - SetWindowLong(win.m_hwnd, GWL_EXSTYLE, win.m_fsExStyle); - - SetWindowPos(win.m_hwnd, NULL, win.m_fsRect.left, win.m_fsRect.top, - win.m_fsRect.right - win.m_fsRect.left, win.m_fsRect.bottom - win.m_fsRect.top, - SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS); - - win.m_fs = false; - } + std::unique_lock lk(g_nwmt); + PostThreadMessageW(g_mainThreadId, WM_USER+5, WPARAM(&win), LPARAM(fs)); + g_nwcv.wait(lk); } +static inline void SetFullscreen(boo::VulkanContext::Window& win, bool fs) +{ + std::unique_lock lk(g_nwmt); + PostThreadMessageW(g_mainThreadId, WM_USER+6, WPARAM(&win), LPARAM(fs)); + g_nwcv.wait(lk); +} +#endif + struct Boo3DAppContextWin32 : Boo3DAppContext { OGLContext m_ctxOgl; diff --git a/lib/win/WindowWin32.cpp b/lib/win/WindowWin32.cpp index 3e74cb6..475a1ac 100644 --- a/lib/win/WindowWin32.cpp +++ b/lib/win/WindowWin32.cpp @@ -1164,7 +1164,7 @@ public: { if (GetCurrentThreadId() != g_mainThreadId) { - if (!PostThreadMessage(g_mainThreadId, WM_USER+3, WPARAM(m_imc), LPARAM(open))) + if (!PostThreadMessageW(g_mainThreadId, WM_USER+3, WPARAM(m_imc), LPARAM(open))) Log.report(logvisor::Fatal, "PostThreadMessage error"); return; } @@ -1181,7 +1181,7 @@ public: if (GetCurrentThreadId() != g_mainThreadId) { - if (!PostThreadMessage(g_mainThreadId, WM_USER+4, WPARAM(m_imc), LPARAM(&m_cForm))) + if (!PostThreadMessageW(g_mainThreadId, WM_USER+4, WPARAM(m_imc), LPARAM(&m_cForm))) Log.report(logvisor::Fatal, "PostThreadMessage error"); return; }