Reduce lock usage during window events

This commit is contained in:
2025-04-19 21:12:38 +03:00
parent f5fbc1e32d
commit c40c31fbb7
4 changed files with 5 additions and 25 deletions

View File

@@ -402,10 +402,11 @@ DEFINE_INTERNAL_CALL(void) EditorInternal_RunVisualScriptBreakpointLoopTick(floa
break; break;
} }
} }
WindowsManager::WindowsLocker.Unlock();
for (const auto& e : inputEvents) for (const auto& e : inputEvents)
{ {
auto window = e.Target ? e.Target : defaultWindow; auto window = e.Target ? e.Target : defaultWindow;
if (!window) if (!window || window->IsClosed())
continue; continue;
switch (e.Type) switch (e.Type)
{ {
@@ -443,7 +444,6 @@ DEFINE_INTERNAL_CALL(void) EditorInternal_RunVisualScriptBreakpointLoopTick(floa
break; break;
} }
} }
WindowsManager::WindowsLocker.Unlock();
} }
WindowsManager::WindowsLocker.Lock(); WindowsManager::WindowsLocker.Lock();
Array<Window*, InlinedAllocation<32>> windows; Array<Window*, InlinedAllocation<32>> windows;

View File

@@ -913,11 +913,10 @@ void InputService::Update()
WindowsManager::WindowsLocker.Unlock(); WindowsManager::WindowsLocker.Unlock();
// Send input events for the focused window // Send input events for the focused window
WindowsManager::WindowsLocker.Lock();
for (const auto& e : InputEvents) for (const auto& e : InputEvents)
{ {
auto window = e.Target ? e.Target : defaultWindow; auto window = e.Target ? e.Target : defaultWindow;
if (!window || !WindowsManager::Windows.Contains(window)) if (!window || window->IsClosed())
continue; continue;
switch (e.Type) switch (e.Type)
{ {
@@ -965,7 +964,6 @@ void InputService::Update()
break; break;
} }
} }
WindowsManager::WindowsLocker.Unlock();
// Skip if game has no focus to handle the input // Skip if game has no focus to handle the input
if (!Engine::HasGameViewportFocus()) if (!Engine::HasGameViewportFocus())

View File

@@ -32,26 +32,9 @@ namespace WinImpl
// The events for releasing the mouse during window dragging are missing, handle the mouse release event here // The events for releasing the mouse during window dragging are missing, handle the mouse release event here
bool SDLCALL SDLPlatform::EventMessageHook(void* userdata, MSG* msg) bool SDLCALL SDLPlatform::EventMessageHook(void* userdata, MSG* msg)
{ {
#define GET_WINDOW_WITH_HWND(window, hwnd) \
do { \
(window) = nullptr; \
WindowsManager::WindowsLocker.Lock(); \
for (int32 i = 0; i < WindowsManager::Windows.Count(); i++) \
{ \
if (WindowsManager::Windows[i]->GetNativePtr() == (hwnd)) \
{ \
(window) = WindowsManager::Windows[i]; \
break; \
} \
} \
WindowsManager::WindowsLocker.Unlock(); \
ASSERT((window) != nullptr); \
} while (false)
if (msg->message == WM_NCLBUTTONDOWN) if (msg->message == WM_NCLBUTTONDOWN)
{ {
Window* window; Window* window = WindowsManager::GetByNativePtr(msg->hwnd);
GET_WINDOW_WITH_HWND(window, msg->hwnd);
Float2 mousePosition(static_cast<float>(static_cast<LONG>(WINDOWS_GET_X_LPARAM(msg->lParam))), static_cast<float>(static_cast<LONG>(WINDOWS_GET_Y_LPARAM(msg->lParam)))); Float2 mousePosition(static_cast<float>(static_cast<LONG>(WINDOWS_GET_X_LPARAM(msg->lParam))), static_cast<float>(static_cast<LONG>(WINDOWS_GET_Y_LPARAM(msg->lParam))));
WinImpl::DraggedWindow = window; WinImpl::DraggedWindow = window;
@@ -81,7 +64,6 @@ bool SDLCALL SDLPlatform::EventMessageHook(void* userdata, MSG* msg)
} }
} }
return true; return true;
#undef GET_WINDOW_WITH_HWND
} }
bool SDLPlatform::InitInternal() bool SDLPlatform::InitInternal()

View File

@@ -589,7 +589,7 @@ void SDLWindow::Restore()
bool SDLWindow::IsClosed() const bool SDLWindow::IsClosed() const
{ {
return _handle == nullptr; return WindowBase::IsClosed() || _handle == nullptr;
} }
bool SDLWindow::IsForegroundWindow() const bool SDLWindow::IsForegroundWindow() const