Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reST/c_api/surface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Header file: src_c/include/pygame.h
by the blit.

The C version of the :py:meth:`pygame.Surface.blit` method.
Return ``1`` on success, ``0`` on an exception.
Return ``0`` on success, ``1`` on an exception.
32 changes: 28 additions & 4 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,17 @@

#define PG_CreateSurface SDL_CreateSurface
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom
#define PG_ConvertSurface SDL_ConvertSurface
#define PG_ConvertSurfaceFormat SDL_ConvertSurface

/* Convert surface using palette and format of dst */
static inline SDL_Surface *
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
{
return SDL_ConvertSurfaceAndColorspace(surface, dst->format,
SDL_GetSurfacePalette(dst),
SDL_GetSurfaceColorspace(dst), 0);
}

#define PG_PixelFormatEnum SDL_PixelFormat

#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
Expand Down Expand Up @@ -143,11 +151,23 @@ PG_SURF_BitsPerPixel(SDL_Surface *surf)

#define PG_PixelFormat const SDL_PixelFormatDetails

static inline SDL_Palette *
PG_GetSurfacePalette(SDL_Surface *surf)
{
SDL_Palette *ret = SDL_GetSurfacePalette(surf);
if (!ret && SDL_ISPIXELFORMAT_INDEXED(surf->format)) {
/* Palette doesn't exist but is expected, so create it.
* SDL will associate the newly created palette with the surface */
ret = SDL_CreateSurfacePalette(surf);
}
Comment on lines +158 to +162
Copy link
Member

@Starbuck5 Starbuck5 Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is how we should be doing this. If our surfaces are missing palettes we deal with that when the surface is not created, not randomly at any point as a side effect of a getter function. And/or make the consumers okay with index formats without palettes, since that's a thing in SDL3 and therefore we will want to support it anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this being part of the getter makes sense considering that the getter is responsible for returning a palette, and we must ensure it doesn't return NULL when it should actually return a palette. But sure, I suppose it being part of PG_CreateSurface could also work (but then we'd also have to think about all the other ways a surface is created, which means more places to modify)

return ret;
}

static inline bool
PG_GetSurfaceDetails(SDL_Surface *surf, PG_PixelFormat **format_p,
SDL_Palette **palette_p)
{
*palette_p = SDL_GetSurfacePalette(surf);
*palette_p = PG_GetSurfacePalette(surf);
*format_p = SDL_GetPixelFormatDetails(surf->format);
return *format_p != NULL;
}
Expand All @@ -158,7 +178,6 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
return SDL_GetPixelFormatDetails(surf->format);
}

#define PG_GetSurfacePalette SDL_GetSurfacePalette
#define PG_SetPaletteColors SDL_SetPaletteColors
#define PG_SetSurfacePalette SDL_SetSurfacePalette
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
Expand Down Expand Up @@ -216,10 +235,15 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format)
#define PG_CreateSurfaceFrom(width, height, format, pixels, pitch) \
SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 0, pitch, format)
#define PG_ConvertSurface(src, fmt) SDL_ConvertSurface(src, fmt, 0)
#define PG_ConvertSurfaceFormat(src, pixel_format) \
SDL_ConvertSurfaceFormat(src, pixel_format, 0)

static inline SDL_Surface *
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
{
return SDL_ConvertSurface(surface, dst->format, 0);
}

#define PG_PixelFormatEnum SDL_PixelFormatEnum

