api: Added SDL_CreateSurfaceNoInit().

Fixes #15715.
This commit is contained in:
Ryan C. Gordon 2026-06-02 10:04:31 -04:00
parent cadd67007c
commit 31f97a1b65
No known key found for this signature in database
GPG key ID: FA148B892AB48044
6 changed files with 47 additions and 9 deletions

View file

@ -167,11 +167,34 @@ typedef struct SDL_Surface SDL_Surface;
*
* \since This function is available since SDL 3.2.0.
*
* \sa SDL_CreateSurfaceNoInit
* \sa SDL_CreateSurfaceFrom
* \sa SDL_DestroySurface
*/
extern SDL_DECLSPEC SDL_Surface * SDLCALL SDL_CreateSurface(int width, int height, SDL_PixelFormat format);
/**
* Allocate a new uninitialized surface with a specific pixel format.
*
* The pixels of the new surface are not initialized at all; the caller should
* plan to set the contents of the whole surface.
*
* \param width the width of the surface.
* \param height the height of the surface.
* \param format the SDL_PixelFormat for the new surface's pixel format.
* \returns the new SDL_Surface structure that is created or NULL on failure;
* call SDL_GetError() for more information.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.6.0.
*
* \sa SDL_CreateSurface
* \sa SDL_CreateSurfaceFrom
* \sa SDL_DestroySurface
*/
extern SDL_DECLSPEC SDL_Surface * SDLCALL SDL_CreateSurfaceNoInit(int width, int height, SDL_PixelFormat format);
/**
* Allocate a new surface with a specific pixel format and existing pixel
* data.

View file

@ -1290,3 +1290,4 @@ _SDL_LoadJPG
_SDL_HasSVE2
_SDL_GamepadHasCapSense
_SDL_GetGamepadCapSense
_SDL_CreateSurfaceNoInit

View file

@ -1291,6 +1291,7 @@ SDL3_0.0.0 {
SDL_HasSVE2;
SDL_GamepadHasCapSense;
SDL_GetGamepadCapSense;
SDL_CreateSurfaceNoInit;
# extra symbols go here (don't modify this line)
local: *;
};

View file

@ -1317,3 +1317,4 @@
#define SDL_HasSVE2 SDL_HasSVE2_REAL
#define SDL_GamepadHasCapSense SDL_GamepadHasCapSense_REAL
#define SDL_GetGamepadCapSense SDL_GetGamepadCapSense_REAL
#define SDL_CreateSurfaceNoInit SDL_CreateSurfaceNoInit_REAL

View file

@ -1325,3 +1325,4 @@ SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadJPG,(const char *a),(a),return)
SDL_DYNAPI_PROC(bool,SDL_HasSVE2,(void),(),return)
SDL_DYNAPI_PROC(bool,SDL_GamepadHasCapSense,(SDL_Gamepad *a,SDL_GamepadCapSenseType b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_GetGamepadCapSense,(SDL_Gamepad *a,SDL_GamepadCapSenseType b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateSurfaceNoInit,(int a,int b,SDL_PixelFormat c),(a,b,c),return)

View file

@ -192,11 +192,8 @@ 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 *CreateSurfaceNoInit(int width, int height, SDL_PixelFormat format, size_t *size)
{
size_t pitch, size;
SDL_Surface *surface;
CHECK_PARAM(width < 0) {
SDL_InvalidParamError("width");
return NULL;
@ -212,13 +209,14 @@ SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
return NULL;
}
if (!SDL_CalculateSurfaceSize(format, width, height, &size, &pitch, false /* not minimal pitch */)) {
size_t pitch;
if (!SDL_CalculateSurfaceSize(format, width, height, size, &pitch, false /* not minimal pitch */)) {
// Overflow...
return NULL;
}
// Allocate and initialize the surface
surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
SDL_Surface *surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
if (!surface) {
return NULL;
}
@ -230,17 +228,30 @@ SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
if (surface->w && surface->h && format != SDL_PIXELFORMAT_MJPG) {
surface->flags &= ~SDL_SURFACE_PREALLOCATED;
if (SDL_GetHintBoolean("SDL_SURFACE_MALLOC", false)) {
surface->pixels = SDL_malloc(size);
surface->pixels = SDL_malloc(*size);
} else {
surface->flags |= SDL_SURFACE_SIMD_ALIGNED;
surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size);
surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), *size);
}
if (!surface->pixels) {
SDL_DestroySurface(surface);
return NULL;
}
}
return surface;
}
// This is important for bitmaps
SDL_Surface *SDL_CreateSurfaceNoInit(int width, int height, SDL_PixelFormat format)
{
size_t size;
return CreateSurfaceNoInit(width, height, format, &size);
}
SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
{
size_t size = 0;
SDL_Surface *surface = CreateSurfaceNoInit(width, height, format, &size);
if (surface && surface->pixels) {
SDL_memset(surface->pixels, 0, size);
}
return surface;