SDL: Added SDL_CreateSurfaceUninitialized()

From fa2a726cc3d6a7ef40dff3a722d021a96739487a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 5 Jun 2026 07:46:31 -0700
Subject: [PATCH] Added SDL_CreateSurfaceUninitialized()

Currently this is an internal API, but we can expose it in the future.
---
 src/camera/SDL_camera.c                       |  4 +-
 src/events/SDL_mouse.c                        |  5 +--
 src/render/SDL_render.c                       |  4 +-
 src/render/SDL_yuv_sw.c                       |  2 +-
 src/render/gpu/SDL_render_gpu.c               |  2 +-
 src/render/metal/SDL_render_metal.m           |  2 +-
 src/render/opengl/SDL_render_gl.c             |  2 +-
 src/render/opengles/SDL_render_gles.c         |  2 +-
 src/render/opengles2/SDL_render_gles2.c       |  2 +-
 src/render/software/SDL_render_sw.c           |  8 ++--
 src/render/software/SDL_triangle.c            |  2 +-
 src/render/vitagxm/SDL_render_vita_gxm.c      |  2 +-
 src/video/SDL_bmp.c                           |  2 +-
 src/video/SDL_rotate.c                        |  4 +-
 src/video/SDL_stb.c                           |  2 +-
 src/video/SDL_stretch.c                       |  2 +-
 src/video/SDL_surface.c                       | 41 +++++++++++++------
 src/video/SDL_surface_c.h                     |  2 +
 src/video/android/SDL_androidmouse.c          |  3 +-
 src/video/dos/SDL_dosframebuffer.c            |  6 +--
 src/video/dummy/SDL_nullframebuffer.c         |  2 +-
 .../emscripten/SDL_emscriptenframebuffer.c    |  2 +-
 src/video/n3ds/SDL_n3dsframebuffer.c          |  2 +-
 .../offscreen/SDL_offscreenframebuffer.c      |  2 +-
 src/video/windows/SDL_windowsmouse.c          |  2 +-
 src/video/windows/SDL_windowsshape.c          |  2 +-
 src/video/x11/SDL_x11shape.c                  |  2 +-
 27 files changed, 65 insertions(+), 48 deletions(-)

diff --git a/src/camera/SDL_camera.c b/src/camera/SDL_camera.c
index 323be59aa9ae7..5879274b48ad6 100644
--- a/src/camera/SDL_camera.c
+++ b/src/camera/SDL_camera.c
@@ -1028,7 +1028,7 @@ bool SDL_PrepareCameraSurfaces(SDL_Camera *device)
         const bool downscaling_first = (device->needs_scaling < 0);
         const SDL_CameraSpec *s = downscaling_first ? appspec : devspec;
         const SDL_PixelFormat fmt = downscaling_first ? devspec->format : appspec->format;
-        device->conversion_surface = SDL_CreateSurface(s->width, s->height, fmt);
+        device->conversion_surface = SDL_CreateSurfaceUninitialized(s->width, s->height, fmt);
         if (!device->conversion_surface) {
             goto failed;
         }
