Skip to content

Commit

Permalink
fix: Added synchronization outside of Render() (hoffstadt#1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
v-ein committed Jan 6, 2023
1 parent 396ec0a commit 7901915
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 55 deletions.
2 changes: 2 additions & 0 deletions src/dearpygui_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,8 @@ static PyObject*
configure_viewport(PyObject* self, PyObject* args, PyObject* kwargs)
{

if (!GContext->manualMutexControl) std::lock_guard<std::mutex> lk(GContext->mutex);

mvViewport* viewport = GContext->viewport;
if (viewport)
{
Expand Down
2 changes: 2 additions & 0 deletions src/mvLayoutWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ void mvLayoutWindow::renderTreeNode(std::shared_ptr<mvAppItem>& item)
void mvLayoutWindow::drawWidgets()
{

std::lock_guard<std::mutex> lk(GContext->mutex);

mvUUID parentName = 0;

if (_itemref == nullptr && GContext->itemRegistry->windowRoots.size() > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/mvToolWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void mvToolWindow::draw()
GContext->input.mousePos.x = (int)x;
GContext->input.mousePos.y = (int)y;

std::lock_guard<std::mutex> lk(GContext->mutex);

if (GContext->itemRegistry->activeWindow != getUUID())
GContext->itemRegistry->activeWindow = getUUID();

Expand Down
130 changes: 75 additions & 55 deletions src/mvViewport_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,45 @@ mvPrerender(mvViewport& viewport)
if (viewportData->msg.message == WM_QUIT)
viewport.running = false;

if (viewport.posDirty)
{
int horizontal_shift = get_horizontal_shift(viewportData->handle);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, viewport.xpos-horizontal_shift, viewport.ypos, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
viewport.posDirty = false;
}

if (viewport.sizeDirty)
{
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, 0, 0, viewport.actualWidth, viewport.actualHeight, SWP_SHOWWINDOW | SWP_NOMOVE);
viewport.sizeDirty = false;
}
// TODO: we probably need a separate mutex for this
std::lock_guard<std::mutex> lk(GContext->mutex);

if (viewport.modesDirty)
{
viewportData->modes = WS_OVERLAPPED;

if (viewport.resizable && viewport.decorated) viewportData->modes |= WS_THICKFRAME;
if (viewport.decorated) {
viewportData->modes |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
if (viewport.posDirty)
{
int horizontal_shift = get_horizontal_shift(viewportData->handle);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, viewport.xpos-horizontal_shift, viewport.ypos, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
viewport.posDirty = false;
}
else {
viewportData->modes |= WS_POPUP;

if (viewport.sizeDirty)
{
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, 0, 0, viewport.actualWidth, viewport.actualHeight, SWP_SHOWWINDOW | SWP_NOMOVE);
viewport.sizeDirty = false;
}

SetWindowLongPtr(viewportData->handle, GWL_STYLE, viewportData->modes);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
viewport.modesDirty = false;
}
if (viewport.modesDirty)
{
viewportData->modes = WS_OVERLAPPED;

if (viewport.titleDirty)
{
SetWindowTextA(viewportData->handle, viewport.title.c_str());
viewport.titleDirty = false;
if (viewport.resizable && viewport.decorated) viewportData->modes |= WS_THICKFRAME;
if (viewport.decorated) {
viewportData->modes |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
}
else {
viewportData->modes |= WS_POPUP;
}

SetWindowLongPtr(viewportData->handle, GWL_STYLE, viewportData->modes);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
viewport.modesDirty = false;
}

if (viewport.titleDirty)
{
SetWindowTextA(viewportData->handle, viewport.title.c_str());
viewport.titleDirty = false;
}
}

// Poll and handle messages (inputs, window resize, etc.)
Expand All @@ -96,11 +101,16 @@ mvPrerender(mvViewport& viewport)
//continue;
}

if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
// Font manager is thread-unsafe, so we'd better sync it
std::lock_guard<std::mutex> lk(GContext->mutex);

if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
}
}

// Start the Dear ImGui frame
Expand Down Expand Up @@ -146,30 +156,34 @@ mvHandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
cheight = crect.bottom - crect.top;
}

viewport->actualWidth = awidth;
viewport->actualHeight = aheight;


if (viewport->decorated)
{
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
}
else
{
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
}

//GContext->viewport->resized = true;
mvOnResize();
GContext->viewport->resized = false;

if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
std::lock_guard<std::mutex> lk(GContext->mutex);

viewport->actualWidth = awidth;
viewport->actualHeight = aheight;


if (viewport->decorated)
{
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
}
else
{
GContext->viewport->clientHeight = cheight + 39;
GContext->viewport->clientWidth = cwidth + 16;
}

//GContext->viewport->resized = true;
mvOnResize();
GContext->viewport->resized = false;

if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
}
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
Expand All @@ -182,6 +196,8 @@ mvHandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept

case WM_GETMINMAXINFO:
{
// TODO: lock the mutex?

LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
lpMMI->ptMinTrackSize.x = viewport->minwidth;
lpMMI->ptMinTrackSize.y = viewport->minheight;
Expand All @@ -192,6 +208,8 @@ mvHandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept

case WM_MOVING:
{
std::lock_guard<std::mutex> lk(GContext->mutex);

int horizontal_shift = get_horizontal_shift(viewportData->handle);
RECT rect = *(RECT*)(lParam);
viewport->xpos = rect.left + horizontal_shift;
Expand Down Expand Up @@ -221,6 +239,8 @@ mvHandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
cheight = crect.bottom - crect.top;
}

std::lock_guard<std::mutex> lk(GContext->mutex);

viewport->actualWidth = awidth;
viewport->actualHeight = aheight;

Expand Down

0 comments on commit 7901915

Please sign in to comment.