Skip to content

Commit 8deb2b6

Browse files
committed
cleanup, always using realloc now, added proper error messages.
1 parent a65e61a commit 8deb2b6

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src_c/alphablit.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ pg_cached_blitcopy(SDL_Surface *src, SDL_Surface *dst,
597597
for (i = 0; i < destinations->size; i++) {
598598
CachedBlitDest *item = &destinations->sequence[i];
599599

600-
const Py_ssize_t src_pitch = item->w * sizeof(Uint32);
601-
const Py_ssize_t src_skip = src->pitch / 4;
602-
const Py_ssize_t dst_skip = dst->pitch / 4;
600+
const int src_pitch = item->w * sizeof(Uint32);
601+
const int src_skip = src->pitch / 4;
602+
const int dst_skip = dst->pitch / 4;
603603

604604
Uint32 *srcp32 = (Uint32 *)src->pixels + item->x + item->y * src_skip;
605605
Uint32 *dstp32 = item->pixels;
@@ -616,24 +616,15 @@ int
616616
SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
617617
BlitSequence *destinations)
618618
{
619-
int okay;
620-
int src_locked;
621-
int dst_locked;
622-
SDL_BlendMode src_blend;
619+
int okay = 1;
620+
int src_locked = 0, dst_locked = 0;
623621

624-
/* Everything is okay at the beginning... */
625-
okay = 1;
626-
627-
/* Lock the destination if it's in hardware */
628-
dst_locked = 0;
629622
if (SDL_MUSTLOCK(dst)) {
630623
if (SDL_LockSurface(dst) < 0)
631624
okay = 0;
632625
else
633626
dst_locked = 1;
634627
}
635-
/* Lock the source if it's in hardware */
636-
src_locked = 0;
637628
if (SDL_MUSTLOCK(src)) {
638629
if (SDL_LockSurface(src) < 0)
639630
okay = 0;
@@ -642,6 +633,7 @@ SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
642633
}
643634

644635
if (okay) {
636+
SDL_BlendMode src_blend;
645637
switch (blend_flags) {
646638
case 0:
647639
/* unhandled cases */
@@ -661,13 +653,12 @@ SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
661653
}
662654
}
663655

664-
/* We need to unlock the surfaces if they're locked */
665656
if (dst_locked)
666657
SDL_UnlockSurface(dst);
667658
if (src_locked)
668659
SDL_UnlockSurface(src);
669-
/* Blit is done! */
670-
return (okay ? 0 : -1);
660+
661+
return okay ? 0 : -1;
671662
}
672663

673664
/* --------------------------------------------------------- */

src_c/surface.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,8 @@ surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
21572157
#define FBLITS_ERR_CACHE_RLE_NOT_SUPPORTED 16
21582158
#define FBLITS_ERR_FLAG_NOT_SUPPORTED 17
21592159
#define FBLITS_ERR_NO_MEMORY 18
2160+
#define FBLITS_ERR_INVALID_SEQUENCE_LENGTH 19
2161+
#define FBLITS_ERR_INVALID_DESTINATION 20
21602162

21612163
int
21622164
_surf_fblits_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
@@ -2255,20 +2257,10 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22552257
return FBLITS_ERR_CACHE_RLE_NOT_SUPPORTED;
22562258
}
22572259

2258-
/* manage destinations memory allocation or reallocation */
2260+
/* manage destinations memory */
22592261
Py_ssize_t new_size = PyList_GET_SIZE(pos_list);
2260-
if (destinations->sequence == NULL) {
2261-
destinations->sequence =
2262-
(CachedBlitDest *)malloc(new_size * sizeof(CachedBlitDest));
2263-
destinations->size = destinations->alloc_size = new_size;
2264-
if (!destinations->sequence) {
2265-
return FBLITS_ERR_NO_MEMORY;
2266-
}
2267-
}
2268-
else if (new_size > 0 && new_size <= destinations->alloc_size) {
2269-
destinations->size = new_size;
2270-
}
2271-
else if (new_size > destinations->alloc_size) {
2262+
if (new_size > destinations->alloc_size) {
2263+
/* no realloc as we don't care about the previous destinations */
22722264
destinations->sequence = (CachedBlitDest *)realloc(
22732265
destinations->sequence, new_size * sizeof(CachedBlitDest));
22742266

@@ -2277,8 +2269,13 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22772269

22782270
destinations->size = destinations->alloc_size = new_size;
22792271
}
2272+
else if (new_size > 0 && new_size <= destinations->alloc_size) {
2273+
destinations->size = new_size;
2274+
}
22802275
else {
2281-
return FBLITS_ERR_INCORRECT_ARGS_NUM;
2276+
if (new_size == 0)
2277+
return 0;
2278+
return FBLITS_ERR_INVALID_SEQUENCE_LENGTH;
22822279
}
22832280

22842281
/* load destinations */
@@ -2294,7 +2291,7 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22942291

22952292
if (!pg_IntFromObj(PyTuple_GET_ITEM(tup, 0), &x) ||
22962293
!pg_IntFromObj(PyTuple_GET_ITEM(tup, 1), &y)) {
2297-
return FBLITS_ERR_INCORRECT_ARGS_NUM;
2294+
return FBLITS_ERR_INVALID_DESTINATION;
22982295
}
22992296

23002297
if (x < -src->w || x > dst->w || y < -src->h || y > dst->h)
@@ -2367,6 +2364,9 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
23672364
pgSurface_Unprep(self);
23682365
pgSurface_Unprep((pgSurfaceObject *)src_surf);
23692366

2367+
if (error == -1)
2368+
error = BLITS_ERR_BLIT_FAIL;
2369+
23702370
return error;
23712371
}
23722372

@@ -2518,6 +2518,12 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
25182518
"supported for this operation");
25192519
case FBLITS_ERR_NO_MEMORY:
25202520
return RAISE(PyExc_MemoryError, "No memory available");
2521+
case FBLITS_ERR_INVALID_SEQUENCE_LENGTH:
2522+
return RAISE(PyExc_ValueError,
2523+
"Invalid sequence length for cached blit");
2524+
case FBLITS_ERR_INVALID_DESTINATION:
2525+
return RAISE(PyExc_TypeError,
2526+
"Invalid destination position for cached blit");
25212527
}
25222528
return RAISE(PyExc_TypeError, "Unknown error");
25232529
}

0 commit comments

Comments
 (0)