Skip to content

Commit ddf97c3

Browse files
committed
Mask SDL3: don't set python error without GIL
1 parent 78800a4 commit ddf97c3

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

src_c/mask.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,11 @@ palette_colors - this only affects surfaces with a palette
956956
957957
*/
958958

959-
int
960-
bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
961-
Uint32 color, Uint32 threshold, int palette_colors)
959+
void
960+
bitmask_threshold(bitmask_t *m, SDL_Surface *surf, PG_PixelFormat *format,
961+
SDL_Palette *palette, SDL_Surface *surf2,
962+
PG_PixelFormat *format2, Uint32 color, Uint32 threshold,
963+
int palette_colors)
962964
{
963965
int x, y, rshift, gshift, bshift, rshift2, gshift2, bshift2;
964966
int rloss, gloss, bloss, rloss2, gloss2, bloss2;
@@ -968,13 +970,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
968970
Uint8 r, g, b, a;
969971
Uint8 tr, tg, tb, ta;
970972
int bpp1, bpp2;
971-
PG_PixelFormat *format, *format2;
972-
SDL_Palette *palette, *palette2;
973-
974-
if (!PG_GetSurfaceDetails(surf, &format, &palette)) {
975-
PyErr_SetString(pgExc_SDLError, SDL_GetError());
976-
return -1;
977-
}
978973

979974
rmask = format->Rmask;
980975
gmask = format->Gmask;
@@ -988,10 +983,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
988983
bpp1 = PG_FORMAT_BytesPerPixel(format);
989984

990985
if (surf2) {
991-
if (!PG_GetSurfaceDetails(surf2, &format2, &palette2)) {
992-
PyErr_SetString(pgExc_SDLError, SDL_GetError());
993-
return -1;
994-
}
995986
rmask2 = format2->Rmask;
996987
gmask2 = format2->Gmask;
997988
bmask2 = format2->Bmask;
@@ -1005,12 +996,9 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
1005996
bpp2 = PG_FORMAT_BytesPerPixel(format2);
1006997
}
1007998
else { /* make gcc stop complaining */
1008-
format2 = NULL;
1009-
palette2 = NULL;
1010999
rmask2 = gmask2 = bmask2 = 0;
10111000
rshift2 = gshift2 = bshift2 = 0;
10121001
rloss2 = gloss2 = bloss2 = 0;
1013-
format2 = NULL;
10141002
pixels2 = NULL;
10151003
bpp2 = 0;
10161004
}
@@ -1120,7 +1108,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
11201108
}
11211109
}
11221110
}
1123-
return 0; // No errors!
11241111
}
11251112

11261113
static PyObject *
@@ -1130,6 +1117,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
11301117
pgSurfaceObject *surfobj2 = NULL;
11311118
pgMaskObject *maskobj = NULL;
11321119
SDL_Surface *surf = NULL, *surf2 = NULL;
1120+
PG_PixelFormat *surf_format, *surf2_format = NULL;
1121+
SDL_Palette *surf_palette;
11331122
PyObject *rgba_obj_color, *rgba_obj_threshold = NULL;
11341123
Uint8 rgba_threshold[4] = {0, 0, 0, 255};
11351124
Uint32 color;
@@ -1150,6 +1139,17 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
11501139
surf2 = pgSurface_AsSurface(surfobj2);
11511140
}
11521141

1142+
if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) {
1143+
return RAISE(pgExc_SDLError, SDL_GetError());
1144+
}
1145+
1146+
if (surf2) {
1147+
surf2_format = PG_GetSurfaceFormat(surf2);
1148+
if (!surf2_format) {
1149+
return RAISE(pgExc_SDLError, SDL_GetError());
1150+
}
1151+
}
1152+
11531153
if (!pg_MappedColorFromObj(rgba_obj_color, surf, &color,
11541154
PG_COLOR_HANDLE_ALL)) {
11551155
return NULL;
@@ -1162,15 +1162,9 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
11621162
}
11631163
}
11641164
else {
1165-
#if SDL_VERSION_ATLEAST(3, 0, 0)
1166-
color_threshold =
1167-
SDL_MapSurfaceRGBA(surf, rgba_threshold[0], rgba_threshold[1],
1168-
rgba_threshold[2], rgba_threshold[3]);
1169-
#else
1170-
color_threshold =
1171-
SDL_MapRGBA(surf->format, rgba_threshold[0], rgba_threshold[1],
1172-
rgba_threshold[2], rgba_threshold[3]);
1173-
#endif
1165+
color_threshold = PG_MapRGBA(surf_format, surf_palette,
1166+
rgba_threshold[0], rgba_threshold[1],
1167+
rgba_threshold[2], rgba_threshold[3]);
11741168
}
11751169

11761170
maskobj = CREATE_MASK_OBJ(surf->w, surf->h, 0);
@@ -1185,8 +1179,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
11851179
}
11861180

11871181
Py_BEGIN_ALLOW_THREADS;
1188-
threshold_ret = bitmask_threshold(maskobj->mask, surf, surf2, color,
1189-
color_threshold, palette_colors);
1182+
bitmask_threshold(maskobj->mask, surf, surf_format, surf_palette, surf2,
1183+
surf2_format, color, color_threshold, palette_colors);
11901184
Py_END_ALLOW_THREADS;
11911185

11921186
pgSurface_Unlock(surfobj);

0 commit comments

Comments
 (0)