-
-
Notifications
You must be signed in to change notification settings - Fork 205
[Part 3] SDL3: surface+pixelarray+font+draw: runtime fixes #3578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
} | ||
|
@@ -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 | ||
|
@@ -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) \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); | ||
|
@@ -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); | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
ankith26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/*this internal blit function is accessible through the C api*/ | ||
int | ||
pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj, | ||
|
@@ -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 */ | ||
|
@@ -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)) || | ||
|
@@ -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; | ||
|
||
ankith26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#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; | ||
|
@@ -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 { | ||
|
@@ -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 */ | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.