sdl12-compat: Try to deal with surface creations with wonky color masks.

From f44f295a0a7ede567dee9610127d069ccf281720 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 18 Jun 2021 10:18:24 -0400
Subject: [PATCH] Try to deal with surface creations with wonky color masks.

This happens to fix Bug #46 by virtue of how this is implemented, and also
causes an intentional bug in an SDL_perl unit test to pass as expected...
it corrects _bug_ compatibility for specific known issues. It's likely not a
perfect solution, but it's only intended to work around apps with bugs that
SDL 1.2 tolerated.
---
 src/SDL12_compat.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 54ab6c7..7c7fc09 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -3290,6 +3290,21 @@ SDL_CreateRGBSurface(Uint32 flags12, int width, int height, int depth, Uint32 Rm
         surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
     }
 
+    /* SDL 1.2 would make a surface from almost any masks, even if it doesn't
+       make sense; specifically, it will make a surface if a color mask is
+       bogus. Sometimes this even worked because it would eventually land in
+       a generic blitter that just copied data blindly. SDL2 wants more strict
+       pixel formats, so try to detect this case and try again with a standard
+       format. */
+    if ((surface20 == NULL) && (depth >= 24) && (SDL20_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask) == SDL_PIXELFORMAT_UNKNOWN)) {
+        /* I have no illusions this is correct, it just works for the known problem cases so far. */
+        Rmask = SDL_SwapLE32(0x000000FF);
+        Gmask = SDL_SwapLE32(0x0000FF00);
+        Bmask = SDL_SwapLE32(0x00FF0000);
+        Amask = SDL_SwapLE32(Amask ? 0xFF000000 : 0x00000000);
+        surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
+    }
+
     surface12 = Surface20to12(surface20);
     if (!surface12) {
         SDL20_FreeSurface(surface20);