sdl12-compat: DisplayYUVOverlay: Don't update screen surface if clean

From a3d1da652229ef7eb19ccebecd8a35ff749e4f59 Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Sun, 27 Nov 2022 14:45:28 +0800
Subject: [PATCH] DisplayYUVOverlay: Don't update screen surface if clean

Currently, if there are multiple SDL_DisplayYUVOverlay() calls in a row,
we force an SDL_Flip() between them, in order to make sure that the
screen is updated. However, this results in the non-YUV video surface
also being updated, even if it otherwise wouldn't be.

Instead, copy the code from all of the other places where we force an
update of the screen, which checks if the surface is dirty, and if not
calls PresentScreen() directly without updating the VideoSurface. (After
checking we're in the correct thread, of course...)

This hopefully fixes video artefacts in Alpha Centauri (bug #279)
---
 src/SDL12_compat.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index f7d96439e..968cc6318 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -7474,6 +7474,7 @@ SDL_DisplayYUVOverlay(SDL12_Overlay *overlay12, SDL12_Rect *dstrect12)
     QueuedOverlayItem *overlay;
     SDL12_YUVData *hwdata;
     SDL_Renderer *renderer = NULL;
+    const SDL_bool ThisIsSetVideoModeThread = (SDL20_ThreadID() == SetVideoModeThread) ? SDL_TRUE : SDL_FALSE;
 
     if (!overlay12) {
         return SDL20_InvalidParamError("overlay");
@@ -7485,7 +7486,15 @@ SDL_DisplayYUVOverlay(SDL12_Overlay *overlay12, SDL12_Rect *dstrect12)
 
     for (overlay = QueuedDisplayOverlays.next; overlay != NULL; overlay = overlay->next) {
         if (overlay->overlay12 == overlay12) {   /* trying to draw the same overlay twice in one frame? Dump the current surface and overlays to the screen now. */
-            SDL_Flip(VideoSurface12);
+            /* Force an update of the screen. */
+            if (ThisIsSetVideoModeThread) {
+                if (VideoSurfaceUpdatedInBackgroundThread) {
+                    SDL_Flip(VideoSurface12);  /* this will update the texture and present. */
+                } else if (VideoSurfacePresentTicks) {
+                    PresentScreen();
+                }
+            }
+
             break;
         }
     }