From 6508586fc2bc03b8ae58bc164db773fd669a8f0a Mon Sep 17 00:00:00 2001 From: Tim Kane Date: Tue, 12 Mar 2024 16:05:19 +1100 Subject: [PATCH] Resolve Xcode runtime warnings regarding access of NSView/NSWindow properties from a non-main thread This issue presents when rendering from a thread that is other than the window thread, more easily reproduced when disabling vsync This commit aims to populate the relevant data into backend_data, by way of dispatch_async (ocurring on the main thread). Those particular backend data items are protected by an NSlock There may be more elegant ways to do this Main Thread Checker imgui/backends/imgui_impl_osx.mm:608 -[NSView window] must be used from main thread only imgui/backends/imgui_impl_osx.mm:608 -[NSWindow backingScaleFactor] must be used from main thread only imgui/backends/imgui_impl_osx.mm:609 -[NSView bounds] must be used from main thread only --- backends/imgui_impl_osx.mm | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_osx.mm b/backends/imgui_impl_osx.mm index 06a6aff8b..0afbf4028 100644 --- a/backends/imgui_impl_osx.mm +++ b/backends/imgui_impl_osx.mm @@ -625,8 +625,18 @@ void ImGui_ImplOSX_NewFrame(NSView* view) // Setup display size if (view) { - const float dpi = (float)[view.window backingScaleFactor]; - io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height); + dispatch_async(dispatch_get_main_queue(), ^{ + [bd->exclusive.lock lock]; + bd->exclusive.backingScaleFactor = view.window.backingScaleFactor; + bd->exclusive.bounds = view.bounds; + [bd->exclusive.lock unlock]; + }); + + [bd->exclusive.lock lock]; + const float dpi = (float) bd->exclusive.backingScaleFactor; + io.DisplaySize = ImVec2((float) bd->exclusive.bounds.size.width, bd->exclusive.bounds.size.height); + [bd->exclusive.lock unlock]; + io.DisplayFramebufferScale = ImVec2(dpi, dpi); }