#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
Expand Down
2 changes: 1 addition & 1 deletion src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs)
if (pgSurface_Check(colorobj)) {
pat_surfobj = ((pgSurfaceObject *)colorobj);

pattern = PG_ConvertSurface(pat_surfobj->surf, surf->format);
pattern = PG_ConvertSurface(pat_surfobj->surf, surf);

if (pattern == NULL) {
return RAISE(PyExc_RuntimeError, "error converting pattern surf");
Expand Down
6 changes: 1 addition & 5 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,7 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
resolve to Render_Solid, that needs to be explicitly handled. */
if (surf != NULL && bg_rgba_obj != Py_None) {
SDL_SetColorKey(surf, 0, 0);
#if SDL_TTF_VERSION_ATLEAST(3, 0, 0)
SDL_Palette *palette = SDL_GetSurfacePalette(surf);
#else
SDL_Palette *palette = surf->format->palette;
#endif
SDL_Palette *palette = PG_GetSurfacePalette(surf);
if (palette) {
palette->colors[0].r = backg.r;
palette->colors[0].g = backg.g;
Expand Down
2 changes: 1 addition & 1 deletion src_c/pixelarray_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args)
}

/* Ensure the new surface has the same format as the original */
new_surf = PG_ConvertSurface(temp_surf, surf->format);
new_surf = PG_ConvertSurface(temp_surf, surf);
if (temp_surf != surf) {
SDL_FreeSurface(temp_surf);
}
Expand Down
6 changes: 1 addition & 5 deletions src_c/pixelcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1197,11 +1197,7 @@ make_surface(PyObject *self, PyObject *arg)
pgBuffer_Release(&pg_view);
return RAISE(pgExc_SDLError, SDL_GetError());
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Palette *palette = SDL_GetSurfacePalette(surf);
#else
SDL_Palette *palette = surf->format->palette;
#endif
SDL_Palette *palette = PG_GetSurfacePalette(surf);
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
/* Give the surface something other than an all white palette.
* */
Expand Down
146 changes: 98 additions & 48 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1544,24 +1544,21 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
bool success =
PG_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
/* HACK HACK HACK */
// TODO SDL3: figure out how to port this or if it's relevant to SDL3.
#if !SDL_VERSION_ATLEAST(3, 0, 0)
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL))) {
if (SDL_MUSTLOCK(surf) && (!(flags & PGS_RLEACCEL))) {
/* hack to strip SDL_RLEACCEL flag off surface immediately when
it is not requested */
SDL_Rect sdlrect;
sdlrect.x = 0;
sdlrect.y = 0;
sdlrect.h = 0;
sdlrect.w = 0;
sdlrect.h = 1;
sdlrect.w = 1;

SDL_Surface *surface =
PG_CreateSurface(1, 1, PG_SURF_FORMATENUM(surf));

SDL_LowerBlit(surf, &sdlrect, surface, &sdlrect);
SDL_FreeSurface(surface);
}
#endif
Comment on lines -1547 to -1564
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some extremely confusing historic code to be changing without a comment or a note in the description or anything about how this makes sense and doesn't change SDL2 behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't change SDL2 behaviour at all because it is changing the srcrect argument of a blit whose destination is a temporary surface that is immediately destroyed after the said blit.

On SDL3 the same code was segfaulting because of the 0,0,0,0 rect, and hence I thought the fix is worth doing on both SDL2 and SDL3 codepaths because it is likely to segfault under SDL2-compat as well.

And yes, I have verified that this hack is still needed on SDL3, it was needed to get a unit test to pass.

/* HACK HACK HACK */
if (success) {
success = PG_SetSurfaceAlphaMod(surf, alpha);
Expand Down Expand Up @@ -1623,7 +1620,7 @@ surf_copy(pgSurfaceObject *self, PyObject *_null)
SURF_INIT_CHECK(surf)

pgSurface_Prep(self);
newsurf = PG_ConvertSurface(surf, surf->format);
newsurf = PG_ConvertSurface(surf, surf);
pgSurface_Unprep(self);

final = surf_subtype_new(Py_TYPE(self), newsurf, 1);
Expand Down Expand Up @@ -1683,7 +1680,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
if (argobject) {
if (pgSurface_Check(argobject)) {
src = pgSurface_AsSurface(argobject);
newsurf = PG_ConvertSurface(surf, src->format);
newsurf = PG_ConvertSurface(surf, src);
}
else {
/* will be updated later, initialize to make static analyzer happy
Expand Down Expand Up @@ -1837,7 +1834,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
if (argobject) {
if (pgSurface_Check(argobject)) {
src = pgSurface_AsSurface(argobject);
newsurf = PG_ConvertSurface(surf, src->format);
newsurf = PG_ConvertSurface(surf, src);
}
else {
/* will be updated later, initialize to make static analyzer happy
Expand Down Expand Up @@ -1961,7 +1958,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
SDL_SetPixelFormatPalette(&format, palette);
}
}
newsurf = PG_ConvertSurface(surf, &format);
newsurf = SDL_ConvertSurface(surf, &format, 0);
SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE);
SDL_FreePalette(palette);
}
Expand Down Expand Up @@ -3016,20 +3013,15 @@ surf_get_flags(PyObject *self, PyObject *_null)
if (sdl_flags & SDL_PREALLOC) {
flags |= PGS_PREALLOC;
}
/* This checks if RLE was requested on the surface */
if (PG_SurfaceHasRLE(surf)) {
flags |= PGS_RLEACCELOK;
}
// TODO SDL3: figure out how to properly emulate SDL2 check/relevance
// Current implementation is just a placeholder.
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_SurfaceHasRLE(surf)) {
flags |= PGS_RLEACCEL;
}
#else
if ((sdl_flags & SDL_RLEACCEL)) {
/* this checks if the surface actually has RLE.
* On SDL2: SDL_MUSTLOCK is (flags & SDL_RLEACCEL) */
if (SDL_MUSTLOCK(surf)) {
flags |= PGS_RLEACCEL;
}
#endif
if (is_window_surf) {
if (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE) {
flags |= PGS_FULLSCREEN;
Expand Down Expand Up @@ -3767,7 +3759,7 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null)

pgSurface_Prep(self);
// Make a copy of the surface first
newsurf = PG_ConvertSurface(surf, surf->format);
newsurf = PG_ConvertSurface(surf, surf);

if ((surf->w > 0 && surf->h > 0)) {
// If the surface has no pixels we don't need to premul
Expand Down Expand Up @@ -4461,6 +4453,76 @@ surface_do_overlap(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
return dstoffset < span || dstoffset > src->pitch - span;
}

int
PG_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst,
SDL_Rect *dstrect)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* SDL3 doesn't modify dstrect, so compat for that.
* Below logic taken from SDL2 source with slight modifications */
SDL_Rect r_src, r_dst;

r_src.x = 0;
r_src.y = 0;
r_src.w = src->w;
r_src.h = src->h;

if (dstrect) {
r_dst.x = dstrect->x;
r_dst.y = dstrect->y;
}
else {
r_dst.x = 0;
r_dst.y = 0;
}

/* clip the source rectangle to the source surface */
if (srcrect) {
SDL_Rect tmp;
if (SDL_IntersectRect(srcrect, &r_src, &tmp) == SDL_FALSE) {
goto end;
}

/* Shift dstrect, if srcrect origin has changed */
r_dst.x += tmp.x - srcrect->x;
r_dst.y += tmp.y - srcrect->y;

/* Update srcrect */
r_src = tmp;
}

/* There're no dstrect.w/h parameters. It's the same as srcrect */
r_dst.w = r_src.w;
r_dst.h = r_src.h;

/* clip the destination rectangle against the clip rectangle */
{
SDL_Rect tmp, clip_rect;
SDL_GetSurfaceClipRect(dst, &clip_rect);
if (SDL_IntersectRect(&r_dst, &clip_rect, &tmp) == SDL_FALSE) {
goto end;
}

/* Update dstrect */
r_dst = tmp;
}

if (r_dst.w > 0 && r_dst.h > 0) {
if (dstrect) { /* update output parameter */
*dstrect = r_dst;
}
return SDL_BlitSurface(src, srcrect, dst, dstrect) ? 0 : -1;
}
end:
if (dstrect) {
dstrect->w = dstrect->h = 0;
}
return 0;
#else
return SDL_BlitSurface(src, srcrect, dst, dstrect);
#endif
}

/*this internal blit function is accessible through the C api*/
int
pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
Expand All @@ -4471,13 +4533,11 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
SDL_Surface *subsurface = NULL;
int result, suboffsetx = 0, suboffsety = 0;
SDL_Rect orig_clip, sub_clip, dstclip;
#if !SDL_VERSION_ATLEAST(3, 0, 0)
Uint8 alpha;
#endif

if (!PG_GetSurfaceClipRect(dst, &dstclip)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return 0;
return 1;
}

/* passthrough blits to the real surface */
Expand Down Expand Up @@ -4528,8 +4588,6 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
result = pygame_Blit(src, srcrect, dst, dstrect, blend_flags);
/* Py_END_ALLOW_THREADS */
}
// TODO SDL3: port the below bit of code. Skipping for initial surface port.
#if !SDL_VERSION_ATLEAST(3, 0, 0)
/* can't blit alpha to 8bit, crashes SDL */
else if (PG_SURF_BytesPerPixel(dst) == 1 &&
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src)) ||
Expand All @@ -4539,17 +4597,22 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
result = pygame_Blit(src, srcrect, dst, dstrect, 0);
}
else {
#if SDL_VERSION_ATLEAST(3, 0, 0)
const SDL_PixelFormatDetails *fmt =
SDL_GetPixelFormatDetails(src->format);
src = fmt ? SDL_ConvertSurface(src,
SDL_GetPixelFormatForMasks(
fmt->bits_per_pixel, fmt->Rmask,
fmt->Gmask, fmt->Bmask, 0))
: NULL;

#else
SDL_PixelFormat *fmt = src->format;
SDL_PixelFormat newfmt;

newfmt.palette = 0; /* Set NULL (or SDL gets confused) */
#if SDL_VERSION_ATLEAST(3, 0, 0)
newfmt.bits_per_pixel = fmt->bits_per_pixel;
newfmt.bytes_per_pixel = fmt->bytes_per_pixel;
#else
newfmt.BitsPerPixel = fmt->BitsPerPixel;
newfmt.BytesPerPixel = fmt->BytesPerPixel;
#endif
newfmt.Amask = 0;
newfmt.Rmask = fmt->Rmask;
newfmt.Gmask = fmt->Gmask;
Expand All @@ -4562,13 +4625,10 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
newfmt.Rloss = fmt->Rloss;
newfmt.Gloss = fmt->Gloss;
newfmt.Bloss = fmt->Bloss;
src = PG_ConvertSurface(src, &newfmt);
if (src) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
result = SDL_BlitSurface(src, srcrect, dst, dstrect) ? 0 : -1;
#else
result = SDL_BlitSurface(src, srcrect, dst, dstrect);
src = SDL_ConvertSurface(src, &newfmt, 0);
#endif
if (src) {
result = PG_BlitSurface(src, srcrect, dst, dstrect);
SDL_FreeSurface(src);
}
else {
Expand All @@ -4577,32 +4637,22 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
}
/* Py_END_ALLOW_THREADS */
}
#endif
else if (blend_flags != PYGAME_BLEND_ALPHA_SDL2 &&
!(pg_EnvShouldBlendAlphaSDL2()) && !SDL_HasColorKey(src) &&
(PG_SURF_BytesPerPixel(dst) == 4 ||
PG_SURF_BytesPerPixel(dst) == 2) &&
_PgSurface_SrcAlpha(src) &&
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src))) &&
!PG_SurfaceHasRLE(src) && !PG_SurfaceHasRLE(dst) &&
#if SDL_VERSION_ATLEAST(3, 0, 0)
1
#else
!(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)
#endif
) {
!SDL_MUSTLOCK(src) && !SDL_MUSTLOCK(dst)) {
/* If we have a 32bit source surface with per pixel alpha
and no RLE we'll use pygame_Blit so we can mimic how SDL1
behaved */
result = pygame_Blit(src, srcrect, dst, dstrect, blend_flags);
}
else {
/* Py_BEGIN_ALLOW_THREADS */
#if SDL_VERSION_ATLEAST(3, 0, 0)
result = SDL_BlitSurface(src, srcrect, dst, dstrect) ? 0 : -1;
#else
result = SDL_BlitSurface(src, srcrect, dst, dstrect);
#endif
result = PG_BlitSurface(src, srcrect, dst, dstrect);
/* Py_END_ALLOW_THREADS */
}

Expand Down
Loading
Loading