From 6584de4a78e34150a07f23e471f4202d14f9a956 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 2 Feb 2023 21:28:28 +0100 Subject: [PATCH] Backends: SDL: Avoid calling SDL_SetCursor() when cursor has not changed. (#6113) --- backends/imgui_impl_sdl.cpp | 10 +++++++++- docs/CHANGELOG.txt | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_sdl.cpp b/backends/imgui_impl_sdl.cpp index 72005784b..bdb4b491a 100644 --- a/backends/imgui_impl_sdl.cpp +++ b/backends/imgui_impl_sdl.cpp @@ -18,6 +18,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). // 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096) // 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019) // 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463) @@ -91,6 +92,7 @@ struct ImGui_ImplSDL2_Data Uint64 Time; int MouseButtonsDown; SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; + SDL_Cursor* LastMouseCursor; int PendingMouseLeaveFrame; char* ClipboardTextData; bool MouseCanUseGlobalState; @@ -439,6 +441,7 @@ void ImGui_ImplSDL2_Shutdown() SDL_free(bd->ClipboardTextData); for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) SDL_FreeCursor(bd->MouseCursors[cursor_n]); + bd->LastMouseCursor = NULL; io.BackendPlatformName = nullptr; io.BackendPlatformUserData = nullptr; @@ -492,7 +495,12 @@ static void ImGui_ImplSDL2_UpdateMouseCursor() else { // Show OS mouse cursor - SDL_SetCursor(bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); + SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; + if (bd->LastMouseCursor != expected_cursor) + { + SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113) + bd->LastMouseCursor = expected_cursor; + } SDL_ShowCursor(SDL_TRUE); } } diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 99b3b74e3..087db7e8a 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -92,7 +92,9 @@ All changes: - Backends: SDL: Removed SDL_MOUSEWHEEL value clamping. (#4019, #6096, #6081) - Backends: SDL: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling as reported by SDL. (#4019, #6096) -- Backend: WebGPU: Fix building for latest WebGPU specs (remove implicit layout generation). +- Backends: SDL: Avoid calling SDL_SetCursor() when cursor has not changed, as the function + is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). (#6113) +- Backends: WebGPU: Fix building for latest WebGPU specs (remove implicit layout generation). (#6117, #4116, #3632) [@tonygrue, @bfierz] - Examples: refactord all examples to use a "MainLoopStep()" function. This is in order to be able to trivially make some compile with Emscripten. (#2492, #3699)