@@ -1047,7 +1047,7 @@ bool SDL_PrepareCameraSurfaces(SDL_Camera *device)
     for (int i = 0; i < SDL_arraysize(device->output_surfaces); i++) {
         SDL_Surface *surf;
         if (device->needs_scaling || device->needs_conversion) {
-            surf = SDL_CreateSurface(appspec->width, appspec->height, appspec->format);
+            surf = SDL_CreateSurfaceUninitialized(appspec->width, appspec->height, appspec->format);
         } else {
             surf = SDL_CreateSurfaceFrom(appspec->width, appspec->height, appspec->format, NULL, 0);
         }
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index eaf1f2cdfdfb0..db622399c17ad 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -299,9 +299,8 @@ void SDL_PostInitMouse(void)
      * so that mouse grab and focus functionality will work.
      */
     if (!mouse->def_cursor) {
-        SDL_Surface *surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
+        SDL_Surface *surface = SDL_CreateSurfaceZeroed(1, 1, SDL_PIXELFORMAT_ARGB8888);
         if (surface) {
-            SDL_memset(surface->pixels, 0, (size_t)surface->h * surface->pitch);
             SDL_SetDefaultCursor(SDL_CreateColorCursor(surface, 0, 0));
             SDL_DestroySurface(surface);
         }
@@ -1527,7 +1526,7 @@ SDL_Cursor *SDL_CreateCursor(const Uint8 *data, const Uint8 *mask, int w, int h,
     w = ((w + 7) & ~7);
 
     // Create the surface from a bitmap
-    surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB8888);
+    surface = SDL_CreateSurfaceUninitialized(w, h, SDL_PIXELFORMAT_ARGB8888);
     if (!surface) {
         return NULL;
     }
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index da1cf72040392..c11c29e71d306 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -1651,7 +1651,7 @@ SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_Propert
                 return NULL;
             }
         } else if (SDL_ISPIXELFORMAT_INDEXED(texture->format)) {
-            texture->palette_surface = SDL_CreateSurface(w, h, texture->format);
+            texture->palette_surface = SDL_CreateSurfaceZeroed(w, h, texture->format);
             if (!texture->palette_surface) {
                 SDL_DestroyTexture(texture);
                 return NULL;
@@ -5956,7 +5956,7 @@ static bool CreateDebugTextAtlas(SDL_Renderer *renderer)
 
     // actually make each glyph two pixels taller/wider, to prevent scaling artifacts.
     const int rows = (SDL_DEBUG_FONT_NUM_GLYPHS / SDL_DEBUG_FONT_GLYPHS_PER_ROW) + 1;
-    SDL_Surface *atlas = SDL_CreateSurface((charWidth + 2) * SDL_DEBUG_FONT_GLYPHS_PER_ROW, rows * (charHeight + 2), SDL_PIXELFORMAT_INDEX8);
+    SDL_Surface *atlas = SDL_CreateSurfaceZeroed((charWidth + 2) * SDL_DEBUG_FONT_GLYPHS_PER_ROW, rows * (charHeight + 2), SDL_PIXELFORMAT_INDEX8);
     if (!atlas) {
         return false;
     }
diff --git a/src/render/SDL_yuv_sw.c b/src/render/SDL_yuv_sw.c
index a837870fe1506..4c4b7dc987345 100644
--- a/src/render/SDL_yuv_sw.c
+++ b/src/render/SDL_yuv_sw.c
@@ -372,7 +372,7 @@ bool SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect, SDL
             swdata->target_format = target_format;
         }
         if (!swdata->stretch) {
-            swdata->stretch = SDL_CreateSurface(swdata->w, swdata->h, target_format);
+            swdata->stretch = SDL_CreateSurfaceUninitialized(swdata->w, swdata->h, target_format);
             if (!swdata->stretch) {
                 return false;
             }
diff --git a/src/render/gpu/SDL_render_gpu.c b/src/render/gpu/SDL_render_gpu.c
index 0884d686ca115..a2e5035d0c2fb 100644
--- a/src/render/gpu/SDL_render_gpu.c
+++ b/src/render/gpu/SDL_render_gpu.c
@@ -1397,7 +1397,7 @@ static SDL_Surface *GPU_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect
         return NULL;
     }
 
-    SDL_Surface *surface = SDL_CreateSurface(rect->w, rect->h, pixfmt);
+    SDL_Surface *surface = SDL_CreateSurfaceUninitialized(rect->w, rect->h, pixfmt);
 
     if (!surface) {
         return NULL;
diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m
index dad59111391d6..9488b4bca7d2c 100644
--- a/src/render/metal/SDL_render_metal.m
+++ b/src/render/metal/SDL_render_metal.m
@@ -2063,7 +2063,7 @@ static bool METAL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd
             SDL_SetError("Unknown framebuffer pixel format");
             return NULL;
         }
-        surface = SDL_CreateSurface(read_rect.w, read_rect.h, format);
+        surface = SDL_CreateSurfaceUninitialized(read_rect.w, read_rect.h, format);
         if (surface) {
             [mtltexture getBytes:surface->pixels bytesPerRow:surface->pitch fromRegion:mtlregion mipmapLevel:0];
             if (SDL_RenderingLinearSpace(renderer) &&
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index 64402dae8514c..f423367080a96 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -1630,7 +1630,7 @@ static SDL_Surface *GL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *
         return NULL;
     }
 
-    surface = SDL_CreateSurface(rect->w, rect->h, format);
+    surface = SDL_CreateSurfaceUninitialized(rect->w, rect->h, format);
     if (!surface) {
         return NULL;
     }
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index 800991face1b5..5bc6fa3caf40c 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -958,7 +958,7 @@ static SDL_Surface *GLES_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect
     SDL_PixelFormat format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_RGBA32;
     SDL_Surface *surface;
 
-    surface = SDL_CreateSurface(rect->w, rect->h, format);
+    surface = SDL_CreateSurfaceUninitialized(rect->w, rect->h, format);
     if (!surface) {
         return NULL;
     }
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index a37f2b1b74174..01e642709c055 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -2235,7 +2235,7 @@ static SDL_Surface *GLES2_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rec
     SDL_PixelFormat format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_RGBA32;
     SDL_Surface *surface;
 
-    surface = SDL_CreateSurface(rect->w, rect->h, format);
+    surface = SDL_CreateSurfaceUninitialized(rect->w, rect->h, format);
     if (!surface) {
         return NULL;
     }
diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c
index ce3a3f5165b6e..a36da46d672ee 100644
--- a/src/render/software/SDL_render_sw.c
+++ b/src/render/software/SDL_render_sw.c
@@ -135,7 +135,7 @@ static bool SW_ChangeTexturePalette(SDL_Renderer *renderer, SDL_Texture *texture
 
 static bool SW_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
 {
-    SDL_Surface *surface = SDL_CreateSurface(texture->w, texture->h, texture->format);
+    SDL_Surface *surface = SDL_CreateSurfaceUninitialized(texture->w, texture->h, texture->format);
     if (!surface) {
         return SDL_SetError("Can't create surface");
     }
@@ -438,7 +438,7 @@ static bool SW_RenderCopyEx(SDL_Renderer *renderer, SDL_Surface *surface, SDL_Te
      * to clear the pixels in the destination surface. The other steps are explained below.
      */
     if (blendmode == SDL_BLENDMODE_NONE && !isOpaque) {
-        mask = SDL_CreateSurface(final_rect->w, final_rect->h, SDL_PIXELFORMAT_ARGB8888);
+        mask = SDL_CreateSurfaceZeroed(final_rect->w, final_rect->h, SDL_PIXELFORMAT_ARGB8888);
         if (!mask) {
             result = false;
         } else {
@@ -451,7 +451,7 @@ static bool SW_RenderCopyEx(SDL_Renderer *renderer, SDL_Surface *surface, SDL_Te
      */
     if (result && (blitRequired || applyModulation)) {
         SDL_Rect scale_rect = tmp_rect;
-        src_scaled = SDL_CreateSurface(final_rect->w, final_rect->h, SDL_PIXELFORMAT_ARGB8888);
+        src_scaled = SDL_CreateSurfaceUninitialized(final_rect->w, final_rect->h, SDL_PIXELFORMAT_ARGB8888);
         if (!src_scaled) {
             result = false;
         } else {
@@ -871,7 +871,7 @@ static bool SW_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, v
                 // Prevent to do scaling + clipping on viewport boundaries as it may lose proportion
                 if (dstrect->x < 0 || dstrect->y < 0 || dstrect->x + dstrect->w > surface->w || dstrect->y + dstrect->h > surface->h) {
                     SDL_PixelFormat tmp_format = SDL_ISPIXELFORMAT_ALPHA(src->format) ? SDL_PIXELFORMAT_ARGB8888 : surface->format;
-                    SDL_Surface *tmp = SDL_CreateSurface(dstrect->w, dstrect->h, tmp_format);
+                    SDL_Surface *tmp = SDL_CreateSurfaceUninitialized(dstrect->w, dstrect->h, tmp_format);
                     // Scale to an intermediate surface, then blit
                     if (tmp) {
                         SDL_Rect r;
diff --git a/src/render/software/SDL_triangle.c b/src/render/software/SDL_triangle.c
index 9017559c908ca..5e87a9d1bf61f 100644
--- a/src/render/software/SDL_triangle.c
+++ b/src/render/software/SDL_triangle.c
@@ -299,7 +299,7 @@ bool SDL_SW_FillTriangle(SDL_Surface *dst, SDL_Point *d0, SDL_Point *d1, SDL_Poi
         }
 
         // Use an intermediate surface
-        tmp = SDL_CreateSurface(dstrect.w, dstrect.h, format);
+        tmp = SDL_CreateSurfaceZeroed(dstrect.w, dstrect.h, format);
         if (!tmp) {
             result = false;
             goto end;
diff --git a/src/render/vitagxm/SDL_render_vita_gxm.c b/src/render/vitagxm/SDL_render_vita_gxm.c
index 7cfbaf18af368..6edcc2ae9a07f 100644
--- a/src/render/vitagxm/SDL_render_vita_gxm.c
+++ b/src/render/vitagxm/SDL_render_vita_gxm.c
@@ -1172,7 +1172,7 @@ static SDL_Surface *VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_
         return NULL;
     }
 
-    surface = SDL_CreateSurface(rect->w, rect->h, format);
+    surface = SDL_CreateSurfaceUninitialized(rect->w, rect->h, format);
     if (!surface) {
         return NULL;
     }
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
index de1db2e986f5a..14e7fb76f11a6 100644
--- a/src/video/SDL_bmp.c
+++ b/src/video/SDL_bmp.c
@@ -432,7 +432,7 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, bool closeio)
 
         // Get the pixel format
         format = SDL_GetPixelFormatForMasks(biBitCount, Rmask, Gmask, Bmask, Amask);
-        surface = SDL_CreateSurface(biWidth, biHeight, format);
+        surface = SDL_CreateSurfaceUninitialized(biWidth, biHeight, format);
 
         if (!surface) {
             goto done;
diff --git a/src/video/SDL_rotate.c b/src/video/SDL_rotate.c
index c5071b525ffef..e17c91da4391a 100644
--- a/src/video/SDL_rotate.c
+++ b/src/video/SDL_rotate.c
@@ -513,13 +513,13 @@ SDL_Surface *SDLgfx_rotateSurface(SDL_Surface *src, double angle, int smooth, in
     rz_dst = NULL;
     if (is8bit) {
         // Target surface is 8 bit
-        rz_dst = SDL_CreateSurface(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format);
+        rz_dst = SDL_CreateSurfaceZeroed(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format);
         if (rz_dst) {
             SDL_SetSurfacePalette(rz_dst, src->palette);
         }
     } else {
         // Target surface is 32 bit with source RGBA ordering
-        rz_dst = SDL_CreateSurface(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format);
+        rz_dst = SDL_CreateSurfaceZeroed(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format);
     }
 
     // Check target
diff --git a/src/video/SDL_stb.c b/src/video/SDL_stb.c
index fe5d0c70e97c0..ef99d3f510e81 100644
--- a/src/video/SDL_stb.c
+++ b/src/video/SDL_stb.c
@@ -330,7 +330,7 @@ static SDL_Surface *SDL_LoadSTB_IO(SDL_IOStream *src)
         }
 
     } else if (format == STBI_grey_alpha) {
-        surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32);
+        surface = SDL_CreateSurfaceUninitialized(w, h, SDL_PIXELFORMAT_RGBA32);
         if (surface) {
             Uint8 *src_ptr = pixels;
             Uint8 *dst = (Uint8 *)surface->pixels;
diff --git a/src/video/SDL_stretch.c b/src/video/SDL_stretch.c
index 073f62c09cb3f..1de329b943a63 100644
--- a/src/video/SDL_stretch.c
+++ b/src/video/SDL_stretch.c
@@ -62,7 +62,7 @@ bool SDL_StretchSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *
         }
 
         SDL_Surface *src_tmp = SDL_ConvertSurface(src, SDL_PIXELFORMAT_XRGB8888);
-        SDL_Surface *dst_tmp = SDL_CreateSurface(dstrect->w, dstrect->h, SDL_PIXELFORMAT_XRGB8888);
+        SDL_Surface *dst_tmp = SDL_CreateSurfaceUninitialized(dstrect->w, dstrect->h, SDL_PIXELFORMAT_XRGB8888);
         if (src_tmp && dst_tmp) {
             result = SDL_StretchSurface(src_tmp, srcrect, dst_tmp, NULL, scaleMode);
             if (result) {
diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index d38d9a48ce4c1..03e435c191860 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -192,7 +192,7 @@ static bool SDL_InitializeSurface(SDL_Surface *surface, int width, int height, S
 /*
  * Create an empty surface of the appropriate depth using the given format
  */
-SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
+static SDL_Surface *SDL_CreateSurfaceInternal(int width, int height, SDL_PixelFormat format, bool clear_surface)
 {
     size_t pitch, size;
     SDL_Surface *surface;
@@ -240,12 +240,29 @@ SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
             return NULL;
         }
 
-        // This is important for bitmaps
-        SDL_memset(surface->pixels, 0, size);
+        if (clear_surface) {
+            SDL_memset(surface->pixels, 0, size);
+        }
     }
     return surface;
 }
 
+SDL_Surface *SDL_CreateSurfaceZeroed(int width, int height, SDL_PixelFormat format)
+{
+    return SDL_CreateSurfaceInternal(width, height, format, true);
+}
+
+SDL_Surface *SDL_CreateSurfaceUninitialized(int width, int height, SDL_PixelFormat format)
+{
+    return SDL_CreateSurfaceInternal(width, height, format, false);
+}
+
+SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
+{
+    return SDL_CreateSurfaceZeroed(width, height, format);
+}
+
+
 /*
  * Create an RGB surface from an existing memory buffer using the given
  * enum SDL_PIXELFORMAT_* format
@@ -1336,7 +1353,7 @@ bool SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect, S
             // Intermediate scaling
             if (is_complex_copy_flags || src->format != dst->format) {
                 SDL_Rect tmprect;
-                SDL_Surface *tmp2 = SDL_CreateSurface(dstrect->w, dstrect->h, src->format);
+                SDL_Surface *tmp2 = SDL_CreateSurfaceUninitialized(dstrect->w, dstrect->h, src->format);
                 SDL_StretchSurface(src, &srcrect2, tmp2, NULL, SDL_SCALEMODE_LINEAR);
 
                 SDL_SetSurfaceColorMod(tmp2, r, g, b);
@@ -1927,7 +1944,7 @@ static SDL_Surface *SDL_ConvertSurfaceRectAndColorspace(SDL_Surface *surface, co
 
     // Create a new surface with the desired format
     if (surface->pixels || SDL_MUSTLOCK(surface)) {
-        convert = SDL_CreateSurface(rect->w, rect->h, format);
+        convert = SDL_CreateSurfaceUninitialized(rect->w, rect->h, format);
     } else {
         convert = SDL_CreateSurfaceFrom(rect->w, rect->h, format, NULL, 0);
     }
@@ -2089,7 +2106,7 @@ static SDL_Surface *SDL_ConvertSurfaceRectAndColorspace(SDL_Surface *surface, co
             int converted_colorkey = 0;
 
             // Create a dummy surface to get the colorkey converted
-            tmp = SDL_CreateSurface(1, 1, surface->format);
+            tmp = SDL_CreateSurfaceUninitialized(1, 1, surface->format);
             if (!tmp) {
                 goto error;
             }
@@ -2265,7 +2282,7 @@ SDL_Surface *SDL_ScaleSurface(SDL_Surface *surface, int width, int height, SDL_S
 
     // Create a new surface with the desired size
     if (surface->pixels || SDL_MUSTLOCK(surface)) {
-        convert = SDL_CreateSurface(width, height, surface->format);
+        convert = SDL_CreateSurfaceUninitialized(width, height, surface->format);
     } else {
         convert = SDL_CreateSurfaceFrom(width, height, surface->format, NULL, 0);
     }
@@ -2342,7 +2359,7 @@ SDL_Surface *SDL_DuplicatePixels(int width, int height, SDL_PixelFormat format,
 {
     SDL_Surface *surface;
     if (pixels) {
-        surface = SDL_CreateSurface(width, height, format);
+        surface = SDL_CreateSurfaceUninitialized(width, height, format);
     } else {
         surface = SDL_CreateSurfaceFrom(width, height, format, NULL, 0);
     }
@@ -2607,7 +2624,7 @@ static bool SDL_PremultiplyAlphaPixelsAndColorspace(int width, int height, SDL_P
     }
 
     if (src_format != format || src_colorspace != colorspace) {
-        convert = SDL_CreateSurface(width, height, format);
+        convert = SDL_CreateSurfaceUninitialized(width, height, format);
         if (!convert) {
             goto done;
         }
@@ -2621,7 +2638,7 @@ static bool SDL_PremultiplyAlphaPixelsAndColorspace(int width, int height, SDL_P
         dst_pitch = convert->pitch;
 
     } else if (dst_format != format || dst_colorspace != colorspace) {
-        convert = SDL_CreateSurface(width, height, format);
+        convert = SDL_CreateSurfaceUninitialized(width, height, format);
         if (!convert) {
             goto done;
         }
@@ -2708,7 +2725,7 @@ bool SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a)
         result = SDL_FillSurfaceRect(surface, NULL, color);
     } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
         // We can't directly set an RGB value on a YUV surface
-        SDL_Surface *tmp = SDL_CreateSurface(surface->w, surface->h, SDL_PIXELFORMAT_ARGB8888);
+        SDL_Surface *tmp = SDL_CreateSurfaceUninitialized(surface->w, surface->h, SDL_PIXELFORMAT_ARGB8888);
         if (!tmp) {
             goto done;
         }
@@ -2719,7 +2736,7 @@ bool SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a)
         SDL_DestroySurface(tmp);
     } else {
         // Take advantage of blit color conversion
-        SDL_Surface *tmp = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA128_FLOAT);
+        SDL_Surface *tmp = SDL_CreateSurfaceUninitialized(1, 1, SDL_PIXELFORMAT_RGBA128_FLOAT);
         if (!tmp) {
             goto done;
         }
diff --git a/src/video/SDL_surface_c.h b/src/video/SDL_surface_c.h
index 231cc7430b69c..5fd5ba94ba890 100644
--- a/src/video/SDL_surface_c.h
+++ b/src/video/SDL_surface_c.h
@@ -91,6 +91,8 @@ extern float SDL_GetDefaultSDRWhitePoint(SDL_Colorspace colorspace);
 extern float SDL_GetSurfaceSDRWhitePoint(SDL_Surface *surface, SDL_Colorspace colorspace);
 extern float SDL_GetDefaultHDRHeadroom(SDL_Colorspace colorspace);
 extern float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace);
+extern SDL_Surface *SDL_CreateSurfaceZeroed(int width, int height, SDL_PixelFormat format);
+extern SDL_Surface *SDL_CreateSurfaceUninitialized(int width, int height, SDL_PixelFormat format);
 extern SDL_Surface *SDL_GetSurfaceImage(SDL_Surface *surface, float display_scale);
 extern SDL_Surface *SDL_ConvertSurfaceRect(SDL_Surface *surface, const SDL_Rect *rect, SDL_PixelFormat format);
 extern bool SDL_IsBMP(SDL_IOStream *src);
diff --git a/src/video/android/SDL_androidmouse.c b/src/video/android/SDL_androidmouse.c
index be2217bf785cc..73898979d8592 100644
--- a/src/video/android/SDL_androidmouse.c
+++ b/src/video/android/SDL_androidmouse.c
@@ -115,9 +115,8 @@ static void Android_FreeCursor(SDL_Cursor *cursor)
 static SDL_Cursor *Android_CreateEmptyCursor(void)
 {
     if (!empty_cursor) {
-        SDL_Surface *empty_surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
+        SDL_Surface *empty_surface = SDL_CreateSurfaceZeroed(1, 1, SDL_PIXELFORMAT_ARGB8888);
         if (empty_surface) {
-            SDL_memset(empty_surface->pixels, 0, (size_t)empty_surface->h * empty_surface->pitch);
             empty_cursor = Android_CreateCursor(empty_surface, 0, 0);
             SDL_DestroySurface(empty_surface);
         }
diff --git a/src/video/dos/SDL_dosframebuffer.c b/src/video/dos/SDL_dosframebuffer.c
index 85d6992d933a4..a81f76a74b17e 100644
--- a/src/video/dos/SDL_dosframebuffer.c
+++ b/src/video/dos/SDL_dosframebuffer.c
@@ -92,7 +92,7 @@ static SDL_Surface *GetConvertedCursorSurface(SDL_CursorData *curdata, SDL_Surfa
 
     int w = src->w;
     int h = src->h;
-    SDL_Surface *conv = SDL_CreateSurface(w, h, dst->format);
+    SDL_Surface *conv = SDL_CreateSurfaceUninitialized(w, h, dst->format);
     if (!conv) {
         return NULL;
     }
@@ -168,7 +168,7 @@ void DOSVESA_InvalidateCachedFramebuffer(void)
 // Create a system-RAM surface (with a blank palette if INDEX8 and update the VGA DAC).
 static SDL_Surface *CreateSystemSurface(SDL_VideoData *data, int w, int h, SDL_PixelFormat surface_format)
 {
-    SDL_Surface *surface = SDL_CreateSurface(w, h, surface_format);
+    SDL_Surface *surface = SDL_CreateSurfaceZeroed(w, h, surface_format);
     if (!surface) {
         return NULL;
     }
@@ -758,7 +758,7 @@ bool DOSVESA_UpdateWindowFramebuffer(SDL_VideoDevice *device, SDL_Window *window
                     have_cursor_rect = true;
 
                     // Save the pixels under the cursor so we can restore them after the copy.
-                    cursor_save = SDL_CreateSurface(cursor_clipped.w, cursor_clipped.h, src->format);
+                    cursor_save = SDL_CreateSurfaceUninitialized(cursor_clipped.w, cursor_clipped.h, src->format);
                     if (cursor_save) {
                         if (src->format == SDL_PIXELFORMAT_INDEX8) {
                             SDL_Palette *sp = SDL_GetSurfacePalette(src);
diff --git a/src/video/dummy/SDL_nullframebuffer.c b/src/video/dummy/SDL_nullframebuffer.c
index 6f834586f524d..93ef412077a8a 100644
--- a/src/video/dummy/SDL_nullframebuffer.c
+++ b/src/video/dummy/SDL_nullframebuffer.c
@@ -37,7 +37,7 @@ bool SDL_DUMMY_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *windo
 
     // Create a new framebuffer
     SDL_GetWindowSizeInPixels(window, &w, &h);
-    surface = SDL_CreateSurface(w, h, surface_format);
+    surface = SDL_CreateSurfaceZeroed(w, h, surface_format);
     if (!surface) {
         return false;
     }
diff --git a/src/video/emscripten/SDL_emscriptenframebuffer.c b/src/video/emscripten/SDL_emscriptenframebuffer.c
index 87e00bbeb3a68..465a3c4cf9262 100644
--- a/src/video/emscripten/SDL_emscriptenframebuffer.c
+++ b/src/video/emscripten/SDL_emscriptenframebuffer.c
@@ -41,7 +41,7 @@ bool Emscripten_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *wind
     // Create a new one
     SDL_GetWindowSizeInPixels(window, &w, &h);
 
-    surface = SDL_CreateSurface(w, h, surface_format);
+    surface = SDL_CreateSurfaceZeroed(w, h, surface_format);
     if (!surface) {
         return false;
     }
diff --git a/src/video/n3ds/SDL_n3dsframebuffer.c b/src/video/n3ds/SDL_n3dsframebuffer.c
index 41813a1a260a8..20c1e7108694a 100644
--- a/src/video/n3ds/SDL_n3dsframebuffer.c
+++ b/src/video/n3ds/SDL_n3dsframebuffer.c
@@ -52,7 +52,7 @@ bool SDL_N3DS_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window
 
     mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(window));
     SDL_GetWindowSizeInPixels(window, &w, &h);
-    framebuffer = SDL_CreateSurface(w, h, mode->format);
+    framebuffer = SDL_CreateSurfaceZeroed(w, h, mode->format);
 
     if (!framebuffer) {
         return false;
diff --git a/src/video/offscreen/SDL_offscreenframebuffer.c b/src/video/offscreen/SDL_offscreenframebuffer.c
index 2143b3e11d49b..db4c5c177dd0d 100644
--- a/src/video/offscreen/SDL_offscreenframebuffer.c
+++ b/src/video/offscreen/SDL_offscreenframebuffer.c
@@ -37,7 +37,7 @@ bool SDL_OFFSCREEN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *w
 
     // Create a new framebuffer
     SDL_GetWindowSizeInPixels(window, &w, &h);
-    surface = SDL_CreateSurface(w, h, surface_format);
+    surface = SDL_CreateSurfaceZeroed(w, h, surface_format);
     if (!surface) {
         return false;
     }
diff --git a/src/video/windows/SDL_windowsmouse.c b/src/video/windows/SDL_windowsmouse.c
index 5bf634b7c2ea6..09211b5592de7 100644
--- a/src/video/windows/SDL_windowsmouse.c
+++ b/src/video/windows/SDL_windowsmouse.c
@@ -470,7 +470,7 @@ static SDL_Cursor *WIN_CreateAnimatedCursor(SDL_CursorFrameInfo *frames, int fra
 static SDL_Cursor *WIN_CreateBlankCursor(void)
 {
     SDL_Cursor *cursor = NULL;
-    SDL_Surface *surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_ARGB8888);
+    SDL_Surface *surface = SDL_CreateSurfaceZeroed(32, 32, SDL_PIXELFORMAT_ARGB8888);
     if (surface) {
         cursor = WIN_CreateCursor(surface, 0, 0);
         SDL_DestroySurface(surface);
diff --git a/src/video/windows/SDL_windowsshape.c b/src/video/windows/SDL_windowsshape.c
index 4e4d71f453cb3..f2f0609409cdb 100644
--- a/src/video/windows/SDL_windowsshape.c
+++ b/src/video/windows/SDL_windowsshape.c
@@ -78,7 +78,7 @@ bool WIN_UpdateWindowShape(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surfa
         RECT rect;
 
         if (shape->w != window->w || shape->h != window->h) {
-            stretched = SDL_CreateSurface(window->w, window->h, SDL_PIXELFORMAT_ARGB32);
+            stretched = SDL_CreateSurfaceUninitialized(window->w, window->h, SDL_PIXELFORMAT_ARGB32);
             if (!stretched) {
                 return false;
             }
diff --git a/src/video/x11/SDL_x11shape.c b/src/video/x11/SDL_x11shape.c
index a8fefab2f06ed..421b061d596df 100644
--- a/src/video/x11/SDL_x11shape.c
+++ b/src/video/x11/SDL_x11shape.c
@@ -67,7 +67,7 @@ bool X11_UpdateWindowShape(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surfa
         Pixmap pixmap;
 
         if (shape->w != window->w || shape->h != window->h) {
-            stretched = SDL_CreateSurface(window->w, window->h, SDL_PIXELFORMAT_ARGB32);
+            stretched = SDL_CreateSurfaceUninitialized(window->w, window->h, SDL_PIXELFORMAT_ARGB32);
             if (!stretched) {
                 return false;
             }