SDL: Only set SDL_PROP_SDL2_COMPAT_WINDOW_PREFERRED_FULLSCREEN_DISPLAY for the public SDL_GetDisplayForWindow() API

From f068399cabf797b30136753e34f7141c96a6b1e1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 2 Oct 2026 07:56:26 -0700
Subject: [PATCH] Only set
 SDL_PROP_SDL2_COMPAT_WINDOW_PREFERRED_FULLSCREEN_DISPLAY for the public
 SDL_GetDisplayForWindow() API

This reduces overhead for internal uses of GetDisplayForWindow(), while still retaining functionality needed by sdl2-compat

Fixes https://github.com/libsdl-org/SDL/issues/16428
---
 src/video/SDL_video.c | 47 ++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 1960d5f2acf24..9100ffe726121 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1003,9 +1003,26 @@ SDL_VideoDisplay *SDL_GetVideoDisplay(SDL_DisplayID displayID)
     return _this->displays[display_index];
 }
 
+static SDL_DisplayID GetDisplayForWindow(SDL_Window *window)
+{
+    SDL_DisplayID displayID = 0;
+
+    // An explicit fullscreen display overrides all
+    if (window->flags & SDL_WINDOW_FULLSCREEN) {
+        displayID = window->current_fullscreen_mode.displayID;
+    }
+
+    if (!displayID) {
+        displayID = SDL_GetDisplayForWindowPosition(window);
+    }
+    return displayID;
+}
+
 SDL_VideoDisplay *SDL_GetVideoDisplayForWindow(SDL_Window *window)
 {
-    return SDL_GetVideoDisplay(SDL_GetDisplayForWindow(window));
+    CHECK_WINDOW_MAGIC(window, 0);
+
+    return SDL_GetVideoDisplay(GetDisplayForWindow(window));
 }
 
 SDL_DisplayID SDL_GetPrimaryDisplay(void)
@@ -1046,7 +1063,9 @@ SDL_DisplayData *SDL_GetDisplayDriverData(SDL_DisplayID displayID)
 
 SDL_DisplayData *SDL_GetDisplayDriverDataForWindow(SDL_Window *window)
 {
-    return SDL_GetDisplayDriverData(SDL_GetDisplayForWindow(window));
+    CHECK_WINDOW_MAGIC(window, 0);
+
+    return SDL_GetDisplayDriverData(GetDisplayForWindow(window));
 }
 
 SDL_PropertiesID SDL_GetDisplayProperties(SDL_DisplayID displayID)
@@ -1219,7 +1238,7 @@ void SDL_SetDisplayHDRProperties(SDL_VideoDisplay *display, const SDL_HDROutputP
 
     if (changed && !SDL_DriverSendsHDRChanges(_this)) {
         for (SDL_Window *w = display->device->windows; w; w = w->next) {
-            if (SDL_GetDisplayForWindow(w) == display->id) {
+            if (GetDisplayForWindow(w) == display->id) {
                 SDL_SetWindowHDRProperties(w, &display->HDR, true);
             }
         }
@@ -1791,8 +1810,6 @@ SDL_VideoDisplay *SDL_GetVideoDisplayForFullscreenWindow(SDL_Window *window)
 
 SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
 {
-    SDL_DisplayID displayID = 0;
-
     CHECK_WINDOW_MAGIC(window, 0);
 
     /* sdl2-compat calls this function to get a display on which to make the window fullscreen,
@@ -1806,15 +1823,7 @@ SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
         SDL_ClearProperty(window_props, SDL_PROP_SDL2_COMPAT_WINDOW_PREFERRED_FULLSCREEN_DISPLAY);
     }
 
-    // An explicit fullscreen display overrides all
-    if (window->flags & SDL_WINDOW_FULLSCREEN) {
-        displayID = window->current_fullscreen_mode.displayID;
-    }
-
-    if (!displayID) {
-        displayID = SDL_GetDisplayForWindowPosition(window);
-    }
-    return displayID;
+    return GetDisplayForWindow(window);
 }
 
 static void SDL_CheckWindowDisplayChanged(SDL_Window *window)
@@ -2213,7 +2222,7 @@ SDL_PixelFormat SDL_GetWindowPixelFormat(SDL_Window *window)
 
     CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
 
-    displayID = SDL_GetDisplayForWindow(window);
+    displayID = GetDisplayForWindow(window);
     mode = SDL_GetCurrentDisplayMode(displayID);
     if (mode) {
         return mode->format;
@@ -2577,7 +2586,7 @@ SDL_Window *SDL_CreateWindowWithProperties(SDL_PropertiesID props)
     window->opacity = 1.0f;
     window->next = _this->windows;
     window->is_destroying = false;
-    window->displayID = SDL_GetDisplayForWindow(window);
+    window->displayID = GetDisplayForWindow(window);
     window->external_graphics_context = external_graphics_context;
     window->constrain_popup = SDL_GetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN, true);
 
@@ -3023,7 +3032,7 @@ bool SDL_SetWindowPosition(SDL_Window *window, int x, int y)
     const int w = window->last_size_pending ? window->pending.w : window->windowed.w;
     const int h = window->last_size_pending ? window->pending.h : window->windowed.h;
 
-    original_displayID = SDL_GetDisplayForWindow(window);
+    original_displayID = GetDisplayForWindow(window);
     window->pending_displayID = 0;
 
     if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
@@ -3115,7 +3124,7 @@ bool SDL_GetWindowPosition(SDL_Window *window, int *x, int *y)
 
         /* Find the window's monitor and update to the
            monitor offset. */
-        displayID = SDL_GetDisplayForWindow(window);
+        displayID = GetDisplayForWindow(window);
         if (displayID != 0) {
             SDL_Rect bounds;
 
@@ -3347,7 +3356,7 @@ bool SDL_GetWindowSizeInPixels(SDL_Window *window, int *w, int *h)
     if (_this->GetWindowSizeInPixels) {
         _this->GetWindowSizeInPixels(_this, window, w, h);
     } else {
-        SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
+        SDL_DisplayID displayID = GetDisplayForWindow(window);
         const SDL_DisplayMode *mode;
 
         SDL_GetWindowSize(window, w, h);