From 3c04be4486d6c9572e9ffe8ce454f7e012b44f32 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 2 Jul 2023 18:45:11 -0700
Subject: [PATCH] The rectangles passed to SDL_BlitSurfaceUnchecked() and
SDL_BlitSurfaceUncheckedScaled() are const.
The destination rectangle passed to SDL_BlitSurface() and SDL_BlitSurfaceScaled() is non-const and filled in with the final destination rectangle after clipping, and now documented as such.
Fixes https://github.com/libsdl-org/SDL/issues/7911
---
include/SDL3/SDL_surface.h | 40 ++++++++++++++++++-----------------
src/dynapi/SDL_dynapi_procs.h | 4 ++--
src/render/SDL_sysrender.h | 2 +-
src/video/SDL_RLEaccel.c | 12 +++++------
src/video/SDL_blit.c | 4 ++--
src/video/SDL_surface.c | 12 +++++------
6 files changed, 38 insertions(+), 36 deletions(-)
diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h
index 6e0f120600c3..574607f35cc7 100644
--- a/include/SDL3/SDL_surface.h
+++ b/include/SDL3/SDL_surface.h
@@ -99,8 +99,8 @@ typedef struct SDL_Surface
/**
* \brief The type of function used for surface blitting functions.
*/
-typedef int (SDLCALL *SDL_blit) (struct SDL_Surface *src, SDL_Rect *srcrect,
- struct SDL_Surface *dst, SDL_Rect *dstrect);
+typedef int (SDLCALL *SDL_blit) (struct SDL_Surface *src, const SDL_Rect *srcrect,
+ struct SDL_Surface *dst, const SDL_Rect *dstrect);
/**
* \brief The formula used for converting between YUV and RGB
@@ -773,8 +773,9 @@ extern DECLSPEC int SDLCALL SDL_FillSurfaceRects
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied, or NULL to copy the entire surface
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface, filled with the actual rectangle
+ * used after clipping
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -796,8 +797,8 @@ extern DECLSPEC int SDLCALL SDL_BlitSurface
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied, or NULL to copy the entire surface
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -806,8 +807,8 @@ extern DECLSPEC int SDLCALL SDL_BlitSurface
* \sa SDL_BlitSurface
*/
extern DECLSPEC int SDLCALL SDL_BlitSurfaceUnchecked
- (SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect);
+ (SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect);
/**
@@ -820,8 +821,8 @@ extern DECLSPEC int SDLCALL SDL_BlitSurfaceUnchecked
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -839,8 +840,8 @@ extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface *src,
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -859,8 +860,9 @@ extern DECLSPEC int SDLCALL SDL_SoftStretchLinear(SDL_Surface *src,
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface, filled with the actual rectangle
+ * used after clipping
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -868,7 +870,7 @@ extern DECLSPEC int SDLCALL SDL_SoftStretchLinear(SDL_Surface *src,
*/
extern DECLSPEC int SDLCALL SDL_BlitSurfaceScaled
(SDL_Surface *src, const SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect);
+ SDL_Surface *dst, SDL_Rect *dstrect);
/**
* Perform low-level surface scaled blitting only.
@@ -880,8 +882,8 @@ extern DECLSPEC int SDLCALL SDL_BlitSurfaceScaled
* \param srcrect the SDL_Rect structure representing the rectangle to be
* copied
* \param dst the SDL_Surface structure that is the blit target
- * \param dstrect the SDL_Rect structure representing the rectangle that is
- * copied into
+ * \param dstrect the SDL_Rect structure representing the target rectangle
+ * in the destination surface
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
@@ -890,8 +892,8 @@ extern DECLSPEC int SDLCALL SDL_BlitSurfaceScaled
* \sa SDL_BlitSurfaceScaled
*/
extern DECLSPEC int SDLCALL SDL_BlitSurfaceUncheckedScaled
- (SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect);
+ (SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect);
/**
* Set the YUV conversion mode
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 75d53e820a9b..30e9c9c0796a 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -136,8 +136,8 @@ SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystick,(SDL_JoystickType a, in
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystickEx,(const SDL_VirtualJoystickDesc *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_BlitSurface,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_BlitSurfaceScaled,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
-SDL_DYNAPI_PROC(int,SDL_BlitSurfaceUnchecked,(SDL_Surface *a, SDL_Rect *b, SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
-SDL_DYNAPI_PROC(int,SDL_BlitSurfaceUncheckedScaled,(SDL_Surface *a, SDL_Rect *b, SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
+SDL_DYNAPI_PROC(int,SDL_BlitSurfaceUnchecked,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, const SDL_Rect *d),(a,b,c,d),return)
+SDL_DYNAPI_PROC(int,SDL_BlitSurfaceUncheckedScaled,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, const SDL_Rect *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_BroadcastCondition,(SDL_Condition *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_CaptureMouse,(SDL_bool a),(a),return)
SDL_DYNAPI_PROC(void,SDL_CleanupTLS,(void),(),)
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 5ecd44b9c93d..c7033f8f26cf 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -307,7 +307,7 @@ extern SDL_BlendOperation SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode
the next call, because it might be in an array that gets realloc()'d. */
extern void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset);
-extern int SDL_PrivateBlitSurfaceUncheckedScaled(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, SDL_ScaleMode scaleMode);
+extern int SDL_PrivateBlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect, SDL_ScaleMode scaleMode);
extern int SDL_PrivateBlitSurfaceScaled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, SDL_ScaleMode scaleMode);
/* Ends C function definitions when using C++ */
diff --git a/src/video/SDL_RLEaccel.c b/src/video/SDL_RLEaccel.c
index 5b2d47114b2b..5c478a58ddff 100644
--- a/src/video/SDL_RLEaccel.c
+++ b/src/video/SDL_RLEaccel.c
@@ -432,7 +432,7 @@
} while (0)
static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
- Uint8 *dstbuf, SDL_Rect *srcrect, unsigned alpha)
+ Uint8 *dstbuf, const SDL_Rect *srcrect, unsigned alpha)
{
SDL_PixelFormat *fmt = surf_dst->format;
@@ -442,8 +442,8 @@ static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
#undef RLECLIPBLIT
/* blit a colorkeyed RLE surface */
-static int SDLCALL SDL_RLEBlit(SDL_Surface *surf_src, SDL_Rect *srcrect,
- SDL_Surface *surf_dst, SDL_Rect *dstrect)
+static int SDLCALL SDL_RLEBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
+ SDL_Surface *surf_dst, const SDL_Rect *dstrect)
{
Uint8 *dstbuf;
Uint8 *srcbuf;
@@ -626,7 +626,7 @@ typedef struct
/* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
- Uint8 *dstbuf, SDL_Rect *srcrect)
+ Uint8 *dstbuf, const SDL_Rect *srcrect)
{
SDL_PixelFormat *df = surf_dst->format;
/*
@@ -717,8 +717,8 @@ static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
}
/* blit a pixel-alpha RLE surface */
-static int SDLCALL SDL_RLEAlphaBlit(SDL_Surface *surf_src, SDL_Rect *srcrect,
- SDL_Surface *surf_dst, SDL_Rect *dstrect)
+static int SDLCALL SDL_RLEAlphaBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
+ SDL_Surface *surf_dst, const SDL_Rect *dstrect)
{
int x, y;
int w = surf_src->w;
diff --git a/src/video/SDL_blit.c b/src/video/SDL_blit.c
index c9540dac7884..cb3f25a16924 100644
--- a/src/video/SDL_blit.c
+++ b/src/video/SDL_blit.c
@@ -29,8 +29,8 @@
#include "SDL_pixels_c.h"
/* The general purpose software blit routine */
-static int SDLCALL SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect)
+static int SDLCALL SDL_SoftBlit(SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect)
{
int okay;
int src_locked;
diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index c7a305add764..64e3b95a13fe 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -636,8 +636,8 @@ int SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
* you know exactly what you are doing, you can optimize your code
* by calling the one(s) you need.
*/
-int SDL_BlitSurfaceUnchecked(SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect)
+int SDL_BlitSurfaceUnchecked(SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect)
{
/* Check to make sure the blit mapping is valid */
if ((src->map->dst != dst) ||
@@ -927,14 +927,14 @@ int SDL_PrivateBlitSurfaceScaled(SDL_Surface *src, const SDL_Rect *srcrect,
* This is a semi-private blit function and it performs low-level surface
* scaled blitting only.
*/
-int SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect)
+int SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect)
{
return SDL_PrivateBlitSurfaceUncheckedScaled(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST);
}
-int SDL_PrivateBlitSurfaceUncheckedScaled(SDL_Surface *src, SDL_Rect *srcrect,
- SDL_Surface *dst, SDL_Rect *dstrect, SDL_ScaleMode scaleMode)
+int SDL_PrivateBlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect,
+ SDL_Surface *dst, const SDL_Rect *dstrect, SDL_ScaleMode scaleMode)
{
static const Uint32 complex_copy_flags = (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL |